RuntimeError: mat1 and mat2 shapes cannot be multiplied (256x3136 and 12544x512)

Aristotle_Aletheia: <p>import torch<br>
import torch.nn as nn<br>
import torch.nn.functional as F</p>
<p>class DahwinFaceNet(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(DahwinFaceNet, self).<strong>init</strong>()<br>
self.conv1 = nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1)<br>
self.bn1 = nn.BatchNorm2d(32)<br>
self.conv2 = nn.Conv2d(32, 32, kernel_size=3, stride=1, padding=1)<br>
self.bn2 = nn.BatchNorm2d(32)<br>
self.conv3 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)<br>
self.bn3 = nn.BatchNorm2d(64)<br>
self.conv4 = nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1)<br>
self.bn4 = nn.BatchNorm2d(64)<br>
self.conv5 = nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1)<br>
self.bn5 = nn.BatchNorm2d(64)<br>
self.conv6 = nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1)<br>
self.bn6 = nn.BatchNorm2d(64)<br>
self.conv7 = nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1)<br>
self.bn7 = nn.BatchNorm2d(64)<br>
self.conv8_1 = nn.Conv2d(64, 64, kernel_size=1, stride=1, padding=0)<br>
self.conv8_2 = nn.Conv2d(64, 64, kernel_size=7, stride=1, padding=0)<br>
self.conv9_1 = nn.Conv2d(64, 64, kernel_size=1, stride=1, padding=0)<br>
self.conv9_2 = nn.Conv2d(64, 64, kernel_size=7, stride=1, padding=0)<br>
self.conv10_1 = nn.Conv2d(64, 64, kernel_size=1, stride=1, padding=0)<br>
self.conv10_2 = nn.Conv2d(64, 64, kernel_size=7, stride=1, padding=0)<br>
self.conv11_1 = nn.Conv2d(64, 64, kernel_size=1, stride=1, padding=0)<br>
self.conv11_2 = nn.Conv2d(64, 64, kernel_size=7, stride=1, padding=0)<br>
self.fc = nn.Linear(64 * 14 * 14, 512)  # Removed BatchNorm1d layer<br>
self.fc1 = nn.Linear(512, 6)</p>
<pre><code>def forward(self, x):
    # Feature extraction
    x = F.relu(self.bn1(self.conv1(x)))
    x = F.max_pool2d(F.relu(self.bn2(self.conv2(x))), 2)
    x = F.relu(self.bn3(self.conv3(x)))
    x = F.max_pool2d(F.relu(self.bn4(self.conv4(x))), 2)
    x = F.relu(self.bn5(self.conv5(x)))
    x = F.max_pool2d(F.relu(self.bn6(self.conv6(x))), 2)
    x = F.relu(self.bn7(self.conv7(x)))
    x = F.max_pool2d(x, 2)
    print(x.shape)
    x = x.view(-1, 64 * 7 * 7)
    x = self.fc(x)  # Removed L2 normalization
    return x
</code></pre>
<h1><a name="create-the-model-1" class="anchor" href="#create-the-model-1"></a>Create the model</h1>
<p>torch.manual_seed(42)<br>
model = DahwinFaceNet()</p>
<p>criterion = nn.CrossEntropyLoss()<br>
optimizer = optim.Adam(model.parameters(), lr=0.001)</p>
<h1><a name="train-model-2" class="anchor" href="#train-model-2"></a>Train model</h1>
<p>for epoch in range(10):<br>
model.train()<br>
for inputs, labels in train_loader:<br>
print(f"Input shape: {inputs.shape}, Labels shape: {labels.shape}")<br>
# Your training code here<br>
optimizer.zero_grad()<br>
outputs = model(inputs)<br>
loss = criterion(outputs, labels)<br>
loss.backward()<br>
optimizer.step()</p>
<pre><code># Evaluate model
model.eval()
correct = 0
total = 0
with torch.no_grad():
    for inputs, labels in test_loader:
        outputs = model(inputs)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

accuracy = 100 * correct / total
print('Epoch: {}, Test Accuracy: {}%'.format(epoch, accuracy))
</code></pre>
<h1><a name="save-model-3" class="anchor" href="#save-model-3"></a>Save model</h1>
<p>torch.save(model.state_dict(), ‘model.pth’)</p>

ptrblck: <p>You are explicitly reshaping the intermediate activation to have <code>3136</code> features via:</p>
<pre><code class="lang-python">x = x.view(-1, 64 * 7 * 7)
</code></pre>
<p>while the following <code>self.fc</code> layer expects <code>12544</code> features as seen in its definition:</p>
<pre><code class="lang-python">self.fc = nn.Linear(64 * 14 * 14, 512)
</code></pre>

Mah_Neh: <p>how are you so good at finding bugs i wonder <img src="https://discuss.pytorch.org/images/emoji/apple/joy.png?v=12" title=":joy:" class="emoji" alt=":joy:" loading="lazy" width="20" height="20"></p>