Creating Tensors

n 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.

import torch
a = torch.tensor([1, 2, 3])
b = torch.tensor([[1], [2], [3]])
print(a)
print(b)

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_numpy function to convert a NumPy array to a PyTorch tensor. You just have to pass the NumPy array object as an argument.

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)

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 like ones. The parameter could be an integer or a tuple that defines the shape of the tensor.
import torch
# Create a identity tensor with 3*3 shape.
eys = torch.eye(3)
print(eys)
# Create a tensor with 2*2 shape whose values are all 1.
ones = torch.ones((2, 2))
print(ones)
# Create a tensor with 3*3 shape whose values are all 0.
zeros = torch.zeros((3, 3))
print(zeros)

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 a list or a tuple.
  • 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 the rand().
  • randint(): Unlike the functions above, this function creates a tensor with integer values with low, high and size parameters. low means the lowest value, it’s optional and the default value is 0. high means the highest value, and size is a tuple that defines the shape of the tensor.
import torch
# Create a tensor with 1*10 shape with random value between 0 and 1
r0 = torch.rand(10)
print(r0)
print("************************************************")
# Create a tensor with 10*1 shape with random value between 0 and 1
r1 = torch.rand((10, 1))
print(r1)
print("************************************************")
# Create a tensor with 2*2 shape with random value between 0 and 1
r2 = torch.rand((2, 2))
print(r2)
print("************************************************")
# Create a tensor with 2*2 shape with random value from a normal distribution.
r3 = torch.randn((2,2))
print(r3)
print("************************************************")
# Create an integer type tensor with 3*3 shape with random value between 0 and 10.
r4 = torch.randint(high=10, size=(3, 3))
print(r4)
print("************************************************")
# Create an integer type tensor with 3*3 shape with random value between 5 and 10.
r5 = torch.randint(low=5, high=10, size=(3, 3))
print(r5)

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)
import torch
a = torch.arange(1, 10)
print(a)

Line 3 creates a tensor by arange. It creates a 1-D dimension tensor with a length of 9.