Caffe2 - Python API
A deep learning, cross platform ML framework
log_normal.py
1 import torch
2 from torch.distributions import constraints
3 from torch.distributions.transforms import ExpTransform
4 from torch.distributions.normal import Normal
5 from torch.distributions.transformed_distribution import TransformedDistribution
6 
7 
9  r"""
10  Creates a log-normal distribution parameterized by
11  :attr:`loc` and :attr:`scale` where::
12 
13  X ~ Normal(loc, scale)
14  Y = exp(X) ~ LogNormal(loc, scale)
15 
16  Example::
17 
18  >>> m = LogNormal(torch.tensor([0.0]), torch.tensor([1.0]))
19  >>> m.sample() # log-normal distributed with mean=0 and stddev=1
20  tensor([ 0.1046])
21 
22  Args:
23  loc (float or Tensor): mean of log of distribution
24  scale (float or Tensor): standard deviation of log of the distribution
25  """
26  arg_constraints = {'loc': constraints.real, 'scale': constraints.positive}
27  support = constraints.positive
28  has_rsample = True
29 
30  def __init__(self, loc, scale, validate_args=None):
31  base_dist = Normal(loc, scale)
32  super(LogNormal, self).__init__(base_dist, ExpTransform(), validate_args=validate_args)
33 
34  def expand(self, batch_shape, _instance=None):
35  new = self._get_checked_instance(LogNormal, _instance)
36  return super(LogNormal, self).expand(batch_shape, _instance=new)
37 
38  @property
39  def loc(self):
40  return self.base_dist.loc
41 
42  @property
43  def scale(self):
44  return self.base_dist.scale
45 
46  @property
47  def mean(self):
48  return (self.loc + self.scale.pow(2) / 2).exp()
49 
50  @property
51  def variance(self):
52  return (self.scale.pow(2).exp() - 1) * (2 * self.loc + self.scale.pow(2)).exp()
53 
54  def entropy(self):
55  return self.base_dist.entropy() + self.loc
def _get_checked_instance(self, cls, _instance=None)