Hello Al!



 AlJazari:

class Net(nn.Module):
    ...
    def forward(self, x):
        ...
        x = self.conv2(x)
        x = F.relu(x)
        x = F.max_pool2d(x, 2)
        x = self.dropout1(x)
        x = self.flatten(x)
        x = self.fc1(x)
        ...
        return output

I want to delete the last layer in the network, fc2. However, when I do so using the approach shown below I get a size mismatch!
new_model = nn.Sequential(*list(model.children())[:-1]).to(device)



When you create new_model it doesn’t know about the detailed
structure of your class Net.  In particular, it doesn’t know about
Net's forward() function.  (It does know about model.children()).
So I don’t think any of the torch.nn.functional functions used
in model (an instance of your Net) get called when you execute
new_model (data).  In particular, F.max_pool2d (x, 2) isn’t
getting called.


This is throwing the following error:
RuntimeError: size mismatch, m1: [32 x 36864], m2: [9216 x 128] at /pytorch/aten/src/THC/generic/THCTensorMathBlas.cu:290


Note that the size of the output of self.conv2 is (64*3)**2 = 36864,
the second dimension of m1 in your size-mismatch error.  The size of
the input to self.fc1 is 9216, the first dimension of m2.  Had
max_pool2d() been called it would have reduced 36864 by a factor
of 2**2 = 4, yielding 9216, eliminating the size-mismatch error.


How can I delete the last layer without getting this error?


Either build a New_Net class (of which new_model will be an
instance) with an appropriate forward function, or only use Modules
to build your Net class (and make Net an nn.Sequential). That is,
use Modules like nn.ReLU and nn.MaxPool2d instead of functions
like F.relu() and F.max_pool2d().
Good luck.
K. Frank