TypeError: forward() takes 1 positional argument but 2 were given

Hi I do not understand why the number of arguments here is not correct, this is my model:

class MancalaModel(nn.Module):

    def __init__(self, n_inputs=16, n_outputs=16):
        super().__init__()

        n_neurons = 256

        def create_block(n_in, n_out):
            block = nn.ModuleList()
            block.append(nn.Linear(n_in, n_out))
            block.append(nn.ReLU())
            return block

        self.blocks = nn.ModuleList()
        self.blocks.append(create_block(n_inputs, n_neurons))
        for _ in range(6):
            self.blocks.append(create_block(n_neurons, n_neurons))

        self.actor_block = nn.ModuleList()
        self.critic_block = nn.ModuleList()
        for _ in range(2):
            self.actor_block.append(create_block(n_neurons, n_neurons))
            self.critic_block.append(create_block(n_neurons, n_neurons))

        self.actor_block.append(create_block(n_neurons, n_outputs))
        self.critic_block.append(create_block(n_neurons, 1))

        self.apply(init_weights)

    def forward(self, x):
        x = self.blocks(x)
        actor = F.softmax(self.actor_block(x))
        critics = self.critic_block(x)
        return actor, critics

Then I do:

model = MancalaModel()
x = model(torch.rand(1, 16))

Then I got the error:

      2 model = MancalaModel()
----> 3 x = model(torch.rand(1, 16))
      4 # summary(model, (16,), device='cpu')
      5 

d:\environments\python\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--> 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

D:\UOM\Year3\AI & Games\KalahPlayer\agents\model_agent.py in forward(self, x)
     54 
     55     def forward(self, x):
---> 56         x = self.blocks(x)
     57         actor = F.softmax(self.actor_block(x))
     58         critics = self.critic_block(x)

d:\environments\python\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--> 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

TypeError: forward() takes 1 positional argument but 2 were given

Any help is appreciated. Thanks

You cannot call nn.ModuleLists directly and should iterate them instead.
I guess you would like to use nn.Sequential instead as shown in this code:

class MancalaModel(nn.Module):

    def __init__(self, n_inputs=16, n_outputs=16):
        super().__init__()

        n_neurons = 256

        def create_block(n_in, n_out):
            block = []
            block.append(nn.Linear(n_in, n_out))
            block.append(nn.ReLU())
            block = nn.Sequential(*block)
            return block

        self.blocks = []
        self.blocks.append(create_block(n_inputs, n_neurons))
        for _ in range(6):
            self.blocks.append(create_block(n_neurons, n_neurons))
        self.blocks = nn.Sequential(*self.blocks)

        self.actor_block = []
        self.critic_block = []
        for _ in range(2):
            self.actor_block.append(create_block(n_neurons, n_neurons))
            self.critic_block.append(create_block(n_neurons, n_neurons))

        self.actor_block.append(create_block(n_neurons, n_outputs))
        self.critic_block.append(create_block(n_neurons, 1))
        self.actor_block = nn.Sequential(*self.actor_block)
        self.critic_block = nn.Sequential(*self.critic_block)


    def forward(self, x):
        x = self.blocks(x)
        actor = F.softmax(self.actor_block(x))
        critics = self.critic_block(x)
        return actor, critics
1 Like

Could you please let me know where am I making mistake?

ENet = EfficientNet.from_pretrained('efficientnet-b5')

class ENET(nn.Module):
  def __init__(self):
    super(ENET, self).__init__()
    
    self.ENet = ENet

    #disect the network to access its last convolutional layer
    self.head0 = nn.Sequential(*list(self.ENet.children())[:4])
    self.BN =  nn.BatchNorm2d(2048, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
    self.tail = nn.Sequential(*list(self.ENet.children())[-4:])

    # placeholder for the gradients
    self.gradients = None

# hook for the gradients of the activations
def activations_hook(self, grad):
    self.gradients = grad
    
def forward(self, x):
    self.x, self.labels = self.head0(x)
    #register the hook
    h = self.x.register_hook(self.activations_hook)       
    x = self.BN(self.x)
    x = x.view((1, -1))
    x = self.tail(x)
    return x

# method for the gradient extraction
def get_activations_gradient(self):
    return self.gradients

# method for the activation exctraction
def get_activations(self, x):
    return self.head0(x)


model = ENET()
# set the evaluation mode
model.eval()
# get the image from the dataloader
 img, _= next(iter(train_loader))
 img = img.to(device)
 print(img.shape)
# get the most likely prediction of the model
pred = model(self.X)

error:

torch.Size([1, 3, 224, 224])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-46c7c5e707f6> in <module>()
  9 print(img.shape)
 10 # get the most likely prediction of the model
 ---> 11 pred = model(img)

 4 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, 
**kwargs)
 725             result = self._slow_forward(*input, **kwargs)
726         else:
--> 727             result = self.forward(*input, **kwargs)
728         for hook in itertools.chain(
729                 _global_forward_hooks.values(),

 <ipython-input-7-4bf4b58e1763> in forward(self, x)
 26 
 27     def forward(self, x):
 ---> 28         self.x, self.labels = self.head0(x)
 29 
 30         #x = self.head(x)

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, 
**kwargs)
725             result = self._slow_forward(*input, **kwargs)
726         else:
 --> 727             result = self.forward(*input, **kwargs)
728         for hook in itertools.chain(
729                 _global_forward_hooks.values(),

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/container.py in forward(self, input)
115     def forward(self, input):
116         for module in self:
--> 117             input = module(input)
118         return input
119 

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, 
**kwargs)
725             result = self._slow_forward(*input, **kwargs)
726         else:
--> 727             result = self.forward(*input, **kwargs)
728         for hook in itertools.chain(
729                 _global_forward_hooks.values(),

TypeError: forward() takes 1 positional argument but 2 were given

I don’t know which EfficientNet implementation you are using, so I cannot reproduce this error directly.
However, checking the efficientnet_b5 from the timm repository works after some changes to the architecture:

class ENET(nn.Module):
    def __init__(self):
        super(ENET, self).__init__()
        
        self.ENet = ENet
    
        #disect the network to access its last convolutional layer
        self.head0 = nn.Sequential(*list(self.ENet.children())[:4])
        self.BN =  nn.BatchNorm2d(512, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
        self.tail = nn.Sequential(*list(self.ENet.children())[4:])


    def forward(self, x):
        x = self.head0(x)
        x = self.BN(x)
        #x = x.view((1, -1))
        x = self.tail(x)
        return x


ENet = efficientnet_b5()
model = ENET()
model.eval()
pred = model(torch.randn(1, 3, 224, 224))

Okay, could you please post the link to timm repo? I couldn’t find it.
Thank you

You can find it here.

I’ve got the same problem. I try some of the tips suggested here, but the problem persists.

class VTransformer(nn.Module):
  def __init__(self, type = 'vit_base_patch16_224', pretrained = True, K = 2):
    super(VTransformer, self).__init__()

    model = timm.create_model(type, pretrained = pretrained)

    self.features = nn.Sequential(*list(model.children())[:-1])
    self.numfeat = list(model.children())[-1].in_features

    #self.numfeat = self.features[-1].in_features
    if pretrained:
      # freeze  weights
      for param in self.features.parameters():
        param.requires_grad = False

    self.classifier = nn.Sequential(
        nn.Linear(self.numfeat, K),
    )
    #self.classifier = nn.Sequential(
    #    nn.Dropout(0.2),
    #    nn.Linear(128 * 3 * 3, 1024),
    #    nn.ReLU(),
    #    nn.Dropout(0.2),
    #    nn.Linear(1024, K)
    #)

  def forward(self, x):
    print(x.size())
    out = self.features(x)
    out = self.classifier(out)
    print(out.size())
    return out

modelRGB = VTransformer()
modelRGB.eval()
pred = modelRGB(torch.randn(1,3,224,224))
torch.Size([1, 3, 224, 224])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-73-28d07edefb6c> in <module>()
      2 #print(modelRGB)
      3 modelRGB.eval()
----> 4 pred = modelRGB(torch.randn(1,3,224,224))

4 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--> 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

TypeError: forward() takes 1 positional argument but 2 were given

I faced similar problem while using pretrained EfficientNet. The issue is with all variants of EfficientNet, when you install from pip install efficientnet-pytorch.

When you load the pretrained network, set include_top as False, and modify classifier (tail part)
EfficientNet.from_pretrained(‘efficientnet-b7’, include_top = False)

Last 3 children (dropout, fc and swish) of model will not be considered now.

Hope it helps!