I define a model

import torch

from torchvision import modelsmodel = models.resnet18()

It has all its layers set as trainable (requires_grad = True for all layers)

Then I freeze the final fc layer

for param in model.fc.parameters():

param.requires_grad = False

Then I save the state_dict of the model

torch.save(model.state_dict(), ‘model.pth’)

Now I want to load the weights again. So I define the model once again and load the saved weights in it…

model_new = models.resnet18()

model_new.load_state_dict(torch.oad(‘model.pth’))

Now when I print the requires_grad of its fc layer, all the requires_grad are back to the original setings (True)

for param in model_new.fc.parameters():

print(param.requires_grad)

Its prints

True

True

So the question is that, how is the requires_grad setting getting changed on loading the weights?

Does saving the model.state_dict() even save the requires_grad settings?