normal.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # mypy: allow-untyped-defs
  2. import math
  3. import torch
  4. from torch import Tensor
  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. from torch.types import _Number, _size
  9. __all__ = ["Normal"]
  10. class Normal(ExponentialFamily):
  11. r"""
  12. Creates a normal (also called Gaussian) distribution parameterized by
  13. :attr:`loc` and :attr:`scale`.
  14. Example::
  15. >>> # xdoctest: +IGNORE_WANT("non-deterministic")
  16. >>> m = Normal(torch.tensor([0.0]), torch.tensor([1.0]))
  17. >>> m.sample() # normally distributed with loc=0 and scale=1
  18. tensor([ 0.1046])
  19. Args:
  20. loc (float or Tensor): mean of the distribution (often referred to as mu)
  21. scale (float or Tensor): standard deviation of the distribution
  22. (often referred to as sigma)
  23. """
  24. # pyrefly: ignore [bad-override]
  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: Tensor | float,
  44. scale: Tensor | float,
  45. validate_args: bool | None = 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. # pyrefly: ignore [unsupported-operation]
  74. var = self.scale**2
  75. log_scale = (
  76. math.log(self.scale)
  77. if isinstance(self.scale, _Number)
  78. else self.scale.log()
  79. )
  80. return (
  81. -((value - self.loc) ** 2) / (2 * var)
  82. - log_scale
  83. - math.log(math.sqrt(2 * math.pi))
  84. )
  85. def cdf(self, value):
  86. if self._validate_args:
  87. self._validate_sample(value)
  88. return 0.5 * (
  89. 1 + torch.erf((value - self.loc) * self.scale.reciprocal() / math.sqrt(2))
  90. )
  91. def icdf(self, value):
  92. return self.loc + self.scale * torch.erfinv(2 * value - 1) * math.sqrt(2)
  93. def entropy(self):
  94. return 0.5 + 0.5 * math.log(2 * math.pi) + torch.log(self.scale)
  95. @property
  96. def _natural_params(self) -> tuple[Tensor, Tensor]:
  97. return (self.loc / self.scale.pow(2), -0.5 * self.scale.pow(2).reciprocal())
  98. # pyrefly: ignore [bad-override]
  99. def _log_normalizer(self, x, y):
  100. return -0.25 * x.pow(2) / y + 0.5 * torch.log(-math.pi / y)