以下是我们尝试过的实验。 tf.random.set_seed 的结果是相同的。
实验 1: tf.random.set_seed(1234) 只设置一次。
import tensorflow as tf
for i in range(5):
print("Iteration Number :", i)
tf.random.set_seed(1234)
print(tf.random.uniform([1])) # generates 'A1'
print(tf.random.uniform([1])) # generates 'A2'
print(tf.random.uniform([1])) # generates 'A3'
输出 - 为 A1、A2 和 A3 的每次迭代生成相同的值。
Iteration Number : 0
tf.Tensor([0.5380393], shape=(1,), dtype=float32)
tf.Tensor([0.3253647], shape=(1,), dtype=float32)
tf.Tensor([0.59750986], shape=(1,), dtype=float32)
Iteration Number : 1
tf.Tensor([0.5380393], shape=(1,), dtype=float32)
tf.Tensor([0.3253647], shape=(1,), dtype=float32)
tf.Tensor([0.59750986], shape=(1,), dtype=float32)
Iteration Number : 2
tf.Tensor([0.5380393], shape=(1,), dtype=float32)
tf.Tensor([0.3253647], shape=(1,), dtype=float32)
tf.Tensor([0.59750986], shape=(1,), dtype=float32)
Iteration Number : 3
tf.Tensor([0.5380393], shape=(1,), dtype=float32)
tf.Tensor([0.3253647], shape=(1,), dtype=float32)
tf.Tensor([0.59750986], shape=(1,), dtype=float32)
Iteration Number : 4
tf.Tensor([0.5380393], shape=(1,), dtype=float32)
tf.Tensor([0.3253647], shape=(1,), dtype=float32)
tf.Tensor([0.59750986], shape=(1,), dtype=float32)
让我们重新启动运行时或内核并验证结果。
输出 - 为 A1、A2 和 A3 的每次迭代生成相同的值。并且结果与之前的运行结果一致。
Iteration Number : 0
tf.Tensor([0.5380393], shape=(1,), dtype=float32)
tf.Tensor([0.3253647], shape=(1,), dtype=float32)
tf.Tensor([0.59750986], shape=(1,), dtype=float32)
Iteration Number : 1
tf.Tensor([0.5380393], shape=(1,), dtype=float32)
tf.Tensor([0.3253647], shape=(1,), dtype=float32)
tf.Tensor([0.59750986], shape=(1,), dtype=float32)
Iteration Number : 2
tf.Tensor([0.5380393], shape=(1,), dtype=float32)
tf.Tensor([0.3253647], shape=(1,), dtype=float32)
tf.Tensor([0.59750986], shape=(1,), dtype=float32)
Iteration Number : 3
tf.Tensor([0.5380393], shape=(1,), dtype=float32)
tf.Tensor([0.3253647], shape=(1,), dtype=float32)
tf.Tensor([0.59750986], shape=(1,), dtype=float32)
Iteration Number : 4
tf.Tensor([0.5380393], shape=(1,), dtype=float32)
tf.Tensor([0.3253647], shape=(1,), dtype=float32)
tf.Tensor([0.59750986], shape=(1,), dtype=float32)
实验 2: tf.random.set_seed(1234) 为每个操作设置。
for i in range(5):
print("Iteration Number :", i)
tf.random.set_seed(1234)
print(tf.random.uniform([1])) # generates 'A1'
tf.random.set_seed(1234)
print(tf.random.uniform([1])) # generates 'A1'
tf.random.set_seed(1234)
print(tf.random.uniform([1])) # generates 'A1'
输出 - 所有值都与为每个操作设置的 tf.random.set_seed 相同。
Iteration Number : 0
tf.Tensor([0.5380393], shape=(1,), dtype=float32)
tf.Tensor([0.5380393], shape=(1,), dtype=float32)
tf.Tensor([0.5380393], shape=(1,), dtype=float32)
Iteration Number : 1
tf.Tensor([0.5380393], shape=(1,), dtype=float32)
tf.Tensor([0.5380393], shape=(1,), dtype=float32)
tf.Tensor([0.5380393], shape=(1,), dtype=float32)
Iteration Number : 2
tf.Tensor([0.5380393], shape=(1,), dtype=float32)
tf.Tensor([0.5380393], shape=(1,), dtype=float32)
tf.Tensor([0.5380393], shape=(1,), dtype=float32)
Iteration Number : 3
tf.Tensor([0.5380393], shape=(1,), dtype=float32)
tf.Tensor([0.5380393], shape=(1,), dtype=float32)
tf.Tensor([0.5380393], shape=(1,), dtype=float32)
Iteration Number : 4
tf.Tensor([0.5380393], shape=(1,), dtype=float32)
tf.Tensor([0.5380393], shape=(1,), dtype=float32)
tf.Tensor([0.5380393], shape=(1,), dtype=float32)
即使重新启动内核,这些值也保持不变。
如果您仍有任何疑问,请与您的期望分享可重现的代码。
希望这能回答您的问题。快乐学习。