Given a 1D tensor (just to be clear, not array): [0,2,0,1,-3], I'd like to get back just the values different than zero. Following the example, I'd like to get back [2,1,-3] as the result. How can I do that in TensorFlow.js?
0
votes
1 Answers
1
votes
First we need to find the indexes of the non-zeros values by using
tf.whereAsyncon the tensor casted tobool. That way all values except 0 values aretrueand their indexes are thus collected.With
tf.gatherwe create a new tensor by collecting the values at the previous indexes.
t = tf.tensor1d([0,2,0,1,-3])
indices = await tf.whereAsync(t.cast('bool'))
tf.gather(t, indices.reshape([-1])).print()