Python tensorflow中的渴望执行和懒惰执行的差异
我正在试验tf.scatter\u and\u update,我注意到在急切执行和不急切(懒惰)执行之间的行为有一些特殊的差异。让我们从一个简单的急切模式示例开始:Python tensorflow中的渴望执行和懒惰执行的差异,python,tensorflow,Python,Tensorflow,我正在试验tf.scatter\u and\u 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_updat
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))
特殊的行为是,在急切执行中,如果我将tf.scatter\u nd\u update
中的索引
参数替换为秩1索引,如本例中的[0]
,它仍然有效,但仅在急切模式下有效
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())
将tensorflow导入为tf
tf.enable_eager_execution()
ref=tf.Variable(tf.ones([5,3],dtype=tf.int32))
updates=tf.Variable([[0,5,2]])
更新=tf.分散和更新(参考[0],更新)#