Deep Learning Notes
Deep Learning Notes
Deep Learning
Unlike traditional Machine Learning (ML), where feature extraction is manual and time-
consuming, Deep Learning automates this process. Deep Neural Networks can
automatically discern relevant features from raw data, reducing the need for domain
expertise in feature engineering. For anyone diving into this, a Deep Learning
Cheatsheet can be an invaluable tool for quickly understanding key concepts. Deep
Learning models can handle unstructured data, such as images, audio, and text, with
remarkable accuracy. It is indispensable in Machine Learning and Artificial Intelligence
(AI).
Being a subset of Machine Learning, Deep Learning employs different Types of Neural
Networks to analyse and learn from data. It mimics the human brain’s ability to process
information and adapt based on experience. Deep Learning models, also recognised
as What is a Neural Network, consist of multiple interconnected layers, enabling them to
perform complicated tasks such as image recognition, language translation, and
decision-making.
Understanding the inner workings of Deep Learning involves delving into the intricate
processes that enable this technology to learn, adapt, and make informed decisions.
Here's a simplified overview of how Deep Learning works:
2
a) Data Collection and Preparation: Start by collecting data relevant to the task. This
data could be images, text, or any other input form. Before using it, clean and organise
the data to ensure accuracy.
c) Initialising Weights: Each connection in the model has a weight. These weights
start as random values. The model will learn how to adjust these weights to make
accurate predictions.
d) Forward Propagation: Input data is fed into the model's first layer. It travels through
each layer, and neurons process it along the way. Neurons use weights to make
calculations.
f) Calculating Loss: The model's output is compared to the actual target. This
comparison is done using a loss function, which calculates the difference between
predicted and actual values.
h) Optimisation and Learning Rate: The learning rate determines how big the weight
adjustments are. A higher rate can lead to faster Learning but might overshoot the
optimal values. It's a balance to strike.
3
i) Training Iterations: The model uses multiple iterations, or epochs, to refine its
predictions. It processes the entire dataset during each epoch (one complete training
dataset), gradually improving its accuracy.
j) Validation and Testing: The model is tested on new, unseen data to check its
performance once trained. This ensures that the model can generalise its Learning to
real-world situations.
l) Making Predictions: With training complete, the model can make predictions on new
data. It uses what it learned during training to provide insights or solve specific tasks.
Deep Learning Networks are intricate systems designed to mimic the human brain’s
ability to learn and process information. Let’s explore its components to grasp how
Deep Learning models function and make decisions:
1) Input Layer
An Artificial Neural Network comprises several nodes that input data into it. These
nodes constitute the system’s input layer.
2) Hidden Layer
The input layer processes and transmits data to subsequent layers within the Neural
Network. These hidden layers handle information at various levels, adjusting their
behaviour as they receive new data. Deep Learning networks can have hundreds of
hidden layers, enabling them to examine a problem from multiple perspectives.
3) Output Layer
4
The output layer comprises the nodes that produce the data. Deep Learning models
that provide “yes” or “no” answers only have two nodes in the output layer. Conversely,
models that generate a broader range of answers contain more nodes.
Deep Learning has come up as a transformative technology with many benefits that
reshape industries and drive innovation across various domains. Let’s take a look at its
benefits:
a) Deep Learning models play a crucial role in recognizing intricate patterns in data,
highlighting the importance of Deep Learning in enhancing accuracy across various
tasks, including image recognition, language translation, and medical diagnoses.
b) Deep Learning excels with large datasets, revealing insights that traditional methods
might miss.
c) Automating complex tasks, like image classification and speech recognition, is a key
strength of Deep Learning.
5
d) Deep Learning models can process and analyse data in real-time, facilitating rapid
decision-making.
g) Deep Learning models can learn and adapt to new information over time, maintaining
their relevance and effectiveness as data evolves.
The widespread adoption of Deep Learning has revolutionised various industries and
domains, with Deep Learning With Python emerging as a go-to tool for developers and
engineers in these fields. It unleashed a wave of innovative solutions and transformed
how we interact with technology. Let’s explore the various areas where Deep Learning
can be used.
a) Computer Vision: Deep Learning has converted computer vision, allowing machines
to interpret and recognise visible records. Its packages consist of object detection,
image and facial reputation, or even autonomous cars. Deep Neural Networks can
stumble on and classify items, making them imperative in surveillance, scientific
imaging, self-riding automobiles, and more.
Learning algorithms can create realistic characters, simulate natural movements, and
generate lifelike environments, making entertainment more immersive and captivating.
Deep Learning utilises three primary types of Artificial Neural Networks, each with
distinct characteristics and Deep Learning applications. Let’s look at them in detail:
c) Recurrent Neural Network (RNN): Recurrent Neural Networks (RNNs) are a class
of Artificial Neural Networks characterised by their looping connections. This unique
structure allows them to process data sequentially, both advancing it forward and
cycling it back through earlier layers for re-evaluation. RNNs excel in tasks that involve
sequential prediction, such as determining the sentiment of a text or predicting the next
element in a series, be it words, audio, or visual cues.
Deep Learning, on the other hand, automates feature engineering through its layered
architecture. Neural Networks in Deep Learning models can automatically comprehend
and extract features from raw data, reducing the need for manual intervention and often
resulting in better performance on complex tasks such as image and speech
recognition.
Machine learning models typically require less data to train compared to Deep Learning
models. This is because traditional Machine Learning algorithms are simpler and rely
heavily on the quality of manually engineered features.
ta to achieve high performance. Training Deep Learning models also takes significantly
more time and computational resources due to their complexity and the need for
extensive parameter tuning.
Machine learning models are generally smaller and less complex than Deep Learning
models. They use simpler algorithms and fewer parameters, making them easier to
interpret and faster to train.
Deep Learning models, especially Deep Neural Networks, are much larger and more
complex, with millions of parameters. This complexity allows them to capture intricate
patterns in data but also makes them more challenging to understand and debug. The
10
Deep Learning technologies have their set of challenges, that include the following:
a) Deep Learning models are shaped by the data they’re trained on, limiting their
understanding to that specific dataset. A narrow or small dataset hinders their general
applicability.
b) Bias is a critical issue; models trained on biased data can replicate these biases,
often with opaque decision-making criteria.
c) The learning rate is pivotal; too high can lead to premature, suboptimal solutions,
while too low can stall progress.
11
e) More complex and precise models demand more parameters, which in turn need
more data.
f) Trained Deep Learning models lack flexibility and are unable to multitask, excelling
only in the specific tasks they were trained for. Retraining is required for even minorly
different problems.
g) Current Deep Learning methods fall short in tasks that demand reasoning, such as
programming or strategic planning, irrespective of the amount of data available.
Conclusion
Motivations
Shortcomings of conventional ML
1. The curse of dimensionality
No. of possible distinct configurations of a set of variables increases exponentially
with no of variables – Poses a statistical challenge
• Ex: 10 regions of interest with one variable – We need to track 100 regions with
two variables – 1000 regions with three variables
2. Local constancy and smoothness regularization
12
Prior beliefs
–To generalize well ML algorithms need prior beliefs
•Form of probability distributions over parameters
• Influencing the function itself, while parameters are influenced only indirectly
• Algorithms biased towards preferring a class of functions – These biases may not
be expressed in terms of a probability distribution
• Most widely used prior is smoothness
– Also called local constancy prior
– States that the function we learn should not change very much within a small
region
Local Constancy Prior
Function should not change very much within a small region • Many simpler
algorithms rely exclusively on this prior to generalize well – Thus fail to scale
statistical challenges in AI tasks • Deep learning introduces additional (explicit and
implicit) priors in order to reduce generalization error on sophisticated tasks • We
now explain why smoothness alone is insufficient
Specifying smoothness
Several methods to encourage learning a function f* that satisfies the condition
f*(x)≈f*(x+ε) – For most configurations x and small change ε • If we know a good
answer for input x then that answer is good in the neighborhood of x • An extreme
example is k-nearest neighbor – Points having the same set of nearest neighbors all
have the same prediction – For k=1, no of regions ≤ no of training examples
Kernel machines and smoothness
Kernel machines interpolate between training set outputs associated with nearby
training examples • With local kernels: k(u,v) is large when u=v and decreases as u
and v grow further apart • Can be thought of as a similarity function that performs
template matching – By measuring how closely test example x resembles training
example x(i) • Much of deep learning is motivated by limitations of template
matching
Decision Trees and Smoothness
13
Also suffers from exclusively smoothnessbased learning – They break input space
into as many regions as there are leaves and use a separate parameter in each
region – For n leaves, at least n training samples are required – Many more needed
for statistical confidence
3. Manifold learning
Sentiment analysis: Deep learning models can analyze the sentiment of a piece
of text, making it possible to determine whether the text is positive, negative or
neutral.
Speech recognition: Deep learning models can recognize and transcribe spoken
words, making it possible to perform tasks such as speech-to-text conversion,
voice search and voice-controlled devices.
3. Reinforcement learning
In reinforcement learning, deep learning works as training agents to take action in an
environment to maximize a reward. Some of the main applications of deep learning in
reinforcement learning include:
Game playing: Deep reinforcement learning models have been able to beat
human experts at games such as Go, Chess and Atari.
Robotics: Deep reinforcement learning models can be used to train robots to
perform complex tasks such as grasping objects, navigation and manipulation.
Control systems: Deep reinforcement learning models can be used to control
complex systems such as power grids, traffic management and supply chain
optimization.
Advantages of Deep Learning
1. High accuracy: Deep Learning algorithms can achieve state-of-the-art
performance in various tasks such as image recognition and natural language
processing.
2. Automated feature engineering: Deep Learning algorithms can automatically
discover and learn relevant features from data without the need for manual feature
engineering.
3. Scalability: Deep Learning models can scale to handle large and complex
datasets and can learn from massive amounts of data.
4. Flexibility: Deep Learning models can be applied to a wide range of tasks and can
handle various types of data such as images, text and speech.
5. Continual improvement: Deep Learning models can continually improve their
performance as more data becomes available.
18
Deep learning (DL) frameworks provide essential building blocks for designing, training, and
validating deep neural networks through a high-level programming interface. These frameworks are
widely used by developers, researchers, and data scientists to create and deploy various deep
learning models. Here are some of the most popular DL frameworks:
PyTorch
PyTorch is a Python package that offers tensor computation with strong GPU acceleration and deep
neural networks built on a tape-based autograd system. It allows users to reuse their favorite Python
19
packages, such as numpy, scipy, and Cython, to extend PyTorch when needed. PyTorch also
supports high-performance inference deployment through Torch-TensorRT integration or by
exporting models to ONNX format and using NVIDIA TensorRT1.
Example:
import torch
import torch.nn as nn
class SimpleNN(nn.Module):
def __init__(self):
super(SimpleNN, self).__init__()
self.fc2 = nn.Linear(50, 1)
x = torch.relu(self.fc1(x))
x = self.fc2(x)
return x
model = SimpleNN()
TensorFlow
. TensorFlow is an open-source software library for numerical computation using data flow graphs. It
allows users to deploy computation to one or more CPUs or GPUs without rewriting code.
TensorFlow offers TensorBoard, a suite of visualization tools, for visualizing results. High-
performance inference deployment can be achieved using TensorFlow-TensorRT integration or by
exporting models to ONNX format and using NVIDIA TensorRT1
Example:
import tensorflow as tf
20
model = tf.keras.Sequential([
tf.keras.layers.Dense(1)
])
model.compile(optimizer='adam', loss='mean_squared_error')
JAX
JAX is a Python library designed for high-performance numerical computing and machine learning
research. It can automatically differentiate native Python and implement the NumPy API. JAX
enables distributed training across multi-node, multi-GPU systems with accelerated performance
through XLA1.
Example:
import jax.numpy as jnp
def f(x):
return jnp.sin(x)
df = grad(f)
f_jit = jit(f)
PaddlePaddle
PaddlePaddle provides an intuitive and flexible interface for loading data and specifying model
structures. It supports CNN, RNN, and multiple variants, and easily configures complicated deep
21
models. PaddlePaddle also offers optimized operations, memory recycling, and network
communication to accelerate the training process1.
Example:
import paddle
import paddle.nn as nn
class SimpleNN(nn.Layer):
def __init__(self):
super(SimpleNN, self).__init__()
self.fc2 = nn.Linear(50, 1)
x = paddle.nn.functional.relu(self.fc1(x))
x = self.fc2(x)
return x
model = SimpleNN()
MATLAB
MATLAB makes deep learning easy for engineers, scientists, and domain experts. It offers tools and
functions for managing and labeling large datasets, as well as specialized toolboxes for working with
machine learning, neural networks, computer vision, and automated driving. MATLAB allows users
to create and visualize models with just a few lines of code and deploy models to servers and
embedded devices1.
Example:
% Define a simple neural network
layers = [
reluLayer
fullyConnectedLayer(1)
];
% Create a network
net = dlnetwork(layers);
These frameworks provide powerful tools and libraries to facilitate the development and deployment
of deep learning models, making it easier for developers and researchers to achieve their goals 1.
Introduction to TensorFlow
TensorFlow is an open-source framework for machine learning (ML) and artificial
intelligence (AI) that was developed by Google Brain. It was designed to facilitate the
development of machine learning models, particularly deep learning models by
providing tools to easily build, train and deploy them across different platforms.
TensorFlow supports a wide range of applications from natural language processing
(NLP) and computer vision (CV) to time series forecasting and reinforcement learning.
TensorFlow Core: The base API for TensorFlow that allows users to define
models, build computations and execute them.
Keras: A high-level API for building neural networks that runs on top of
TensorFlow, simplifying model development.
TensorFlow Lite: A lightweight solution for deploying models on mobile and
embedded devices.
TensorFlow.js: A library for running machine learning models directly in the
browser using JavaScript.
TensorFlow Extended (TFX): A production-ready solution for deploying machine
learning models in production environments.
TensorFlow Hub: A repository of pre-trained models that can be easily integrated
into applications.
3. Automatic Differentiation (Autograd)
TensorFlow automatically calculates gradients for all trainable variables in the model
which simplifies the backpropagation process during training. This is a core feature
that enables efficient model optimization using techniques like gradient descent.
4. Multi-language Support
TensorFlow is primarily designed for Python but it also provides APIs for other
languages like C++, Java and JavaScript making it accessible to developers with
different programming backgrounds.
5. TensorFlow Serving and TensorFlow Model Optimization
TensorFlow includes tools for serving machine learning models in production
environments and optimizing them for inference allowing for lower latency and higher
efficiency.
TensorFlow Architecture
The architecture of TensorFlow revolves around the concept of a computational graph
which is a network of nodes (operations) and edges (data). Here's a breakdown of key
components:
Tensors: Tensors are the fundamental units of data in TensorFlow. They are multi-
dimensional arrays or matrices used for storing data. A tensor can have one
dimension (vector), two dimensions (matrix) or more dimensions.
Graph: A TensorFlow graph represents a computation as a flow of tensors through
a series of operations. Each operation in the graph performs a specific
mathematical function on the input tensors such as matrix multiplication, addition
or activation.
Session: A session in TensorFlow runs the computation defined by the graph and
evaluates the tensors. This is where the actual execution of the model happens
enabling the training and inference processes.
24
TensorFlow Workflow
Supports use cases like image recognition, voice detection and sensor data
analysis.
Building a Simple Model with TensorFlow
Let's learn how to create and train a simple neural network with TensorFlow using the
steps discussed above.
Here, we have loaded the MNIST Dataset and processed the image. Then we have
built a simple neural network using TensorFlow's Sequential API with two layers:
dense layer with ReLU activation
an output layer with softmax activation function
At last we compiled the model using Adam Optimizer and Sparse Categorical
Crossentropy and trained the model for 5 epochs.
# Preprocess the data: flatten the images and normalize the pixel values
train_images = train_images.reshape((train_images.shape[0], 28 * 28)).astype('float32')
/ 255
test_images = test_images.reshape((test_images.shape[0], 28 * 28)).astype('float32') /
255
Traditional
High-level API
machine
for building
Deep learning, Deep learning, learning
Primary deep learning
production-level research and algorithms like
Focus models that
deployment experimentation decision trees,
runs on top of
SVMs, linear
TensorFlow
regression, etc.
Extensive
Built for
support like Not focused on
Primarily focused TensorFlow,
TensorFlow Lite deployment;
on research, hence
for mobile, more suitable
Deployment limited deployment
TensorFlow.js for small-to-
Options deployment follows
for the web, medium scale
options compared TensorFlow’s
TensorFlow machine
to TensorFlow deployment
Serving for learning tasks
pipeline
production
User-friendly
More flexible and
Moderate Simplifies API for classical
user-friendly,
learning curve building deep machine
especially for
with more learning learning
Ease of Use rapid prototyping,
extensive models algorithms,
due to dynamic
configuration especially for simpler for
computation
needed beginners smaller-scale
graph
models
27
Supports both
Focused on
research and Simplified
traditional
production More flexible, interface for
machine
models, but less great for rapid model creation,
Model learning, not
flexible prototyping, limited flexibility
Flexibility deep learning;
compared to research and compared to
limited flexibility
PyTorch for experimentation raw
for neural
research TensorFlow
networks
purposes
Classical
Image machine
classification, learning tasks
Building deep
NLP, time series Research, NLP, like
learning
Popular Use forecasting, computer vision, classification,
models quickly
Cases reinforcement prototyping deep regression,
on top of
learning, learning models clustering,
TensorFlow
production dimensionality
deployment reduction and
more
High-level API
for neural Not designed
Strong, Strong support for
networks, for deep
especially for neural networks,
focused on learning, lacks
complex neural particularly for
Support for simplifying the direct support
networks like models requiring
Neural process of for neural
CNNs, RNNs dynamic
Networks building models networks or
and deep computation
without large-scale
reinforcement graphs like RNNs,
needing much deep learning
learning models GANs, LSTMs
detail about models
architecture
Strong Growing
community, community, Large
Part of the
extensive strong support for community in
TensorFlow
ecosystem research but the machine
Community ecosystem,
including ecosystem learning space
& simplifying
TensorFlow Lite, focused more on but limited to
Ecosystem model
TensorFlow.js, academic classical ML
development
TensorFlow Hub applications tools and
and training
and TensorFlow rather than libraries
Extended (TFX) production tools
TensorFlow continues to evolve and with each update and becomes even more
accessible and efficient for building state-of-the-art machine learning models.