Difference in eager and lazy execution in tensorflow

Viewed 403

I am experimenting with tf.scatter_nd_update and I have noticed a somewhat peculiar difference in behavior between eager and not-eager (lazy) execution. Let's start with a simple eager mode example:

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())

which works fine:

[[0 5 2]
 [1 1 1]
 [1 1 1]
 [1 1 1]
 [1 1 1]]

The same script also works on not-eager execution:

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))

The peculiar behavior is that in eager execution if I replace the indices parameter in tf.scatter_nd_update with a rank-1 index like [0] in the example it still works but only in eager mode.

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())

In the lazy execution mode there is an error stating:

ValueError: The inner 1 dimensions of input.shape=[5,3] must match the inner 2 dimensions of updates.shape=[1,3]: Shapes must be equal rank, but are 1 and 2 for 'ScatterNdUpdate' (op: 'ScatterNdUpdate') with input shapes: [5,3], [1], [1,3].

Can someone explain this difference in behavior? I know that eager execution provides some information earlier (when declared actually) and this affects some functions but I don't seem to get how it effects this case?

Tested on tf 1.15.

0 Answers
Related