Issue
For example the input:
a = tf.constant([1, 2, 3, 1, 2, 1, 4])
I want:
a_mask = [True, True, True, False, False, False, True]
Solution
Try using tf.math.unsorted_segment_min
with tf.tensor_scatter_nd_update
:
import tensorflow as tf
a = tf.constant([1, 2, 3, 1, 2, 1, 4])
v, i = tf.unique(a)
indices = tf.math.unsorted_segment_min(tf.range(tf.shape(a)[0]), i, tf.shape(v)[0])
updates = tf.ones_like(indices, dtype=tf.bool)
mask = tf.tensor_scatter_nd_update(tf.zeros_like(a, dtype=tf.bool), tf.expand_dims(indices, axis=-1), updates)
print(mask)
tf.Tensor([ True True True False False False True], shape=(7,), dtype=bool)
Answered By - AloneTogether
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.