Skip to content Skip to sidebar Skip to footer

How To Increment Matrix Element In Tensorflow Using Tf.scatter_add?

tf.scatter_add works nicely for 1d (shape 1) tensors: > S = tf.Variable(tf.constant([1,2,3,4])) > sess.run(tf.initialize_all_variables()) > sess.run(tf.scatter_add(S, [0],

Solution 1:

tf.scatter_add updates slices of a tensor and not capable of updating individual coefficients. For instance, it can update entire rows of a matrix at once.

Also, the shape of the updates argument to tf.scatter_add depends on the shape of its indices argument. When the ref argument is a matrix with shape (M, N), then

  • If indices is a scalar i, then updates should be a vector with shape (N).
  • If indices is a vector [i1, i2, .. ik] with shape (k), then updates should have the shape (k, N).

In your case, you can simply add [1, 0] to the first row of M as follows to get the effect you want:

sess.run(tf.scatter_add(M, 0, [1, 0]))
array([[2, 2],
   [3, 4]], dtype=int32)

Post a Comment for "How To Increment Matrix Element In Tensorflow Using Tf.scatter_add?"