Complete the code to select the first row of the tensor.
import tensorflow as tf tensor = tf.constant([[1, 2], [3, 4], [5, 6]]) first_row = tensor[1]
The correct way to select the first row of a 2D tensor is using [0, :], which means row 0 and all columns.
Complete the code to slice the tensor to get the last two rows.
import tensorflow as tf tensor = tf.constant([[10, 20], [30, 40], [50, 60], [70, 80]]) last_two_rows = tensor[1]
Using [-2:] slices the tensor to get the last two rows.
Fix the error in the code to select the second column of the tensor.
import tensorflow as tf tensor = tf.constant([[5, 10, 15], [20, 25, 30], [35, 40, 45]]) second_column = tensor[1]
To select the second column, use [:, 1] which means all rows and column index 1.
Fill both blanks to create a tensor slice with the first two rows and last two columns.
import tensorflow as tf tensor = tf.constant([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) slice_tensor = tensor[1][2]
To get the first two rows and last two columns, slice rows with [0:2, and columns with 2:].
Fill both blanks to create a slice selecting the middle row and middle two columns.
import tensorflow as tf tensor = tf.constant([[10, 20, 30, 40], [50, 60, 70, 80], [90, 100, 110, 120]) slice_tensor = tensor[1][2]]
To select the middle row and middle two columns, slice rows with [1:2, and columns with 1:3. The last blank is the closing bracket ].