Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add two tensors element-wise.
PyTorch
import torch a = torch.tensor([1, 2, 3]) b = torch.tensor([4, 5, 6]) result = a[1]b print(result)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
- or * instead of + for addition.✗ Incorrect
Using the + operator adds two tensors element-wise.
2fill in blank
mediumComplete the code to multiply two tensors element-wise.
PyTorch
import torch a = torch.tensor([1, 2, 3]) b = torch.tensor([4, 5, 6]) result = a[1]b print(result)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
+ or @ instead of * for element-wise multiplication.✗ Incorrect
The * operator multiplies tensors element-wise in PyTorch.
3fill in blank
hardFix the error in the code to perform matrix multiplication of two 2D tensors.
PyTorch
import torch a = torch.tensor([[1, 2], [3, 4]]) b = torch.tensor([[5, 6], [7, 8]]) result = a[1]b print(result)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
* which does element-wise multiplication, not matrix multiplication.✗ Incorrect
The @ operator performs matrix multiplication in PyTorch.
4fill in blank
hardFill both blanks to create a tensor of squares for numbers 1 to 5, but only include squares greater than 10.
PyTorch
squares = {x: x[1]2 for x in range(1, 6) if x[2]10}
print(squares) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
* instead of ** for square.Using
< instead of > for filtering.✗ Incorrect
** is used for exponentiation (square), and > filters values greater than 10.
5fill in blank
hardFill all three blanks to create a dictionary with uppercase keys, values as original numbers, and only include numbers greater than 2.
PyTorch
result = [1]: [2] for [3] in range(1, 6) if [3] > 2} print(result)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Not converting keys to uppercase strings.
✗ Incorrect
Use str(x).upper() to uppercase keys, x as values, and x as the loop variable.