Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a tensor from a list.
PyTorch
import torch my_tensor = torch.[1]([1, 2, 3, 4]) print(my_tensor)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'array' instead of 'tensor' causes an error.
Trying to use 'list' is invalid in PyTorch.
✗ Incorrect
The torch.tensor function creates a tensor from a Python list.
2fill in blank
mediumComplete the code to get the shape of a tensor.
PyTorch
import torch x = torch.tensor([[1, 2], [3, 4], [5, 6]]) shape = x.[1] print(shape)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'size' as a method requires parentheses.
Using 'length' is not valid for tensors.
✗ Incorrect
The shape attribute returns the dimensions of the tensor as a tuple.
3fill in blank
hardFix the error in the code to perform element-wise addition of two tensors.
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 '-' subtracts instead of adds.
Using '*' multiplies instead of adds.
✗ Incorrect
The + operator performs element-wise addition between tensors.
4fill in blank
hardFill both blanks to create a tensor of zeros with shape (3, 4) and type float.
PyTorch
import torch zeros_tensor = torch.[1]((3, 4), dtype=torch.[2]) print(zeros_tensor)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ones' creates a tensor of ones.
Using 'int32' sets integer type instead of float.
✗ Incorrect
torch.zeros creates a tensor filled with zeros. The dtype=torch.float32 sets the data type to float.
5fill in blank
hardFill all three blanks to create a tensor from a list, move it to GPU if available, and print its device.
PyTorch
import torch x = torch.[1]([1, 2, 3]) if torch.cuda.is_available(): x = x.[2]('cuda') print(x.[3])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'cuda' as a method instead of a string argument.
Trying to print 'to' instead of 'device'.
✗ Incorrect
torch.tensor creates the tensor. to('cuda') moves it to GPU. device shows where the tensor is stored.