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

Redcxx: <p>Hi I do not understand why the number of arguments here is not correct, this is my model:</p>
<pre><code class="lang-auto">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
</code></pre>
<p>Then I do:</p>
<pre><code class="lang-auto">model = MancalaModel()
x = model(torch.rand(1, 16))
</code></pre>
<p>Then I got the error:</p>
<pre><code class="lang-auto">      2 model = MancalaModel()
----&gt; 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:
--&gt; 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

D:\UOM\Year3\AI &amp; Games\KalahPlayer\agents\model_agent.py in forward(self, x)
     54 
     55     def forward(self, x):
---&gt; 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:
--&gt; 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
</code></pre>
<p>Any help is appreciated. Thanks</p>

ptrblck: <p>You cannot call <code>nn.ModuleList</code>s directly and should iterate them instead.<br>
I guess you would like to use <code>nn.Sequential</code> instead as shown in this code:</p>
<pre><code class="lang-python">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
</code></pre>

Akshay_A_J: <p>Could you please let me know where am I making mistake?</p>
<pre><code>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)
</code></pre>
<p>error:</p>
<pre><code>torch.Size([1, 3, 224, 224])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-10-46c7c5e707f6&gt; in &lt;module&gt;()
  9 print(img.shape)
 10 # get the most likely prediction of the model
 ---&gt; 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:
--&gt; 727             result = self.forward(*input, **kwargs)
728         for hook in itertools.chain(
729                 _global_forward_hooks.values(),

 &lt;ipython-input-7-4bf4b58e1763&gt; in forward(self, x)
 26 
 27     def forward(self, x):
 ---&gt; 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:
 --&gt; 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:
--&gt; 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:
--&gt; 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</code></pre>

ptrblck: <p>I don’t know which <code>EfficientNet</code> implementation you are using, so I cannot reproduce this error directly.<br>
However, checking the <code>efficientnet_b5</code> from the <code>timm</code> repository works after some changes to the architecture:</p>
<pre><code class="lang-python">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))
</code></pre>

Akshay_A_J: <p>Okay, could you please post the link to timm repo? I couldn’t find it.<br>
Thank you</p>

ptrblck: <p>You can find it <a href="https://github.com/rwightman/pytorch-image-models">here</a>.</p>

GCCh: <p>I’ve got the same problem. I try some of the tips suggested here, but the problem persists.</p>
<pre><code class="lang-auto">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))
</code></pre>
<pre><code class="lang-auto">torch.Size([1, 3, 224, 224])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-73-28d07edefb6c&gt; in &lt;module&gt;()
      2 #print(modelRGB)
      3 modelRGB.eval()
----&gt; 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:
--&gt; 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
</code></pre>

Madhan: <p>I faced similar problem while using pretrained EfficientNet. The issue is with all variants of EfficientNet, when you install from <strong>pip install efficientnet-pytorch</strong>.</p>
<p>When you load the pretrained network, set <em>include_top</em> as <em>False</em>, and modify classifier (tail part)<br>
EfficientNet.from_pretrained(‘efficientnet-b7’, include_top = False)</p>
<p>Last 3 children (dropout, fc and swish) of model will not be considered now.</p>
<p>Hope it helps!</p>