defaultdict accepts the default_factory argument, which is the Python dict class in this case. I’m not 100% sure, but think this was added to return an empty dict for missing keys:

state = defaultdict()
print(state)
> defaultdict(None, {})
state['lala']
> KeyError: 'lala'

state = defaultdict(dict)
print(state)
> defaultdict(<class 'dict'>, {})
state['lala']
> {}


I don’t know exactly why this is needed. I assume thr __getstate__ method of the parent class is used and since SGD has additional parameters, they would need to be reset to the defaults in the corresponding __setstate__.