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)图与Eager图中出现了此问题或其他问题。更重要的是,TF1的RNG序列可以在TF2中使用吗
有足够的挖掘-是的。TL;博士:
from tensorflow.python.keras.initializers import GlorotUniformV2 as GlorotUniform
from tensorflow.python.keras.initializers import GlorotUniform
TF2基本上执行引擎盖下的第一个项目符号
GlorotUniform
实际上是GlorotUniformV2
一些细节:
已找到文档-但代码本身终止于某个pywrapped编译代码(TF1TF2出于某种原因,Github拒绝显示TF2的
gen_stateless_random_ops
和TF1的gen_random_ops
,但您可以在本地安装中找到这两个代码):第一个和第二个最终分别是
GlorotUniform
和GlorotUniformV2
的路径。TF2的from tensorflow.keras.initializers
从init_ops_v2
(即V2
)导入,而TF1从init_ops
导入相关问题 更多 >
编程相关推荐