Error printing trainable/frozen parameters

I created this function to print the model, and mark the trainable/frozen modules:

def get_model_desc(model):
    for name, p in model.state_dict().items():
        frozen= " ·t· " if p.requires_grad else " [F] "
        network_desc_spaces=" "*(55-len(name))
        network_desc+=f"{name} {network_desc_spaces} {frozen} {list(p.size())}\n"
    return network_desc

It shows al the parameters as frozen, but they musn’t be frozen.

I must be missing something, because there are modules whose parameters I have explicitly assigned as requires_grad=True

Also, I uncommented without success the line p.requires_grad=True

The state_dict will store all parameters as tensors, which do not require gradients.
Have a look at this small example:

model = models.resnet18()

for p in model.parameters():

for _, p in model.state_dict().items():

If you want to check the parameters of a model, use model.parameters() instead.

1 Like

thanks a lot @ptrblck !

1 Like