Skip to content Skip to sidebar Skip to footer

How Can I Get Specific Rows Of A Tensor In Tensorflow?

I have several tensors: logits: This tensor contains the final prediction scores. tf.Tensor 'MemN2N_1/MatMul_3:0' shape=(?, 18230) dtype=float32 The final prediction is computed

Solution 1:

Did you try out tf.equal? This compares two tensors and creates a new tensor containing True where equal, and False where not equal.

With this bool-tensor you feed tf.select, which choses from one tensor or another, element-wise, depending on the bool-value you created in step one.

Didn't look deeply into the particular shapes you provided, but with those two ops you can create the kind of flow you are asking for.

Solution 2:

Try tf.gather.

row_indices = [1]
row = tf.gather(tf.constant([[1, 2],[3, 4]]), row_indices)
tf.Session().run(row)  # returns [[3, 4]]

You can remove the leading dimension of size 1 using tf.squeeze:

row = tf.squeeze(row, squeeze_dims=0)

Solution 3:

You could use tf.slice(input_, begin, size, name=None). See the TensorFlow documentation for more details: https://www.tensorflow.org/api_docs/python/tf/slice

Post a Comment for "How Can I Get Specific Rows Of A Tensor In Tensorflow?"