distribution.py 12 KB

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