RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x24057 and 33x512)

vimal_william: <p>Hi people,<br>
I’m Vimal and I’m new PyTorch, I’m converting a model from tensor flow to PyTorch manually and so i constructed the model and loaded weights. During the model predict I got the error which is mentioned as title. Kindly help with me people.</p>
<p>I attach the model structure for reference</p>
<p><span class="hashtag-raw">#construct</span> the skeleton for PyTorch model<br>
import torch<br>
import torch.nn as nn</p>
<p>class Senic(nn.Module):<br>
def <strong>init</strong>(self):<br>
super().<strong>init</strong>()</p>
<pre><code>    #initalize network layers
    self.conv2d_1 =       nn.Conv2d(in_channels=3, out_channels=10, kernel_size=(5,5))
    self.activation1 =    nn.Tanh()
    self.maxpool1  =      nn.MaxPool2d(kernel_size=(2,2))
    self.dropout1 =       nn.Dropout2d(p=0.4)

    self.conv2d_2 =       nn.Conv2d(in_channels=10, out_channels=96, kernel_size=(4,4))
    self.activation2 =    nn.SELU()
    self.maxpool2 =       nn.MaxPool2d(kernel_size=(2,2))
    self.dropout2 =       nn.Dropout2d(p=0.2)

    self.conv2d_3 =       nn.Conv2d(in_channels=96, out_channels=33, kernel_size=(3,3))
    self.activation3 =    nn.SELU()
    self.maxpool3 =       nn.MaxPool2d(kernel_size=(2,2))
    self.dropout3 =       nn.Dropout2d(p=0.2)
    
    self.dense1 =         nn.Linear(in_features=33, out_features=512)
    self.activation4 =    nn.ReLU()
    self.dropout4 =       nn.Dropout(p=0.2)

    self.dense2 =         nn.Linear(in_features=512, out_features=3)
    self.output =         nn.Softmax(dim=1)

def forward(self, x):

    x = self.conv2d_1(x)
    x = self.activation1(x)
    x = self.maxpool1(x)
    x = self.dropout1(x)

    x = self.conv2d_2(x)
    x = self.activation2(x)
    x = self.maxpool2(x)
    x = self.dropout2(x)

    x = self.conv2d_3(x)
    x = self.activation3(x)
    x = self.maxpool3(x)
    x = self.dropout3(x)

    x = nn.Flatten()(x)
    
    x = self.dense1(x)
    x = self.activation4(x)
    x = self.dropout4(x)

    x = self.dense2(x)
    x = self.output(x)

    return x 
</code></pre>
<p><span class="hashtag-raw">#pack</span> the model<br>
senic_model = Senic()</p>

ptrblck: <p>What’s the input shape of your model?<br>
Based on the error message it seems <code>self.dense1</code>’s <code>in_features</code> do not match the activation features.</p>

vimal_william: <p>I took one image for testing which is [1, 3, 240, 240] in shape</p>

Mah_Neh: <p>Figured out one way, i.e using LazyLinear, So you do not need to pass the input feature size if you do not know, mind that, even though the feature exist from a long time ago there is a warning:</p>
<blockquote>
<p>Warning: Lazy modules are a new feature under heavy development so changes to the API or functionality can happen at any moment.<br>
warnings.warn('Lazy modules are a new feature under heavy development</p>
</blockquote>
<pre><code class="lang-auto"># construct the skeleton for PyTorch model
import torch
import torch.nn as nn
from torchinfo import summary


class Senic(nn.Module):
    def __init__(self):
        super().__init__()
        # initalize network layers
        self.conv2d_1 = nn.Conv2d(in_channels=3, out_channels=10, kernel_size=(5, 5))
        self.activation1 = nn.Tanh()
        self.maxpool1 = nn.MaxPool2d(kernel_size=(2, 2))
        self.dropout1 = nn.Dropout2d(p=0.4)

        self.conv2d_2 = nn.Conv2d(in_channels=10, out_channels=96, kernel_size=(4, 4))
        self.activation2 = nn.SELU()
        self.maxpool2 = nn.MaxPool2d(kernel_size=(2, 2))
        self.dropout2 = nn.Dropout2d(p=0.2)

        self.conv2d_3 = nn.Conv2d(in_channels=96, out_channels=33, kernel_size=(3, 3))
        self.activation3 = nn.SELU()
        self.maxpool3 = nn.MaxPool2d(kernel_size=(2, 2))
        self.dropout3 = nn.Dropout2d(p=0.2)

        self.dense1 = nn.LazyLinear(out_features=512)  # &lt;--- this is new
        self.activation4 = nn.ReLU()
        self.dropout4 = nn.Dropout(p=0.2)

        self.dense2 = nn.Linear(in_features=512, out_features=3)
        self.output = nn.Softmax(dim=1)

    def forward(self, x):
        x = self.conv2d_1(x)
        x = self.activation1(x)
        x = self.maxpool1(x)
        x = self.dropout1(x)

        x = self.conv2d_2(x)
        x = self.activation2(x)
        x = self.maxpool2(x)
        x = self.dropout2(x)
        x = self.conv2d_3(x)
        x = self.activation3(x)
        x = self.maxpool3(x)
        x = self.dropout3(x)

        x = nn.Flatten()(x)

        x = self.dense1(x)
        x = self.activation4(x)
        x = self.dropout4(x)

        x = self.dense2(x)
        x = self.output(x)

        return x
        # pack the model

# example
image = torch.randn(1, 3, 224, 224)
senic_model = Senic()
summary(senic_model, input_data=image, verbose=2)
</code></pre>

ptrblck: <p><a class="mention" href="/u/mah_neh">@Mah_Neh</a>’s suggestion is valid, but alternatively you could also manually check the activation shape before the <code>Flatten()</code> module is used, which will show <code>[batch_size, 33, 27, 27]</code>. Your <code>self.dense1</code> layer currently expects an activation with 33 input features, which will cause the error, since the activation has <code>33 * 27 * 27 = 24057</code> features. Use either the lazy layer or set <code>in_features=24057</code> in <code>self.dense1</code>.</p>

vimal_william: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> and <a class="mention" href="/u/mah_neh">@Mah_Neh</a> thanks people for your suggestions. I will make those changes and test it again</p>