You've learned what tensors are and how to create them from data, with specific values like zeros, or with random numbers. Let's put that knowledge to the test and build some tensors!
Engagement Message
Ready to practice your tensor construction skills?
Type
Multiple Choice
Practice Question
What is the shape of the tensor created by torch.tensor([[5, 1, 7], [6, 2, 8]])
?
A. (3, 2) B. (2, 3) C. (6,) D. 6
Suggested Answers
- A
- B - Correct
- C
- D
Type
Fill In The Blanks
Markdown With Blanks
Fill in the blanks to create a 2x4 tensor of ones and a 3x3 tensor of random numbers.
Suggested Answers
- ones
- rand
- zeros
- tensor
Type
Sort Into Boxes
Practice Question
Sort these tensor creation functions based on whether they create a tensor from existing data or based on a desired shape.
Labels
- First Box Label: From Data
- Second Box Label: From Shape
First Box Items
- torch.tensor()
- torch.from_numpy()
Second Box Items
- torch.zeros()
- torch.ones()
- torch.rand()
Type
Swipe Left or Right
Practice Question
Is the following code a valid way to create a tensor in PyTorch? Swipe left for valid and right for invalid.
Labels
- Left Label: Valid
- Right Label: Invalid
Left Label Items
- torch.tensor([1, 2, 3])
- torch.zeros((2, 2))
- torch.rand(2, 3, 4)
Right Label Items
- torch.tensor([1, [2, 3]])
- torch.ones(3, 'a')
Type
Multiple Choice
Practice Question
You need to store a grid representing a game board where each cell is either occupied (True
) or empty (False
). Which dtype
is most memory-efficient for this task?
A. torch.float32 B. torch.int64 C. torch.bool D. torch.complex64
Suggested Answers
- A
- B
- C - Correct
- D
Type
Fill In The Blanks
Markdown With Blanks
Let's identify the tensor type from its definition.
A single number like
torch.tensor(42)
is a [[blank:Scalar]]. A list of numbers liketorch.tensor([10, 20, 30])
is a [[blank:Vector]]. A table of numbers liketorch.tensor([[1, 2], [3, 4]])
is a [[blank:Matrix]].
Suggested Answers
- Scalar
- Vector
- Matrix
