【问题标题】:What are all the random seeds for TensorFlow V2.x and tf.keras?TensorFlow V2.x 和 tf.keras 的所有随机种子是什么?
【发布时间】:2020-09-13 14:47:53
【问题描述】:

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

【问题讨论】:

  • 我还设置了 np.random.seed 和 os.environ['PYTHONHASHSEED']。我使用 CPU 进行计算。但结果仍然因运行而异。
  • 您能否详细说明您的要求。在此链接中,您可以解释不同的可能性tensorflow.org/api_docs/python/tf/random/set_seed
  • 我正在尝试开发一个可以产生可重复结果的模型。我只使用 tf.keras。 set_seed() 仍然会产生可变结果。还有什么我可以设置的,以便我的模型可以产生可重复的结果?

标签: tensorflow2.0 tensorflow2.x


【解决方案1】:

以下是我们尝试过的实验。 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)

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

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

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

【讨论】:

  • 谢谢,但你描述的是 tf.Tensor。我也可以获得 tf.Tensor 的可重现值。但我的问题是关于 tf.keras。你能举一个例子,你可以用 tf.keras 获得可重现的结果吗?谢谢。
  • 能否请您分享可重现的代码,以便我们更好地理解。我没有得到您在 tf.keras 中查找的操作。 tf.keras 是一个框架,我们可以从中导入库。
  • 问题已解决。它现在产生可重复的结果。谢谢,感谢您的帮助。
猜你喜欢
  • 2014-06-01
  • 2017-10-19
  • 1970-01-01
  • 2012-09-04
  • 2019-11-12
  • 1970-01-01
  • 2014-05-19
  • 2019-05-31
  • 2021-07-28
相关资源
最近更新 更多