Tensorflow `set_random_seed` 不工作
Tensorflow `set_random_seed` not working
调用 tf.set_random_seed(SEED)
没有任何效果,据我所知...
例如,运行在 IPython notebook 中多次使用下面的代码每次都会产生不同的输出:
import tensorflow as tf
tf.set_random_seed(42)
sess = tf.InteractiveSession()
a = tf.constant([1, 2, 3, 4, 5])
tf.initialize_all_variables().run()
a_shuf = tf.random_shuffle(a)
print(a.eval())
print(a_shuf.eval())
sess.close()
如果我明确设置种子:a_shuf = tf.random_shuffle(a, seed=42)
,每个 运行 后的输出都是相同的。但是,如果我已经调用 tf.set_random_seed(42)
,为什么还需要设置种子?
使用 numpy 的等效代码可以正常工作:
import numpy as np
np.random.seed(42)
a = [1,2,3,4,5]
np.random.shuffle(a)
print(a)
那只设置图级随机种子。如果连续多次执行此代码段,图形将发生变化,并且两个 shuffle 语句将获得不同的操作级别种子。详细信息在 doc string for set_random_seed
中描述
要获得确定性a_shuf
,您可以
- 在调用之间调用
tf.reset_default_graph()
或
- 设置随机播放的操作级种子:
a_shuf = tf.random_shuffle(a, seed=42)
调用 tf.set_random_seed(SEED)
没有任何效果,据我所知...
例如,运行在 IPython notebook 中多次使用下面的代码每次都会产生不同的输出:
import tensorflow as tf
tf.set_random_seed(42)
sess = tf.InteractiveSession()
a = tf.constant([1, 2, 3, 4, 5])
tf.initialize_all_variables().run()
a_shuf = tf.random_shuffle(a)
print(a.eval())
print(a_shuf.eval())
sess.close()
如果我明确设置种子:a_shuf = tf.random_shuffle(a, seed=42)
,每个 运行 后的输出都是相同的。但是,如果我已经调用 tf.set_random_seed(42)
,为什么还需要设置种子?
使用 numpy 的等效代码可以正常工作:
import numpy as np
np.random.seed(42)
a = [1,2,3,4,5]
np.random.shuffle(a)
print(a)
那只设置图级随机种子。如果连续多次执行此代码段,图形将发生变化,并且两个 shuffle 语句将获得不同的操作级别种子。详细信息在 doc string for set_random_seed
要获得确定性a_shuf
,您可以
- 在调用之间调用
tf.reset_default_graph()
或 - 设置随机播放的操作级种子:
a_shuf = tf.random_shuffle(a, seed=42)