Bird
Raised Fist0
PyTorchml~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which PyTorch function creates a tensor filled with zeros of a specified shape?
easy
A. torch.zeros()
B. torch.ones()
C. torch.rand()
D. torch.tensor()

Solution

  1. Step 1: Understand the purpose of each function

    torch.zeros() creates a tensor filled with zeros, torch.ones() creates ones, torch.rand() creates random values, and torch.tensor() converts data to tensor.
  2. Step 2: Match function to requirement

    Since the question asks for a tensor filled with zeros, torch.zeros() is the correct choice.
  3. Final Answer:

    torch.zeros() -> Option A
  4. Quick Check:

    Zeros tensor = torch.zeros() [OK]
Hint: Zeros tensor? Use torch.zeros() [OK]
Common Mistakes:
  • Confusing torch.ones() with zeros
  • Using torch.tensor() without data
  • Thinking torch.rand() creates zeros
2. Which of the following is the correct syntax to create a 2x3 tensor of ones in PyTorch?
easy
A. torch.ones{2, 3}
B. torch.ones([2 3])
C. torch.ones(2, 3)
D. torch.ones((2 3))

Solution

  1. Step 1: Check valid argument types for torch.ones()

    PyTorch accepts shape as separate ints or tuple/list. torch.ones(2, 3) uses separate ints (valid). torch.ones([2 3]) missing comma (syntax error). torch.ones{2, 3} invalid braces. torch.ones((2 3)) missing comma (syntax error).
  2. Step 2: Identify syntax errors

    Only torch.ones(2, 3) is valid Python syntax.
  3. Final Answer:

    torch.ones(2, 3) -> Option C
  4. Quick Check:

    Separate args syntax = torch.ones(2, 3) [OK]
Hint: Separate dimensions with commas: torch.ones(2, 3) [OK]
Common Mistakes:
  • Using curly braces instead of parentheses
  • Confusing list and tuple syntax
  • Missing commas between shape dimensions
3. What is the output shape and type of the tensor created by torch.rand(3, 2)?
medium
A. A tensor of shape (3, 2) filled with zeros
B. A tensor of shape (2, 3) with random integer values
C. A tensor of shape (3, 2) filled with ones
D. A tensor of shape (3, 2) with random float values between 0 and 1

Solution

  1. Step 1: Understand torch.rand() behavior

    torch.rand(3, 2) creates a tensor with shape (3, 2) filled with random floats between 0 and 1.
  2. Step 2: Compare options to expected output

    A tensor of shape (3, 2) with random float values between 0 and 1 matches the shape and value type. Options A, B, and D are incorrect because they describe wrong shapes or values.
  3. Final Answer:

    A tensor of shape (3, 2) with random float values between 0 and 1 -> Option D
  4. Quick Check:

    torch.rand() = random floats [OK]
Hint: torch.rand(shape) = random floats in [0,1) [OK]
Common Mistakes:
  • Thinking torch.rand() creates integers
  • Mixing up shape dimensions
  • Assuming torch.rand() creates zeros or ones
4. Identify the error in this code snippet:
import torch
t = torch.zeros(2 3)
medium
A. Missing comma between dimensions in torch.zeros()
B. torch.zeros() cannot create 2D tensors
C. torch.zeros() requires a list, not separate arguments
D. torch.zeros() needs dtype argument

Solution

  1. Step 1: Check syntax of torch.zeros() arguments

    The code uses torch.zeros(2 3) missing a comma between 2 and 3, which is a syntax error.
  2. Step 2: Validate other options

    torch.zeros() can create 2D tensors and accepts multiple int arguments or a tuple/list. dtype is optional.
  3. Final Answer:

    Missing comma between dimensions in torch.zeros() -> Option A
  4. Quick Check:

    Comma separates dimensions [OK]
Hint: Separate dimensions with commas: torch.zeros(2, 3) [OK]
Common Mistakes:
  • Omitting commas between shape dimensions
  • Thinking dtype is mandatory
  • Believing torch.zeros() can't create 2D tensors
5. You want to create a tensor from a Python list [[1,2], [3,4]] and then create a tensor of the same shape filled with ones. Which code correctly does this?
hard
A.
import torch
list_data = [[1,2], [3,4]]
t1 = torch.ones(list_data)
t2 = torch.tensor(t1.shape)
B.
import torch
list_data = [[1,2], [3,4]]
t1 = torch.tensor(list_data)
t2 = torch.ones(t1.shape)
C.
import torch
list_data = [[1,2], [3,4]]
t1 = torch.tensor(list_data)
t2 = torch.ones(len(t1))
D.
import torch
list_data = [[1,2], [3,4]]
t1 = torch.ones(len(list_data))
t2 = torch.tensor(t1.shape)

Solution

  1. Step 1: Create tensor from list

    torch.tensor(list_data) correctly converts the nested Python list to a (2,2) tensor.
  2. Step 2: Create tensor of ones with same shape

    torch.ones(t1.shape) creates a tensor filled with ones matching the shape of t1 ((2,2)).
  3. Step 3: Check other options for correctness

    import torch
    list_data = [[1,2], [3,4]]
    t1 = torch.ones(list_data)
    t2 = torch.tensor(t1.shape)
    misuses torch.ones() with nested list (ValueError),
    import torch
    list_data = [[1,2], [3,4]]
    t1 = torch.tensor(list_data)
    t2 = torch.ones(len(t1))
    uses length=2 instead of shape (creates (2,) not (2,2)),
    import torch
    list_data = [[1,2], [3,4]]
    t1 = torch.ones(len(list_data))
    t2 = torch.tensor(t1.shape)
    creates t1 wrong (ones not data) and t2 not ones tensor.
  4. Final Answer:

    Correctly create tensor and ones tensor with matching shape -> Option B
  5. Quick Check:

    Use tensor.shape for shape in torch.ones() [OK]
Hint: Use tensor.shape to match shape in torch.ones() [OK]
Common Mistakes:
  • Passing nested list directly to torch.ones()
  • Using len() instead of shape for multi-dim tensors
  • Confusing torch.tensor() and torch.ones() usage
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard