我希望tensorflow在f(...)中执行以下操作
但tf.control_dependencies不做我想做的事。
如何修复控件依赖关系?
结果:
cache_ 0.0
x_ 2.0
AssertionError测试:
import tensorflow as tf
import numpy as np
def f(a, cache):
assign_op = tf.assign(cache, a)
with tf.control_dependencies([assign_op]):
return a
def main():
dtype = np.float32
data = tf.range(5, dtype=dtype)
cache = tf.Variable(0, dtype=dtype)
x = f(data[2], cache)
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init_op)
x_ = sess.run(x)
cache_ = sess.run(cache)
print("cache_", cache_)
print("x_", x_)
assert np.allclose(cache_, x_)
main()发布于 2019-06-26 01:55:14
问题是return a是Python。您没有在TensorFlow块中创建任何with操作。您可以使用tf.identity创建一个op,以确保在从assign_op读取a时首先执行。以下是更新的代码:
def f(a, cache):
assign_op = tf.assign(cache, a)
with tf.control_dependencies([assign_op]):
return tf.identity(a)https://stackoverflow.com/questions/56763087
复制相似问题