A generic probability distribution base class. `Distribution` is a base class for constructing and organizing properties (e.g., mean, variance) of random variables (e.g, Bernoulli, Gaussian). #### Subclassing Subclasses are expected to implement a leading-underscore version of the same-
| 278 | @six.add_metaclass(_DistributionMeta) |
| 279 | @tf_export(v1=["distributions.Distribution"]) |
| 280 | class Distribution(_BaseDistribution): |
| 281 | """A generic probability distribution base class. |
| 282 | |
| 283 | `Distribution` is a base class for constructing and organizing properties |
| 284 | (e.g., mean, variance) of random variables (e.g, Bernoulli, Gaussian). |
| 285 | |
| 286 | #### Subclassing |
| 287 | |
| 288 | Subclasses are expected to implement a leading-underscore version of the |
| 289 | same-named function. The argument signature should be identical except for |
| 290 | the omission of `name="..."`. For example, to enable `log_prob(value, |
| 291 | name="log_prob")` a subclass should implement `_log_prob(value)`. |
| 292 | |
| 293 | Subclasses can append to public-level docstrings by providing |
| 294 | docstrings for their method specializations. For example: |
| 295 | |
| 296 | ```python |
| 297 | @util.AppendDocstring("Some other details.") |
| 298 | def _log_prob(self, value): |
| 299 | ... |
| 300 | ``` |
| 301 | |
| 302 | would add the string "Some other details." to the `log_prob` function |
| 303 | docstring. This is implemented as a simple decorator to avoid python |
| 304 | linter complaining about missing Args/Returns/Raises sections in the |
| 305 | partial docstrings. |
| 306 | |
| 307 | #### Broadcasting, batching, and shapes |
| 308 | |
| 309 | All distributions support batches of independent distributions of that type. |
| 310 | The batch shape is determined by broadcasting together the parameters. |
| 311 | |
| 312 | The shape of arguments to `__init__`, `cdf`, `log_cdf`, `prob`, and |
| 313 | `log_prob` reflect this broadcasting, as does the return value of `sample` and |
| 314 | `sample_n`. |
| 315 | |
| 316 | `sample_n_shape = [n] + batch_shape + event_shape`, where `sample_n_shape` is |
| 317 | the shape of the `Tensor` returned from `sample_n`, `n` is the number of |
| 318 | samples, `batch_shape` defines how many independent distributions there are, |
| 319 | and `event_shape` defines the shape of samples from each of those independent |
| 320 | distributions. Samples are independent along the `batch_shape` dimensions, but |
| 321 | not necessarily so along the `event_shape` dimensions (depending on the |
| 322 | particulars of the underlying distribution). |
| 323 | |
| 324 | Using the `Uniform` distribution as an example: |
| 325 | |
| 326 | ```python |
| 327 | minval = 3.0 |
| 328 | maxval = [[4.0, 6.0], |
| 329 | [10.0, 12.0]] |
| 330 | |
| 331 | # Broadcasting: |
| 332 | # This instance represents 4 Uniform distributions. Each has a lower bound at |
| 333 | # 3.0 as the `minval` parameter was broadcasted to match `maxval`'s shape. |
| 334 | u = Uniform(minval, maxval) |
| 335 | |
| 336 | # `event_shape` is `TensorShape([])`. |
| 337 | event_shape = u.event_shape |