2 from collections
import OrderedDict
6 r"""A kind of Tensor that is to be considered a module parameter. 8 Parameters are :class:`~torch.Tensor` subclasses, that have a 9 very special property when used with :class:`Module` s - when they're 10 assigned as Module attributes they are automatically added to the list of 11 its parameters, and will appear e.g. in :meth:`~Module.parameters` iterator. 12 Assigning a Tensor doesn't have such effect. This is because one might 13 want to cache some temporary state, like last hidden state of the RNN, in 14 the model. If there was no such class as :class:`Parameter`, these 15 temporaries would get registered too. 18 data (Tensor): parameter tensor. 19 requires_grad (bool, optional): if the parameter requires gradient. See 20 :ref:`excluding-subgraphs` for more details. Default: `True` 23 def __new__(cls, data=None, requires_grad=True):
26 return torch.Tensor._make_subclass(cls, data, requires_grad)
28 def __deepcopy__(self, memo):
32 result = type(self)(self.data.clone(), self.requires_grad)
33 memo[id(self)] = result
37 return 'Parameter containing:\n' + super(Parameter, self).__repr__()
39 def __reduce_ex__(self, proto):
42 torch._utils._rebuild_parameter,
43 (self.data, self.requires_grad, OrderedDict())