Caffe2 - Python API
A deep learning, cross platform ML framework
gamma.py
1 from numbers import Number
2 
3 import torch
4 from torch.distributions import constraints
5 from torch.distributions.exp_family import ExponentialFamily
6 from torch.distributions.utils import broadcast_all, lazy_property
7 
8 
9 def _standard_gamma(concentration):
10  return torch._standard_gamma(concentration)
11 
12 
14  r"""
15  Creates a Gamma distribution parameterized by shape :attr:`concentration` and :attr:`rate`.
16 
17  Example::
18 
19  >>> m = Gamma(torch.tensor([1.0]), torch.tensor([1.0]))
20  >>> m.sample() # Gamma distributed with concentration=1 and rate=1
21  tensor([ 0.1046])
22 
23  Args:
24  concentration (float or Tensor): shape parameter of the distribution
25  (often referred to as alpha)
26  rate (float or Tensor): rate = 1 / scale of the distribution
27  (often referred to as beta)
28  """
29  arg_constraints = {'concentration': constraints.positive, 'rate': constraints.positive}
30  support = constraints.positive
31  has_rsample = True
32  _mean_carrier_measure = 0
33 
34  @property
35  def mean(self):
36  return self.concentration / self.rate
37 
38  @property
39  def variance(self):
40  return self.concentration / self.rate.pow(2)
41 
42  def __init__(self, concentration, rate, validate_args=None):
43  self.concentration, self.rate = broadcast_all(concentration, rate)
44  if isinstance(concentration, Number) and isinstance(rate, Number):
45  batch_shape = torch.Size()
46  else:
47  batch_shape = self.concentration.size()
48  super(Gamma, self).__init__(batch_shape, validate_args=validate_args)
49 
50  def expand(self, batch_shape, _instance=None):
51  new = self._get_checked_instance(Gamma, _instance)
52  batch_shape = torch.Size(batch_shape)
53  new.concentration = self.concentration.expand(batch_shape)
54  new.rate = self.rate.expand(batch_shape)
55  super(Gamma, new).__init__(batch_shape, validate_args=False)
56  new._validate_args = self._validate_args
57  return new
58 
59  def rsample(self, sample_shape=torch.Size()):
60  shape = self._extended_shape(sample_shape)
61  value = _standard_gamma(self.concentration.expand(shape)) / self.rate.expand(shape)
62  value.detach().clamp_(min=torch.finfo(value.dtype).tiny) # do not record in autograd graph
63  return value
64 
65  def log_prob(self, value):
66  if self._validate_args:
67  self._validate_sample(value)
68  return (self.concentration * torch.log(self.rate) +
69  (self.concentration - 1) * torch.log(value) -
70  self.rate * value - torch.lgamma(self.concentration))
71 
72  def entropy(self):
73  return (self.concentration - torch.log(self.rate) + torch.lgamma(self.concentration) +
74  (1.0 - self.concentration) * torch.digamma(self.concentration))
75 
76  @property
77  def _natural_params(self):
78  return (self.concentration - 1, -self.rate)
79 
80  def _log_normalizer(self, x, y):
81  return torch.lgamma(x + 1) + (x + 1) * torch.log(-y.reciprocal())
def _get_checked_instance(self, cls, _instance=None)
def _extended_shape(self, sample_shape=torch.Size())