Reputation: 3566
I have a tensor and an index tensor of the same rank. I want to set the values of the tensor that correspond to the indices in the index tensor to a certain scalar. How do I do this?
In other words, I'm looking for the Tensorflow equivalent of the following Numpy operation:
array[indices] = scalar
In my concrete case we're talking about a 1D tensor:
mask = tf.zeros_like(some_1D_tensor)
(e.g. mask = [0, 0, 0, 0, 0])
Let indices
be a 1D tensor that contains the indices of mask
that I'd like to set to the scalar value 1. So I want:
mask[indices] = 1
(e.g. for indices = [1, 3] the output should be mask == [0, 1, 0, 1, 0])
Upvotes: 2
Views: 655
Reputation: 3566
I don't know if it wasn't there before or if I just haven't seen it, but the general case equivalent of
array[indices] = scalar
is
tensor = tf.scatter_nd_update(tensor, indices, updates)
using tf.scatter_nd_update()
Upvotes: 3