The params in model.named_parameters don't update

I save my model’s initial parameters with names using the following code in a dict.

for name, param in model.named_parameters():
  if param.requires_grad:
    pre_params[name] = param

Then I save the parameters again after a single epoch into a different dict. When I try to compare if these parameters were changed using the code below, I don’t see them being changed. Although, if I look at just model.parameters(), they are changed. I need to store the weights using the name for a specific use case.

for name in pre_params:
  print(pre_params[name] == curr_params[name])

Any insights on this behavior? Am I missing something here?

You are storing references so you might want to clone the parameters:

pre_params[name] = param.clone()
1 Like

That worked. Thank you @ptrblck!