How to drop a row of a tensor tensor containing nan in TensorFlow

Question:

I have a tensor:

tensor = tf.convert_to_tensor(np.array([[0, 1, 2, 3, 4, np.nan],
                                       [6, 7, 8, 9, 10, 11]]))

and would like to drop the row that has nan. For instance:

<tf.Tensor: shape=(6,), dtype=int64, numpy=array([ 6,  7,  8,  9, 10, 11])>

How do you do this in TensorFlow?

Asked By: mCalado

||

Answers:

This code will remove nan rows:

tensor[~np.isnan(tensor).any(axis=1)]
Answered By: Adarsh Wase

The accepted answer only works in eager mode. For all situations, one can try,

tf.boolean_mask(tensor, tf.reduce_all(~tf.math.is_nan(tensor), axis=-1))
Answered By: V.M
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.