简体   繁体   English

Tensorflow使Assign Op成为计算张量的显式依赖项

[英]Tensorflow make assign op an explicit dependency for computing a tensor

I want to be able to implicitly run an assign Op every single time I run another tensor which depends on the tf.Variable which is changed during the assign Op. 我希望每次运行另一个张量取决于tf.Variable时都隐式地运行assign Op,这在assign Op期间会更改。 I don't want to run the assign Op manually every single step. 我不想手动运行assign操作。 I tried 2 different approaches. 我尝试了2种不同的方法。 Here is a simple example illustration: 这是一个简单的示例插图:

target_prob     = tf.placeholder(dtype=tf.float32, shape=[None, 2])
target_var      = tf.Variable(0, trainable=False, dtype=tf.float32)
init_target_var = tf.assign(target_var, tf.zeros_like(target_prob),
                            validate_shape=False)

# First approach
with tf.control_dependencies([init_target_var]):
  result = target_prob + target_var

# Second approach
# [target_var] = tf.tuple([target_var], control_inputs=[init_target_var])
# result = target_prob + target_var

sess = tf.Session()
sess.run(tf.global_variables_initializer())
res1 = sess.run(result, feed_dict={target_prob: np.ones([10, 2], dtype=np.float32)})
res2 = sess.run(result, feed_dict={target_prob: np.ones([12, 2], dtype=np.float32)})

Both fail with the error InvalidArgumentError (see above for traceback): Incompatible shapes: [12,2] vs. [10,2] when res2 is being computed. 两者都失败,并显示错误InvalidArgumentError (see above for traceback): Incompatible shapes: [12,2] vs. [10,2]计算res2时, InvalidArgumentError (see above for traceback): Incompatible shapes: [12,2] vs. [10,2] It all works if I instead do: 如果我改为这样做,这一切都可行:

res1 = sess.run(result, feed_dict={target_prob: np.ones([10, 2], dtype=np.float32)})
sess.run(init_target_var, feed_dict={target_prob: np.ones([12, 2], dtype=np.float32)})
res2 = sess.run(result, feed_dict={target_prob: np.ones([12, 2], dtype=np.float32)})

But again, running init_target_var explicitly is exactly what I am trying to avoid. 但是,再次明确地运行init_target_var正是我要避免的事情。

PS The above is just a simplistic example. PS以上只是一个简单的例子。 My final goal is to use the resulting tensor from tf.scatter_add which unfortunately requires a mutable tensor as input. 我的最终目标是使用来自tf.scatter_add的结果张量,不幸的是,该张量需要可变的张量作为输入。

For anyone who comes across this, I was actually using the wrong tensor when computing result . 对于遇到这种情况的任何人,在计算result时我实际上使用了错误的张量。 The correct code is: 正确的代码是:

import tensorflow as tf
import numpy as np

target_prob         = tf.placeholder(dtype=tf.float32, shape=[None, 2])
tmp_var             = tf.Variable(0, trainable=False, dtype=tf.float32, validate_shape=False)
target_var          = tf.assign(tmp_var, tf.zeros_like(target_prob), validate_shape=False)

with tf.control_dependencies([target_var]):
  result = target_prob + target_var

sess = tf.Session()
sess.run(tf.global_variables_initializer())

res1 = sess.run(result, feed_dict={target_prob: np.ones([10, 2], dtype=np.float32)})
res2 = sess.run(result, feed_dict={target_prob: np.ones([12, 2], dtype=np.float32)})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM