Mat1 dim doesn't matches the mat2 dim

Kaneda: <p>Hey, newbie here</p>
<p>Trying to build this classification model<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/0/203b91f1109ebb8d98c9fa715e979336ee26821f.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/203b91f1109ebb8d98c9fa715e979336ee26821f" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/2/0/203b91f1109ebb8d98c9fa715e979336ee26821f_2_690x286.jpeg" alt="image" data-base62-sha1="4B8UfW9Yc9CryoEIak3XEA24eAL" width="690" height="286" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/2/0/203b91f1109ebb8d98c9fa715e979336ee26821f_2_690x286.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/2/0/203b91f1109ebb8d98c9fa715e979336ee26821f_2_1035x429.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/2/0/203b91f1109ebb8d98c9fa715e979336ee26821f_2_1380x572.jpeg 2x" data-dominant-color="E8E8E9"><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">image</span><span class="informations">1685×700 165 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>
<pre><code class="lang-auto">class Net(nn.Module):
  def __init__(self):
    super(Net, self).__init__()
    self.conv1 = nn.Conv2d(in_channels=1, out_channels=32, kernel_size=3)  
    self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3) 
    self.pool = nn.MaxPool2d(2,2)
    self.fc1 = nn.Linear(32*7*7, 128) # or 64, not sure here
    self.fc2 = nn.Linear(128, 10)
    self.dropout = torch.nn.Dropout(p=0.5)
    self.relu = torch.nn.ReLU()

  def forward(self, x):
    x = self.conv1(x)
    x = self.relu(x)
    x = self.pool(x)
    x = self.conv2(x) 
    x = self.relu(x)
    x = self.pool(x) 
    x = x.reshape(x.size(0), -1) 
    x = self.fc1(x) 
    x = self.dropout(x)
    prediction = self.fc2(x) 

    return prediction
</code></pre>
<p>Outputs this error</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "main.py", line 36, in &lt;module&gt;
    outputs = net(inputs)
  File "/home/kaneda/anaconda3/envs/home/lib/python3.8/site-packages/torch/nn/modules/module.py", line 722, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/kaneda/Documents/mnist_dataset/models/cnn.py", line 23, in forward
    x = self.fc1(x) # 128 * 10
  File "/home/kaneda/anaconda3/envs/home/lib/python3.8/site-packages/torch/nn/modules/module.py", line 722, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/kaneda/anaconda3/envs/home/lib/python3.8/site-packages/torch/nn/modules/linear.py", line 91, in forward
    return F.linear(input, self.weight, self.bias)
  File "/home/kaneda/anaconda3/envs/home/lib/python3.8/site-packages/torch/nn/functional.py", line 1674, in linear
    ret = torch.addmm(bias, input, weight.t())
RuntimeError: mat1 dim 1 must match mat2 dim 0
</code></pre>
<p>Any ideas? Using pytorch 1.6</p>

user_123454321: <p>Can you replace with this?</p>
<pre><code class="lang-auto">class Net(nn.Module):
  def __init__(self):
    super(Net, self).__init__()
    self.conv1 = nn.Conv2d(in_channels=1, out_channels=32, kernel_size=3, padding=1)  # notice the padding
    self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, padding=1) # again...
    self.pool = nn.MaxPool2d(2,2)
    self.fc1 = nn.Linear(64*7*7, 128) # it is 64....
    self.fc2 = nn.Linear(128, 10)
    self.dropout = torch.nn.Dropout(p=0.5)
    self.relu = torch.nn.ReLU()
  def forward(self, x):
    x = self.conv1(x)
    x = self.relu(x)
    x = self.pool(x)
    x = self.conv2(x) 
    x = self.relu(x)
    x = self.pool(x) 
    x = x.reshape(x.size(0), -1) 
    x = self.fc1(x) 
    x = self.dropout(x)
    prediction = self.fc2(x) 
</code></pre>