normal.py 3.3 KB

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