我正在试验tf.scatter_nd_update
,我注意到渴望和不渴望(懒惰)执行之间在行为上有些特殊的差异。让我们从一个简单的渴望模式示例开始:
import tensorflow as tf
tf.enable_eager_execution()
ref = tf.Variable(tf.ones([5, 3], dtype=tf.int32))
updates = tf.Variable([[0, 5, 2]])
update = tf.scatter_nd_update(ref, [[0]], updates)
print(update.numpy())
工作正常:
[[0 5 2]
[1 1 1]
[1 1 1]
[1 1 1]
[1 1 1]]
同一脚本也可用于不急于执行的程序:
import tensorflow as tf
ref = tf.Variable(tf.ones([5, 3], dtype=tf.int32))
updates = tf.Variable([[0, 5, 2]])
update = tf.scatter_nd_update(ref, [[0]], updates)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print(sess.run(update))
特殊的行为是在急切执行中,如果我将indices
中的tf.scatter_nd_update
参数替换为示例中的[0]
这样的等级1索引,它仍然有效,但仅在急切模式下有效
import tensorflow as tf
tf.enable_eager_execution()
ref = tf.Variable(tf.ones([5, 3], dtype=tf.int32))
updates = tf.Variable([[0, 5, 2]])
update = tf.scatter_nd_update(ref, [0], updates) # <- indices here are rank 1
print(update.numpy())
在惰性执行模式下,将显示一条错误消息:
ValueError:input.shape = [5,3]的内部1个维必须与 的内部2个维度。shape= [1,3]:形状必须等于等级, 但对于'ScatterNdUpdate'(op:'ScatterNdUpdate'),则为1和2 输入形状:[5,3],[1],[1,3]。
有人可以解释这种行为差异吗?我知道急切的执行会在较早时(实际上是在声明时)提供一些信息,这会影响某些功能,但是我似乎不了解它如何影响这种情况?
在tf 1.15上测试。