Size mismatch, m1: [7168 x 56], m2: [50176 x 56] at /pytorch/aten/src/TH/generic/THTensorMath.cpp:41

username: MushroomNas
cooked: <p>Help me, please. I looked at similar questions and tried some of the solutions, but it didn’t help.</p>
<p>This is my code</p>
<pre><code class="lang-auto">class NeuralNetwork(nn.Module):
    def __init__(self):
        super(NeuralNetwork, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=3, out_channels=6, kernel_size=3, padding=1)
        self.conv2 = nn.Conv2d(in_channels=6, out_channels=6, kernel_size=3, padding=1)
        self.act1 = nn.ReLU()
        self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2)
        self.bn1 = torch.nn.BatchNorm2d(num_features=6)
        
        self.conv3 = nn.Conv2d(in_channels=6, out_channels=16, kernel_size=3, padding=1)
        self.conv4 = nn.Conv2d(in_channels=16, out_channels=16, kernel_size=3, padding=1)
        self.act2 = nn.ReLU()
        self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2)
        self.bn2 = torch.nn.BatchNorm2d(num_features=16)
        
        self.fc1 = nn.Linear(in_features=56 * 56 * 16, out_features=56)
        self.fc2 = nn.Linear(56, 10)
        

    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        x = self.act1(x)
        x = self.pool1(x)
        x = self.bn1(x)
        
        x = self.conv3(x)
        x = self.conv4(x)
        x = self.act2(x)
        x = self.pool2(x)
        print(x.shape)
        x = self.bn2(x)
        print(x.shape)
        x = self.fc1(x)
        print(x.shape)
        x = self.fc2(x)
        
        return x
</code></pre>
<p>And this is code for training:</p>
<pre><code class="lang-auto">def train(net, n_epoch):
    #device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
    #net = net.to(device)
    loss = torch.nn.CrossEntropyLoss()
    optimizer = torch.optim.Adam(net.parameters(), lr=1.0e-3)
    
    test_accuracy_history = []
    test_loss_history = []
    
    dataloader = train_dataloader

    for epoch in range(n_epoch):
        
        for phase in ['train', 'val']:
            if phase == 'train':
                dataloader = train_dataloader
                #scheduler.step()
                net.train()  # Set model to training mode
            else:
                dataloader = val_dataloader
                net.eval() 
                
            running_loss = 0.
            running_acc = 0.
                
            for inputs, labels in tqdm(dataloader):
                #inputs = inputs.to(device)
                #labels = labels.to(device)

                optimizer.zero_grad()
                with torch.set_grad_enabled(phase == 'train'):
                    preds = net.forward(inputs)
                    loss_value = loss(preds, labels)
                    preds_class = preds.argmax(dim=1)
                    
                    if phase == 'train':
                        loss_value.backward()
                        optimizer.step()
                        
                    else:
                        test_loss_history.append(loss(inputs, labels).data.cpu())
                        accuracy = (preds.argmax(dim=1) == labels).float().mean().data.cpu()
                        test_accuracy_history.append(accuracy)
                
                running_loss += loss_value.item()
                running_acc += (preds_class == labels.data).float().mean()
                
            epoch_loss = running_loss / len(dataloader)
            epoch_acc = running_acc / len(dataloader)
            
            print('{} Loss: {:.4f} Acc: {:.4f}'.format(phase, epoch_loss, epoch_acc), flush=True)
    return test_loss_history, test_accuracy_history

</code></pre>
<p>And I get this traceback:</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-14-a7c73a6166af&gt; in &lt;module&gt;
----&gt; 1 accuracy_train, loss_train = train(15)
      2 #

&lt;ipython-input-13-dcc24964612c&gt; in train(n_epoch)
     15             optimizer.zero_grad()
     16 
---&gt; 17             preds = network(inputs)
     18             losses = loss(preds, labels)
     19             losses.backward()

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    720             result = self._slow_forward(*input, **kwargs)
    721         else:
--&gt; 722             result = self.forward(*input, **kwargs)
    723         for hook in itertools.chain(
    724                 _global_forward_hooks.values(),

&lt;ipython-input-12-978f558521c2&gt; in forward(self, x)
     37         x = self.pool2(x)
     38         print(x.shape)
---&gt; 39         x = self.fc1(x)
     40         print(x.shape)
     41         x = self.act3(x)

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    720             result = self._slow_forward(*input, **kwargs)
    721         else:
--&gt; 722             result = self.forward(*input, **kwargs)
    723         for hook in itertools.chain(
    724                 _global_forward_hooks.values(),

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/linear.py in forward(self, input)
     89 
     90     def forward(self, input: Tensor) -&gt; Tensor:
---&gt; 91         return F.linear(input, self.weight, self.bias)
     92 
     93     def extra_repr(self) -&gt; str:

/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in linear(input, weight, bias)
   1674         ret = torch.addmm(bias, input, weight.t())
   1675     else:
-&gt; 1676         output = input.matmul(weight.t())
   1677         if bias is not None:
   1678             output += bias
RuntimeError: size mismatch, m1: [7168 x 56], m2: [50176 x 56] at /pytorch/aten/src/TH/generic/THTensorMath.cpp:41
</code></pre>
<p>I tried changing value of in_features to 7186 and value of out_features to 56 just to see what happens and got the following mistake:<br>
RuntimeError: size mismatch, m1: [7168 x 56], m2: [7168 x 56] at /pytorch/aten/src/TH/generic/THTensorMath.cpp:41</p>
<pre><code class="lang-auto">self.fc1 = nn.Linear(in_features=7168, out_features=56)
</code></pre>

username: diegoaichele
cooked: <p>that’s because before the self.fc1 layers you need to flatten.</p>
<pre><code class="lang-auto">import torch
from torch import nn

class NeuralNetwork(nn.Module):
    def __init__(self):
        super(NeuralNetwork, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=3, out_channels=6, kernel_size=3, padding=1)
        self.conv2 = nn.Conv2d(in_channels=6, out_channels=6, kernel_size=3, padding=1)
        self.act1 = nn.ReLU()
        self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2)
        self.bn1 = nn.BatchNorm2d(num_features=6)
        
        self.conv3 = nn.Conv2d(in_channels=6, out_channels=16, kernel_size=3, padding=1)
        self.conv4 = nn.Conv2d(in_channels=16, out_channels=16, kernel_size=3, padding=1)
        self.act2 = nn.ReLU()
        self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2)
        self.bn2 = nn.BatchNorm2d(num_features=16)
        self.flatten = nn.Flatten() # add Flatten
        self.fc1 = nn.Linear(in_features=56 * 56 * 16 * 56 , out_features=56) # multiply by 56
        self.fc2 = nn.Linear(56, 10)
        

    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        x = self.act1(x)
        x = self.pool1(x)
        x = self.bn1(x)
        
        x = self.conv3(x)
        x = self.conv4(x)
        x = self.act2(x)
        x = self.pool2(x)
        print(x.shape)
        x = self.bn2(x)
        print(x.shape)
        x = self.flatten(x)  # flatten
        print(x.shape)
        x = self.fc1(x)
        print(x.shape)
        x = self.fc2(x)
        
        return x


Net = NeuralNetwork()
t =torch.rand( (1,3,12544,224))

Net(t)


#torch.Size([1, 16, 3136, 56])
#torch.Size([1, 16, 3136, 56])
#torch.Size([1, 2809856])
#torch.Size([1, 56])
#tensor([[-0.3037,  0.3958, -0.1080, -0.4891, -0.3739, -0.2028, -0.0532,  0.0999,
#         0.4444,  0.2417]], grad_fn=&lt;AddmmBackward0&gt;)
</code></pre>
<p>EDIT:<br>
Another way is to perform matrix multiplication well</p>
<pre><code class="lang-auto">RuntimeError: size mismatch, m1: [7168 x 56], m2: [50176 x 56] 
</code></pre>
<p>If is m1: [7168 x 56], So the m2 need a shape of [56 x  Y ]</p>
<p>So with simple</p>
<pre><code class="lang-auto">        self.fc1 = nn.Linear(in_features=56, out_features=56)
</code></pre>
<p>You can solve your problem.</p>

username: MushroomNas
cooked: <p>Thank you very much! Adding Flatten layer solved the problem, but there’s a new one. Maybe you know the solution to this problem too?</p>
<p>Traceback of new error:</p>
<pre><code class="lang-auto">  0%|          | 3/1457 [00:00&lt;06:12,  3.91it/s]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
&lt;ipython-input-11-b83fdb81195d&gt; in &lt;module&gt;
      1 network = LeNet()
      2 
----&gt; 3 loss_train, accuracy_train = train(network, 15)
      4 #

&lt;ipython-input-9-2401c7483eb2&gt; in train(net, n_epoch)
     32                 with torch.set_grad_enabled(phase == 'train'):
     33                     preds = net.forward(inputs)
---&gt; 34                     loss_value = loss(preds, labels)
     35                     preds_class = preds.argmax(dim=1)
     36 

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    720             result = self._slow_forward(*input, **kwargs)
    721         else:
--&gt; 722             result = self.forward(*input, **kwargs)
    723         for hook in itertools.chain(
    724                 _global_forward_hooks.values(),

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/loss.py in forward(self, input, target)
    946     def forward(self, input: Tensor, target: Tensor) -&gt; Tensor:
    947         return F.cross_entropy(input, target, weight=self.weight,
--&gt; 948                                ignore_index=self.ignore_index, reduction=self.reduction)
    949 
    950 

/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction)
   2420     if size_average is not None or reduce is not None:
   2421         reduction = _Reduction.legacy_get_string(size_average, reduce)
-&gt; 2422     return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
   2423 
   2424 

/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce, reduction)
   2216                          .format(input.size(0), target.size(0)))
   2217     if dim == 2:
-&gt; 2218         ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
   2219     elif dim == 4:
   2220         ret = torch._C._nn.nll_loss2d(input, target, weight, _Reduction.get_enum(reduction), ignore_index)

IndexError: Target 10 is out of bounds.
</code></pre>
<p>I create dataset by the following code:</p>
<pre><code class="lang-auto">train_dir = 'images_splited/train'
val_dir = 'images_splited/val'
test_dir = 'images_splited/test'

transforms = transforms.Compose([
    transforms.ToTensor()
])

train_dataset = torchvision.datasets.ImageFolder(train_dir, transforms)
val_dataset = torchvision.datasets.ImageFolder(val_dir, transforms)
test_dataset = torchvision.datasets.ImageFolder(test_dir, transforms)

batch_size = 8
train_dataloader = torch.utils.data.DataLoader(
    train_dataset, batch_size=batch_size, shuffle=True, num_workers=batch_size)
val_dataloader = torch.utils.data.DataLoader(
    val_dataset, batch_size=batch_size, shuffle=False, num_workers=batch_size)
test_dataloader = torch.utils.data.DataLoader(
    test_dataset, batch_size=batch_size, shuffle=True, num_workers=batch_size)
</code></pre>
<p>The structure of my files looks like this:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/9/c97a18cfafe918e8b01ac1a37d2cad1d5d0114e6.png" data-download-href="https://discuss.pytorch.org/uploads/default/c97a18cfafe918e8b01ac1a37d2cad1d5d0114e6" title="Frame 1"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/c/9/c97a18cfafe918e8b01ac1a37d2cad1d5d0114e6_2_690x423.png" alt="Frame 1" data-base62-sha1="sKlyUblPeq5qkAMSppi7bRyIJMO" width="690" height="423" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/c/9/c97a18cfafe918e8b01ac1a37d2cad1d5d0114e6_2_690x423.png, https://discuss.pytorch.org/uploads/default/optimized/3X/c/9/c97a18cfafe918e8b01ac1a37d2cad1d5d0114e6_2_1035x634.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/c/9/c97a18cfafe918e8b01ac1a37d2cad1d5d0114e6.png 2x" data-dominant-color="F3F4F5"><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">Frame 1</span><span class="informations">1320×811 165 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>
<p>As a result, my neural network should predict the probability of a set of data belonging to each of 10 classes: Inter, Montserrat, Nunito, etc</p>

username: diegoaichele
cooked: <p>Hi,</p>
<p>I think your problem is from Loss, now I can’t simulate your dataset, that is, I don’t know what size your prediction comes out of</p>
<aside class="quote no-group" data-username="MushroomNas" data-post="1" data-topic="151614">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/m/bcef8e/48.png" class="avatar"> MushroomNas:</div>
<blockquote>
<pre><code class="lang-auto">    loss = torch.nn.CrossEntropyLoss()
</code></pre>
</blockquote>
</aside>
<p><a href="https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html" rel="noopener nofollow ugc">CrossEntropyLoss — PyTorch 1.11.0 documentation</a></p>
<p>Look out, in the example:</p>
<pre><code class="lang-auto">&gt;&gt;&gt; # Example of target with class indices
&gt;&gt;&gt; loss = nn.CrossEntropyLoss()
&gt;&gt;&gt; input = torch.randn(3, 5, requires_grad=True)
&gt;&gt;&gt; target = torch.empty(3, dtype=torch.long).random_(5)
&gt;&gt;&gt; output = loss(input, target)
&gt;&gt;&gt; output.backward()
&gt;&gt;&gt;
&gt;&gt;&gt; # Example of target with class probabilities
&gt;&gt;&gt; input = torch.randn(3, 5, requires_grad=True)
&gt;&gt;&gt; target = torch.randn(3, 5).softmax(dim=1)
&gt;&gt;&gt; output = loss(input, target)
&gt;&gt;&gt; output.backward()
</code></pre>

username: MushroomNas
cooked: <p>I tried to display labels in my batches and got the following result:</p>
<pre><code class="lang-auto">tensor([8, 8, 6, 5, 1, 3, 1, 6])
tensor([ 7,  4, 10,  4, 10, 10,  7,  1])
tensor([ 6, 10, 10,  2,  6,  6, 10,  6])
</code></pre>
<p>I don’t exactly understand, why I get this error, because according to example in documentation function can identify by itself if there are classes or probabilities in the input</p>

username: diegoaichele
cooked: <p>Ok, using the same Net</p>
<pre><code class="lang-auto">Net = NeuralNetwork()
t =torch.rand( (8,3,12544,224))  # simulate batch_size =  8

input = Net(t)
print(input.shape)
# torch.Size([8, 10])
input.argmax(axis=0)
# tensor([4, 7, 3, 3, 4, 3, 4, 0, 2, 6])
</code></pre>
<p>So your outputs are going from [0 to 9], but your targets have value from [1 to 10]</p>
<p>How to solve?<br>
my advice is to modify the target  N_class -1</p>

username: MushroomNas
cooked: <p>Thank you very much! It worked!</p>