RuntimeError: mat1 and mat2 shapes cannot be multiplied (64x13056 and 153600x2048)

who can help me?

import numpy as np
from PIL import Image
import glob
import torch
import torch.nn as nn
import torch.optim
from torch.autograd import Variable
from torch.utils.data import DataLoader, Dataset
import torch.nn.functional as F
from torchvision import transforms as T


def read_data():
    """
    read img_file data
    :return: img_path, img_name
    """
    train_data = glob.glob('../train-images/*.jpeg')
    train_label = np.array(
        [train_data[index].split('/')[-1].split('.')[0].split('_')[0] for index in
         range(len(train_data))])
    return train_data, train_label


out_place = (
    "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
    'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z')
transform = T.Compose([
   # T.Resize((128,128)),
    T.ToTensor(),
    T.Normalize(std=[0.5, 0.5, 0.5], mean=[0.5, 0.5, 0.5])
])


def one_hot(word: str) -> np.array:
    tmp = []
    # for i in range(len(word)):
    for i in range(4):
        item_tmp = [0 for x in range(144)]   # [0 for x in range(len(word) * len(out_place))]
        word_idx = out_place.index(word[i].lower())
        item_tmp[i*36+word_idx] = 1
        tmp.append(item_tmp)
    return np.array(tmp)


class DataSet(Dataset):
    def __init__(self):
        self.img_path, self.label = read_data()

    def __getitem__(self, index):
        img_path = self.img_path[index]
        img = Image.open(img_path).convert("RGB")
        img = transform(img)
        label = torch.from_numpy(one_hot(self.label[index])).float()
        return img, label

    def __len__(self):
        return len(self.img_path)


data = DataSet()
data_loader = DataLoader(data, shuffle=True, batch_size=64, drop_last=True)


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

        self.layer1 = nn.Sequential(
            nn.Conv2d(3, 16, stride=1, kernel_size=3, padding=1),
            nn.BatchNorm2d(16),
            nn.ReLU(inplace=True)


        )
        self.layer2 = nn.Sequential(
            nn.Conv2d(16, 32, stride=1, kernel_size=3, padding=1),
            nn.BatchNorm2d(32),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(stride=2, kernel_size=2),  # 30 80
        )
        self.layer3 = nn.Sequential(
            nn.Conv2d(32, 64, stride=1, kernel_size=3, padding=1),
            nn.BatchNorm2d(64),
            nn.ReLU(inplace=True),
            nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(128),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=2, stride=2),   # 15 40
        )

        self.fc = nn.Sequential(
            nn.Linear(256 * 15 * 40, 2048),
            nn.ReLU(inplace=True),
            nn.Linear(2048, 1024),
            nn.ReLU(inplace=True),
            nn.Linear(1024, 40)
        )

    def forward(self, x):
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)

        x = x.view(x.size(0), -1)
        x = self.fc(x)
        x = F.softmax(x, dim=-1)
        return x


model = CNN_Network()

model.train()
optimizer = torch.optim.Adam(model.parameters(), lr=0.002)

error = nn.MultiLabelSoftMarginLoss()


for i in range(5):
    for (batch_x, batch_y) in data_loader:
        optimizer.zero_grad()
        image = Variable(batch_x)
        label = Variable(batch_y)

        out = model(image)

        loss = error(out, label)

        print(loss)

        loss.backward()
        optimizer.step()

torch.save(model.state_dict(), "model.pth")

my image size [70, 26] and one of images name is okzi.jpeg
I am first touch pytorch so i don`t understand this question

Summary

This text will be hidden

The activation input x to self.fc doesn’t have the expected number of features, so you would need to change the in_features of the first nn.Linear layer in self.fc to 13056.

Also, unrelated to this issue, but Variables are deprecated since PyTorch 0.4.0, so you can use tensors now. :wink:

3 Likes

thanks for you :wink:

maybe i make a mistation, this is difficut for me :thinking:

The error points to a shape mismatch in the model output and target.
It seems one of these tensors has a batch size of 144 while the other has only 4 samples.
Print the shapes of the output and target before passing them to the criterion and make sure they have the same number of samples.

I’m getting RuntimeError: mat1 and mat2 shapes cannot be multiplied (836x35344 and 836x35344).

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(836, 16*47*47)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x.float())))
        x = self.pool(F.relu(self.conv2(x.float())))
        x = x.view(836, 16 * 47 * 47)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

The linear layer in self.fc1 expects 836 input features, while you are reshaping the activation to [batch_size=836, features=16*47*47=35344].
Use x = x.view(x.size(0), -1) instead and make sure the in_features of self.fc1 are matching the number of features of the incoming activation.

1 Like

I’m brand new to deep learning and I’m curious as to the principles that inform manipulation of the CNN architecture as well as tuning hyperparameters. I have a test case of 11383 (200x200x3) training samples of 5 classes which I am modifying to 200x200x1. What would be the ideal starting point for architecture design/parameters as well as batch size and other hyperparameters based on information presented herein and my prior post?

You could start with some known architectures and either play around with them or use them as a base line to create your custom model. The same applies for all hyperparameters.
Also, I would recommend to take a look at the PyTorch tutorials and/or some courses such as FastAI.

class Net(nn.Module):

def __init__(self):

    super(Net, self).__init__()
    self.conv1 = nn.Conv2d(1, 32, 5)

    self.pool = nn.MaxPool2d(2, 2)

    self.conv2 = nn.Conv2d(32, 32, 5)

    self.fc = nn.Linear(86528 ,136)

def forward(self, x):

    x = self.pool(F.relu(self.conv1(x)))

    x = self.pool(F.relu(self.conv2(x)))

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

    x = self.fc(x) 

    return x

I’m getting RuntimeError: mat1 and mat2 shapes cannot be multiplied (10x89888 and 86528x136)

This error seems to be raised, as x = x.view(x.size(0), -1) creates a tensor in the shape [batch_size, 89888], while 86528 features are expected.
You could either make sure the input to this layer has the right shape or change the in_features of self.fc to 89888.

I am getting the same error:

def __init__(self, input_dim=100, conv_input=3, conv_num=2, conv_dim=16, kernel_size=11, hidden_num=3, hidden_dim=200, output_dim=7):
      # Building the network from here
        super(ConvNet, self).__init__()

      # Class attributes
        self.input_dim = input_dim
        self.conv_input = conv_input
        self.conv_num = conv_num
        self.conv_dim = conv_dim
        self.kernel_size = kernel_size
        self.hidden_num = hidden_num
        self.hidden_dim = hidden_dim
        self.pool = nn.MaxPool2d(2)
        self.output_conv = input_dim

      # Output dimension after convolution
        for i in range(conv_num):
            self.output_conv = (self.output_conv-(self.kernel_size-1)-1)/1+1
            self.output_conv = math.floor(self.output_conv/2)
        self.output_conv = self.output_conv*self.output_conv*self.conv_dim
        print("Output:", self.output_conv)

      # Convolutional layers
        convolutional = [nn.Conv2d(conv_input, conv_dim, kernel_size) if i==0 else nn.Conv2d(conv_dim, conv_dim, kernel_size) for i in range(conv_num)]
        self.convolution = nn.ModuleList(convolutional)
        kernels = [kernel_size]*conv_num
        paddings = [0]*conv_num

      # Hidden layers
        hidden = [nn.Linear(self.output_conv, hidden_dim) if i==0 else nn.Linear(hidden_dim, hidden_dim) for i in range(hidden_num)]
        self.linears = nn.ModuleList(hidden)

      # Output layer
        self.ol = nn.Linear(hidden_dim, output_dim)

    def forward(self, X):
        for i, cv in enumerate(self.convolution):
            X = self.convolution[i](X)
            print(X.shape)
            X = F.relu(X)
            X = self.pool(X)
            print(X.shape)
        X = X.view(-1, self.num_flat_features(X))
        print(X.shape)
        for j, f in enumerate(self.linears):
            print(j)
            X = self.linears[i](X)
            X = F.relu(X)
        return F.softmax(self.ol(X), dim=-1)
    
    def num_flat_features(self, x):
        size = x.size()[1:]  
        num_features = 1
        for s in size:
            num_features *= s
        return num_features

Manual implementation actually works so I do not know what could I be doing wrong. My input images are 100x100. The error reads:

RuntimeError: mat1 and mat2 shapes cannot be multiplied (128x4624 and 200x200)

Why is the shape suddenly changed from the batch size to 200?

The activation shape before flattening it is [batch_size, 16, 17, 17], which corresponds to [batch_size, 4624]. This will raise an error, since the linear layer is using in_features=200, so you would need to change it to 4624.

1 Like

Hi, thank you for your answer but isn’t the line

hidden = [nn.Linear(self.output_conv, hidden_dim) if i==0 else nn.Linear(hidden_dim, hidden_dim) for i in range(hidden_num)]

generating a line of linear layers with the first layer with input size self.output_conv? Or am I missing something?

Thank you once again.

Thanks for pointing this out. You are right and the input shape is indeed correct. The error is raised, since you are indexing self.linears with i instead of j.

1 Like

You are totally right! Thank you very much for your help.

type or paimport json # importing the module
from nlp_utils import tokenize, stemming, bag_of_words
import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
from model import NeuralNet


with open("input_data.json", "r") as f:  # opening the json file in read mode
    intents = json.load(f)  # fiiting the file in the variable

all_words = []
tags = []
xy = []

for intent in intents['intents']:
    tag = intent["tag"]
    tags.append(tag)
    for pattern in intent['patterns']:
        w = tokenize(pattern)
        all_words.extend(w)
        xy.append((w, tag))

ignore_words = ['?', '.', ',', '!']
all_words = [stemming(w) for w in all_words if w not in ignore_words]
all_words = sorted(set(all_words))
tags = sorted(set(tags))
x_train = []
y_train = []
for (pattern_sentence, tag) in xy:
    bag = bag_of_words(pattern_sentence, all_words)
    x_train.append(bag)

    label = tags.index(tag)
    y_train.append(label)


x_train = np.array(x_train)
y_train = np.array(y_train)

class Chat_Dataset(Dataset):
    def __init__(self):
        self.n_samples = len(x_train)
        self.x_data = x_train
        self.y_data = y_train
    def __getitem__(self, index):
        return self.x_data[index], self.y_data[index]

    def __len__(self):
        return self.n_samples

# hyperparameter

batch_size = 8
h_s = 8
o_s = len(tags)
i_s = len(x_train[0])
learning_rate = 0.001
num_epochs = 1000


dataset = Chat_Dataset()
train_loader = DataLoader(dataset=dataset, batch_size=batch_size, shuffle=True, num_workers=0)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

model = NeuralNet(input_size=i_s, hidden_size=h_s, num_classes=o_s).to(device)

# loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

for epoch in range(num_epochs):
    for (word, labels) in train_loader:
        word = word.to(device)
        labels = labels.to(device)

        # forwards
        outputs = model(word)
        loss = criterion(outputs, labels)

        # backward and optimizer
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

    if (epoch + 1) % 100 == 0:
        print(f'epoch {epoch+1}/{num_epochs}, loss={loss.item():.4f}')


print(f'final loss, loss={loss.item():.4f}')





i am getting this error
C:\Users\docto\anaconda3\python.exe C:/Users/docto/PycharmProjects/DataScience/DocVedic_updates/train.py
Traceback (most recent call last):
File “C:/Users/docto/PycharmProjects/DataScience/DocVedic_updates/train.py”, line 79, in
outputs = model(word)
File “C:\Users\docto\anaconda3\lib\site-packages\torch\nn\modules\module.py”, line 889, in _call_impl
result = self.forward(*input, **kwargs)
File “C:\Users\docto\PycharmProjects\DataScience\DocVedic_updates\model.py”, line 18, in forward
out = self.l3(x)
File “C:\Users\docto\anaconda3\lib\site-packages\torch\nn\modules\module.py”, line 889, in _call_impl
result = self.forward(*input, **kwargs)
File “C:\Users\docto\anaconda3\lib\site-packages\torch\nn\modules\linear.py”, line 94, in forward
return F.linear(input, self.weight, self.bias)
File “C:\Users\docto\anaconda3\lib\site-packages\torch\nn\functional.py”, line 1753, in linear
return torch._C._nn.linear(input, weight, bias)
RuntimeError: mat1 and mat2 shapes cannot be multiplied (8x44 and 8x7)

The shape mismatch is caused by the nn.Linear layer called self.l3 in your model, so you could check, if the input activation is properly reshaped and if so adapt the in_features of self.l3 to match the features of the input.

This is the code for my CNN(I know, way too many layers). I am getting following runtime error:-
RuntimeError: mat1 and mat2 shapes cannot be multiplied (64x6272 and 57344x7)

self.cnn_layers = nn.Sequential(

            # Defining a 2D convolution layer

            nn.Conv2d(3, 8, kernel_size=3, stride=1, padding=1),

            # Putting a 2D Batchnorm after CNN layer

            nn.BatchNorm2d(8),

            # Adding Relu Activation

            nn.ReLU(inplace=True),

            nn.Conv2d(8, 8, kernel_size=3, stride=1, padding=1),

            nn.BatchNorm2d(8),

            nn.ReLU(inplace=True),

            nn.MaxPool2d(kernel_size=2, stride=2),

            nn.Conv2d(8, 16, kernel_size=3, stride=1, padding=1),

            # Putting a 2D Batchnorm after CNN layer

            nn.BatchNorm2d(16),

            # Adding Relu Activation

            nn.ReLU(inplace=True),

            nn.Conv2d(16, 16, kernel_size=3, stride=1, padding=1),

            nn.BatchNorm2d(16),

            nn.ReLU(inplace=True),

            nn.MaxPool2d(kernel_size=2, stride=2),

            nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=1),

            # Putting a 2D Batchnorm after CNN layer

            nn.BatchNorm2d(32),

            # Adding Relu Activation

            nn.ReLU(inplace=True),

            nn.Conv2d(32, 32, kernel_size=3, stride=1, padding=1),

            nn.BatchNorm2d(32),

            nn.ReLU(inplace=True),

            nn.MaxPool2d(kernel_size=2, stride=2),

            nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),

            # Putting a 2D Batchnorm after CNN layer

            nn.BatchNorm2d(64),

            # Adding Relu Activation

            nn.ReLU(inplace=True),

            nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1),

            nn.BatchNorm2d(64),

            nn.ReLU(inplace=True),

            nn.MaxPool2d(kernel_size=2, stride=2),

            nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),

            # Putting a 2D Batchnorm after CNN layer

            nn.BatchNorm2d(128),

            # Adding Relu Activation

            nn.ReLU(inplace=True),

            nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1),

            nn.BatchNorm2d(128),

            nn.ReLU(inplace=True),

            nn.MaxPool2d(kernel_size=2, stride=2),

            nn.Flatten(),

            nn.Linear(in_features=57344, out_features=7)

        )

Hello, im new to AI and im trying to make net for certain images to determinate cats,dogs etc but I have this error and im kinda confused. I made my own MNIST dataset.

class Neurons:
    def __init__(self):
        dataset = MNIST('C:\\Users\\kratu\\PycharmProjects\\Projekt_AI-Automatyczny_saper\\Engine', gz=True, return_type='numpy')
        images, labels = dataset.load_training()
        test_images, test_labels = dataset.load_testing()
        images = images / 255
        test_images = test_images / 255

        self.train_images = [torch.tensor(image, dtype=torch.float32) for image in images]
        self.train_labels = [torch.tensor(label, dtype=torch.long) for label in labels]
        test_images = [torch.tensor(image, dtype=torch.float32) for image in test_images]
        test_labels = [torch.tensor(label, dtype=torch.long) for label in test_labels]

     input_dim = 28 * 28
        output_dim = 10
        hidden_dim = 10

        self.model = nn.Sequential(
            nn.Linear(input_dim, hidden_dim),
            nn.ReLU(),
            nn.Linear(hidden_dim, output_dim),
            nn.LogSoftmax()
        )
        self.model.eval()
        torch.save(self.train(self.model, 100), 'model.pth')
        imsize = 28
        loader = transforms.Compose([transforms.Resize((imsize, imsize)), transforms.ToTensor()])
        image = Image.open("$FILENAME")
        image = image.convert("1")
        image = loader(image).float()
        image = Variable(image, requires_grad=True)
        predicted = self.model(image)
        print(predicted)

    def train(self,model, n_iter):
        criterion = nn.NLLLoss()
        optimizer = optim.SGD(model.parameters(), lr=0.001)

        for epoch in range(n_iter):
            for image, label in zip(self.train_images, self.train_labels):
                optimizer.zero_grad()

                output = model(image)
                loss = criterion(output.unsqueeze(0), label.unsqueeze(0))
                loss.backward()
                optimizer.step()

            print(f'epoch: {epoch:03}')
        return model


I just want to predict the solution for single image.

RuntimeError: mat1 and mat2 shapes cannot be multiplied (28x28 and 784x10)