distribution.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. # mypy: allow-untyped-defs
  2. import warnings
  3. from typing import Optional
  4. from typing_extensions import deprecated
  5. import torch
  6. from torch import Tensor
  7. from torch.distributions import constraints
  8. from torch.distributions.utils import lazy_property
  9. from torch.types import _size
  10. __all__ = ["Distribution"]
  11. class Distribution:
  12. r"""
  13. Distribution is the abstract base class for probability distributions.
  14. Args:
  15. batch_shape (torch.Size): The shape over which parameters are batched.
  16. event_shape (torch.Size): The shape of a single sample (without batching).
  17. validate_args (bool, optional): Whether to validate arguments. Default: None.
  18. """
  19. has_rsample = False
  20. has_enumerate_support = False
  21. _validate_args = __debug__
  22. @staticmethod
  23. def set_default_validate_args(value: bool) -> None:
  24. """
  25. Sets whether validation is enabled or disabled.
  26. The default behavior mimics Python's ``assert`` statement: validation
  27. is on by default, but is disabled if Python is run in optimized mode
  28. (via ``python -O``). Validation may be expensive, so you may want to
  29. disable it once a model is working.
  30. Args:
  31. value (bool): Whether to enable validation.
  32. """
  33. if value not in [True, False]:
  34. raise ValueError
  35. Distribution._validate_args = value
  36. def __init__(
  37. self,
  38. batch_shape: torch.Size = torch.Size(),
  39. event_shape: torch.Size = torch.Size(),
  40. validate_args: Optional[bool] = None,
  41. ) -> None:
  42. self._batch_shape = batch_shape
  43. self._event_shape = event_shape
  44. if validate_args is not None:
  45. self._validate_args = validate_args
  46. if self._validate_args:
  47. try:
  48. arg_constraints = self.arg_constraints
  49. except NotImplementedError:
  50. arg_constraints = {}
  51. warnings.warn(
  52. f"{self.__class__} does not define `arg_constraints`. "
  53. + "Please set `arg_constraints = {}` or initialize the distribution "
  54. + "with `validate_args=False` to turn off validation.",
  55. stacklevel=2,
  56. )
  57. for param, constraint in arg_constraints.items():
  58. if constraints.is_dependent(constraint):
  59. continue # skip constraints that cannot be checked
  60. if param not in self.__dict__ and isinstance(
  61. getattr(type(self), param), lazy_property
  62. ):
  63. continue # skip checking lazily-constructed args
  64. value = getattr(self, param)
  65. valid = constraint.check(value)
  66. if not torch._is_all_true(valid):
  67. raise ValueError(
  68. f"Expected parameter {param} "
  69. f"({type(value).__name__} of shape {tuple(value.shape)}) "
  70. f"of distribution {repr(self)} "
  71. f"to satisfy the constraint {repr(constraint)}, "
  72. f"but found invalid values:\n{value}"
  73. )
  74. super().__init__()
  75. def expand(self, batch_shape: _size, _instance=None):
  76. """
  77. Returns a new distribution instance (or populates an existing instance
  78. provided by a derived class) with batch dimensions expanded to
  79. `batch_shape`. This method calls :class:`~torch.Tensor.expand` on
  80. the distribution's parameters. As such, this does not allocate new
  81. memory for the expanded distribution instance. Additionally,
  82. this does not repeat any args checking or parameter broadcasting in
  83. `__init__.py`, when an instance is first created.
  84. Args:
  85. batch_shape (torch.Size): the desired expanded size.
  86. _instance: new instance provided by subclasses that
  87. need to override `.expand`.
  88. Returns:
  89. New distribution instance with batch dimensions expanded to
  90. `batch_size`.
  91. """
  92. raise NotImplementedError
  93. @property
  94. def batch_shape(self) -> torch.Size:
  95. """
  96. Returns the shape over which parameters are batched.
  97. """
  98. return self._batch_shape
  99. @property
  100. def event_shape(self) -> torch.Size:
  101. """
  102. Returns the shape of a single sample (without batching).
  103. """
  104. return self._event_shape
  105. @property
  106. def arg_constraints(self) -> dict[str, constraints.Constraint]:
  107. """
  108. Returns a dictionary from argument names to
  109. :class:`~torch.distributions.constraints.Constraint` objects that
  110. should be satisfied by each argument of this distribution. Args that
  111. are not tensors need not appear in this dict.
  112. """
  113. raise NotImplementedError
  114. @property
  115. def support(self) -> Optional[constraints.Constraint]:
  116. """
  117. Returns a :class:`~torch.distributions.constraints.Constraint` object
  118. representing this distribution's support.
  119. """
  120. raise NotImplementedError
  121. @property
  122. def mean(self) -> Tensor:
  123. """
  124. Returns the mean of the distribution.
  125. """
  126. raise NotImplementedError
  127. @property
  128. def mode(self) -> Tensor:
  129. """
  130. Returns the mode of the distribution.
  131. """
  132. raise NotImplementedError(f"{self.__class__} does not implement mode")
  133. @property
  134. def variance(self) -> Tensor:
  135. """
  136. Returns the variance of the distribution.
  137. """
  138. raise NotImplementedError
  139. @property
  140. def stddev(self) -> Tensor:
  141. """
  142. Returns the standard deviation of the distribution.
  143. """
  144. return self.variance.sqrt()
  145. def sample(self, sample_shape: _size = torch.Size()) -> Tensor:
  146. """
  147. Generates a sample_shape shaped sample or sample_shape shaped batch of
  148. samples if the distribution parameters are batched.
  149. """
  150. with torch.no_grad():
  151. return self.rsample(sample_shape)
  152. def rsample(self, sample_shape: _size = torch.Size()) -> Tensor:
  153. """
  154. Generates a sample_shape shaped reparameterized sample or sample_shape
  155. shaped batch of reparameterized samples if the distribution parameters
  156. are batched.
  157. """
  158. raise NotImplementedError
  159. @deprecated(
  160. "`sample_n(n)` will be deprecated. Use `sample((n,))` instead.",
  161. category=FutureWarning,
  162. )
  163. def sample_n(self, n: int) -> Tensor:
  164. """
  165. Generates n samples or n batches of samples if the distribution
  166. parameters are batched.
  167. """
  168. return self.sample(torch.Size((n,)))
  169. def log_prob(self, value: Tensor) -> Tensor:
  170. """
  171. Returns the log of the probability density/mass function evaluated at
  172. `value`.
  173. Args:
  174. value (Tensor):
  175. """
  176. raise NotImplementedError
  177. def cdf(self, value: Tensor) -> Tensor:
  178. """
  179. Returns the cumulative density/mass function evaluated at
  180. `value`.
  181. Args:
  182. value (Tensor):
  183. """
  184. raise NotImplementedError
  185. def icdf(self, value: Tensor) -> Tensor:
  186. """
  187. Returns the inverse cumulative density/mass function evaluated at
  188. `value`.
  189. Args:
  190. value (Tensor):
  191. """
  192. raise NotImplementedError
  193. def enumerate_support(self, expand: bool = True) -> Tensor:
  194. """
  195. Returns tensor containing all values supported by a discrete
  196. distribution. The result will enumerate over dimension 0, so the shape
  197. of the result will be `(cardinality,) + batch_shape + event_shape`
  198. (where `event_shape = ()` for univariate distributions).
  199. Note that this enumerates over all batched tensors in lock-step
  200. `[[0, 0], [1, 1], ...]`. With `expand=False`, enumeration happens
  201. along dim 0, but with the remaining batch dimensions being
  202. singleton dimensions, `[[0], [1], ..`.
  203. To iterate over the full Cartesian product use
  204. `itertools.product(m.enumerate_support())`.
  205. Args:
  206. expand (bool): whether to expand the support over the
  207. batch dims to match the distribution's `batch_shape`.
  208. Returns:
  209. Tensor iterating over dimension 0.
  210. """
  211. raise NotImplementedError
  212. def entropy(self) -> Tensor:
  213. """
  214. Returns entropy of distribution, batched over batch_shape.
  215. Returns:
  216. Tensor of shape batch_shape.
  217. """
  218. raise NotImplementedError
  219. def perplexity(self) -> Tensor:
  220. """
  221. Returns perplexity of distribution, batched over batch_shape.
  222. Returns:
  223. Tensor of shape batch_shape.
  224. """
  225. return torch.exp(self.entropy())
  226. def _extended_shape(self, sample_shape: _size = torch.Size()) -> torch.Size:
  227. """
  228. Returns the size of the sample returned by the distribution, given
  229. a `sample_shape`. Note, that the batch and event shapes of a distribution
  230. instance are fixed at the time of construction. If this is empty, the
  231. returned shape is upcast to (1,).
  232. Args:
  233. sample_shape (torch.Size): the size of the sample to be drawn.
  234. """
  235. if not isinstance(sample_shape, torch.Size):
  236. sample_shape = torch.Size(sample_shape)
  237. return torch.Size(sample_shape + self._batch_shape + self._event_shape)
  238. def _validate_sample(self, value: Tensor) -> None:
  239. """
  240. Argument validation for distribution methods such as `log_prob`,
  241. `cdf` and `icdf`. The rightmost dimensions of a value to be
  242. scored via these methods must agree with the distribution's batch
  243. and event shapes.
  244. Args:
  245. value (Tensor): the tensor whose log probability is to be
  246. computed by the `log_prob` method.
  247. Raises
  248. ValueError: when the rightmost dimensions of `value` do not match the
  249. distribution's batch and event shapes.
  250. """
  251. if not isinstance(value, torch.Tensor):
  252. raise ValueError("The value argument to log_prob must be a Tensor")
  253. event_dim_start = len(value.size()) - len(self._event_shape)
  254. if value.size()[event_dim_start:] != self._event_shape:
  255. raise ValueError(
  256. f"The right-most size of value must match event_shape: {value.size()} vs {self._event_shape}."
  257. )
  258. actual_shape = value.size()
  259. expected_shape = self._batch_shape + self._event_shape
  260. for i, j in zip(reversed(actual_shape), reversed(expected_shape)):
  261. if i != 1 and j != 1 and i != j:
  262. raise ValueError(
  263. f"Value is not broadcastable with batch_shape+event_shape: {actual_shape} vs {expected_shape}."
  264. )
  265. try:
  266. support = self.support
  267. except NotImplementedError:
  268. warnings.warn(
  269. f"{self.__class__} does not define `support` to enable "
  270. + "sample validation. Please initialize the distribution with "
  271. + "`validate_args=False` to turn off validation.",
  272. stacklevel=2,
  273. )
  274. return
  275. assert support is not None
  276. valid = support.check(value)
  277. if not torch._is_all_true(valid):
  278. raise ValueError(
  279. "Expected value argument "
  280. f"({type(value).__name__} of shape {tuple(value.shape)}) "
  281. f"to be within the support ({repr(support)}) "
  282. f"of the distribution {repr(self)}, "
  283. f"but found invalid values:\n{value}"
  284. )
  285. def _get_checked_instance(self, cls, _instance=None):
  286. if _instance is None and type(self).__init__ != cls.__init__:
  287. raise NotImplementedError(
  288. f"Subclass {self.__class__.__name__} of {cls.__name__} that defines a custom __init__ method "
  289. "must also define a custom .expand() method."
  290. )
  291. return self.__new__(type(self)) if _instance is None else _instance
  292. def __repr__(self) -> str:
  293. param_names = [k for k, _ in self.arg_constraints.items() if k in self.__dict__]
  294. args_string = ", ".join(
  295. [
  296. f"{p}: {self.__dict__[p] if self.__dict__[p].numel() == 1 else self.__dict__[p].size()}"
  297. for p in param_names
  298. ]
  299. )
  300. return self.__class__.__name__ + "(" + args_string + ")"