0
0
PyTorchml~5 mins

Indexing and slicing in PyTorch - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is indexing in PyTorch tensors?
Indexing means selecting a single element from a tensor using its position, like picking one item from a list by its number.
Click to reveal answer
beginner
How does slicing work in PyTorch tensors?
Slicing means selecting a range of elements from a tensor, similar to cutting a piece from a loaf of bread by specifying start and end positions.
Click to reveal answer
beginner
What does the colon ':' symbol mean in tensor slicing?
The ':' means 'take all elements' along that dimension, like saying 'give me everything in this row or column'.
Click to reveal answer
intermediate
How do you select the first 3 rows and last 2 columns of a 2D tensor in PyTorch?
Use slicing like tensor[:3, -2:] which means 'rows from start to 3 (not including 3)' and 'last 2 columns'.
Click to reveal answer
intermediate
What happens if you use a negative index in PyTorch tensor indexing?
Negative index counts from the end, so -1 means the last element, -2 means second last, just like counting backwards in a list.
Click to reveal answer
In PyTorch, what does tensor[2] select?
AThe second element along the first dimension
BThe third element along the first dimension
CAll elements except the third
DThe last element
What does tensor[:, 1:4] do?
ASelects all rows and columns 1 to 3
BSelects rows 1 to 4 and all columns
CSelects only the first row and columns 1 to 4
DSelects all rows and columns 1 to 4
How do you select the last element of a tensor?
Atensor[-1]
Btensor[0]
Ctensor[1]
Dtensor[last]
What does tensor[1:5:2] mean?
ASelect elements from index 1 to 5, stepping by 2
BSelect elements from index 1 to 5, stepping by 1
CSelect elements from index 2 to 5, stepping by 2
DSelect elements from index 1 to 4, stepping by 2
If tensor is 3D, what does tensor[:, 0, :] select?
AAll elements in the first slice of the first dimension
BAll elements in the last slice of the third dimension
CAll elements in the first slice of the second dimension
DOnly the first element of the tensor
Explain how to use indexing and slicing to select parts of a PyTorch tensor.
Think about how you pick items from a list or a grid.
You got /4 concepts.
    Describe how negative indices work in PyTorch tensor indexing and slicing.
    Imagine counting backwards from the end of a line.
    You got /4 concepts.