1

我只在 TensorFlow V2.x 中使用 tf.keras。我可以设置的所有种子是什么?我只找到了 tf.random.set_seed()。还有其他种子吗?

4

1 回答 1

1

以下是我们尝试过的实验。结果tf.random.set_seed是相同的。

实验一: 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为和的每次迭代生成相同的A2A3

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为和的每次迭代生成相同的A2A3。并且结果与之前的运行结果相匹配。

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)

即使重新启动内核,这些值也保持不变。

如果您仍有任何疑问,请与您的期望分享可重现的代码。

希望这能回答你的问题。快乐学习。

于 2020-06-09T23:41:19.097 回答