'tf.compat.v1.set_random_seed' is compatible with eager mode. However,
in eager mode this API will set the global seed instead of the
graph-level seed of the default graph. In TF2 this API is changed to
tf.random.set_seed.
Operations that rely on a random seed actually derive it from two seeds:
the graph-level and operation-level seeds. This sets the graph-level seed.
Its interactions with operation-level seeds is as follows:
If neither the graph-level nor the operation seed is set:
A random seed is used for this op.
If the graph-level seed is set, but the operation seed is not:
The system deterministically picks an operation seed in conjunction with
the graph-level seed so that it gets a unique random sequence. Within the
same version of tensorflow and user code, this sequence is deterministic.
However across different versions, this sequence might change. If the
code depends on particular seeds to work, specify both graph-level
and operation-level seeds explicitly.
If the graph-level seed is not set, but the operation seed is set:
A default graph-level seed and the specified operation seed are used to
determine the random sequence.
If both the graph-level and the operation seed are set:
Both seeds are used in conjunction to determine the random sequence.
To illustrate the user-visible effects, consider these examples:
To generate different sequences across sessions, set neither
graph-level nor op-level seeds:
To generate the same repeatable sequence for an op across sessions, set the
seed for the op:
a=tf.random.uniform([1],seed=1)b=tf.random.normal([1])# Repeatedly running this block with the same graph will generate the same# sequence of values for 'a', but different sequences of values for 'b'.print("Session 1")withtf.compat.v1.Session()assess1:print(sess1.run(a))# generates 'A1'print(sess1.run(a))# generates 'A2'print(sess1.run(b))# generates 'B1'print(sess1.run(b))# generates 'B2'print("Session 2")withtf.compat.v1.Session()assess2:print(sess2.run(a))# generates 'A1'print(sess2.run(a))# generates 'A2'print(sess2.run(b))# generates 'B3'print(sess2.run(b))# generates 'B4'
To make the random sequences generated by all ops be repeatable across
sessions, set a graph-level seed:
tf.compat.v1.random.set_random_seed(1234)a=tf.random.uniform([1])b=tf.random.normal([1])# Repeatedly running this block with the same graph will generate the same# sequences of 'a' and 'b'.print("Session 1")withtf.compat.v1.Session()assess1:print(sess1.run(a))# generates 'A1'print(sess1.run(a))# generates 'A2'print(sess1.run(b))# generates 'B1'print(sess1.run(b))# generates 'B2'print("Session 2")withtf.compat.v1.Session()assess2:print(sess2.run(a))# generates 'A1'print(sess2.run(a))# generates 'A2'print(sess2.run(b))# generates 'B1'print(sess2.run(b))# generates 'B2'
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-26 UTC."],[],[],null,["# tf.compat.v1.set_random_seed\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/framework/random_seed.py#L96-L207) |\n\nSets the graph-level random seed for the default graph.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.random.set_random_seed`](https://www.tensorflow.org/api_docs/python/tf/compat/v1/set_random_seed)\n\n\u003cbr /\u003e\n\n tf.compat.v1.set_random_seed(\n seed\n )\n\n\u003cbr /\u003e\n\nMigrate to TF2\n--------------\n\n\u003cbr /\u003e\n\n| **Caution:** This API was designed for TensorFlow v1. Continue reading for details on how to migrate from this API to a native TensorFlow v2 equivalent. See the [TensorFlow v1 to TensorFlow v2 migration guide](https://www.tensorflow.org/guide/migrate) for instructions on how to migrate the rest of your code.\n\n'tf.compat.v1.set_random_seed' is compatible with eager mode. However,\nin eager mode this API will set the global seed instead of the\ngraph-level seed of the default graph. In TF2 this API is changed to\n[tf.random.set_seed](https://www.tensorflow.org/api_docs/python/tf/random/set_seed).\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nDescription\n-----------\n\n### Used in the notebooks\n\n| Used in the tutorials |\n|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [TensorFlow Constrained Optimization Example Using CelebA Dataset](https://www.tensorflow.org/responsible_ai/fairness_indicators/tutorials/Fairness_Indicators_TFCO_CelebA_Case_Study) - [Wiki Talk Comments Toxicity Prediction](https://www.tensorflow.org/responsible_ai/fairness_indicators/tutorials/Fairness_Indicators_TFCO_Wiki_Case_Study) - [Fitting Dirichlet Process Mixture Model Using Preconditioned Stochastic Gradient Langevin Dynamics](https://www.tensorflow.org/probability/examples/Fitting_DPMM_Using_pSGLD) |\n\nOperations that rely on a random seed actually derive it from two seeds:\nthe graph-level and operation-level seeds. This sets the graph-level seed.\n\nIts interactions with operation-level seeds is as follows:\n\n1. If neither the graph-level nor the operation seed is set: A random seed is used for this op.\n2. If the graph-level seed is set, but the operation seed is not: The system deterministically picks an operation seed in conjunction with the graph-level seed so that it gets a unique random sequence. Within the same version of tensorflow and user code, this sequence is deterministic. However across different versions, this sequence might change. If the code depends on particular seeds to work, specify both graph-level and operation-level seeds explicitly.\n3. If the graph-level seed is not set, but the operation seed is set: A default graph-level seed and the specified operation seed are used to determine the random sequence.\n4. If both the graph-level and the operation seed are set: Both seeds are used in conjunction to determine the random sequence.\n\nTo illustrate the user-visible effects, consider these examples:\n\nTo generate different sequences across sessions, set neither\ngraph-level nor op-level seeds: \n\n a = tf.random.uniform([1])\n b = tf.random.normal([1])\n\n print(\"Session 1\")\n with tf.compat.v1.Session() as sess1:\n print(sess1.run(a)) # generates 'A1'\n print(sess1.run(a)) # generates 'A2'\n print(sess1.run(b)) # generates 'B1'\n print(sess1.run(b)) # generates 'B2'\n\n print(\"Session 2\")\n with tf.compat.v1.Session() as sess2:\n print(sess2.run(a)) # generates 'A3'\n print(sess2.run(a)) # generates 'A4'\n print(sess2.run(b)) # generates 'B3'\n print(sess2.run(b)) # generates 'B4'\n\nTo generate the same repeatable sequence for an op across sessions, set the\nseed for the op: \n\n a = tf.random.uniform([1], seed=1)\n b = tf.random.normal([1])\n\n # Repeatedly running this block with the same graph will generate the same\n # sequence of values for 'a', but different sequences of values for 'b'.\n print(\"Session 1\")\n with tf.compat.v1.Session() as sess1:\n print(sess1.run(a)) # generates 'A1'\n print(sess1.run(a)) # generates 'A2'\n print(sess1.run(b)) # generates 'B1'\n print(sess1.run(b)) # generates 'B2'\n\n print(\"Session 2\")\n with tf.compat.v1.Session() as sess2:\n print(sess2.run(a)) # generates 'A1'\n print(sess2.run(a)) # generates 'A2'\n print(sess2.run(b)) # generates 'B3'\n print(sess2.run(b)) # generates 'B4'\n\nTo make the random sequences generated by all ops be repeatable across\nsessions, set a graph-level seed: \n\n tf.compat.v1.random.set_random_seed(1234)\n a = tf.random.uniform([1])\n b = tf.random.normal([1])\n\n # Repeatedly running this block with the same graph will generate the same\n # sequences of 'a' and 'b'.\n print(\"Session 1\")\n with tf.compat.v1.Session() as sess1:\n print(sess1.run(a)) # generates 'A1'\n print(sess1.run(a)) # generates 'A2'\n print(sess1.run(b)) # generates 'B1'\n print(sess1.run(b)) # generates 'B2'\n\n print(\"Session 2\")\n with tf.compat.v1.Session() as sess2:\n print(sess2.run(a)) # generates 'A1'\n print(sess2.run(a)) # generates 'A2'\n print(sess2.run(b)) # generates 'B1'\n print(sess2.run(b)) # generates 'B2'\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|--------|----------|\n| `seed` | integer. |\n\n\u003cbr /\u003e"]]