unit-4-part-3
unit-4-part-3
Introduction
The advancement of interest in Deep Learning in recent years and the explosion of
Machine Learning tools like TensorFlow, PyTorch, etc., will also be cited, which will
provide ease of use and easy debugging of codes.
Tensor
Tensor is a fundamental building block of Pytorch, and it is basically the same as
a Numpy array. It is mostly used for converting Images, Audio into a mathematical form
used for processing, as computers don’t understand images but standard numbers. Hence,
it is important to convert images into numerical forms.
One of the important features which are offered by tensors is they can store track of all
the operations performed on them, which helps to compute the optimized output; this can
be done by using the Autograd functionality of a tensor.
In simplistic terms, scaler – vector – matrices – tensor as flow
1. Scaler is a 0-dimensional Vector.
2. Vector is a 1-dimensional Vector.
3. Matrices are 2-dimensional Vectors.
4. Tensors are generalized N-dimensional Tensors.
PyTorch in a lot of ways behaves like the arrays we love from Numpy. These Numpy
arrays, after all, are just tensors.
PyTorch takes these tensors and makes it simple to move them to GPUs for the faster
processing needed when training neural networks.
It also provides a module that automatically calculates gradients (for backpropagation)
and another module specifically for building neural networks.
All together, PyTorch ends up being more flexible with Python and the Numpy stack
compared to TensorFlow and other frameworks.
Neural Networks: Deep Learning is based on artificial neural networks which have been
around in some form since the late 1950s.
The networks are built from individual parts approximating neurons, typically called
units or simply “neurons.” Each unit has some number of weighted inputs.
These weighted inputs are summed together (a linear combination) then passed through
an activation function to get the unit’s output.
Deep Learning is based on artificial neural networks which have been around in some
form since the late 1950s. The networks are built from individual parts approximating
neurons, typically called units or simply “neurons.” Each unit has some number of
weighted inputs. These weighted inputs are summed together (a linear combination) then
passed through an activation function to get the unit’s output. Below is an example of a
simple
PyTorch is an open-source machine learning library based on the Torch library, developed
by Facebook’s AI Research lab. It is widely used in deep learning, natural language
processing, and computer vision applications. PyTorch provides a dynamic computational
graph, which allows for easy and efficient modeling of complex neural networks.
Here’s a general overview of how to use PyTorch for deep learning:
Define the model: PyTorch provides a wide range of pre-built neural network
architectures, such as fully connected networks, convolutional neural networks (CNNs),
and recurrent neural networks (RNNs). The model can be defined using PyTorch’s
torch.nn module.
Define the loss function: The loss function is used to evaluate the model’s performance
and update its parameters. PyTorch provides a wide range of loss functions, such as cross-
entropy loss and mean squared error loss.
Define the optimizer: The optimizer is used to update the model’s parameters based on the
gradients of the loss function. PyTorch provides a wide range of optimizers, such as Adam
and SGD.
Train the model: The model is trained on a dataset using the defined loss function and
optimizer. PyTorch provides a convenient data loading and processing library,
torch.utils.data, that can be used to load and prepare the data.
Evaluate the model: After training, the model’s performance can be evaluated on a test
dataset.
Deploy the model: The trained model can be deployed in a wide range of applications,
such as computer vision and natural language processing.
neural net.
Tensors: It turns out neural network computations are just a bunch of linear algebra
operations on tensors, which are a generalization of matrices. A vector is a 1-dimensional
tensor, a matrix is a 2-dimensional tensor, an array with three indices is a 3-dimensional
tensor. The fundamental data structure for neural networks are tensors and PyTorch is built
around tensors. It’s time to explore how we can use PyTorch to build a simple neural
network.
Python3
import torch
Python3
def activation(x):
""" Sigmoid activation function
Arguments
---------
x: torch.Tensor
"""
Python3
weights = torch.randn_like(features)
On the architecture side, we’ll be using a simple model that employs three convolution
layers with depths 32, 64, and 64, respectively, followed by two fully connected layers
for performing classification.
Each convolutional layer involves a convolutional operation involving a 3×3
convolution filter and is followed by a ReLU activation operation for
introducing nonlinearity into the system and a max-pooling operation with a
2×2 filter to reduce the dimensionality of the feature map.
After the end of the convolutional blocks, we flatten the multidimensional
layer into a low dimensional structure for starting our classification blocks.
After the first linear layer, the last output layer(also a linear layer) has ten
neurons for each of the ten unique classes in our dataset.
The architecture is as follows:
For building our model, we’ll make a CNN class inherited from
the torch.nn.Module class for taking advantage of the Pytorch utilities. Apart from that,
we’ll be using the torch.nn.Sequential container to combine our layers one after the
other.
The Conv2D(), ReLU(), and MaxPool2D() layers perform the convolution,
activation, and pooling operations. We used padding of 1 to give sufficient
learning space to the kernel as padding gives the image more coverage area,
especially the pixels in the outer frame.
After the convolutional blocks, the Linear() fully connected layers perform
classification.