Deep Learning Unit 4
Deep Learning Unit 4
REPRESENTATION LEARNING
Neural networks can automatically discover combinations of the original
features that are important through their training process.
The process starts by creating initially random combinations of the original
features via multiplication by a random weight matrix
From the training, the neural network learns to refine combinations that are
helpful and discard those that aren’t helpful combinations.
This process of learning which combinations of features are important is known
as representation learning.
THE CONVOLUTION OPERATION:
Here are the three elements that enter into the convolution operation
Input image
Feature detector
Feature map
You place it over the input image beginning from the top-left corner within
the borders you see demarcated above, and then you count the number of
cells in which the feature detector matches the input image.
The number of matching cells is then inserted in the top-left cell of the
feature map.
You then move the feature detector one cell to the right and do the same
thing. This movement is called a and since we are moving the feature
detector one cell at time, that would be called a stride of one pixel.
What you will find in this example is that the feature detector's middle-left
cell with the number 1 inside it matches the cell that it is standing over
inside the input image. That's the only matching cell, and so you write “1”
in the next cell in the feature map, and so on and so forth. After you
have gone through the whole first row, you can then move it over to the
next row and go through the same process.
THE MULTICHANNEL CONVOLUTION OPERATION
What actually happens in a convolutional Layer in a neural network goes
one step further
we’ll create f sets of n features, each with a corresponding (initially
random) set of weights defining a visual pattern
It detection at each location in the input image will be captured in the
feature map.
These f feature maps will be created via f convolution operations.
CONVOLUTIONAL LAYERS
A convolution neural network has multiple hidden layers that help in
extracting information from an image.
The four important layers in CNN are:
Convolution layer
ReLU layer
Pooling layer
Fully connected layer
CONVOLUTION LAYER:
This is the first step in the process of extracting valuable features from an
image.
A convolution layer has several filters that perform the convolution
operation.
Every image is considered as a matrix of pixel values.
Consider the following 5x5 image whose pixel values are either 0 or 1.
There’s also a filter matrix with a dimension of 3x3.
Slide the filter matrix over the image and compute
the dot product to get the convolved feature matrix
ReLU layer:
ReLU stands for the rectified linear unit.
The feature maps are extracted, the next step is to move them to a ReLU
layer.
ReLU performs an element-wise operation and sets all the negative pixels to
0.
It introduces non-linearity to the network, and the generated output is
a rectified feature map.
POOLING LAYER
PyTorch Tensors
A Pytorch Tensor is basically the same as a NumPy array
PyTorch does not know anything about deep learning or computational
graphs or gradients and is just a generic n-dimensional array to be used
for arbitrary numeric computation.
The biggest difference between a NumPy array and a PyTorch Tensor is
that a PyTorch Tensor can run on either CPU or GPU.
How to create a Tensor
import torch
V_data = [1, 2, 3, 4, 5]
V = torch.tensor(V_data)
print(V)
Output:
tensor([1, 2, 3, 4, 5])
You can also create a tensor of random data with a given dimensionality
import torch
x = torch.randn((3, 4, 5))
print(x)
tensor([[[ 0.8332, -0.2102, 0.0213, 0.4375, -0.9506],
[ 0.0877, -1.5845, -0.1520, 0.3944, -0.7282],
[-0.6923, 0.0332, -0.4628, -0.9127, -1.4349],
[-0.3641, -0.5880, -0.5963, -1.4126, 0.5308]],
import torch
z= torch.zeros([3,3], dtype=torch.int32)
print(z)
Output:
tensor([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]], dtype=torch.int32
import torch
# Example for torch.full_like()
x = torch.full_like(newTensor,3.24, dtype=None )
print(x)
Output:
tensor([[3.2400, 3.2400, 3.2400],
[3.2400, 3.2400, 3.2400],
[3.2400, 3.2400, 3.2400],
[3.2400, 3.2400, 3.2400]])
TENSOR ATTRIBUTES:
Each tensor( torch.Tensor ) has a torch.dtype, torch.device,
and torch.layout attributes.
• torch.dtype: A torch.dtype is an object that represents the data type
of torch.Tensor. PyTorch has twelve different data types.
• torch.device: A torch.device is an object representing the device on
which a torch.Tensor is or will be allocated. The torch.device contains a
device type (‘cpu’ or ‘cuda’) and an optional device ordinal for the device
type.
• torch.layout: A torch.layout is an object that represents the memory
layout of a torch.Tensor. Currently, the torch supports two types of
memory layout.
DEEP LEARNING WITH PYTORCH
Deep learning models have several elements that work together to produce
a trained model:
• A Model, which contains Layers
• An Optimizer
• A Loss
• A Trainer
It turns out that with PyTorch, the Optimizer and the Loss are one-liners,
and the Model and Layers are straightforward.
A key feature of PyTorch is the ability to define models and layers as easy-
to-use objects that handle sending gradients backward and storing
parameters automatically, simply by having them inherit from
the torch.nn.Module class
o raise NotImplementedError()
CONVOLUTIONAL NEURAL NETWORKS IN PYTORCH
The multichannel convolution operation had the following shapes for its
two inputs and its output:
•The data input shape [batch_size, in_channels, image_height,
image_width]
•The parameters input shape [in_channels, out_channels, filter_size,
filter_size]
•The output shape [batch_size, out_channels, image_height, image_width]