[docs]classParameterContainer(NodeContainer):"""A container of parameter nodes."""
[docs]defparameters(self):"""Return a flattned list of all the parameters in the model's parameters_dict, useful for optimization."""parameters=[]fork,vinself.parameters_dict().items():ifisinstance(v,ParameterNode):parameters.append(v)elifisinstance(v,ParameterContainer):parameters.extend(v.parameters())else:raiseValueError("The model contains an unknown parameter type.")returnparameters
[docs]defparameters_dict(self):"""Return a dictionary of all the parameters in the model, including both trainable and non-trainable parameters. The dict contains ParameterNodes or ParameterContainers. """parameters={}forname,attrininspect.getmembers(self):ifisinstance(attr,functools.partial):# this is a class methodmethod=attr.func.__self__iftrainable_method(method):parameters[name]=method.parametereliftrainable_method(attr):# method attributeparameters[name]=attr.parameterelifisinstance(attr,ParameterNode):parameters[name]=attrelifisinstance(attr,ParameterContainer):parameters[name]=attrassertall(isinstance(v,(ParameterNode,ParameterContainer))forvinparameters.values())returnparameters# include both trainable and non-trainable parameters
[docs]classSeq(UserList,ParameterContainer):""" Seq is defined as having a length and an index. Python's list/tuple will be converted to Seq """
[docs]defparameters_dict(self):"""Return a dictionary of all the parameters in the model, including both trainable and non-trainable parameters. The dict contains ParameterNodes or ParameterContainers. """parameters={}forattrinself.data:ifisinstance(attr,ParameterNode):parameters[attr.name]=attrelifisinstance(attr,ParameterContainer):parameters[str(attr)]=attr# TODO: what is the name of the container?assertall(isinstance(v,(ParameterNode,ParameterContainer))forvinparameters.values())returnparameters
[docs]classMap(UserDict,ParameterContainer):""" Map is defined as key and value Python's dict will be converted to Map """
[docs]defparameters_dict(self):"""Return a dictionary of all the parameters in the model, including both trainable and non-trainable parameters. The dict contains ParameterNodes or ParameterContainers. """parameters={}fork,vinself.data.items():ifisinstance(v,ParameterNode):parameters[k]=velifisinstance(v,ParameterContainer):parameters[str(v)]=v# TODO: what is the name of the container?ifisinstance(k,ParameterNode):parameters[str(k)]=kelifisinstance(k,ParameterContainer):raiseException("The key of a Map cannot be a container.")assertall(isinstance(v,(ParameterNode,ParameterContainer))forvinparameters.values())returnparameters