0% found this document useful (0 votes)
28 views

Deep Learning

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Deep Learning

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 45

By : Dr.

Uma N Dulhare
(Professor & HEAD)

Fundamentals of
Deep Learning
Implementing Neural Networks in TensorFlow

What Is TensorFlow?

 Tensorflow is an end-to-end open source machine learning platform. It


allows developers to create large-scale neural networks with many layers.
 An open source for framework for creating computational graphs.
 Computational graphs are actually extremely flexible.
 Linear Regression (regular, multiple, logistic, lasso, ridge, elasticnet,..)
 Support Vector Machines (any kernel)
 Nearest Neighbor methods
 Neural Networks
 ODE/PDE Solvers
How Does TensorFlow Compare to Alternatives?

 TensorFlow has market share of 37.31% in data-


science-machine-learning market.
 The top three of TensorFlow’s competitors in the Data
Science And Machine Learning category are OpenCV
with 21.61%, PyTorch with 21.01%, Keras with 19.90%
market share.
Installing TensorFlow
 Installing TensorFlow in your local development
environment is straightforward.
 If you don’t already have Pip installed on your computer,
use the following commands in your terminal:
• # Ubuntu/Linux 64-bit
• $ sudo apt-get install python-pip python-dev
• # Mac OS X
• $ sudo easy_install pip
Once we have Pip (version 8.1 or later)
installed on our computers, we can use the
following commands to install TensorFlow.
$ pip install --upgrade tensorflow # for
Python 2.7
$ pip3 install --upgrade tensorflow # for
Python 3.n
$ pip install --upgrade tensorflow-gpu # for
Python 2.7 # and GPU $ pip3 install --upgrade
tensorflow-gpu # for Python 3.n # and GPU
 Using Pip, installing IPython only requires
the following command:
 $ pip install ipython
 Then we can test that our installation of
TensorFlow functions as expected:
 $ ipython ...
 In [1]: import tensorflow as tf
 In [2]: deep_learning = tf.constant('Deep
Learning')
 In [3]: session = tf.Session()
 In [4]: session.run(deep_learning)
 Out[4]: 'Deep Learning'
 In [5]: a = tf.constant(2)
 In [6]: a = tf.constant(2)
 In [7]: multiply = tf.mul(a, b)
 In [7]: session.run(multiply)
 Out[7]: 6
Creating and Manipulating TensorFlow Variables
 Initialize and assing
TensorFlow Variables
 Create and manipulate tensors
 Refresh your memory about
addition and multiplication in
linear algebra
 Familiarize yourself with basic
TensorFlow math and array
operations
TensorFlow Operations
• Add
• Subtract
• Multiply
• Divide
• Square
• Reshape

 Tensor Addition
You can add two tensors
using tensorA.add(tensorB):
 Tensor Subtraction
You can subtract two tensors
using tensorA.sub(tensorB):
 Tensor Multiplication
You can multiply two tensors using tensorA.mul(tensorB)
 Tensor Division
You can divide two tensors using tensorA.div(tensorB)
 Tensor Square
You can square a tensor using tensor.square()
 Tensor Reshape
You can reshape a tensor using tensor.reshape()
Placeholder Tensors

A placeholder is a variable in Tensorflow to which data


will be assigned sometime later on. It enables us to
create processes or operations without the requirement
for data. Data is fed into the placeholder as the session
starts, and the session is run. We can feed in data into
tensorflow graphs using placeholders.
 Syntax: tf.compat.v1.placeholder(dtype, shape=None,
name=None)
Sessions in TensorFlow
• A session allows to execute graphs or part of
graphs. It allocates resources (on one or more
machines) for that and holds the actual values of
intermediate results and variables.
• A graph defines the computation. It doesn’t
compute anything, it doesn’t hold any values, it just
defines the operations that you specified in your
code.
Navigating Variable Scopes and Sharing
Variables

● tf. get_variable(<name>, <shape>,


<initializer>) : Creates or returns a variable
with a given name.
● tf. variable_scope(<scope_name>) : Manages
namespaces for names passed to tf.
get_variable() .
● A variable scope is used to manage the names of variables in TensorFlow.
When you define a variable in TensorFlow, it must have a unique name so that
you can reference it later. However, if you have many variables in your code, it
can be difficult to come up with unique names for all of them.
● Variable scope is used to manage variables and share them across different
parts of the graph. The variables created inside a variable scope are
automatically added to the scope, and their names are automatically prefixed
with the scope name. The variable scope is created using tf.variable_scope().
Managing Models over the CPU and GPU

The first step to optimizing machine learning models on


GPU and CPU is to choose the right framework. Some
popular frameworks include TensorFlow, PyTorch, and
MXNet. These frameworks provide high-level APIs that
make it easy to develop and train machine learning
models. They also support GPU and CPU acceleration.
Specifying the Logistic Regression Model in TensorFlow
Using Tensorflow to implement Logistic Regression model
Step 1: Read the data. iris = pd. ...
Step 2: Numerical processing. replace 'Iris-setosa' as 0. ...
Step 3: Split data. trainset: 80% ...
Step 4: Normalized processing. In [16]: ...
Step 5: Build the model framework. ...
Step 6: Model training. ...
Step 7: Visualization.
Logging and Training the Logistic Regression Model

● Logistic regression is used to predict the categorical dependent


variable using a given set of independent variables.
● It is used for solving classification problems.
● Maximum likelihood estimation method is used for Estimation
of accuracy.
● Output is must be categorical value such as 0 or 1, Yes or no, etc.
● There should not be collinearity between independent varible.
Leveraging TensorBoard to Visualize Computation
Graphs
and Learning

● TensorBoard comes preinstalled, upon installing TensorFlow on


your machine. Not only can TensorFlow create graphs, but it
also assists you in the event of debugging or optimization. If for
instance, your model is not working as you expect it to be,
TensorBoard can help you fix it.
● TensorBoard is the interface dedicated by Google to visualizing
the computational operations in a model.
● One of the reasons TensorFlow stands out as the library for
building neural networks is because it can perform fast model
visualization.
Building a Multilayer Model for MNIST in TensorFlow
● Using a logistic regression model, we were able to achieve an 8.1% error
rate on the
● MNIST dataset.
● import pandas as pd. import matplotlib. ...
● import tensorflow as tf. import tensorflow_datasets as tfds. ...
● train_data, val_data, test_data = tfds. load("mnist", ...
● x_viz, y_viz = tfds. load("mnist", split=['train[:1500]'], batch_size=-1,
as_supervised=True)[0] ...
● sns. countplot(x=y_viz. ...
● def preprocess(x, y): ...
● x = tf. ...
● x = tf.
Convolutional Neural Networks
Neurons in Human Vision

The human sense of vision is unbelievably advanced. Within


fractions of seconds, we can identify objects within our field of
view, without thought or hesitation. Not only can we name
objects we are looking at, we can also perceive their depth,
perfectly distinguish their contours, and separate the objects from
their backgrounds.
● Foundational to the human sense of vision is the neuron. Specialized
neurons are
● responsible for capturing light information in the human eye.2 This light
information
● is then preprocessed, transported to the visual cortex of the brain, and then
finally
● analyzed to completion. Neurons are single-handedly responsible for all of
these
● functions. As a result, intuitively, it would make a lot of sense to extend our
neural
● network models to build better computer vision systems.
The Shortcomings of Feature Selection
The two main disadvantages of these methods are:
● The increasing overfitting risk when the number of observations is
insufficient.
● The significant computation time when the number of variables is large.
Vanilla Deep Neural Networks Don’t Scale

The fundamental goal in applying deep learning to computer vision is to remove


the cumbersome, and ultimately limiting, feature selection process.
deep neural networks are perfect for this process because each layer of a neural
network is responsible for learning and building up features to represent the
input data that it receives.
If we attempt to tackle the image classification problem in this way, however,
we’ll quickly face a pretty daunting challenge, visually demonstrated in
Figure. In MNIST, our images were only 28 x 28 pixels and were black and
white. As a result, a neuron in a fully connected hidden layer would have
784 incoming weights. This seems pretty tractable for the MNIST task, and
our vanilla neural net performed quite well.
The convolutional network takes advantage of the fact that we’re analyzing
images, and sensibly constrains the architecture of the deep network so that
we drastically reduce the number of parameters in our model.
Filters and Feature Maps

Feature maps are used in convolutional neural networks for several reasons, some of
which are mentioned below.
● Detect important features: Initially, the feature maps capture low-level patterns, but
as they propagate to successive layers, they detect new patterns and combine them to
form high-level features.
● Feature sharing: A feature map is passed through multiple layers in a neural network
for image processing; hence, the features found by the previous layers are propagated
to each successive layer.
● Object recognition: Feature maps can also be passed to an artificial neural network
which can be trained to predict the object in the image.
● Image segmentation: Feature maps can divide an image into different segments, each
representing a meaningful part of the unsegmented image.
Full Description of the Convolutional Layer

● The convolutional layer is the core building block of a CNN, and it is where the
majority of computation occurs. It requires a few components, which are input data,
a filter, and a feature map. Let's assume that the input will be a color image, which
is made up of a matrix of pixels in 3D.
● Neural networks are a subset of machine learning, and they are at the heart of deep
learning algorithms. They are comprised of node layers, containing an input layer,
one or more hidden layers, and an output layer. Each node connects to another and
has an associated weight and threshold. If the output of any individual node is above
the specified threshold value, that node is activated, sending data to the next layer of
the network. Otherwise, no data is passed along to the next layer of the network.
Max Pooling

● Max Pooling is a pooling operation that


calculates the maximum value for patches of
a feature map, and uses it to create a
downsampled (pooled) feature map. It is
usually used after a convolutional layer.
● More rigorously, we can describe a pooling
layer with two parameters:
• Its spatial extent e
• Its stride s
Full Architectural Description of Convolution Networks

● It has three layers namely,


convolutional, pooling, and a fully
connected layer. It is a class of neural
networks and processes data having a
grid-like topology. The convolution
layer is the building block of CNN
carrying the main responsibility for
computation.
Closing the Loop on MNIST with Convolutional
Networks

● CNN is basically a model known to be Convolutional Neural Network and


in recent times it has gained a lot of popularity because of its usefulness.
CNN uses multilayer perceptrons to do computational works. CNN uses
relatively little pre-processing compared to other image classification
algorithms. This means the network learns through filters that in traditional
algorithms were hand-engineered. So, for the image processing tasks CNNs
are the best-suited option.
Here is a basic approach to applying a CNN on the MNIST dataset using
the Python programming language and the Keras library:
● Load and preprocess the data: The MNIST dataset can be loaded using the
Keras library, and the images can be normalized to have pixel values between
0 and 1.
● Define the model architecture: The CNN can be constructed using the Keras
Sequential API, which allows for easy building of sequential models layer-by-
layer. The architecture should typically include convolutional layers, pooling
layers, and fully-connected layers.
● Compile the model: The model needs to be compiled with a loss function, an
optimizer, and a metric for evaluation.
● Train the model: The model can be trained on the training set using the Keras
fit() function. It is important to monitor the training accuracy and loss to
ensure the model is converging properly.
● Evaluate the model: The trained model can be evaluated on the test set using
the Keras evaluate() function. The evaluation metric typically used for
classification tasks is accuracy
Image Preprocessing Pipelines Enable More Robust
Models
● Image preprocessing may also decrease model training time and increase
model inference speed. If input images are particularly large, reducing the
size of these images will dramatically improve model training time without
significantly reducing model performance.
● The handwritten digits are perfectly cropped in just the same way; there are
no color aberrations because MNIST is black and white; and so on. Natural
images, however, are an entirely different beast.
● Natural images are messy, and as a result, there are a number of
preprocessing operations that we can utilize in order to make training
slightly easier. The first technique that is supported out of the box in
TensorFlow is approximate per-image whitening.
Accelerating Training with Batch Normalization

Training Deep Neural Networks is complicated by the fact that the distribution of each
layer's inputs changes during training, as the parameters of the previous layers change.
This slows down the training by requiring lower learning rates and careful parameter
initialization, and makes it notoriously hard to train models with saturating
nonlinearities. We refer to this phenomenon as internal covariate shift, and address the
problem by normalizing layer inputs. Our method draws its strength from making
normalization a part of the model architecture and performing the normalization for
each training mini-batch. Batch Normalization allows us to use much higher learning
rates and be less careful about initialization. It also acts as a regularizer, in some cases
eliminating the need for Dropout.
Building a Convolutional Network for CIFAR-10

Convolutional Neural Network (CNN)


● Import TensorFlow.
● Download and prepare the CIFAR10 dataset.
● Verify the data.
● Create the convolutional base.
● Add Dense layers on top.
● Compile and train the model.
● Evaluate the model.
Visualizing Learning in Convolutional Networks
● To understand how convolutional neural networks learn spatial and temporal
dependencies of an image, different features captured at each layer can be
visualized in the following manner.
● To visualize the features at each layer, Keras Model class is used. It allows
the model to have multiple outputs. It maps given a list of input tensors to
list of output tensors.
● tf.keras.Model()
● Arguments:
● inputs: It can be a single input or a list of inputs which are objects of
keras.Input class
● outputs: Output/ List of outputs.
Leveraging Convolutional Filters to Replicate Artistic
Styles
● The goal of neural style is to be able to take an arbitrary photograph and rerender it
as if it were painted in the style of a famous artist. This seems like a daunting task,
and it’s not exactly clear how we might approach this problem if we didn’t have a
convolutional network. However, it turns out that clever manipulation of
convolutional filters can produce spectacular results on this problem.
● There are three images that we’re dealing with. The first two are the source of
content p and the source of style a. The third image is the generated image x. Our
goal will be to derive an error function that we can backpropagate that, when
minimized, will perfectly combine the content of the desired photograph and the
style of the desired artwork.
Learning Convolutional Filters for Other Problem
Domains

● A natural extension of image analysis is video analysis. In fact, using five-


dimensional tensors (including time as a dimension) and applying three-
dimensional convolutions is an easy way to extend the convolutional
paradigm to video.
● Convolutional filters have also been successfully used to analyze
audiograms. In these applications, a convolutional network slides over an
audiogram input to predict phonemes on the other side.
Thank You
Hope you enjoyed the presentation

You might also like