PyTorch For Deep learning part-1

kamal_DS
4 min readFeb 14, 2023

--

The field of deep learning and artificial intelligence relies heavily on linear algebra and tensor operations. Understanding the differences between scalars, vectors, matrices, and tensors is crucial for the effective use of PyTorch, a popular deep-learning framework. In this blog, we will explore these concepts and their operations in PyTorch.

  1. Difference between Scalar, Vector, Matrix, and Tensor:

In mathematical terms, a scalar is a single value, a vector is an ordered array of values, a matrix is a 2D array of values, and a tensor is a multi-dimensional array of values. In PyTorch, scalars are represented as 0-dimensional tensors, vectors are represented as 1-dimensional tensors, matrices are represented as 2-dimensional tensors, and tensors can have more than 2 dimensions.

import torch

# Scalar
scalar = torch.tensor(3.14)
print("Scalar: ", scalar)

# Vector
vector = torch.tensor([1, 2, 3])
print("Vector: ", vector)

# Matrix
matrix = torch.tensor([[1, 2, 3], [4, 5, 6]])
print("Matrix: ", matrix)

# Tensor
tensor = torch.tensor([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print("Tensor: ", tensor)

2.Addition, Subtraction, Multiplication, Division and Matmul:

PyTorch supports basic arithmetic operations such as addition, subtraction, multiplication, and division for tensors. These operations can be performed using the corresponding operator symbols (+, -, *, /). The dot product of two matrices can be calculated using the torch.matmul() function. In PyTorch, the matmul function supports broadcasting, which allows for the computation of dot products between tensors of different shapes.

import torch

# Addition
a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
c = a + b
print(c) # tensor([5, 7, 9])

# Subtraction
a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
c = a - b
print(c) # tensor([-3, -3, -3])

# Multiplication
a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
c = a * b
print(c) # tensor([4, 10, 18])

# Division
a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
c = a / b
print(c) # tensor([0.25, 0.4, 0.5])

# Matmul
a = torch.tensor([[1, 2], [3, 4]])
b = torch.tensor([[5, 6], [7, 8]])
c = torch.matmul(a, b)
print(c) # tensor([[19, 22], [43, 50]])

3. Reshaping Tensors:

Tensors are multi-dimensional arrays in PyTorch and can be of any size and shape. Reshaping a tensor means changing the number of rows and columns in the tensor without changing its data. PyTorch provides the method view() for reshaping tensors.

x.view(new_shape)

where x is the tensor to be reshaped and new_shape is the desired shape of the tensor after reshaping.

For example, let’s consider a tensor x of shape (2, 3). We can reshape it to (3, 2) using the following code:

import torch

x = torch.tensor([[1, 2, 3], [4, 5, 6]])
print("Original tensor: ", x)

x = x.view(3, 2)
print("Reshaped tensor: ", x)

The output of the above code will be:

Original tensor:  tensor([[1, 2, 3],
[4, 5, 6]])
Reshaped tensor: tensor([[1, 2],
[3, 4],
[5, 6]])
values = torch.arange(1,61).reshape(5,2,6)
print(values.ndim)
print(values)

In the above example, we are changing 60 values into 2 rows, 6 columns in every dim, and the total dims are 5.

4. Squeezing and Unsqueezing:

The squeeze method is used to remove any single-dimensional entries from the shape of a tensor. The unsqueeze method is used to add a single-dimensional entry to the shape of a tensor.

# Squeeze -> removes all the single dimensions from the selected tensor 
# unsqueeze -> adds single dim to the selected tensor

# squeeze
values = torch.arange(1,61)
print(f'original dim: {values.shape}')
a = values.reshape(1,-1)
print(f'original dim: {a.shape}')
# removing 1 dimensions value using squeeze
remove_dims = torch.squeeze(a)
print(f'original dim: {remove_dims.shape}')


# Unsqueeze

# unsqueeze
import warnings
warnings.filterwarnings('ignore')

values = torch.range(1,60)
print(f'original dim: {values.shape}')

#a = values.reshape(1,-1)
#print(f'original dim: {a.shape}')
# Add 1 dimensions value using squeeze

add_dims = torch.unsqueeze(values,dim=0)
print(f'original dim: {add_dims.shape}')

5. Permute

Permute is a function in PyTorch that allows you to rearrange the dimensions of a tensor. The permute function takes as input a tensor and a list of dimensions and returns a tensor with the same data but with the order of its dimensions changed according to the list of dimensions provided.

Here is an example to understand the permute function in PyTorch:

import torch

# Create a tensor with shape (2, 3, 4)
tensor = torch.randn(2, 3, 4)

# Permute the tensor to shape (4, 2, 3)
permuted_tensor = tensor.permute(2, 0, 1)

print("Original Tensor Shape: ", tensor.shape)
print("Permuted Tensor Shape: ", permuted_tensor.shape)

In the above example, we created a tensor with shapes (2, 3, 4) and then used the permute function to rearrange its dimensions to shape (4, 2, 3). As you can see from the output, the shapes of the original tensor and the permuted tensor are different.

The permute function is useful when you want to change the way a tensor is processed, for example, when you want to perform operations on a specific dimension of a tensor or when you want to change the way data is stored in a tensor for optimization purposes.

You can get the coding file from here https://github.com/saikamal3344/Pytorch-Framwork-Basics

To get complete information about Images using OpenCV please to follow https://medium.com/@korlakuntasaikamal10/the-difference-in-reading-an-image-using-opencv-and-matplotlib-5529415704e5

--

--

kamal_DS

Interested to work in the field of Artificial Intelligence, Machine Learning, Deep Learning, NLP and Computer Vision.