TypeError: empty() received an invalid combination of arguments - got (tuple, dtype=NoneType, device=NoneType), but expected one of: * (tuple of ints size, *, tuple of names names, torch.memory_format memory_format, torch.dtype dtype, torch.layout layout

neeraj1909: <p>I am trying to do a memes analysis using the fusion model. For this, I have extracted the text feature using a pre-trained BERT model and the image feature using EfficientNet_B7. After that, I am passing these two parameters to my custom model class.</p>
<pre><code class="lang-auto">class FusionNet(nn.Module):
    def __init__(self, num_classes, drop_prob = 0.1):
        super().__init__()
        
        self.concat_layer = nn.Linear(in_features=768+1000, out_features= 512)
        self.bn = nn.BatchNorm1d(512)
        self.bn1 = nn.BatchNorm1d(768)
        self.bn2 = nn.BatchNorm1d(1000)
        # self.dropout = nn.Dropout(drop_prob)
        self.linear = nn.Linear(512, num_classes)
        self.relu = nn.ReLU()
        self.softmax = nn.Softmax()
        
    def forward(self, image_features, text_features):
        text_features = self.bn1(text_features)
        image_features = self.bn2(image_features)
        fused_input =  torch.cat((text_features, image_features), dim=1)
        x = self.concat_layer(fused_input)
        x = self.relu(self.bn(x)) 
        x = self.dropout(x)
        x = self.softmax(self.classify(x)) 
        
        return x
</code></pre>
<p>While running the code, I am getting the following error:</p>
<pre><code class="lang-auto">Epoch 1/100
----------
  0%|          | 0/70000 [00:00&lt;?, ?it/s]torch.Size([8, 1, 256, 256])
torch.Size([8, 1000]) torch.Size([8, 768])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File &lt;timed exec&gt;:10, in &lt;module&gt;

/home/guest/Code/memotion_2022/task_1.ipynb Cell 39' in train_epoch(model, data_loader, criterion, optimizer, device, n_examples)
     25 text_features = bert_output['pooler_output'].to(device)
     27 print(image_features.shape, text_features.shape)
---&gt; 29 outputs = FusionNet(
     30     image_features,
     31     text_features
     32 )
     34 _, preds = torch.max(outputs, dim=1)
     35 loss = criterion(outputs, targets)

/home/guest/Code/memotion_2022/task_1.ipynb Cell 43' in FusionNet.__init__(self, num_classes, drop_prob)
      8 self.bn2 = nn.BatchNorm1d(1000)
      9 # self.dropout = nn.Dropout(drop_prob)
---&gt; 10 self.linear = nn.Linear(512, num_classes)
     11 self.relu = nn.ReLU()
     12 self.softmax = nn.Softmax()

File /home/anaconda3/envs/koyel/lib/python3.8/site-packages/torch/nn/modules/linear.py:96, in Linear.__init__(self, in_features, out_features, bias, device, dtype)
     94 self.in_features = in_features
     95 self.out_features = out_features
---&gt; 96 self.weight = Parameter(torch.empty((out_features, in_features), **factory_kwargs))
     97 if bias:
     98     self.bias = Parameter(torch.empty(out_features, **factory_kwargs))

TypeError: empty() received an invalid combination of arguments - got (tuple, dtype=NoneType, device=NoneType), but expected one of:
 * (tuple of ints size, *, tuple of names names, torch.memory_format memory_format, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)
 * (tuple of SymInts size, *, torch.memory_format memory_format, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)
</code></pre>
<p>I am not able to fix this error for the last two days.<br>
Please help me out in identifying the issue <img src="https://discuss.pytorch.org/images/emoji/apple/smile.png?v=12" title=":smile:" class="emoji" alt=":smile:" loading="lazy" width="20" height="20"><br>
Thanks,<br>
Neeraj</p>

ptrblck: <p>Make sure you are passing an <code>int</code> as the <code>num_classes</code> argument as it seems you are using a <code>tuple</code> as seen here:</p>
<pre><code class="lang-python"># works    
model = FusionNet(num_classes=10)

# passing a tuple raises your error
model = FusionNet(num_classes=(10,))
# TypeError: empty() received an invalid combination of arguments - got (tuple, dtype=NoneType, device=NoneType), but expected one of:
#  * (tuple of ints size, *, tuple of names names, torch.memory_format memory_format, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)
#  * (tuple of SymInts size, *, torch.memory_format memory_format, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)
</code></pre>

neeraj1909: <p>Thanks for your reply.<br>
The issue has been resolved. I was using Class-name instead of the instance of that class, i.e. <code>FusionNet(image_features, text_features)</code> instead of <code>Fusion_model(image_features, text_features)</code>.</p>

Li_Chengjin: <p>in_features must be int, so use int(768+1000) instead</p>