This is a somewhat advanced question, but I am going through the source code for the optimizer, and I found something interesting about the pickle-specific function __getstate__
:
def __getstate__(self):
return {
'defaults': self.defaults,
'state': self.state,
'param_groups': self.param_groups,
}
def __setstate__(self, state):
self.__dict__.update(state)
My limited understanding of pickling/serialization is that __getstate__
is going to just return the state of the class in whatever implemented form, often by just returning a copy of the dictionary of attributes self.__dict__
. Here, it is returning a defined dictionary of the attributes self.defaults
, self.state
, and self.param_groups
. But as far as I can tell, these are the only attributes of the optimizer class. So why can’t __getstate__
just return self.__dict__.copy()
?