Challenge: Initializing Tensors
Task
Swipe to start coding
- Create a
2x3
tensor filled with zeros using the appropriate creation function. - Create a 1D tensor with integer values from
1
to10
inclusive (11
exclusive) using the appropriate creation function. - Create a 1D tensor with
10
evenly spaced values between2
and4
inclusive using the appropriate creation function.
Solution
99
1
2
3
4
5
6
7
8
9
10
11
12
import torch
# Create a 2x3 tensor filled with zeros
tensor_1 = torch.zeros(2, 3)
# Create a 1D tensor with values from 1 to 10 inclusive
tensor_2 = torch.arange(1, 11)
# Create a 1D tensor with 10 evenly spaced values between 2 and 4
tensor_3 = torch.linspace(2, 4, steps=10)
# Print the tensors
print(tensor_1)
print(tensor_2)
print(tensor_3)
Everything was clear?
Thanks for your feedback!
Section 1. Chapter 4
99
1
2
3
4
5
6
7
8
9
10
11
12
import torch
# Create a 2x3 tensor filled with zeros
tensor_1 = ___
# Create a 1D tensor with values from 1 to 10 inclusive
tensor_2 = ___
# Create a 1D tensor with 10 evenly spaced values between 2 and 4
tensor_3 = ___
# Print the tensors
print(tensor_1)
print(tensor_2)
print(tensor_3)
Ask AI
Ask anything or try one of the suggested questions to begin our chat