TypeError: forward() takes 2 positional arguments but 3 were given

Could you post your model definition (or the forward signature)?
It looks like you are trying to pass too many inputs to your model.

class SimpleNet(nn.Module):
def init(self):
super(SimpleNet, self).init()
self.fc1 = nn.Linear(12288, 84)
self.fc2 = nn.Linear(84, 50)
self.fc3 = nn.Linear(50,2)

def forward(self):
    x = x.view(-1, 12288)
    x = F.relu(self.fc1(x))
    x = F.relu(self.fc2(x))
    x = self.fc3(x)
    return x

model = SimpleNet()

Your forward does not take any inputs, so you should define it as:

def forward(self, x):
1 Like

many Thanks bro. it is working fine now

@ptrblck
I am facing similar error. My forward function’s format is also correct. Please help me to resolve this.
This is my code:

class SpatialPyramidPooling(nn.Module):

  def __init__(self, num_pools=[1, 4, 16, 64], mode='max'):
    super(SpatialPyramidPooling, self).__init__()

    self.name = 'SpatialPyramidPooling'
    if mode == 'max':
      pool_func = nn.AdaptiveMaxPool2d
    elif mode == 'avg':
      pool_func = nn.AdaptiveAvgPool2d
    else:
      raise NotImplementedError(f"Unknown pooling mode '{mode}', expected 'max' or 'avg'")
    
    self.pools = nn.ModuleList([])

    for p in num_pools:
      side_length = sqrt(p)
      if not side_length.is_integer():
        raise ValueError(f'Bin size {p} is not a perfect square')

      self.pools.append(pool_func(int(side_length)))

  def forward(self,feature_maps):
    assert feature_maps.dim() == 4, 'Expected 4D input of (N, C, H, W)'

    batch_size = feature_maps.size(0)
    channels = feature_maps.size(1)

    pooled = []

    for p in self.pools:
      pooled.append(p(feature_maps).view(batch_size, channels, -1))
    
    return torch.cat(pooled, dim=2)

Your model works fine for me:

class SpatialPyramidPooling(nn.Module):

  def __init__(self, num_pools=[1, 4, 16, 64], mode='max'):
    super(SpatialPyramidPooling, self).__init__()

    self.name = 'SpatialPyramidPooling'
    if mode == 'max':
      pool_func = nn.AdaptiveMaxPool2d
    elif mode == 'avg':
      pool_func = nn.AdaptiveAvgPool2d
    else:
      raise NotImplementedError(f"Unknown pooling mode '{mode}', expected 'max' or 'avg'")
    
    self.pools = nn.ModuleList([])

    for p in num_pools:
      side_length = math.sqrt(p)
      if not side_length.is_integer():
        raise ValueError(f'Bin size {p} is not a perfect square')

      self.pools.append(pool_func(int(side_length)))

  def forward(self,feature_maps):
    assert feature_maps.dim() == 4, 'Expected 4D input of (N, C, H, W)'

    batch_size = feature_maps.size(0)
    channels = feature_maps.size(1)

    pooled = []

    for p in self.pools:
      pooled.append(p(feature_maps).view(batch_size, channels, -1))
    
    return torch.cat(pooled, dim=2)

model = SpatialPyramidPooling()
x = torch.randn(1, 1, 4, 4)
out = model(x)
print(out.shape)
> torch.Size([1, 1, 85])

Ok, thanks. I will try executing the code once again.

@ptrblck Here is my code but it says " forward() takes 2 positional arguments but 3 were given". Anyone please help me to resolve this.

simple_net = torch.nn.Sequential(
torch.nn.Linear(4, 2, bias= False),
torch.nn.ReLU(),
torch.nn.Linear(2, 1, bias= False),
)

print(simple_net)

input_x = torch.Tensor([10.])
label = torch.Tensor([1.])
n = simple_net(4,2)
loss_fn = nn.BCELoss()

out = n(input_x)
loss = simple_net.loss_fn(out,label)
n.zero_grad()
loss.backward()

for param in n.parameters():
g = param.grad

print(‘Gradient’%(g))

You are currently trying to pass two integers to the model:

n = simple_net(4,2)

which won’t work, as an input tensor is expected.
The tensor should have the shape [batch_size, 4] based on the model definition, so input_x also wouldn’t work.

Also, loss = simple_net.loss_fn(out,label) will create an error, since simple_net doesn’t have a loss_fn.

PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier. :wink:

1 Like

@ptrblck thank you so much for clarifying this. I really appreciate it. My below code runs smoothly.
net = torch.nn.Sequential(

                     torch.nn.Linear(4,2, bias=False),

                     torch.nn.ReLU(),

                     torch.nn.Linear(2,1, bias=False),

                     torch.nn.Sigmoid()

                     );

print(net)

criterion = torch.nn.BCELoss()
x = torch.Tensor([[-2, -0.3, 1.7, 0.2]])
o = net.forward(x)
t = torch.Tensor([[1.]])

loss = criterion(o, t)
loss.backward()

print(‘Weight gradient net[0] \n’,net[0].weight.grad)

print(‘Weight gradient net[2] \n’,net[2].weight.grad)

But what I am trying now is to find the gradient along the given network manually and I tried to extract the weight net[0] and then convert it into an array and then compute the gradient of this array.

first = net[0].weight
print(first)
a = first.data.numpy()
print(a)
a_final = np.gradient(a)
a_final

But the result doen’t match what i got from net[0].weight.grad through back propagation. Any tricks or suggestions to compute the gradient manually.