0
0
PyTorchml~10 mins

Tensor creation (torch.tensor, zeros, ones, rand) 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 create a tensor from a list of numbers.

PyTorch
import torch

my_tensor = torch.[1]([1, 2, 3, 4])
print(my_tensor)
Drag options to blanks, or click blank then click option'
Aones
Bzeros
Ctensor
Drand
Attempts:
3 left
💡 Hint
Common Mistakes
Using torch.zeros or torch.ones instead of torch.tensor.
Trying to call torch.rand with a list argument.
2fill in blank
medium

Complete the code to create a 3x3 tensor filled with zeros.

PyTorch
import torch

zero_tensor = torch.[1]((3, 3))
print(zero_tensor)
Drag options to blanks, or click blank then click option'
Azeros
Bones
Ctensor
Drand
Attempts:
3 left
💡 Hint
Common Mistakes
Using torch.ones instead of torch.zeros.
Passing a list instead of a tuple for the shape.
3fill in blank
hard

Fix the error in the code to create a 2x4 tensor filled with ones.

PyTorch
import torch

one_tensor = torch.[1]([2, 4])
print(one_tensor)
Drag options to blanks, or click blank then click option'
Aones
Btensor
Czeros
Drand
Attempts:
3 left
💡 Hint
Common Mistakes
Using torch.tensor instead of torch.ones.
Using torch.zeros which creates zeros instead of ones.
4fill in blank
hard

Fill both blanks to create a 4x2 tensor with random values between 0 and 1.

PyTorch
import torch

random_tensor = torch.[1]([2])
print(random_tensor)
Drag options to blanks, or click blank then click option'
Arand
B(4, 2)
C[4, 2]
Dones
Attempts:
3 left
💡 Hint
Common Mistakes
Using torch.ones instead of torch.rand.
Passing the shape as a list instead of a tuple.
5fill in blank
hard

Fill all three blanks to create a tensor from a list, then create zeros and ones tensors with the same shape.

PyTorch
import torch

base_tensor = torch.[1]([5, 6, 7])
zeros_tensor = torch.[2](base_tensor.[3])
ones_tensor = torch.ones(base_tensor.shape)

print(base_tensor)
print(zeros_tensor)
print(ones_tensor)
Drag options to blanks, or click blank then click option'
Atensor
Bzeros
Cshape
Drand
Attempts:
3 left
💡 Hint
Common Mistakes
Using torch.ones instead of torch.zeros for the zeros tensor.
Using .size() instead of .shape (both work but .shape is more common).
Using torch.rand instead of torch.tensor for the base tensor.