Saving array of state_dicts

friends, in a callback, (after_step), I’m trying to create an array that saves the state_dicts of the model every x iterations, issue is the following. After I check the array, once training is done, all the keys of the array contain identical state_dicts, the last one of them. Seems like the assignment is being done by reference, not copying. I tried to use clone() but it’s not allowed in collections, any tips? thank you :wink:

class JCallback(Callback):
    def after_step(self):
        if self.n_iter%20==0:                    
          learn.modelDict.append(learn.model.state_dict())  
          learn.optDict.append(learn.opt.state)

Apparently I had to use deepcopy:

dict_copy = copy.deepcopy(learn.model.state_dict())
learn.modelDict.append(dict_copy)

now it seems to be working, we can delete this or maybe leave it for others that may have similar issues, thank you :slight_smile:

2 Likes

Have you tried Copy.deepcopy() and then append that into the list?

1 Like

thank you Matthew, yess, I found out about deepcopy and it works yeah
dict_copy = copy.deepcopy(learn.model.state_dict())
learn.modelDict.append(dict_copy)

:wink: