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

zhengquantao: <p>who can help me?</p>
<pre><code class="lang-auto">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) -&gt; 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")
</code></pre>
<p>my image size [70, 26]  and one of images name is  okzi.jpeg<br>
I am first touch pytorch so i don`t understand this question<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/8/08e557d6777a47ceeffb5002b64cb183f5f4b0dc.png" data-download-href="https://discuss.pytorch.org/uploads/default/08e557d6777a47ceeffb5002b64cb183f5f4b0dc" title="PP7{}5GRRC7EVLMTQJYD1L"><img src="https://discuss.pytorch.org/uploads/default/original/3X/0/8/08e557d6777a47ceeffb5002b64cb183f5f4b0dc.png" alt="PP7{}5GRRC7EVLMTQJYD1L" data-base62-sha1="1gHbtCezKxpst2H4bITQnnNnUAc" width="690" height="400" data-dominant-color="313137"><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">PP7{}5GRRC7EVLMTQJYD1L</span><span class="informations">926×537 77.6 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>
<details>
<summary>
Summary</summary>
<p>This text will be hidden</p>
</details>

ptrblck: <p>The activation input <code>x</code> to <code>self.fc</code> doesn’t have the expected number of features, so you would need to change the <code>in_features</code> of the first <code>nn.Linear</code> layer in <code>self.fc</code> to <code>13056</code>.</p>
<p>Also, unrelated to this issue, but <code>Variable</code>s are deprecated since PyTorch <code>0.4.0</code>, so you can use tensors now. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

zhengquantao: <p>thanks  for you <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

zhengquantao: <p>maybe  i make a mistation,  this is difficut for me <img src="https://discuss.pytorch.org/images/emoji/apple/thinking.png?v=12" title=":thinking:" class="emoji" alt=":thinking:" loading="lazy" width="20" height="20"><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/e/ae87ba795869106e768aea6d270db0858f08b04d.png" data-download-href="https://discuss.pytorch.org/uploads/default/ae87ba795869106e768aea6d270db0858f08b04d" title="D~Q(XDE%6R4CX(`OEJVWKX"><img src="https://discuss.pytorch.org/uploads/default/original/3X/a/e/ae87ba795869106e768aea6d270db0858f08b04d.png" alt="D~Q(XDE%6R4CX(`OEJVWKX" data-base62-sha1="oTXSlOQMfhtuVPv9yTIzHrqPjFH" width="690" height="334" data-dominant-color="333133"><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">D~Q(XDE%6R4CX(`OEJVWKX</span><span class="informations">1366×662 42.7 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>

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

darnoceloc: <p>I’m getting RuntimeError: mat1 and mat2 shapes cannot be multiplied (836x35344 and 836x35344).</p>
<pre><code class="lang-auto">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
</code></pre>

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

darnoceloc: <p>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?</p>

ptrblck: <p>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.<br>
Also, I would recommend to take a look at the <a href="https://pytorch.org/tutorials/">PyTorch tutorials</a> and/or some courses such as <a href="https://www.fast.ai/">FastAI</a>.</p>

fares_haouas: <p>class Net(nn.Module):</p>
<pre><code>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
</code></pre>
<p>I’m getting RuntimeError: mat1 and mat2 shapes cannot be multiplied (10x89888 and 86528x136)</p>

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

jorgerodpen: <p>I am getting the same error:</p>
<pre><code class="lang-auto">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
</code></pre>
<p>Manual implementation actually works so I do not know what could I be doing wrong. My input images are 100x100. The error reads:</p>
<blockquote>
<p>RuntimeError: mat1 and mat2 shapes cannot be multiplied (128x4624 and 200x200)</p>
</blockquote>
<p>Why is the shape suddenly changed from the batch size to 200?</p>

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

jorgerodpen: <p>Hi, thank you for your answer but isn’t the line</p>
<pre><code class="lang-auto">hidden = [nn.Linear(self.output_conv, hidden_dim) if i==0 else nn.Linear(hidden_dim, hidden_dim) for i in range(hidden_num)]
</code></pre>
<p>generating a line of linear layers with the first layer with input size self.output_conv? Or am I missing something?</p>
<p>Thank you once again.</p>

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

jorgerodpen: <p>You are totally right! Thank you very much for your help.</p>

Melody_Fusion: <pre><code class="lang-auto">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}')





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

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

rovernova: <p>This is the code for my CNN(I know, way too many layers). I am getting following runtime error:-<br>
RuntimeError: mat1 and mat2 shapes cannot be multiplied (64x6272 and 57344x7)</p>
<pre><code class="lang-auto">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)

        )
</code></pre>

Kraton99: <p>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.</p>
<pre><code class="lang-auto">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


</code></pre>
<p>I just want to predict the solution for single image.</p>
<pre><code class="lang-auto">RuntimeError: mat1 and mat2 shapes cannot be multiplied (28x28 and 784x10)

</code></pre>