TypeError: forward() takes 2 positional arguments but 4 were given, Pytorch
I am trying to write a GAN generator based on Densenet and Deconv method. I am new to PyTorch and unable to figure out
TypeError: forward() takes 2 positional arguments but 4 were given. 

I tried the approach as suggested in
Pytorch TypeError: forward() takes 2 positional arguments but 4 were given
but I cannot figure out the solution.
My code:
class DenseLayer(nn.Module):
  def __init__(self, in_size, out_size, drop_rate=0.0):
    super(DenseLayer, self).__init__()
    self.bottleneck = nn.Sequential() # define bottleneck layers
    self.bottleneck.add_module('btch1', nn.BatchNorm2d(in_size))
    self.bottleneck.add_module('relu1', nn.ReLU(inplace=True))
    self.bottleneck.add_module('conv1', nn.ConvTranspose2d(in_size, int(out_size/4), kernel_size=1, stride=1, padding=0, bias=False))

    self.basic = nn.Sequential() # define basic block
    self.basic.add_module('btch2', nn.BatchNorm2d(int(out_size/4)))
    self.basic.add_module('relu2', nn.ReLU(inplace=True))
    self.basic.add_module('conv2', nn.ConvTranspose2d(int(out_size/4), out_size, kernel_size=3, stride=1, padding=1, bias=False))

    self.droprate = drop_rate

  def forward(self, input):
    out = self.bottleneck(input)
    if self.droprate &gt; 0:
      out = F.dropout(out, p=self.droprate, inplace=False, training=self.training)
    
    out = self.basic(out)
    if self.droprate &gt; 0:
      out = F.dropout(out, p=self.droprate, inplace=False, training=self.training)
    return torch.cat((x,out), 1)

class DenseBlock(nn.Module):
  def __init__(self, num_layers, in_size, growth_rate, block, droprate=0.0):
    super(DenseBlock, self).__init__()
    self.layer = self._make_layer(block, in_size, growth_rate, num_layers, droprate)

  def _make_layer(self, block, in_size, growth_rate, num_layers, droprate):
    layers = []
    for i in range(num_layers):
      layers.append(block(in_size, in_size-i*growth_rate, droprate))
    return nn.Sequential(*layers)

  def forward(self, input):
    return self.layer(input)

class MGenDenseNet(nn.Module):
  def __init__(self, ngpu, growth_rate=32, block_config=(16,24,12,6), in_size=1024, drop_rate=0.0):
    super(MGenDenseNet, self).__init__()
    self.ngpu = ngpu
    self.features = nn.Sequential()
    self.features.add_module('btch0', nn.BatchNorm2d(in_size))

    block = DenseLayer
    num_features = in_size
    for i, num_layers in enumerate(block_config):
      block = DenseBlock(num_layers=num_layers, in_size=num_features, growth_rate=growth_rate, block=block, droprate=drop_rate) ### Error thrown on this line
      self.features.add_module('denseblock{}'.format(i+1), block)
      num_features -= num_layers*growth_rate

      if i!=len(block_config)-1:
        trans = TransitionLayer(in_size=num_features, out_size=num_features*2, drop_rate=drop_rate)
        self.features.add_module('transitionblock{}'.format(i+1), trans)
        num_features *= 2

    self.features.add_module('convfinal', nn.ConvTranspose2d(num_features, 3, kernel_size=7, stride=2, padding=3, bias=False))
    self.features.add_module('Tanh', nn.Tanh())

  def forward(self, input):
    return self.features(input)

mGen = MGenDenseNet(ngpu).to(device)
mGen.apply(weights_init)

print(mGen)



class MGenDenseNet(nn.Module):
  def __init__(self, ngpu, growth_rate=32, block_config=(16,24,12,6), in_size=1024, drop_rate=0.0):
    super(MGenDenseNet, self).__init__()
    import pdb; pdb.set_trace()
    self.ngpu = ngpu
    self.features = nn.Sequential()
    self.features.add_module('btch0', nn.BatchNorm2d(in_size))
    block_placeholder = DenseLayer &lt;&lt;&lt;&lt;
    num_features = in_size
    for i, num_layers in enumerate(block_config):
      block = DenseBlock(num_layers=num_layers, in_size=num_features, growth_rate=growth_rate, block=block_placeholder, droprate=drop_rate) &lt;&lt;&lt;&lt; look at change
      self.features.add_module('denseblock{}'.format(i+1), block)
      num_features -= num_layers*growth_rate
    self.features.add_module('convfinal', nn.ConvTranspose2d(num_features, 3, kernel_size=7, stride=2, padding=3, bias=False))
    self.features.add_module('Tanh', nn.Tanh())
  def forward(self, input):
    return self.features(input)

It is because you define block as DenseLayer, then reassign block it to an initalized DenseBlock() and then pass that as block=block. So after one iteration through the for loop it is passing a DenseBlock() object instead of DenseLayer so it's wrongly using the forward pass.
Just change block = DenseLayer to block_placeholder and use that variable instead.
I spotted this by placing a debugger in your code and noticing that the DenseBlock line only fails on second call.
