I am working on a simple architecture, LeNet, with the following architecture:
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 32, 3, 1)
self.conv2 = nn.Conv2d(32, 64, 3, 1)
self.dropout1 = nn.Dropout2d(0.25)
self.dropout2 = nn.Dropout2d(0.5)
self.flatten = nn.Flatten()
self.fc1 = nn.Linear(9216, 128)
self.fc2 = nn.Linear(128, 10)
def forward(self, x):
x = self.conv1(x)
x = F.relu(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)
x = F.relu(x)
x = self.dropout2(x)
x = self.fc2(x)
output = F.log_softmax(x, dim=1)
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!
model = Net().to(device)
new_model = nn.Sequential(*list(model.children())[:-1]).to(device)
data, label = next(iter(train_loader))
data, label = data.to(device), label.to(device)
output = new_model(data)
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
How can I delete the last layer without getting this error?