Hi Johny!



 johny:

    self.layer1 = self._make_layer(block, 64, num_blocks[0], stride = 1)
    self.layer2 = self._make_layer(block, 128, num_blocks[1], stride = 2)
    self.layer3 = self._make_layer(block, 256, num_blocks[2], stride = 2)
    self.layer4 = self._make_layer(block, 512, num_blocks[3], stride = 2)
    self.linear = nn.Linear(512, num_classes)
...
  def forward(self, x):
    out = F.relu(self.bn1(self.conv1(x)))
    out = self.layer1(out)
    out = self.layer2(out)
    out = self.layer3(out)
    out = F.avg_pool2d(out, 4)
    out = out.view(out.size(0), -1)
    out = self.linear(out)
    return out



Note that you define self.layer4, but you never use it.  Perhaps
you want in forward()
    ...
    out = self.layer2(out)
    out = self.layer3(out)
    out = self.layer4(out)   # should layer4 appear here?
    out = F.avg_pool2d(out, 4)
    out = out.view(out.size(0), -1)
    out = self.linear(out)



I get issues like this.
-> 1848     return torch._C._nn.linear(input, weight, bias)

RuntimeError: mat1 and mat2 shapes cannot be multiplied (32x50176 and 512x3)



Regardless of whether or not using layer4 is relevant to your issue,
the 512x3 in the error message refers to the weight tensor of
self.linear.  This Linear layer expects an input tensor of shape
[nBatch, 512].  But the error message is telling you that its input
has shape [32, 50176].  This means that nBatch = 32, which is
fine, but the number of “features” (in each “sample” in the batch)
is 50176, rather than the expected 512.
This line:
    out = out.view(out.size(0), -1)

takes the output of avg_pool2d and “flattens” all but the nBatch
dimension together.  That is the “planes” (“channels”) dimension
together with the “height” and “width” dimensions are flattened
together into one single dimension that the error message is telling
us has size 50176.  (Note that height and width of the tensor being
flattened here depend on the height and width of the image input
into your ResNet.  As written, your ResNet will only work for images
with a certain given height and width.)
To debug your issue, you should print out the shapes of the result of
self.layer3(out), the result of self.layer4(out) (should you
decide to add it to your network), the result of F.avg_pool2d(out, 4),
and the result of out.view(out.size(0), -1).  Do these shapes
make sense and are they what you expect them to be?  Does it make
sense that self.linear is expecting a tensor of shape [nBatch, 512]?
Best.
K. Frank