我只在tf.keras V2.x中使用TensorFlow。我能播下的种子是什么?我只找到了tf.random.set_seed()。还有别的种子吗?
发布于 2020-06-09 23:41:19
以下是我们尝试过的实验。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)
即使在重新启动内核之后,这些值仍然是相同的。
如果您仍然有任何疑问,请与您的期望分享可复制代码。
希望这能回答你的问题。学习愉快。
https://stackoverflow.com/questions/62028248
复制相似问题