Mat1 and mat2 shapes cannot be multiplied (1x57344 and 8192x256)

Shabih_Haider: <p>I am trying to make a simple neural network with CNN and RNN model but somehow I am getting this error. What I am missing here ?</p>
<p>import torch<br>
import torch.nn as nn<br>
from torchsummary import summary</p>
<p>def fit(a, b):<br>
def ctc_loss_func(y_pred, names, input_length, name_length):<br>
y_pred = y_pred[:, 2:, :]<br>
return nn.functional.ctc_loss(y_pred.transpose(0, 1), names, input_length.squeeze(), name_length.squeeze(), blank=0)</p>
<pre><code>input_data = nn.Sequential(
    nn.Conv2d(1, 32, kernel_size=3, padding=1),
    nn.BatchNorm2d(32),
    nn.ReLU(),
    nn.MaxPool2d(kernel_size=2),
    nn.Conv2d(32, 64, kernel_size=3, padding=1),
    nn.BatchNorm2d(64),
    nn.ReLU(),
    nn.MaxPool2d(kernel_size=2),
    nn.Dropout2d(p=0.3),
    nn.Conv2d(64, 128, kernel_size=3, padding=(1, 0)),
    nn.BatchNorm2d(128),
    nn.ReLU(),
    nn.MaxPool2d(kernel_size=(1, 2)),
    nn.Dropout2d(p=0.3),
    nn.Flatten()
)

inner = input_data(torch.randn(1, 1, 256, 64))

# CNN to RNN

inner = nn.Sequential(
    nn.Linear(64*128, 64),
    nn.ReLU()
)(inner)

# RNN

num_features = inner.size()[1]
inner = inner.unsqueeze(0)
inner, _ = nn.LSTM(num_features, 256, bidirectional=True, batch_first=True)(inner)
inner = inner.squeeze(0)

alphabets = a
num_characters = len(a) + 1
max_str_len = b

# Output
num_features = 2 * 256
batch_size = 1
inner = nn.Linear(num_features, max_str_len * num_characters)(inner)
inner = inner.reshape(batch_size, max_str_len * num_characters, num_features)
y_pred = nn.functional.softmax(inner, dim=2)

model = nn.Sequential(input_data, inner, y_pred)

summary(model, input_size=(1, 256, 64))

return model
</code></pre>
<p>alphabets = u’“0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ#$%'”‘"’ ()*+,-:.=?@^`~µÂ×öΩμ√_’<br>
max_str_len = 87 # max length of input name<br>
fit(alphabets,max_str_len)</p>
<p>This is the error I am getting,Please Help</p>
<p> in fit(a, b)<br>
34     # CNN to RNN<br>
35<br>
—&gt; 36     inner = nn.Sequential(<br>
37         nn.Linear(64*128, 256),<br>
38         nn.ReLU()</p>
<p>/usr/local/lib/python3.9/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
1192         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks<br>
1193                 or _global_forward_hooks or _global_forward_pre_hooks):<br>
 → 1194             return forward_call(*input, **kwargs)<br>
1195         # Do not call functions when jit is used<br>
1196         full_backward_hooks, non_full_backward_hooks = [], []</p>
<p>/usr/local/lib/python3.9/dist-packages/torch/nn/modules/container.py in forward(self, input)<br>
202     def forward(self, input):<br>
203         for module in self:<br>
 → 204             input = module(input)<br>
205         return input<br>
206</p>
<p>/usr/local/lib/python3.9/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
1192         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks<br>
1193                 or _global_forward_hooks or _global_forward_pre_hooks):<br>
 → 1194             return forward_call(*input, **kwargs)<br>
1195         # Do not call functions when jit is used<br>
1196         full_backward_hooks, non_full_backward_hooks = [], []</p>
<p>/usr/local/lib/python3.9/dist-packages/torch/nn/modules/linear.py in forward(self, input)<br>
112<br>
113     def forward(self, input: Tensor) → Tensor:<br>
 → 114         return F.linear(input, self.weight, self.bias)<br>
115<br>
116     def extra_repr(self) → str:</p>
<p>RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x57344 and 8192x256)</p>

AbdulsalamBande: <p>The error you are encountering is due to a mismatch in the input and output dimensions of the Linear layer in your <code>nn.Sequential</code>. The input shape is not matching the expected shape of the Linear layer.</p>
<p>The input tensor <code>inner</code> has the shape <code>(1, 1, 256, 64)</code>, but you’re using a Linear layer with input dimension <code>64*128</code> which is <code>8192</code>. This is not the correct input size for this layer.</p>
<p>To resolve this issue, you need to first flatten the input tensor and then use the correct input dimension for the Linear layer. Based on the shape of <code>inner</code>, the input dimension should be <code>1*256*64</code>.</p>
<blockquote>
<p>Here’s the corrected code part of the code to be replaced:</p>
</blockquote>
<pre><code class="lang-auto">import torch
import torch.nn as nn

input_data = torch.randn(1, 1, 256, 64)

# Flatten the input tensor
inner = input_data.view(1, -1) # torch.Size([1, 16384])

# CNN to RNN
inner = nn.Sequential(
    nn.Linear(1*256*64, 256),
    nn.ReLU()
)(inner)

</code></pre>

Shabih_Haider: <p>import torch<br>
import torch.nn as nn<br>
from torchsummary import summary</p>
<p>def fit(a, b):<br>
def ctc_loss_func(y_pred, names, input_length, name_length):<br>
y_pred = y_pred[:, 2:, :]<br>
return nn.functional.ctc_loss(y_pred.transpose(0, 1), names, input_length.squeeze(), name_length.squeeze(), blank=0)</p>
<pre><code>input_data = nn.Sequential(
    nn.Conv2d(1, 32, kernel_size=3, padding=1),
    nn.BatchNorm2d(32),
    nn.ReLU(),
    nn.MaxPool2d(kernel_size=2),
    nn.Conv2d(32, 64, kernel_size=3, padding=1),
    nn.BatchNorm2d(64),
    nn.ReLU(),
    nn.MaxPool2d(kernel_size=2),
    nn.Dropout2d(p=0.3),
    nn.Conv2d(64, 128, kernel_size=3, padding=(1, 0)),
    nn.BatchNorm2d(128),
    nn.ReLU(),
    nn.MaxPool2d(kernel_size=(1, 2)),
    nn.Dropout2d(p=0.3),
    nn.Flatten()
)

input_data = torch.randn(1, 1, 256, 64)

# Flatten the input tensor
inner = input_data.view(1, -1) # torch.Size([1, 16384])

# CNN to RNN
inner = nn.Sequential(
    nn.Linear(1*256*64, 256),
    nn.ReLU()
)(inner)


# RNN

num_features = inner.size()[1]
inner = inner.unsqueeze(0)
inner, _ = nn.LSTM(num_features, 256, bidirectional=True, batch_first=True)(inner)
inner = inner.squeeze(0)

alphabets = a
num_characters = len(a) + 1
max_str_len = b

# Output
num_features = 2 * 256
batch_size = 1
inner = nn.Linear(num_features, max_str_len * num_characters)(inner)
inner = inner.reshape(batch_size, max_str_len * num_characters, num_features)
y_pred = nn.functional.softmax(inner, dim=2)

model = nn.Sequential(input_data, inner, y_pred)

summary(model, input_size=(1, 256, 64))

return model
</code></pre>
<p>alphabets = u’“0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ#$%'”‘"’ ()*+,-:.=?@^`~µÂ×öΩμ√_’<br>
max_str_len = 87 # max length of input name<br>
fit(alphabets,max_str_len)</p>
<p>so here is the updated code with your feedback but now I am getting this error:</p>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in &lt;cell line: 66&gt;()<br>
64 alphabets = u’“0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ#$%'”‘"’ ()*+,-:.=?@^`~µÂ×öΩμ√_’<br>
65 max_str_len = 87 # max length of input name<br>
—&gt; 66 fit(alphabets,max_str_len)</p>
<p> in fit(a, b)<br>
53     batch_size = 1<br>
54     inner = nn.Linear(num_features, max_str_len * num_characters)(inner)<br>
—&gt; 55     inner = inner.reshape(batch_size, max_str_len * num_characters, num_features)<br>
56     y_pred = nn.functional.softmax(inner, dim=2)<br>
57</p>
<p>RuntimeError: shape ‘[1, 7917, 512]’ is invalid for input of size 7917</p>