It creates a tensor from a Python list or array with the exact values you provide.
Click to reveal answer
beginner
How do torch.zeros() and torch.ones() differ?
torch.zeros() creates a tensor filled with zeros, while torch.ones() creates a tensor filled with ones.
Click to reveal answer
beginner
What kind of values does torch.rand() generate?
It generates a tensor with random values between 0 and 1, following a uniform distribution.
Click to reveal answer
beginner
Why is tensor creation important in machine learning?
Tensors hold data like images or numbers that models learn from. Creating tensors correctly helps models train and predict well.
Click to reveal answer
beginner
How do you specify the shape of a tensor when using torch.zeros()?
You pass the shape as separate arguments or as a tuple, like torch.zeros(2, 3) or torch.zeros((2, 3)) to create a 2 by 3 tensor filled with zeros.
Click to reveal answer
Which function creates a tensor with all elements set to one?
Atorch.tensor()
Btorch.zeros()
Ctorch.rand()
Dtorch.ones()
✗ Incorrect
torch.ones() creates a tensor filled with ones.
What does torch.rand((3,2)) produce?
AA 3x2 tensor with random values between 0 and 1
BA 3x2 tensor filled with zeros
CA 3x2 tensor filled with ones
DA tensor created from a Python list
✗ Incorrect
torch.rand() creates a tensor with random values between 0 and 1.
How do you create a tensor from a Python list [1, 2, 3]?
Atorch.rand([1, 2, 3])
Btorch.tensor([1, 2, 3])
Ctorch.ones([1, 2, 3])
Dtorch.zeros([1, 2, 3])
✗ Incorrect
torch.tensor() creates a tensor from the exact values in the list.
Which function would you use to create a tensor filled with zeros of shape (4, 5)?
Atorch.zeros((4, 5))
Btorch.ones((4, 5))
Ctorch.rand((4, 5))
Dtorch.tensor((4, 5))
✗ Incorrect
torch.zeros() creates a tensor filled with zeros of the given shape.
What is the range of values generated by torch.rand()?
ABetween -1 and 1
BOnly zeros
CBetween 0 and 1
DOnly ones
✗ Incorrect
torch.rand() generates random values uniformly between 0 and 1.
Explain how to create a tensor filled with ones of shape (3, 4) using PyTorch.
Think about the function that fills with ones and how to specify size.
You got /4 concepts.
Describe the difference between torch.tensor() and torch.rand().
One uses your data, the other creates random numbers.
You got /4 concepts.
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
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.
Step 2: Match function to requirement
Since the question asks for a tensor filled with zeros, torch.zeros() is the correct choice.
Final Answer:
torch.zeros() -> Option A
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
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).
Step 2: Identify syntax errors
Only torch.ones(2, 3) is valid Python syntax.
Final Answer:
torch.ones(2, 3) -> Option C
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
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.
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.
Final Answer:
A tensor of shape (3, 2) with random float values between 0 and 1 -> Option D
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
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.
Step 2: Validate other options
torch.zeros() can create 2D tensors and accepts multiple int arguments or a tuple/list. dtype is optional.
Final Answer:
Missing comma between dimensions in torch.zeros() -> Option A
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?