Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a tensor with values [1, 2, 3].
PyTorch
import torch x = torch.[1]([1, 2, 3]) print(x)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'array' instead of 'tensor' causes an error because PyTorch does not have torch.array.
Using 'list' or 'matrix' are not valid PyTorch functions.
✗ Incorrect
The torch.tensor function creates a tensor from a list of numbers.
2fill in blank
mediumComplete the code to add two tensors x and y.
PyTorch
import torch x = torch.tensor([1, 2]) y = torch.tensor([3, 4]) z = x [1] y print(z)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' subtracts instead of adds.
Using '*' multiplies instead of adds.
Using '/' divides instead of adds.
✗ Incorrect
The plus sign + adds two tensors element-wise.
3fill in blank
hardFix the error in the code to multiply tensor x by 2.
PyTorch
import torch x = torch.tensor([1, 2, 3]) y = x [1] 2 print(y)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'add' or 'multiply' as operators causes syntax errors.
Using '+' adds instead of multiplies.
✗ Incorrect
The * operator multiplies each element of the tensor by 2.
4fill in blank
hardFill both blanks to create a tensor of zeros with shape (2, 3) and print its shape.
PyTorch
import torch zeros_tensor = torch.[1]((2, 3)) print(zeros_tensor.[2])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'empty' creates uninitialized values, not zeros.
Using 'size' instead of 'shape' is incorrect in PyTorch.
✗ Incorrect
torch.zeros creates a tensor filled with zeros. The shape attribute shows its dimensions.
5fill in blank
hardFill all three blanks to create a tensor from a list, convert it to float type, and print its data type.
PyTorch
import torch x = torch.tensor([1, 2, 3]).[1]([2]) print(x.[3])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'float' instead of 'torch.float32' causes errors.
Using 'float' as a method or attribute is incorrect.
Using 'dtype' as a method instead of attribute causes errors.
✗ Incorrect
The to method converts the tensor to a new type. torch.float32 is the float type. The dtype attribute shows the tensor's data type.