Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to select the first row of the tensor.
PyTorch
import torch x = torch.tensor([[1, 2], [3, 4], [5, 6]]) first_row = x[1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 1 instead of 0 to get the first row.
Using slicing syntax instead of indexing.
✗ Incorrect
The first row of a tensor is accessed by using index 0 inside square brackets.
2fill in blank
mediumComplete the code to select the last column of the tensor.
PyTorch
import torch x = torch.tensor([[1, 2, 3], [4, 5, 6]]) last_col = x[1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using row index -1 instead of column index -1.
Swapping row and column indices.
✗ Incorrect
To select the last column, use ':' for all rows and '-1' for the last column index.
3fill in blank
hardFix the error in the code to correctly slice the tensor to get rows 1 and 2.
PyTorch
import torch x = torch.tensor([[10, 20], [30, 40], [50, 60]]) subset = x[1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a comma instead of colon in slicing.
Using incorrect stop index.
✗ Incorrect
Slicing uses start:stop syntax, and stop is exclusive. To get rows 1 and 2, use 1:3.
4fill in blank
hardFill both blanks to select the middle column and rows 0 and 1.
PyTorch
import torch x = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) subset = x[1][2]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping row and column indices.
Using incorrect slice ranges.
✗ Incorrect
Use [0:2] to select rows 0 and 1, and [:, 1] to select the middle column.
5fill in blank
hardFill all three blanks to create a tensor slice with rows 1 to 2 and columns 0 to 1.
PyTorch
import torch x = torch.tensor([[5, 10, 15], [20, 25, 30], [35, 40, 45]]) subset = x[1][2][3]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up row and column slices.
Using incorrect slice indices.
✗ Incorrect
Use [1:3] to select rows 1 and 2, and [:, 0:2] to select columns 0 and 1.