Creating Tensors
In this lesson, we will look at different ways of creating tensors.
Creating a tensor from a list
Creating a tensor from a list or a nested list is easy. First, we need to import the torch library and call the tensor function.
import torch
a = torch.tensor([1 ,2, 3])
b = torch.tensor([[1], [2], [3]])
The tensor function supports different types, which will be discussed in a later lesson. In this example, we use the default type,torch.int64.
Line 3 creates a tensor from a list and stores it in the variable a.
Line 4 create a tensor from a nested list and stores it in the variable b. The dimension of this tensor is 2. The shape of this tensor is 3*1, which means it’s a matrix with 3 rows and 1 column.
Creating a tensor from a NumPy array
If we have a NumPy array and want to convert it to a PyTorch tensor, we just pass it to the tensor function as an argument, as shown below.
import torch
import numpy as np
na = np.array([1, 2, 3])
a = torch.tensor(na)
b = torch.from_numpy(na)
print(a)
print(b)
Notice: You can also use the
from_numpyfunction to convert a NumPy array to a PyTorch tensor. You just have to pass the NumPy array object as an argument.
Line 4 creates a NumPy array.
Line 5 creates a tensor from a NumPy array.
Line 6 creates a tensor by from_numpy function.
Creating special tensors
PyTorch provides some useful functions to create special tensors, such as the identity tensor and tensors having all zeros or ones.
eye(): Creates an identity tensor with an integer.zeros(): Creates a tensor with all zeros, the parameter could be an integer or a tuple that defines the shape of the tensor.ones(): Creates a tensor with all ones likeones. The parameter could be an integer or a tuple that defines the shape of the tensor.
Line 4 creates an identity tensor by eye().
Line 8 creates an all ones tensor by ones(). In this example, it creates a matrix with a 2*2 shape. You could create any shape you want. Just pass a tuple to define the shape.
Line 12 creates an all zeros tensor by zeros(). In this example, it creates a matrix with a 2*2 shape. You could create any shape you want. Just pass a tuple to define the shape.
Creating a random tensor
PyTorch provides some useful functions to create a tensor with a random value.
rand(): It creates a tensor filled with random numbers from a uniform distribution. The parameter is a sequence of integers defining the shape of the output tensor. It can be a variable number of arguments or a collection like alistor atuple.randn(): It creates a tensor filled with random numbers from a normal distribution with mean 0 and variance 1. The parameter is the same as therand().randint(): Unlike the functions above, this function creates atensorwith integer values withlow,highandsizeparameters.lowmeans the lowest value, it’s optional and the default value is 0.highmeans the highest value, andsizeis a tuple that defines the shape of the tensor.
Line 4 creates a tensor with a 1*10 shape with random values between 0 and 1.
Line 8 creates a tensor with a 10*1 shape with random values between 0 and 1.
Line 12 creates a tensor with a 2*2 shape with random values between 0 and 1.
Line 16 creates a tensor with a 2*2 shape with random values from a normal distribution.
Line 20 creates an integer type tensor with a 3*3 shape with random values between 0 and 10.
Line 24 creates an integer type tensor with a 3*3 shape with random values between 5 and 10.
Creating a range tensor
PyTorch also provides a function arange that generates values in [start; end), like NumPy.
torch.arange(1, 10)
Line 3 creates a tensor by arange. It creates a 1-D dimension tensor with a length of 9.