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

Help me, please. I looked at similar questions and tried some of the solutions, but it didn’t help.

This is my code

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

And this is code for training:

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

And I get this traceback:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-14-a7c73a6166af> in <module>
----> 1 accuracy_train, loss_train = train(15)
      2 #

<ipython-input-13-dcc24964612c> in train(n_epoch)
     15             optimizer.zero_grad()
     16 
---> 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:
--> 722             result = self.forward(*input, **kwargs)
    723         for hook in itertools.chain(
    724                 _global_forward_hooks.values(),

<ipython-input-12-978f558521c2> in forward(self, x)
     37         x = self.pool2(x)
     38         print(x.shape)
---> 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:
--> 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) -> Tensor:
---> 91         return F.linear(input, self.weight, self.bias)
     92 
     93     def extra_repr(self) -> 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:
-> 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

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:
RuntimeError: size mismatch, m1: [7168 x 56], m2: [7168 x 56] at /pytorch/aten/src/TH/generic/THTensorMath.cpp:41

self.fc1 = nn.Linear(in_features=7168, out_features=56)

that’s because before the self.fc1 layers you need to flatten.

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=<AddmmBackward0>)

EDIT:
Another way is to perform matrix multiplication well

RuntimeError: size mismatch, m1: [7168 x 56], m2: [50176 x 56] 

If is m1: [7168 x 56], So the m2 need a shape of [56 x Y ]

So with simple

        self.fc1 = nn.Linear(in_features=56, out_features=56)

You can solve your problem.

1 Like

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?

Traceback of new error:

  0%|          | 3/1457 [00:00<06:12,  3.91it/s]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-11-b83fdb81195d> in <module>
      1 network = LeNet()
      2 
----> 3 loss_train, accuracy_train = train(network, 15)
      4 #

<ipython-input-9-2401c7483eb2> in train(net, n_epoch)
     32                 with torch.set_grad_enabled(phase == 'train'):
     33                     preds = net.forward(inputs)
---> 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:
--> 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) -> Tensor:
    947         return F.cross_entropy(input, target, weight=self.weight,
--> 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)
-> 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:
-> 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.

I create dataset by the following code:

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)

The structure of my files looks like this:

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

Hi,

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

CrossEntropyLoss — PyTorch 1.11.0 documentation

Look out, in the example:

>>> # Example of target with class indices
>>> loss = nn.CrossEntropyLoss()
>>> input = torch.randn(3, 5, requires_grad=True)
>>> target = torch.empty(3, dtype=torch.long).random_(5)
>>> output = loss(input, target)
>>> output.backward()
>>>
>>> # Example of target with class probabilities
>>> input = torch.randn(3, 5, requires_grad=True)
>>> target = torch.randn(3, 5).softmax(dim=1)
>>> output = loss(input, target)
>>> output.backward()
1 Like

I tried to display labels in my batches and got the following result:

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])

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

Ok, using the same Net

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])

So your outputs are going from [0 to 9], but your targets have value from [1 to 10]

How to solve?
my advice is to modify the target N_class -1

1 Like

Thank you very much! It worked!