How do i unfreeze the last layer

Hello,
I brought in a resnet model and set the requires_grad to false. However I changed the last layer and want the requires grad to true. How do I do that? I tried this:

model = models.resnet50(pretrained = True)
for param in model.parameters():
param.requires_grad = False
model.Linear = nn.Sequential(nn.Linear(in_features = 2048, out_features = 1000),
nn.ReLU(),
nn.Dropout(0.4),
nn.Linear(1000, 2),
nn.LogSoftmax(dim=1))
model.Linear.parameters(requires_grad = True)

That brought this error:

TypeError: parameters() got an unexpected keyword argument ‘requires_grad’

You need to do the same thing you do to set it to false:

for param in model.Linear.parameters():
    param.requires_grad = True
1 Like