I am using Tensorflow 2.0, and am trying to update slices within my tensor.
Using a plain item assignment like in PyTorch, it does not work.
import tensorflow as tf
tensor = tf.ones((10, 192, 85))
tensor[:, :, 0] = tf.math.sigmoid([:, :, 0])
>>> Output
TypeError: 'tensorflow.python.framework.ops.EagerTensor' object does not support item assignment
I see that it is possible to use tf.tensor_scatter_nd_update
, but it feels too verbose to be efficient, as I have to separately derive the indices to be updated. Thus, I am not sure if this is the best way to do item assignment in eager tensors (I need the code block below to achieve the simpler "PyTorch 2-liner style" above):
import tensorflow as tf
def get_indices(tensor):
indices = []
for i in range(tensor.shape[0]):
for j in range(tensor.shape[1]):
indices.append([i, j, 0])
return tf.convert_to_tensor(indices)
tensor = tf.ones((10, 192, 85))
indices = get_indices(tensor)
updates = tf.reshape(tf.math.sigmoid(tensor[:, :, 0]), (-1,))
tensor = tf.tensor_scatter_nd_update(tensor, indices, updates)
Is there an easier/more efficient way to do item assignment of EagerTensor
s in Tensorflow 2.0?