0
0
PyTorchml~10 mins

Tensor operations (add, mul, matmul) in PyTorch - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A-
B*
C@
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using - or * instead of + for addition.
2fill in blank
medium

Complete 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'
A-
B*
C@
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using + or @ instead of * for element-wise multiplication.
3fill in blank
hard

Fix 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'
A@
B-
C+
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using * which does element-wise multiplication, not matrix multiplication.
4fill in blank
hard

Fill 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'
A**
B>
C<
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using * instead of ** for square.
Using < instead of > for filtering.
5fill in blank
hard

Fill 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'
Astr(x).upper()
Bx
Dnum
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Not converting keys to uppercase strings.