Caffe2 - Python API
A deep learning, cross platform ML framework
parameter.py
1 import torch
2 from collections import OrderedDict
3 
4 
5 class Parameter(torch.Tensor):
6  r"""A kind of Tensor that is to be considered a module parameter.
7 
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.
16 
17  Arguments:
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`
21  """
22 
23  def __new__(cls, data=None, requires_grad=True):
24  if data is None:
25  data = torch.Tensor()
26  return torch.Tensor._make_subclass(cls, data, requires_grad)
27 
28  def __deepcopy__(self, memo):
29  if id(self) in memo:
30  return memo[id(self)]
31  else:
32  result = type(self)(self.data.clone(), self.requires_grad)
33  memo[id(self)] = result
34  return result
35 
36  def __repr__(self):
37  return 'Parameter containing:\n' + super(Parameter, self).__repr__()
38 
39  def __reduce_ex__(self, proto):
40  # See Note [Don't serialize hooks]
41  return (
42  torch._utils._rebuild_parameter,
43  (self.data, self.requires_grad, OrderedDict())
44  )