Subclass it and implement forward. Any Tensor or Module assigned as an
attribute is discovered automatically by parameters(), state_dict(), and
the train/eval switches.
defparameters(self):"""Collect every trainable `Tensor` in this module and its children."""params=[]forvalinself.__dict__.values():ifisinstance(val,Tensor)andval.requires_grad:params.append(val)elifisinstance(val,Module):params.extend(val.parameters())elifisinstance(val,(list,tuple)):foriteminval:ifisinstance(item,Tensor)anditem.requires_grad:params.append(item)elifisinstance(item,Module):params.extend(item.parameters())return_dedup(params)
defload(self,path):"""Load parameters from a .npz file written by save()."""withnp.load(path)asdata:self.load_state_dict({k:data[k]forkindata.files})returnself