TypeError: conv2d(): argument 'input' (position 1) must be Tensor, not method

username: nshrimali
cooked: <p>Creating my first pytorch image classification project, stuck at this error, not really sure what I am doing wrong. Please help!!!..</p>
<p>class GarbageNet(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(GarbageNet,self).<strong>init</strong>()<br>
self.conv1 = nn.Conv2d(in_channels=3, out_channels=12, kernel_size=3,stride=1, padding=1)<br>
self.relu1 = nn.ReLU()<br>
self.maxpool1 = nn.MaxPool2d(kernel_size=2)<br>
self.conv2 = nn.Conv2d(in_channels=12, out_channels=24, kernel_size=3, stride=1, padding=1)<br>
self.relu2 = nn.ReLU()<br>
self.lf = nn.Linear(in_features=32 * 32 * 24, out_features=6)</p>
<pre><code>    #Defining a forward pass
    
def forward(self, x):
    output = self.conv1(input)
    output = self.relu1(output)
    output = self.maxpool1(output)
    output = self.conv2(output)
    output = self.relu2(output)
    return x
</code></pre>
<p>def trainNet(net, n_epochs, learning_rate):<br>
<span class="hashtag">#Backpropagation</span> - Using SGD<br>
optimizer = optim.SGD(net.parameters(), lr = 0.001, momentum = 0.9)<br>
<span class="hashtag">#Defining</span> an optimizer - SGD<br>
loss = nn.CrossEntropyLoss()<br>
print(net)<br>
for epoch in range(n_epochs):<br>
running_loss = 0.0<br>
start_time = time.time()<br>
total_train_loss = 0</p>
<pre><code>    for i,data in enumerate(train_loader):
        images, labels = data
        print((images.shape))
        print((labels.shape))
        optimizer.zero_grad()

        #Forward pass, backward pass
        outputs = net(images)
        loss_size = loss(outputs, labels)
        loss_size.backward()
        optimizer.step()
</code></pre>
<h1>=======Hyperparameters=========<br>
epochs= 5<br>
learning_rate= 0.001</h1>
<p>GarbageNet(<br>
(conv1): Conv2d(3, 12, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(relu1): ReLU()<br>
(maxpool1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)<br>
(conv2): Conv2d(12, 24, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(relu2): ReLU()<br>
(lf): Linear(in_features=24576, out_features=6, bias=True)<br>
)<br>
torch.Size([4, 3, 224, 224])<br>
torch.Size([4])</p>
<hr>
<p>TypeError                                 Traceback (most recent call last)<br>
 in <br>
----&gt; 1 trainNet(cnn_model, n_epochs=5, learning_rate=0.001)</p>
<p> in trainNet(net, n_epochs, learning_rate)<br>
43<br>
44             <span class="hashtag">#Forward</span> pass, backward pass<br>
—&gt; 45             outputs = net(images)<br>
46             loss_size = loss(outputs, labels)<br>
47             loss_size.backward()</p>
<p>~\AppData\Local\conda\conda\envs\asdf\lib\site-packages\torch\nn\modules\module.py in <strong>call</strong>(self, *input, **kwargs)<br>
530             result = self._slow_forward(*input, **kwargs)<br>
531         else:<br>
–&gt; 532             result = self.forward(*input, **kwargs)<br>
533         for hook in self._forward_hooks.values():<br>
534             hook_result = hook(self, input, result)</p>
<p> in forward(self, x)<br>
14<br>
15     def forward(self, x):<br>
—&gt; 16         output = self.conv1(input)<br>
17         output = self.relu1(output)<br>
18         output = self.maxpool1(output)</p>
<p>~\AppData\Local\conda\conda\envs\asdf\lib\site-packages\torch\nn\modules\module.py in <strong>call</strong>(self, *input, **kwargs)<br>
530             result = self._slow_forward(*input, **kwargs)<br>
531         else:<br>
–&gt; 532             result = self.forward(*input, **kwargs)<br>
533         for hook in self._forward_hooks.values():<br>
534             hook_result = hook(self, input, result)</p>
<p>~\AppData\Local\conda\conda\envs\asdf\lib\site-packages\torch\nn\modules\conv.py in forward(self, input)<br>
343<br>
344     def forward(self, input):<br>
–&gt; 345         return self.conv2d_forward(input, self.weight)<br>
346<br>
347 class Conv3d(_ConvNd):</p>
<p>~\AppData\Local\conda\conda\envs\asdf\lib\site-packages\torch\nn\modules\conv.py in conv2d_forward(self, input, weight)<br>
339                             weight, self.bias, self.stride,<br>
340                             _pair(0), self.dilation, self.groups)<br>
–&gt; 341         return F.conv2d(input, weight, self.bias, self.stride,<br>
342                         self.padding, self.dilation, self.groups)<br>
343</p>
<p>TypeError: conv2d(): argument ‘input’ (position 1) must be Tensor, not method</p>

username: ptrblck
cooked: <p>Your <code>forward</code> method accepts an <code>x</code> input, while you are using <code>input</code> as the first argument to your conv layer.<br>
Change it to <code>self.conv1(x)</code> and it should work.</p>

username: nshrimali
cooked: <p>Careless mistake, Thanks <a class="mention" href="/u/ptrblck">@ptrblck</a> for the solution. It works!!!</p>