tfp.substrates.numpy.random.sanitize_seed
Stay organized with collections
Save and categorize content based on your preferences.
Map various PRNG seed flavors to a seed Tensor
.
tfp.substrates.numpy.random.sanitize_seed(
seed, salt=None, name=None
)
This function implements TFP's standard PRNG seeding semantics.
See https://github.com/tensorflow/probability/blob/main/PRNGS.md
for details.
Operationally, sanitize_seed
maps any seed flavor to a
"stateless-compatible" seed. Under TensorFlow and NumPy this means:
- If the
seed
argument is an int
or None
, we use tf.random.stateless_uniform
to statefully draw a pair of unbounded int32
s and wrap them into a
Tensor.
- If the
seed
argument is a stateless-compatible seed already, we
just cast it to an int32[2]
Tensor.
Under JAX, this function only accepts outputs from jax.random.PRNGKey
, being
a no-op except for the salting behavior described below.
This, any function that accepts a seed
argument can be written in
stateless-seed style internally, and acquires TFP's
seed-type-directed stateless/stateful switching behavior by just
running the input seed through sanitize_seed
on entry.
The sanitize_seed
function also allows salting the seed: if a user
accidentally passes the same stateful seed to two different calls to
sanitize_seed
with different salts, they will get independent
randomness. We may micro-optimize by removing salting from
sanitize_seed
of already-stateless seeds in the future, as using a
stateless seed already requires seed uniqueness discipline.
Args |
seed
|
An int32[2] Tensor or a Python list or tuple of 2 ints ,
which will be treated as stateless seeds; or a Python int or
None , which will be treated as stateful seeds.
|
salt
|
An optional Python string.
|
name
|
An optional Python string, name to add to TF ops created by
this function.
|
Returns |
seed
|
An int32[2] Tensor suitable for use as a stateless PRNG
seed.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2023-11-21 UTC.
[null,null,["Last updated 2023-11-21 UTC."],[],[],null,["# tfp.substrates.numpy.random.sanitize_seed\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/probability/blob/v0.23.0/tensorflow_probability/substrates/numpy/internal/samplers.py#L62-L148) |\n\nMap various PRNG seed flavors to a seed `Tensor`.\n\n#### View aliases\n\n\n**Main aliases**\n\n[`tfp.experimental.substrates.numpy.random.sanitize_seed`](https://www.tensorflow.org/probability/api_docs/python/tfp/substrates/numpy/random/sanitize_seed)\n\n\u003cbr /\u003e\n\n tfp.substrates.numpy.random.sanitize_seed(\n seed, salt=None, name=None\n )\n\nThis function implements TFP's standard PRNG seeding semantics.\nSee \u003chttps://github.com/tensorflow/probability/blob/main/PRNGS.md\u003e\nfor details.\n\nOperationally, `sanitize_seed` maps any seed flavor to a\n\"stateless-compatible\" seed. Under TensorFlow and NumPy this means:\n\n- If the `seed` argument is an `int` or `None`, we use [`tf.random.stateless_uniform`](https://www.tensorflow.org/api_docs/python/tf/random/stateless_uniform) to *statefully* draw a pair of unbounded `int32`s and wrap them into a Tensor.\n- If the `seed` argument is a stateless-compatible seed already, we just cast it to an `int32[2]` Tensor.\n\nUnder JAX, this function only accepts outputs from `jax.random.PRNGKey`, being\na no-op except for the salting behavior described below.\n\nThis, any function that accepts a `seed` argument can be written in\nstateless-seed style internally, and acquires TFP's\nseed-type-directed stateless/stateful switching behavior by just\nrunning the input seed through `sanitize_seed` on entry.\n\nThe `sanitize_seed` function also allows salting the seed: if a user\naccidentally passes the same stateful seed to two different calls to\n`sanitize_seed` with different salts, they will get independent\nrandomness. We may micro-optimize by removing salting from\n`sanitize_seed` of already-stateless seeds in the future, as using a\nstateless seed already requires seed uniqueness discipline.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|--------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `seed` | An `int32[2]` Tensor or a Python list or tuple of 2 `ints`, which will be treated as stateless seeds; or a Python `int` or `None`, which will be treated as stateful seeds. |\n| `salt` | An optional Python string. |\n| `name` | An optional Python string, name to add to TF ops created by this function. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|--------|-----------------------------------------------------------------|\n| `seed` | An `int32[2]` Tensor suitable for use as a stateless PRNG seed. |\n\n\u003cbr /\u003e"]]