zenna
zenna

Reputation: 9176

How do I update elements of a tensor using indices?

I'm looking for an "update" function that takes in a tensor t1, some indices and values and returns a new tensor t2, which is t1 but with the values at the indices changed accordingly.

This seems like the most basic of functions, but I do not see it in the documentation.

What I do see is tf.scatter_update, which updates values in a Variable: in a way it is what I want. I could conceivably construct a Variable for t2 (would this work?), but t2 is not supposed to be a stateful variable and I would be populating it with arbitrary initial values.

Or, I could manually construct a Python nested list adding each element of t1 and t2 individually, then calling tf.convert_to_tensor. I could be wrong, but this seems likely to horribly inefficient.

Upvotes: 9

Views: 10171

Answers (1)

Frederic Shen
Frederic Shen

Reputation: 181

You can try tf.scatter_update or tf.scatter_add to update tensor value according to indices.

See the question adjust-single-value-within-tensor-tensorflow for a reference.

Upvotes: 6

Related Questions