normal.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # mypy: allow-untyped-defs
  2. import math
  3. from typing import Optional, Union
  4. import torch
  5. from torch import Tensor
  6. from torch.distributions import constraints
  7. from torch.distributions.exp_family import ExponentialFamily
  8. from torch.distributions.utils import _standard_normal, broadcast_all
  9. from torch.types import _Number, _size
  10. __all__ = ["Normal"]
  11. class Normal(ExponentialFamily):
  12. r"""
  13. Creates a normal (also called Gaussian) distribution parameterized by
  14. :attr:`loc` and :attr:`scale`.
  15. Example::
  16. >>> # xdoctest: +IGNORE_WANT("non-deterministic")
  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. Args:
  21. loc (float or Tensor): mean of the distribution (often referred to as mu)
  22. scale (float or Tensor): standard deviation of the distribution
  23. (often referred to as sigma)
  24. """
  25. arg_constraints = {"loc": constraints.real, "scale": constraints.positive}
  26. support = constraints.real
  27. has_rsample = True
  28. _mean_carrier_measure = 0
  29. @property
  30. def mean(self) -> Tensor:
  31. return self.loc
  32. @property
  33. def mode(self) -> Tensor:
  34. return self.loc
  35. @property
  36. def stddev(self) -> Tensor:
  37. return self.scale
  38. @property
  39. def variance(self) -> Tensor:
  40. return self.stddev.pow(2)
  41. def __init__(
  42. self,
  43. loc: Union[Tensor, float],
  44. scale: Union[Tensor, float],
  45. validate_args: Optional[bool] = None,
  46. ) -> None:
  47. self.loc, self.scale = broadcast_all(loc, scale)
  48. if isinstance(loc, _Number) and isinstance(scale, _Number):
  49. batch_shape = torch.Size()
  50. else:
  51. batch_shape = self.loc.size()
  52. super().__init__(batch_shape, validate_args=validate_args)
  53. def expand(self, batch_shape, _instance=None):
  54. new = self._get_checked_instance(Normal, _instance)
  55. batch_shape = torch.Size(batch_shape)
  56. new.loc = self.loc.expand(batch_shape)
  57. new.scale = self.scale.expand(batch_shape)
  58. super(Normal, new).__init__(batch_shape, validate_args=False)
  59. new._validate_args = self._validate_args
  60. return new
  61. def sample(self, sample_shape=torch.Size()):
  62. shape = self._extended_shape(sample_shape)
  63. with torch.no_grad():
  64. return torch.normal(self.loc.expand(shape), self.scale.expand(shape))
  65. def rsample(self, sample_shape: _size = torch.Size()) -> Tensor:
  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. def log_prob(self, value):
  70. if self._validate_args:
  71. self._validate_sample(value)
  72. # compute the variance
  73. var = self.scale**2
  74. log_scale = (
  75. math.log(self.scale)
  76. if isinstance(self.scale, _Number)
  77. else self.scale.log()
  78. )
  79. return (
  80. -((value - self.loc) ** 2) / (2 * var)
  81. - log_scale
  82. - math.log(math.sqrt(2 * math.pi))
  83. )
  84. def cdf(self, value):
  85. if self._validate_args:
  86. self._validate_sample(value)
  87. return 0.5 * (
  88. 1 + torch.erf((value - self.loc) * self.scale.reciprocal() / math.sqrt(2))
  89. )
  90. def icdf(self, value):
  91. return self.loc + self.scale * torch.erfinv(2 * value - 1) * math.sqrt(2)
  92. def entropy(self):
  93. return 0.5 + 0.5 * math.log(2 * math.pi) + torch.log(self.scale)
  94. @property
  95. def _natural_params(self) -> tuple[Tensor, Tensor]:
  96. return (self.loc / self.scale.pow(2), -0.5 * self.scale.pow(2).reciprocal())
  97. def _log_normalizer(self, x, y):
  98. return -0.25 * x.pow(2) / y + 0.5 * torch.log(-math.pi / y)