title: RuntimeError: mat1 dim 1 must match mat2 dim 0 Pytorch
body: <p>I am trying to evaluate my model on random weight and bias before training by calling the  <code>evaluate(model, valid_dl)</code> but I get the below error.</p>
  <p>I have also tried debugging my code by the printing the size of tensor after taking a step by step walk through on my error traceback.</p>
  <p>I have also check similar problem on SO but the solutions are not working for me.</p>
  <p>I think the error is coming from <code>self.classifier</code>. it appear there is something I am not getting right there. Please I need your help .</p>
  <p><strong>Error traceback</strong></p>
  <pre><code>---------------------------------------------------------------------------
  RuntimeError                              Traceback (most recent call last)
  &lt;ipython-input-31-1c4791c5f87e&gt; in &lt;module&gt;
  ----&gt; 1 history = [evaluate(model, valid_dl)]
        2 history
  
  /opt/conda/lib/python3.7/site-packages/torch/autograd/grad_mode.py in decorate_context(*args, **kwargs)
       24         def decorate_context(*args, **kwargs):
       25             with self.__class__():
  ---&gt; 26                 return func(*args, **kwargs)
       27         return cast(F, decorate_context)
       28 
  
  &lt;ipython-input-30-b49ebc28d6a3&gt; in evaluate(model, val_loader)
        2 def evaluate(model, val_loader):
        3     model.eval()
  ----&gt; 4     outputs = [model.validation_step(batch) for batch in val_loader]
        5     return model.validation_epoch_end(outputs)
        6 
  
  &lt;ipython-input-30-b49ebc28d6a3&gt; in &lt;listcomp&gt;(.0)
        2 def evaluate(model, val_loader):
        3     model.eval()
  ----&gt; 4     outputs = [model.validation_step(batch) for batch in val_loader]
        5     return model.validation_epoch_end(outputs)
        6 
  
  &lt;ipython-input-23-e9609521578c&gt; in validation_step(self, batch)
       12     def validation_step(self, batch):
       13         images, labels = batch
  ---&gt; 14         out = self(images)                   # Generate predictions
       15         loss = F.cross_entropy(out, labels)   # Calculate loss
       16         acc = accuracy(out, labels)           # Calculate accuracy
  
  /opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
      725             result = self._slow_forward(*input, **kwargs)
      726         else:
  --&gt; 727             result = self.forward(*input, **kwargs)
      728         for hook in itertools.chain(
      729                 _global_forward_hooks.values(),
  
  &lt;ipython-input-28-3b2b2b49d22e&gt; in forward(self, xb)
       34         print(out.shape)
       35 
  ---&gt; 36         out = self.classifier(out)
       37         print(out.shape)
       38         return out
  
  /opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
      725             result = self._slow_forward(*input, **kwargs)
      726         else:
  --&gt; 727             result = self.forward(*input, **kwargs)
      728         for hook in itertools.chain(
      729                 _global_forward_hooks.values(),
  
  /opt/conda/lib/python3.7/site-packages/torch/nn/modules/container.py in forward(self, input)
      115     def forward(self, input):
      116         for module in self:
  --&gt; 117             input = module(input)
      118         return input
      119 
  
  /opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
      725             result = self._slow_forward(*input, **kwargs)
      726         else:
  --&gt; 727             result = self.forward(*input, **kwargs)
      728         for hook in itertools.chain(
      729                 _global_forward_hooks.values(),
  
  /opt/conda/lib/python3.7/site-packages/torch/nn/modules/linear.py in forward(self, input)
       91 
       92     def forward(self, input: Tensor) -&gt; Tensor:
  ---&gt; 93         return F.linear(input, self.weight, self.bias)
       94 
       95     def extra_repr(self) -&gt; str:
  
  /opt/conda/lib/python3.7/site-packages/torch/nn/functional.py in linear(input, weight, bias)
     1688     if input.dim() == 2 and bias is not None:
     1689         # fused op is marginally faster
  -&gt; 1690         ret = torch.addmm(bias, input, weight.t())
     1691     else:
     1692         output = input.matmul(weight.t())
  
  RuntimeError: mat1 dim 1 must match mat2 dim 0
  </code></pre>
  <p>Model</p>
  <pre><code>def conv_block(in_channels, out_channels, pool=False):
      layers = [nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1), 
                nn.BatchNorm2d(out_channels), 
                nn.ReLU(inplace=True)]
      if pool: layers.append(nn.MaxPool2d(2))
      return nn.Sequential(*layers)
  
  class ResNet9(ImageClassificationBase):
      def __init__(self, in_channels, num_classes):
          super().__init__()
          
          self.conv1 = conv_block(in_channels, 64)
          self.conv2 = conv_block(64, 128, pool=True)
          self.res1 = nn.Sequential(conv_block(128, 128), conv_block(128, 128))
          
          self.conv3 = conv_block(128, 256, pool=True)
          self.conv4 = conv_block(256, 512, pool=True)
          self.res2 = nn.Sequential(conv_block(512, 512), conv_block(512, 512))
          
          self.classifier = nn.Sequential(nn.MaxPool2d(4), 
                                          nn.Flatten(), 
                                          nn.Dropout(0.2),
                                          nn.Linear(512, num_classes))
          
      def forward(self, xb):
          out = self.conv1(xb)
          print(out.shape)
          out = self.conv2(out)
          print(out.shape)
          out = self.res1(out) + out
          print(out.shape)
          out = self.conv3(out)
          print(out.shape)
          out = self.conv4(out)
          print(out.shape)
          out = self.res2(out) + out 
          print(out.shape)       
          out = self.classifier(out)
          #Error
          print(out.shape)
          
          return out
  enter code here
  </code></pre>
  <p>Output shapes from the forward pass</p>
  <pre><code>torch.Size([32, 64, 400, 400])
  torch.Size([32, 128, 200, 200])
  torch.Size([32, 128, 200, 200])
  torch.Size([32, 256, 100, 100])
  torch.Size([32, 512, 50, 50])
  torch.Size([32, 512, 50, 50])
  </code></pre>
  <p>but when I check the shape after <code>self.classifier</code> I get am error</p>