为什么 TensorFlow 2 和 1 的 RNG 不同?
Why is RNG different for TensorFlow 2 and 1?
import numpy as np
np.random.seed(1)
import random
random.seed(2)
import tensorflow as tf
tf.compat.v1.set_random_seed(3) # graph-level seed
if tf.__version__[0] == '2':
tf.random.set_seed(4) # global seed
else:
tf.set_random_seed(4) # global seed
from tensorflow.keras.initializers import glorot_uniform as GlorotUniform
from tensorflow.keras import backend as K
init = GlorotUniform(seed=5)(shape=(4, 4))
print(K.eval(init))
[[-0.75889236 0.5744677 0.82025963 -0.26889956]
[ 0.0180248 -0.24747121 -0.0666492 0.23440498]
[ 0.61886185 0.05548459 0.39713246 0.126324 ]
[ 0.6639387 -0.58397514 0.39671892 0.67872125]] # TF 2
[[ 0.2515846 -0.41902617 -0.7859829 0.41573995]
[ 0.8099498 -0.6861247 -0.46198446 -0.7579694 ]
[ 0.29976922 0.0310365 0.5031274 0.314076 ]
[-0.62062943 -0.01889879 0.7725797 -0.65635633]] # TF 1
为什么不同?这在两个版本之间造成了严重的可重复性问题——在 相同版本 的 (TF2) Graph vs. Eager 中,这个或其他问题。更重要的是,TF1的RNG序列可以用在TF2中吗?
经过足够的挖掘 - 是的。长话短说:
- TF1 中的 TF2 行为:
from tensorflow.python.keras.initializers import GlorotUniformV2 as GlorotUniform
- TF2 中的 TF1 行为:
from tensorflow.python.keras.initializers import GlorotUniform
TF2 本质上执行了引擎盖下的第一颗子弹; GlorotUniform
实际上是 GlorotUniformV2
.
一些细节:
找到文档 - 但代码本身终止于一些 pywrapped 编译代码(TF1 -- TF2 - 由于某种原因 Github 拒绝显示 gen_stateless_random_ops
的 TF2 和 gen_random_ops
TF1, 但你可以在本地安装中找到两者):
tensorflow.python.ops.gen_random_ops.truncated_normal
Outputs random values from a truncated normal distribution.
The generated values follow a normal distribution with mean 0 and
standard deviation 1, except that values whose magnitude is more
than 2 standard deviations from the mean are dropped and re-picked.
tensorflow.python.ops.gen_stateless_random_ops.truncated_normal
Outputs deterministic pseudorandom values from a truncated normal distribution.
The generated values follow a normal distribution with mean 0 and
standard deviation 1, except that values whose magnitude is more
than 2 standard deviations from the mean are dropped and re-picked.
The outputs are a deterministic function of shape
and seed
.
第一个和第二个最终分别是 GlorotUniform
和 GlorotUniformV2
路由到的位置。 TF2 的 from tensorflow.keras.initializers
从 init_ops_v2
导入(即 V2
),而 TF1 从 init_ops
.
import numpy as np
np.random.seed(1)
import random
random.seed(2)
import tensorflow as tf
tf.compat.v1.set_random_seed(3) # graph-level seed
if tf.__version__[0] == '2':
tf.random.set_seed(4) # global seed
else:
tf.set_random_seed(4) # global seed
from tensorflow.keras.initializers import glorot_uniform as GlorotUniform
from tensorflow.keras import backend as K
init = GlorotUniform(seed=5)(shape=(4, 4))
print(K.eval(init))
[[-0.75889236 0.5744677 0.82025963 -0.26889956]
[ 0.0180248 -0.24747121 -0.0666492 0.23440498]
[ 0.61886185 0.05548459 0.39713246 0.126324 ]
[ 0.6639387 -0.58397514 0.39671892 0.67872125]] # TF 2
[[ 0.2515846 -0.41902617 -0.7859829 0.41573995]
[ 0.8099498 -0.6861247 -0.46198446 -0.7579694 ]
[ 0.29976922 0.0310365 0.5031274 0.314076 ]
[-0.62062943 -0.01889879 0.7725797 -0.65635633]] # TF 1
为什么不同?这在两个版本之间造成了严重的可重复性问题——在 相同版本 的 (TF2) Graph vs. Eager 中,这个或其他问题。更重要的是,TF1的RNG序列可以用在TF2中吗?
经过足够的挖掘 - 是的。长话短说:
- TF1 中的 TF2 行为:
from tensorflow.python.keras.initializers import GlorotUniformV2 as GlorotUniform
- TF2 中的 TF1 行为:
from tensorflow.python.keras.initializers import GlorotUniform
TF2 本质上执行了引擎盖下的第一颗子弹; GlorotUniform
实际上是 GlorotUniformV2
.
一些细节:
找到文档 - 但代码本身终止于一些 pywrapped 编译代码(TF1 -- TF2 - 由于某种原因 Github 拒绝显示 gen_stateless_random_ops
的 TF2 和 gen_random_ops
TF1, 但你可以在本地安装中找到两者):
tensorflow.python.ops.gen_random_ops.truncated_normal
Outputs random values from a truncated normal distribution.The generated values follow a normal distribution with mean 0 and standard deviation 1, except that values whose magnitude is more than 2 standard deviations from the mean are dropped and re-picked.
tensorflow.python.ops.gen_stateless_random_ops.truncated_normal
Outputs deterministic pseudorandom values from a truncated normal distribution.The generated values follow a normal distribution with mean 0 and standard deviation 1, except that values whose magnitude is more than 2 standard deviations from the mean are dropped and re-picked.
The outputs are a deterministic function of
shape
andseed
.
第一个和第二个最终分别是 GlorotUniform
和 GlorotUniformV2
路由到的位置。 TF2 的 from tensorflow.keras.initializers
从 init_ops_v2
导入(即 V2
),而 TF1 从 init_ops
.