Tensors in PyTorch

Free Machine Learning courses with 130+ real-time projects Start Now!!

Tensor is the data-type in which all the deep learning models using pytorch are built. They are just like numpy arrays with the added advantage that they can be loaded on a GPU. The first step of building any machine learning model is the preprocessing of the data which always involves converting the available data into tensors if they are in some other format.

Creating Tensors using PyTorch

Using PyTorch, we can create tensors and if a dataset is available we can convert it into tensors too.

import torch

Analogous to numpy, we can create an empty tensor, tensor with all elements 0 or 1 and tensors having random numbers.

a. Creating an empty Tensor in Pytorch:

a=torch.empty((2,3,4))

b. Creating a tensor with all elements equal to 0

b=torch.zeros(3,4)

c. Creating a tensor with all elements equal to1

c=torch.ones(3,2,4)
print(a)
print(b)
print(c)

Output

tensor([[[9.1837e-39, 4.6837e-39, 9.2755e-39, 1.0837e-38],               [8.4490e-39, 6.9796e-39, 9.5510e-39, 1.0010e-38],

               [1.0929e-38, 1.0469e-38, 1.0561e-38, 9.9184e-39]],

               [[9.0000e-39, 1.0561e-38, 1.0653e-38, 4.1327e-39],

               [8.9082e-39, 9.8265e-39, 9.4592e-39, 1.0561e-38],

               [1.0653e-38, 1.0469e-38, 9.5510e-39, 1.0745e-38]]])

tensor([[0., 0., 0., 0.],

             [0., 0., 0., 0.],

             [0., 0., 0., 0.]])

tensor([[[1., 1., 1., 1.],

             [1., 1., 1., 1.]],

             [[1., 1., 1., 1.],

             [1., 1., 1., 1.]],

             [[1., 1., 1., 1.],

             [1., 1., 1., 1.]]])

We can also create a tensor with 1 occupying all the diagonal positions the non diagonal elements being 0

d. Creating a tensor with diagonal elements equal to 1.

d=torch.eye(3)
print(d)

Output

tensor([[1., 0., 0.],             [0., 1., 0.],

             [0., 0., 1.]])

We will use the MNIST dataset which is available with t pytorch to see how we can convert a dataframe into tensor from any other format.

To do so we will import additional libraries.

e. Converting a dataset into a tensor

import torchvision
from torchvision import transforms,datasets

Torch vision has to be installed before being imported. Run the command “pip install torchvision” on the terminal or anaconda prompt and it will get installed in a few minutes.

train=datasets.MNIST("", train=True,download=True,transform=transforms.Compose([transforms.ToTensor()]))
test=datasets.MNIST("", train=False,download=True,transform=transforms.Compose([transforms.ToTensor()]))

In the above code we have downloaded the MNIST dataset and transformed it into tensor.

train

Output

Dataset MNIST    Number of datapoints: 60000

    Root location: 

    Split: Train

    StandardTransform

Transform: Compose(

               ToTensor()

           )

test

Output

Dataset MNIST    Number of datapoints: 10000

    Root location: 

    Split: Test

    StandardTransform

Transform: Compose(

               ToTensor()

           )

f. Converting NumPy arrays into tensors.

import numpy as np
arr=np.array([1,2,3])#We have created a numpy array which we will convert into tensor.
arr

Output

array([1, 2, 3])
torch.from_numpy(arr)

Output

tensor([1, 2, 3], dtype=torch.int32)

The numpy array has been converted into a tensor.

PyTorch Tensor Operations

Just like numpy, we can perform many operations such as addition, subtraction, multiplication, exponentiation of two tensors using PyTorch.

t1=torch.ones(4,4)
t2=torch.eye(4)
t1

Output

tensor([[1., 1., 1., 1.],              [1., 1., 1., 1.],

              [1., 1., 1., 1.],

              [1., 1., 1., 1.]])

t2

Output

tensor([[1., 0., 0., 0.],             [0., 1., 0., 0.],

             [0., 0., 1., 0.],

             [0., 0., 0., 1.]])

a. Tensor Addition

t1+t2

Output

tensor([[2., 1., 1., 1.],              [1., 2., 1., 1.],

              [1., 1., 2., 1.],

              [1., 1., 1., 2.]])

b. Tensor Multiplication

t1*t2

Output

tensor([[1., 0., 0., 0.],             [0., 1., 0., 0.],

             [0., 0., 1., 0.],

             [0., 0., 0., 1.]])

c. Tensor Subtraction

t1-t2

Output

tensor([[0., 1., 1., 1.],             [1., 0., 1., 1.],

             [1., 1., 0., 1.],

             [1., 1., 1., 0.]])

d. Tensor Exponentiation

t1**t2

e. Scalar multiple of a Tensor

2*t1

f. Resizing a tensor

a
a.shape
a.resize(2,12)

Summary

Tensors are a data structure just like numpy. All the operations on tensors that we require for training our model can easily be done using the different PyTorch methods as we have seen above. Due to the fact that tensors of multiple dimensions can be built, the training process can be made easier by increasing the dimensions of the dataset which is very effective especially in classification models.

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

courses

DataFlair Team

DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.

Leave a Reply

Your email address will not be published. Required fields are marked *