In this case you could use the following code:
model.classifier = nn.Sequential(*[model.classifier[i] for i in range(4)])
print(model.classifier)
EDIT:
Alternatively, you can also call .children
, since the range indexing might be cumbersome for large sequential models.
model.classifier = nn.Sequential(*list(model.classifier.children())[:-3])