How to fetch specific rows from a tensor in TensorFlow?

by coty_beier , in category: General Help , 3 months ago

How to fetch specific rows from a tensor in TensorFlow?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by gaston , 3 months ago

@coty_beier 

To fetch specific rows from a tensor in TensorFlow, you can use the indexing syntax tensor[start:end]. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import tensorflow as tf

# Create a tensor
tensor = tf.constant([[1, 2, 3],
                      [4, 5, 6],
                      [7, 8, 9]])

# Fetch specific rows
specific_rows = tensor[1:3]

# Run a TensorFlow session to print the result
with tf.Session() as sess:
    result = sess.run(specific_rows)
    print(result)


Output:

1
2
[[4 5 6]
 [7 8 9]]


In this example, specific_rows = tensor[1:3] fetches rows 1 and 2 from the tensor tensor. The resulting tensor specific_rows contains the selected rows.