Caffe2 - Python API
A deep learning, cross platform ML framework
normal.py
1 import math
2 from numbers import Number
3 
4 import torch
5 from torch.distributions import constraints
6 from torch.distributions.exp_family import ExponentialFamily
7 from torch.distributions.utils import _standard_normal, broadcast_all
8 
9 
11  r"""
12  Creates a normal (also called Gaussian) distribution parameterized by
13  :attr:`loc` and :attr:`scale`.
14 
15  Example::
16 
17  >>> m = Normal(torch.tensor([0.0]), torch.tensor([1.0]))
18  >>> m.sample() # normally distributed with loc=0 and scale=1
19  tensor([ 0.1046])
20 
21  Args:
22  loc (float or Tensor): mean of the distribution (often referred to as mu)
23  scale (float or Tensor): standard deviation of the distribution
24  (often referred to as sigma)
25  """
26  arg_constraints = {'loc': constraints.real, 'scale': constraints.positive}
27  support = constraints.real
28  has_rsample = True
29  _mean_carrier_measure = 0
30 
31  @property
32  def mean(self):
33  return self.loc
34 
35  @property
36  def stddev(self):
37  return self.scale
38 
39  @property
40  def variance(self):
41  return self.stddev.pow(2)
42 
43  def __init__(self, loc, scale, validate_args=None):
44  self.loc, self.scale = broadcast_all(loc, scale)
45  if isinstance(loc, Number) and isinstance(scale, Number):
46  batch_shape = torch.Size()
47  else:
48  batch_shape = self.loc.size()
49  super(Normal, self).__init__(batch_shape, validate_args=validate_args)
50 
51  def expand(self, batch_shape, _instance=None):
52  new = self._get_checked_instance(Normal, _instance)
53  batch_shape = torch.Size(batch_shape)
54  new.loc = self.loc.expand(batch_shape)
55  new.scale = self.scale.expand(batch_shape)
56  super(Normal, new).__init__(batch_shape, validate_args=False)
57  new._validate_args = self._validate_args
58  return new
59 
60  def sample(self, sample_shape=torch.Size()):
61  shape = self._extended_shape(sample_shape)
62  with torch.no_grad():
63  return torch.normal(self.loc.expand(shape), self.scale.expand(shape))
64 
65  def rsample(self, sample_shape=torch.Size()):
66  shape = self._extended_shape(sample_shape)
67  eps = _standard_normal(shape, dtype=self.loc.dtype, device=self.loc.device)
68  return self.loc + eps * self.scale
69 
70  def log_prob(self, value):
71  if self._validate_args:
72  self._validate_sample(value)
73  # compute the variance
74  var = (self.scale ** 2)
75  log_scale = math.log(self.scale) if isinstance(self.scale, Number) else self.scale.log()
76  return -((value - self.loc) ** 2) / (2 * var) - log_scale - math.log(math.sqrt(2 * math.pi))
77 
78  def cdf(self, value):
79  if self._validate_args:
80  self._validate_sample(value)
81  return 0.5 * (1 + torch.erf((value - self.loc) * self.scale.reciprocal() / math.sqrt(2)))
82 
83  def icdf(self, value):
84  if self._validate_args:
85  self._validate_sample(value)
86  return self.loc + self.scale * torch.erfinv(2 * value - 1) * math.sqrt(2)
87 
88  def entropy(self):
89  return 0.5 + 0.5 * math.log(2 * math.pi) + torch.log(self.scale)
90 
91  @property
92  def _natural_params(self):
93  return (self.loc / self.scale.pow(2), -0.5 * self.scale.pow(2).reciprocal())
94 
95  def _log_normalizer(self, x, y):
96  return -0.25 * x.pow(2) / y + 0.5 * torch.log(-math.pi / y)
def _get_checked_instance(self, cls, _instance=None)
def _extended_shape(self, sample_shape=torch.Size())