0
0
PyTorchml~20 mins

Tensor creation (torch.tensor, zeros, ones, rand) in PyTorch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Tensor Creator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
What is the shape of the tensor created?
Consider the following PyTorch code that creates a tensor. What will be the shape of the resulting tensor?
PyTorch
import torch
x = torch.ones((3, 4))
shape = x.shape
print(shape)
A(4, 3)
B(4,)
C(3,)
D(3, 4)
Attempts:
2 left
💡 Hint
Remember that torch.ones takes a tuple for the shape.
Predict Output
intermediate
1:30remaining
What is the output of this tensor creation?
What will be the output of the following PyTorch code?
PyTorch
import torch
x = torch.tensor([[1, 2], [3, 4]], dtype=torch.float32)
print(x)
Atensor([[1., 2.], [3., 4.]])
Btensor([[1, 2], [3, 4]])
Ctensor([1., 2., 3., 4.])
Dtensor([[1, 2], [3, 4]], dtype=torch.int32)
Attempts:
2 left
💡 Hint
Check the dtype argument and how it affects the tensor values.
Model Choice
advanced
1:30remaining
Which tensor creation method produces a tensor with random values between 0 and 1?
You want to create a tensor of shape (2, 3) filled with random values between 0 and 1. Which PyTorch function should you use?
Atorch.zeros((2, 3))
Btorch.ones((2, 3))
Ctorch.rand((2, 3))
Dtorch.tensor([[0, 1], [0, 1], [0, 1]])
Attempts:
2 left
💡 Hint
Think about which function generates random numbers.
Metrics
advanced
1:30remaining
How many elements are in the tensor created by torch.zeros((4, 5, 2))?
If you create a tensor with torch.zeros((4, 5, 2)), how many total elements does this tensor contain?
A40
B20
C10
D50
Attempts:
2 left
💡 Hint
Multiply the dimensions to find the total number of elements.
🔧 Debug
expert
2:00remaining
What error does this code raise?
What error will the following PyTorch code raise when executed?
PyTorch
import torch
x = torch.tensor([1, 2, 3], dtype='float64')
y = torch.ones(3, 3)
z = x + y
ATypeError: dtype must be a torch.dtype, not str
BRuntimeError: The size of tensor a (3) must match the size of tensor b (3) at non-singleton dimension 1
CNo error, z is a tensor of shape (3, 3)
DValueError: operands could not be broadcast together with shapes (3,) (3,3)
Attempts:
2 left
💡 Hint
Check the shapes of x and y before adding.