TypeError: new() received an invalid combination of arguments - got (Tensor, int)

jitesh: <p>Hi, I am defining a Custom Model for image classification. My code is as follows</p>
<pre><code class="lang-auto">class CustomNeuralNet(nn.Module):
    def __init__(self,num_classes):
        super(CustomNeuralNet,self).__init__()

        self.conv_1 = nn.Conv2d(in_channels=3,out_channels=32,kernel_size=3,padding=1,stride=1)
        self.activation_1 = nn.LeakyReLU()

        self.conv_2 = nn.Conv2d(in_channels=32,out_channels=64,kernel_size=3,padding=1,stride=1)
        self.activation_2 = nn.LeakyReLU()
        
        self.conv_3 = nn.Conv2d(in_channels=64,out_channels=64,kernel_size=3,padding=1,stride=1)
        self.activation_3 = nn.LeakyReLU()

        self.fc = nn.Linear(in_features=300*300*64,out_features=num_classes)

    
    def forward(self,inp):
        out = self.conv_1(inp)
        out = self.activation_1(out)

        out = self.conv_2(out)
        out = self.activation_2(out)

        out = self.conv_3(out)
        out = self.activation_3(out)

        out = out.view(-1,150*150*64)

        out = self.fc(out)
        return out

training_dir = 'train/'
validation_dir = 'val/'

train_transforms = transforms.Compose([
    transforms.Resize((300,300)),
    transforms.RandomCrop(300),
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor(),
    transforms.Normalize((0.5,0.5,0.5), (0.5,0.5,0.5)),
])

validation_transforms = transforms.Compose([
    transforms.Resize((300,300)),
    transforms.ToTensor(),
    transforms.Normalize((0.5,0.5,0.5), (0.5,0.5,0.5)),
])

train_data = ImageFolder(training_dir,transform=train_transforms)
validation_data = ImageFolder(validation_dir,transform=validation_transforms)
train_loader = DataLoader(train_data,batch_size=64)
val_loader = DataLoader(validation_data,batch_size=64)

device = torch.device("cuda" if torch.cuda.is_available() 
                                  else "cpu")

model = CustomNeuralNet(num_classes=20)
model.to(device)

optimizer = Adam(model.parameters(), weight_decay=0.0001)
loss_function = nn.CrossEntropyLoss()

epochs = 50
print_every = 10
train_losses, test_losses = [], []
prev_loss = -1000

for epoch in range(epochs):
    running_loss = 0.0
    model.train()
    for i,data in enumerate(train_loader):
        inputs,labels = data
        inputs, labels = inputs.to(device), labels.to(device)
        
        optimizer.zero_grad()

        outputs = CustomNeuralNet(inputs)
        loss = loss_function(outputs,labels)
        loss.backward()
        optimizer.step()

        running_loss += loss.item()

        if i % print_every == 0:
            print('[%d, %5d] loss: %.3f' %
                  (epoch + 1, i + 1, running_loss / print_every))
    
    train_losses.append(running_loss/len(train_loader))
</code></pre>
<p>However, when I begin training, I am getting the following error</p>
<pre><code class="lang-auto">File "pytorch_image_classification.py", line 121, in &lt;module&gt;
    outputs = CustomNeuralNet(inputs)
  File "pytorch_image_classification.py", line 25, in __init__
    self.fc = nn.Linear(in_features=300*300*64,out_features=num_classes)
  File "/home/jitesh/anaconda3/envs/torch_cpu/lib/python3.8/site-packages/torch/nn/modules/linear.py", line 72, in __init__
    self.weight = Parameter(torch.Tensor(out_features, in_features))
TypeError: new() received an invalid combination of arguments - got (Tensor, int), but expected one of:
 * (torch.device device)
 * (torch.Storage storage)
 * (Tensor other)
 * (tuple of ints size, torch.device device)
      didn't match because some of the arguments have invalid types: (Tensor, int)
 * (object data, torch.device device)
      didn't match because some of the arguments have invalid types: (Tensor, int)
</code></pre>
<p>I am not quite able to understand what is wrong in line 25 of my Model definition, I am resizing all images to 300x300 and thus gave that(multiplied by the number of output filters) as the input for the last fully connected layer.  Please correct me if I am wrong</p>

ptrblck: <p>You would have to create a model instance before passing in the inputs:</p>
<pre><code class="lang-python">model = CustomNeuralNet(num_classes)
outputs = model(inputs)
</code></pre>
<p>Based on the error message it seems you are trying to pass the inputs directly to the initialization of the model.</p>

jitesh: <p>I  missed that. Thanks, that worked</p>

Kaushik_Ramabhotla: <p>Hi sir, I am getting this error :</p>
<p>TypeError: conv2d() received an invalid combination of arguments - got (Tensor, Parameter, Parameter, tuple, tuple, tuple, int), but expected one of:</p>
<ul>
<li>(Tensor input, Tensor weight, Tensor bias, tuple of ints stride, tuple of ints padding, tuple of ints dilation, int groups)<br>
didn’t match because some of the arguments have invalid types: (Tensor, !Parameter!, !Parameter!, !tuple!, !tuple!, !tuple!, int)</li>
<li>(Tensor input, Tensor weight, Tensor bias, tuple of ints stride, str padding, tuple of ints dilation, int groups)<br>
didn’t match because some of the arguments have invalid types: (Tensor, !Parameter!, !Parameter!, !tuple!, !tuple!, !tuple!, int)</li>
</ul>
<p>while printing the summary:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/0/20452d085e74abc7a96202a8c4f9fcd9c5be7fd4.png" data-download-href="https://discuss.pytorch.org/uploads/default/20452d085e74abc7a96202a8c4f9fcd9c5be7fd4" title="kk"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/2/0/20452d085e74abc7a96202a8c4f9fcd9c5be7fd4_2_690x235.png" alt="kk" data-base62-sha1="4BtueVR3TvMUvrRoCBClWQZwpO4" width="690" height="235" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/2/0/20452d085e74abc7a96202a8c4f9fcd9c5be7fd4_2_690x235.png, https://discuss.pytorch.org/uploads/default/optimized/3X/2/0/20452d085e74abc7a96202a8c4f9fcd9c5be7fd4_2_1035x352.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/2/0/20452d085e74abc7a96202a8c4f9fcd9c5be7fd4_2_1380x470.png 2x" data-dominant-color="3A3A3B"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">kk</span><span class="informations">1418×484 88.3 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

Kaushik_Ramabhotla: <p>Any clarification would be very helpful <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

ptrblck: <p>Are you seeing this issue also while directly executing the model or only when using the <code>summary</code>?<br>
In the latter case I would guess the <code>summary</code> package might not be compatible with your PyTorch version or could have a known issue.<br>
Also, I don’t know which <code>summary</code> library you are using, but note that e.g. <a href="https://github.com/sksq96/pytorch-summary">pytorch-summary</a> seems to be deprecated in favor of <a href="https://github.com/TylerYep/torchinfo">torchinfo</a>.</p>

Kaushik_Ramabhotla: <p>Ok sir, will try importing torchinfo and update here</p>

Zhuoyi416: <p>hello sir,I got the similar error that u got in the past:<br>
TypeError: conv2d() received an invalid combination of arguments - got (Tensor, Parameter, NoneType, tuple, tuple, tuple, int), but expected one of:</p>
<ul>
<li>(Tensor input, Tensor weight, Tensor bias, tuple of ints stride, tuple of ints padding, tuple of ints dilation, int groups)<br>
didn’t match because some of the arguments have invalid types: (Tensor, !Parameter!, !NoneType!, !tuple!, !tuple!, !tuple!, int)</li>
<li>(Tensor input, Tensor weight, Tensor bias, tuple of ints stride, str padding, tuple of ints dilation, int groups)<br>
didn’t match because some of the arguments have invalid types: (Tensor, !Parameter!, !NoneType!, !tuple!, !tuple!, !tuple!, int)<br>
!<br>
<a href="/uploads/short-url/oSBEwJacd2ZWtlKFabAUTQoHhZs.png">image|690x369</a><br>
How do u solve this error,sir? thank u very much</li>
</ul>

Kaushik_Ramabhotla: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> sir, reworking on this project, the issue is still pertaining. I have used torch info but still, I am getting the same error</p>

Kaushik_Ramabhotla: <p><a class="mention" href="/u/zhuoyi416">@Zhuoyi416</a> were u able to solve this bro ?</p>

ptrblck: <p><code>torchinfo</code> works properly for me using the initially posted model:</p>
<pre><code class="lang-python">import torch
import torch.nn as nn
from torchinfo import summary


class CustomNeuralNet(nn.Module):
    def __init__(self,num_classes):
        super(CustomNeuralNet,self).__init__()

        self.conv_1 = nn.Conv2d(in_channels=3,out_channels=32,kernel_size=3,padding=1,stride=1)
        self.activation_1 = nn.LeakyReLU()

        self.conv_2 = nn.Conv2d(in_channels=32,out_channels=64,kernel_size=3,padding=1,stride=1)
        self.activation_2 = nn.LeakyReLU()
        
        self.conv_3 = nn.Conv2d(in_channels=64,out_channels=64,kernel_size=3,padding=1,stride=1)
        self.activation_3 = nn.LeakyReLU()

        self.fc = nn.Linear(in_features=300*300*64,out_features=num_classes)

    
    def forward(self,inp):
        out = self.conv_1(inp)
        out = self.activation_1(out)

        out = self.conv_2(out)
        out = self.activation_2(out)

        out = self.conv_3(out)
        out = self.activation_3(out)

        out = out.view(out.size(0), -1)

        out = self.fc(out)
        return out

model = CustomNeuralNet(10)
summary(model, input_size=(1, 3, 300, 300))

==========================================================================================
Layer (type:depth-idx)                   Output Shape              Param #
==========================================================================================
CustomNeuralNet                          --                        --
├─Conv2d: 1-1                            [1, 32, 300, 300]         896
├─LeakyReLU: 1-2                         [1, 32, 300, 300]         --
├─Conv2d: 1-3                            [1, 64, 300, 300]         18,496
├─LeakyReLU: 1-4                         [1, 64, 300, 300]         --
├─Conv2d: 1-5                            [1, 64, 300, 300]         36,928
├─LeakyReLU: 1-6                         [1, 64, 300, 300]         --
├─Linear: 1-7                            [1, 10]                   57,600,010
==========================================================================================
Total params: 57,656,330
Trainable params: 57,656,330
Non-trainable params: 0
Total mult-adds (G): 5.13
==========================================================================================
Input size (MB): 1.08
Forward/backward pass size (MB): 115.20
Params size (MB): 230.63
Estimated Total Size (MB): 346.91
==========================================================================================
</code></pre>