|
1 |
| -.. _libdoc_tensor_random: |
| 1 | + |
| 2 | +.. _libdoc_tensor_random_basic: |
2 | 3 |
|
3 | 4 | =============================================
|
4 |
| -:mod:`random` -- Low-level random numbers |
| 5 | +:mod:`random` -- Random number functionality |
5 | 6 | =============================================
|
6 | 7 |
|
7 |
| -Low-level random numbers |
8 |
| ------------------------- |
| 8 | +.. module:: pytensor.tensor.random |
| 9 | + :synopsis: symbolic random variables |
| 10 | + |
9 | 11 |
|
10 | 12 | The :mod:`pytensor.tensor.random` module provides random-number drawing functionality
|
11 | 13 | that closely resembles the :mod:`numpy.random` module.
|
12 | 14 |
|
13 |
| -.. toctree:: |
14 |
| - :maxdepth: 2 |
15 | 15 |
|
16 |
| - basic |
17 |
| - utils |
| 16 | +High-level API |
| 17 | +============== |
| 18 | + |
| 19 | +PyTensor assigns NumPy RNG states (i.e. `Generator` objects) to |
| 20 | +each `RandomVariable`. The combination of an RNG state, a specific |
| 21 | +`RandomVariable` type (e.g. `NormalRV`), and a set of distribution parameters |
| 22 | +uniquely defines the `RandomVariable` instances in a graph. |
| 23 | + |
| 24 | +This means that a "stream" of distinct RNG states is required in order to |
| 25 | +produce distinct random variables of the same kind. `RandomStream` provides a |
| 26 | +means of generating distinct random variables in a fully reproducible way. |
| 27 | + |
| 28 | +`RandomStream` is also designed to produce simpler graphs and work with more |
| 29 | +sophisticated `Op`\s like `Scan`, which makes it a user-friendly random variable |
| 30 | +interface in PyTensor. |
| 31 | + |
| 32 | +For an example of how to use random numbers, see :ref:`Using Random Numbers <using_random_numbers>`. |
| 33 | + |
| 34 | + |
| 35 | +.. class:: RandomStream() |
| 36 | + |
| 37 | + This is a symbolic stand-in for `numpy.random.Generator`. |
| 38 | + |
| 39 | + .. method:: updates() |
| 40 | + |
| 41 | + :returns: a list of all the (state, new_state) update pairs for the |
| 42 | + random variables created by this object |
| 43 | + |
| 44 | + This can be a convenient shortcut to enumerating all the random |
| 45 | + variables in a large graph in the ``update`` argument to |
| 46 | + `pytensor.function`. |
| 47 | + |
| 48 | + .. method:: seed(meta_seed) |
| 49 | + |
| 50 | + `meta_seed` will be used to seed a temporary random number generator, |
| 51 | + that will in turn generate seeds for all random variables |
| 52 | + created by this object (via `gen`). |
| 53 | + |
| 54 | + :returns: None |
| 55 | + |
| 56 | + .. method:: gen(op, *args, **kwargs) |
| 57 | + |
| 58 | + Return the random variable from ``op(*args, **kwargs)``. |
| 59 | + |
| 60 | + This function also adds the returned variable to an internal list so |
| 61 | + that it can be seeded later by a call to `seed`. |
| 62 | + |
| 63 | + .. method:: uniform, normal, binomial, multinomial, random_integers, ... |
| 64 | + |
| 65 | + See :ref: Available distributions `<_libdoc_tensor_random_distributions>`. |
| 66 | + |
| 67 | + |
| 68 | + .. testcode:: constructors |
| 69 | + |
| 70 | + from pytensor.tensor.random.utils import RandomStream |
| 71 | + |
| 72 | + rng = RandomStream() |
| 73 | + sample = rng.normal(0, 1, size=(2, 2)) |
| 74 | + |
| 75 | + fn = pytensor.function([], sample) |
| 76 | + print(fn(), fn()) # different numbers due to default updates |
| 77 | + |
| 78 | + |
| 79 | +Low-level objects |
| 80 | +================= |
| 81 | + |
| 82 | +.. automodule:: pytensor.tensor.random.op |
| 83 | + :members: RandomVariable, default_rng |
18 | 84 |
|
| 85 | +..automodule:: pytensor.tensor.random.type |
| 86 | + :members: RandomType, RandomGeneratorType, random_generator_type |
19 | 87 |
|
20 |
| -.. automodule:: pytensor.tensor.random.basic |
21 |
| - :members: |
| 88 | +.. automodule:: pytensor.tensor.random.var |
| 89 | + :members: RandomGeneratorSharedVariable |
0 commit comments