RuntimeError: size mismatch, m1: [32 x 3888], m2: [2048 x 32] at /pytorch/aten/src/TH/generic/THTensorMath.cpp:197

username: video_analytics
cooked: <p>Hi</p>
<p>I am performing simple image classification task my net is provided below but whenever I am runiing th code I ma getting the follwoing error. Can anyone please help me out hot to fix it.</p>
<p>class CNN(torch.nn.Module):</p>
<pre><code>def __init__(self):
    super(CNN, self).__init__()
    # L1 ImgIn shape=(?, 28, 28, 1)
    #    Conv     -&gt; (?, 28, 28, 32)
    #    Pool     -&gt; (?, 14, 14, 32)
    self.layer1 = torch.nn.Sequential(
        torch.nn.Conv2d(3, 12, kernel_size=3, stride=1, padding=1),
        torch.nn.ReLU(),
        torch.nn.MaxPool2d(kernel_size=2, stride=2),
        torch.nn.Dropout(p=1 - keep_prob))
    # L2 ImgIn shape=(?, 14, 14, 32)
    #    Conv      -&gt;(?, 14, 14, 64)
    #    Pool      -&gt;(?, 7, 7, 64)
    self.layer2 = torch.nn.Sequential(
        torch.nn.Conv2d(12, 24, kernel_size=3, stride=1, padding=1),
        torch.nn.ReLU(),
        torch.nn.MaxPool2d(kernel_size=2, stride=2),
        torch.nn.Dropout(p=1 - keep_prob))
    # L3 ImgIn shape=(?, 7, 7, 64)
    #    Conv      -&gt;(?, 7, 7, 128)
    #    Pool      -&gt;(?, 4, 4, 128)
    self.layer3 = torch.nn.Sequential(
        torch.nn.Conv2d(24, 48, kernel_size=3, stride=1, padding=1),
        torch.nn.ReLU(),
        torch.nn.MaxPool2d(kernel_size=2, stride=2, padding=1),
        torch.nn.Dropout(p=1 - keep_prob))

    # L4 FC 4x4x128 inputs -&gt; 625 outputs
    self.fc1 = torch.nn.Linear(4 * 4 * 128, 32, bias=True)
    torch.nn.init.xavier_uniform(self.fc1.weight)
    self.layer4 = torch.nn.Sequential(
        self.fc1,
        torch.nn.ReLU(),
        torch.nn.Dropout(p=1 - keep_prob))
    # L5 Final FC 625 inputs -&gt; 10 outputs
    self.fc2 = torch.nn.Linear(32, 2, bias=True)
    torch.nn.init.xavier_uniform_(self.fc2.weight) # initialize parameters

def forward(self, x):
    out = self.layer1(x)
    out = self.layer2(out)
    out = self.layer3(out)
    out = out.view(out.size(0), -1)   # Flatten them for FC
    out = self.fc1(out)
    out = self.fc2(out)
    return out
</code></pre>
<h1>instantiate CNN model</h1>
<p>model = CNN()<br>
model</p>
<p>learning_rate = 0.001<br>
criterion = torch.nn.CrossEntropyLoss()    # Softmax is internally computed.<br>
optimizer = torch.optim.Adam(params=model.parameters(), lr=learning_rate)</p>
<p>print(‘Training the Deep Learning network …’)<br>
train_cost = []<br>
train_accu = []</p>
<p>training_epochs = 50<br>
total_batch = train_data_size // bs</p>
<p>print(‘Size of the training dataset is {}’.format(train_data_size))<br>
print(‘Size of the testing dataset is {}’.format(test_data_size))<br>
print(‘Batch size is : {}’.format(bs))<br>
print(‘Total number of batches is : {0:2.0f}’.format(total_batch))<br>
print(’\nTotal number of epochs is : {0:2.0f}’.format(training_epochs))</p>
<p>for epoch in range(training_epochs):<br>
avg_cost = 0<br>
for i, (batch_X, batch_Y) in enumerate(train_data_loader):<br>
X = Variable(batch_X)    # image is already size of (28x28), no reshape<br>
Y = Variable(batch_Y)    # label is not one-hot encoded</p>
<pre><code>    optimizer.zero_grad() # &lt;= initialization of the gradients
    
    # forward propagation
    hypothesis = model(X)
    cost = criterion(hypothesis, Y) # &lt;= compute the loss function
    
    # Backward propagation
    cost.backward() # &lt;= compute the gradient of the loss/cost function     
    optimizer.step() # &lt;= Update the gradients
         
    # Print some performance to monitor the training
    prediction = hypothesis.data.max(dim=1)[1]
    train_accu.append(((prediction.data == Y.data).float().mean()).item())
    train_cost.append(cost.item())   
    if i % 200 == 0:
        print("Epoch= {},\t batch = {},\t cost = {:2.4f},\t accuracy = {}".format(epoch+1, i, train_cost[-1], train_accu[-1]))
   
    avg_cost += cost.data / total_batch

print("[Epoch: {:&gt;4}], averaged cost = {:&gt;.9}".format(epoch + 1, avg_cost.item()))
</code></pre>
<p>print(‘Learning Finished!’)</p>
<h1>_, argmax = torch.max(outputs, 1)</h1>
<p>accuracy = (labels == argmax.squeeze()).float().mean()</p>
<p>RuntimeError: size mismatch, m1: [32 x 3888], m2: [2048 x 32] at /pytorch/aten/src/TH/generic/THTensorMath.cpp:197</p>

username: albanD
cooked: <p>Hi,</p>
<p>Do you have the python stack trace? This happens because you give matrices of the wrong size to a matrix multiply most likely.<br>
You can add some prints during your forward pass to check if the Tensors are of the size you expect.</p>

username: video_analytics
cooked: <aside class="quote no-group" data-username="albanD" data-post="2" data-topic="64337">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/48/215_2.png" class="avatar"> albanD:</div>
<blockquote>
<p>python stack trace</p>
</blockquote>
</aside>
<p>So both the m1 and m2 should be exactly of same size ?</p>

username: albanD
cooked: <p>If it is a matrix multiply, then only the second dimension of m1 and the first dimension of m2 needs to match.</p>

username: video_analytics
cooked: <p>I am design cnn network the code is provided above can you please help me out how to fix it.</p>
<p>Thanks in advance …</p>

username: albanD
cooked: <p>If you changed the architecture, this most likely happens because the output of the convolution that you <code>view</code> for the linear layer does not have the right size compared to what the linear layer expects.</p>
<p>In particular, you want to add prints around this line <code>out = out.view(out.size(0), -1)</code> to make sure the size you have match what <code>self.fc1</code> expects.</p>

username: video_analytics
cooked: <p>Thanks problem is resolved …</p>