TF - 在 model_fn 中将 global_step 传递给种子
TF - in model_fn pass global_step to seed
在 Tensorflow Estimator API (tf.estimator
) 中,有没有办法使用 model_fn
中的当前会话来评估张量并将值传递给 python?我希望根据 global_step
的值在 dropout 中有一个种子,但由于前者需要 int
,后者是 tensor
。
我看不出有什么方法可以访问 model_fn
中的 session
来获取 global_step
的当前值。
即使可能,在每一步更改 tf.nn.dropout
的种子也会在每一步使用不同的种子创建一个新的图操作,这将使图变得越来越大。即使没有 tf.estimator
,我也不知道你如何实现这个?
我想你想要的是确保在两次运行之间获得相同的随机性。使用 tf.set_random_seed()
设置图级随机种子或仅在 dropout 中使用正常的 seed
应该创建可重现的掩码序列。这是一个代码示例:
x = tf.ones(10)
y = tf.nn.dropout(x, 0.5, seed=42)
sess1 = tf.Session()
y1 = sess1.run(y)
y2 = sess1.run(y)
sess2 = tf.Session()
y3 = sess2.run(y)
y4 = sess2.run(y)
assert (y1 == y3).all() # y1 and y3 are the same
assert (y2 == y4).all() # y2 and y4 are the same
答案 提供了有关如何使图形随机性可重现的更多详细信息。
在 Tensorflow Estimator API (tf.estimator
) 中,有没有办法使用 model_fn
中的当前会话来评估张量并将值传递给 python?我希望根据 global_step
的值在 dropout 中有一个种子,但由于前者需要 int
,后者是 tensor
。
我看不出有什么方法可以访问 model_fn
中的 session
来获取 global_step
的当前值。
即使可能,在每一步更改 tf.nn.dropout
的种子也会在每一步使用不同的种子创建一个新的图操作,这将使图变得越来越大。即使没有 tf.estimator
,我也不知道你如何实现这个?
我想你想要的是确保在两次运行之间获得相同的随机性。使用 tf.set_random_seed()
设置图级随机种子或仅在 dropout 中使用正常的 seed
应该创建可重现的掩码序列。这是一个代码示例:
x = tf.ones(10)
y = tf.nn.dropout(x, 0.5, seed=42)
sess1 = tf.Session()
y1 = sess1.run(y)
y2 = sess1.run(y)
sess2 = tf.Session()
y3 = sess2.run(y)
y4 = sess2.run(y)
assert (y1 == y3).all() # y1 and y3 are the same
assert (y2 == y4).all() # y2 and y4 are the same
答案