mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 00:21:07 +01:00
Summary: Should resolve #9981. Pull Request resolved: https://github.com/pytorch/pytorch/pull/10296 Differential Revision: D9196353 Pulled By: soumith fbshipit-source-id: 109b6da42b7240cdbc7a0586745c735bce5e1279
35 lines
1.3 KiB
Python
35 lines
1.3 KiB
Python
import torch
|
|
|
|
|
|
class Parameter(torch.Tensor):
|
|
r"""A kind of Tensor that is to be considered a module parameter.
|
|
|
|
Parameters are :class:`~torch.Tensor` subclasses, that have a
|
|
very special property when used with :class:`Module` s - when they're
|
|
assigned as Module attributes they are automatically added to the list of
|
|
its parameters, and will appear e.g. in :meth:`~Module.parameters` iterator.
|
|
Assigning a Tensor doesn't have such effect. This is because one might
|
|
want to cache some temporary state, like last hidden state of the RNN, in
|
|
the model. If there was no such class as :class:`Parameter`, these
|
|
temporaries would get registered too.
|
|
|
|
Arguments:
|
|
data (Tensor): parameter tensor.
|
|
requires_grad (bool, optional): if the parameter requires gradient. See
|
|
:ref:`excluding-subgraphs` for more details. Default: `True`
|
|
"""
|
|
|
|
def __new__(cls, data=None, requires_grad=True):
|
|
if data is None:
|
|
data = torch.Tensor()
|
|
return torch.Tensor._make_subclass(cls, data, requires_grad)
|
|
|
|
def __repr__(self):
|
|
return 'Parameter containing:\n' + super(Parameter, self).__repr__()
|
|
|
|
def __reduce_ex__(self, proto):
|
|
return (
|
|
torch._utils._rebuild_parameter,
|
|
(self.data, self.requires_grad, self._backward_hooks)
|
|
)
|