0
0
TensorFlowml~5 mins

Indexing and slicing tensors in TensorFlow - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a tensor in TensorFlow?
A tensor is a multi-dimensional array used to store data in TensorFlow. It can have any number of dimensions, like scalars (0D), vectors (1D), matrices (2D), or higher.
Click to reveal answer
beginner
How do you get the first element of a 1D tensor named t?
You use t[0] to get the first element, just like accessing the first item in a list.
Click to reveal answer
beginner
What does the slice t[1:4] do on a 1D tensor?
It extracts elements starting from index 1 up to, but not including, index 4. So it gets elements at positions 1, 2, and 3.
Click to reveal answer
intermediate
How do you select the second row and third column element from a 2D tensor t?
Use t[1, 2]. The first index is the row (starting at 0), the second is the column.
Click to reveal answer
intermediate
What does t[:, 1] mean for a 2D tensor t?
It selects all rows (the :) but only the second column (index 1). This extracts a 1D tensor of that column.
Click to reveal answer
What does t[2:5] return for a 1D tensor t?
AElements at indices 3, 4, and 5
BElements at indices 2, 3, and 4
CElements at indices 2, 3, 4, and 5
DElements at indices 0 to 5
How do you select the last element of a 1D tensor t?
At[-1]
Bt[0]
Ct[len(t)]
Dt[1]
What does t[0, :] select in a 2D tensor t?
ALast row, all columns
BAll rows, first column
CFirst element only
DFirst row, all columns
If t is a 3D tensor, what does t[:, 1, :] select?
AAll elements in the second slice of the second dimension
BOnly the first element
CAll elements in the first slice of the first dimension
DOnly the last element
What is the shape of the tensor after slicing t[:, 2:5] if t is shape (4, 6)?
A(4, 6)
B(3, 6)
C(4, 3)
D(3, 4)
Explain how to use indexing and slicing to extract a sub-tensor from a 2D tensor in TensorFlow.
Think about how you pick rows and columns like selecting cells in a spreadsheet.
You got /4 concepts.
    Describe how negative indices work when indexing tensors in TensorFlow.
    Imagine counting backwards from the last item.
    You got /4 concepts.