What Is Deep Learning and How Does It Work - Towards Data Science
What Is Deep Learning and How Does It Work - Towards Data Science
Follow 577K Followers · Editors' Picks Features Deep Dives Grow Contribute About
You have 2 free member-only stories left this month. Sign up for Medium and get an extra one
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 1/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
If you liked the article and want to share your thoughts, ask questions
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 2/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
Table of Content
1. What exactly is Deep Learning?
6. Layer Connections
8. Loss Functions
9. Gradient Descent
Have you ever wondered how Google’s translator App is able to translate
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 3/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
How Netflix and YouTube are able to figure out our taste in movies or
videos and give us appropriate recommendations?
All of this is a product of Deep Learning and Artificial Neural Networks. The
definition of Deep Learning and Neural networks will be addressed in the
following.
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 4/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 5/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
The design of the neural network is based on the structure of the human
brain. Just as we use our brains to identify patterns and classify different
types of information, neural networks can be taught to perform the same
tasks on data.
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 6/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
In general, neural networks can perform the same tasks as classical algorithms
of machine learning. However, it is not the other way around.
All recent advances in artificial intelligence in recent years are due to deep
learning. Without deep learning, we would not have self-driving cars,
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 7/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
chatbots or personal assistants like Alexa and Siri. The Google Translate
app would continue to be as primitive as 10 years ago (before Google
switched to neural networks for this App), and Netflix or Youtube would
have no idea which movies or TV series we like or dislike. Behind all these
technologies are neural networks.
At the end of the day, deep learning is the best and most obvious approach
to real machine intelligence we’ve had so far.
The first advantage of deep learning over machine learning is the needlessness
of the so-called feature extraction.
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 8/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
Long before deep learning was used, traditional machine learning methods
were mainly used. Such as Decision Trees, SVM, Naïve Bayes Classifier and
Logistic Regression.
These algorithms are also called flat algorithms. Flat here means that these
algorithms can not normally be applied directly to the raw data (such as
.csv, images, text, etc.). We need a preprocessing step called Feature
Extraction.
On the other side are the artificial neural networks of Deep Learning.
These do not need the Feature Extraction step.
The layers are able to learn an implicit representation of the raw data
directly and on their own. Here, a more and more abstract and compressed
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 9/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
In other words, we can also say that the feature extraction step is already part
of the process that takes place in an artificial neural network.
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 10/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
During the training process, this step is also optimized by the neural
network to obtain the best possible abstract representation of the input
data. This means that the models of deep learning thus require little to no
manual effort to perform and optimize the feature extraction process.
“The analogy to deep learning is that the rocket engine is the deep learning
models and the fuel is the huge amounts of data we can feed to these
algorithms.”
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 12/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
Deep Learning Algorithms get better with the increasing amount of data.
Deep Learning models tend to increase their accuracy with the increasing
amount of training data, where’s traditional machine learning models such
as SVM and Naive Bayes classifier stop improving after a saturation point.
Artificial neural networks are inspired by the biological neurons that are
found in our brains. In fact, the artificial neural networks simulate some
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 13/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
At the majority of synapses, signals cross from the axon of one neuron to
the dendrite of another. All neurons are electrically excitable due to the
maintenance of voltage gradients in their membranes. If the voltage
changes by a large enough amount over a short interval, the neuron
generates an electrochemical pulse called an action potential. This potential
travels rapidly along the axon and activates synaptic connections as it
reaches them.
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 15/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 16/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
neural network has to learn them. The process of learning we also call as
training.
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 17/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
The input layer receives the input x, data from which the neural network
learns. In our previous example of classification of handwritten numbers,
these input x would represent the images of these numbers ( x is basically
an entire vector where each entry is a pixel).
The input layer has the same number of neurons as there are entries in the
vector x. Meaning: each input neuron represents one element in the vector
x.
The last layer is called the output layer, which outputs a vector y
representing the result that the neural network came up with. The entries in
this vector represent the values of the neurons in the output layer. In our
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 18/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
In this case, the value of an output neuron gives the probability that the
handwritten digit given by the features x belongs to one of the possible
classes (one of the digits 0–9). As you can imagine the number of output
neurons must be the same as there are classes.
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 19/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
Layer Connections
As you can see in the picture, each connection between two neurons is
represented by a different weight w. Each of these weight w has indices.
The first value of the indices stands for the number of neurons in the layer
from which the connection originates, the second value for the number of
the neurons in the layer to which the connection leads.
A weight matrix.
A weight matrix has the same number of entries as there are connections
between neurons. The dimensions of a weight matrix result from the sizes
of the two layers that are connected by this weight matrix.
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 21/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
Forward Propagation.
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 22/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
This step is also referred to as the forward propagation. With the input
vector x and the weight matrix W connecting the two neuron layers, we
compute the dot product between the vector x and the matrix W.
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 23/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
There are 3 activation functions that are used in Deep Learning, which are
tanh, sigmoid, and ReLu.
At this point, you may recognize the meaning behind neurons in a neural
network. A neuron is simply a representation of a numeric value.
Let’s take a closer look at vector z for a moment. As you can see, each
element of z consists of the input vector x. At this point, the role of the
weights unfolds beautifully. A value of a neuron in a layer consists of a
linear combination of neuron values of the previous layer weighted by some
numeric values.
These numerical values are the weights that tell us how strongly these neurons
are connected with each other.
During training, these weights are adjusted, some neurons become more
connected, some neurons become less connected. As in a biological neural
network, learning means the alteration of weights. Accordingly, the
values of z, h and the final output vector y are changing with the weights.
Some weights make the predictions of a neural network us closer to the
actual ground truth vector y_hat, some weights increase the distance to the
ground truth vector.
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 24/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
Now that we know how the mathematical calculations between two neural
network layers look like, we can extend our knowledge to a deeper
architecture that consists of 5 layers.
Same as before we calculate the dot product between the input x and the
first weight matrix W1 and apply an activation function to the resulting
vector to obtain the first hidden vector h1. h1 is now considered as the
input for the upcoming third layer. The whole procedure from before is
repeated until we obtain the final output y:
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 25/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 26/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
8. Loss Functions
After we get the prediction of the neural network, in the second step we
must compare this prediction vector to the actual ground truth label. We
call the ground truth label as vector y_hat.
While the vector y contains the predictions that the neural network has
computed during the forward propagation (and which may, in fact, be very
different from the actual values), the vector y_hat contains the actual
values.
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 27/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
Quadratic Loss.
The value of this loss function depends on the difference between y_hat
and y. A higher difference means a higher loss value, a smaller difference
means a smaller loss value.
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 28/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
Since the loss depends on the weights, we must find a certain set of
weights for which the value of the loss function is as small as possible. The
method of minimizing the loss function is achieved mathematically by a
method called gradient descent
9. Gradient Descent
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 29/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
During gradient descent, we use the gradient of a loss function (or in other
words the derivative of the loss function) to improve the weights of a neural
network.
This neural network receives an input x and outputs a prediction y. Let say
the initial weight value of this neural network is 5 and the input x is 2.
Therefore the prediction y of this network has a value of 10, while the label
y_hat might have a value of 6.
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 30/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
This means that the prediction is not accurate and we must use the gradient
descent method to find a new weight value that causes the neural network
to make the correct prediction. In the first step, we must choose a loss
function for the task. Let’s take the quadratic loss that I have defined earlier
and plot this function, which basically is just a quadratic function:
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 31/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
The y-axis is the loss value which depends on the difference between the
label and the prediction, and thus the network parameters, in this case, the
one weight w. The x-axis represents the values for this weight. As you can
see there is a certain weight w for which the loss function reaches a global
minimum. This value is the optimal weight parameter that would cause the
neural network to make the correct prediction which is 6. In this case, the
value for the optimal weight would be 3:
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 32/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
Our initial weight, on the other hand, is 5, which leads to a fairly high loss.
The goal now is to repeatedly update the weight parameter until we
reach the optimal value for that particular weight. This is the time when we
need to use the gradient of the loss function. Fortunately, in this case, the
loss function is a function of one single variable, which is the weight w:
Loss Function.
In the next step, we calculate the derivative of the loss function with respect
to this parameter:
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 33/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
In the end, we get a result of 8, which gives us the value of the slope or the
tangent of the loss function for the corresponding point on the x-axis at
which our initial weight lies.
This tangent points towards the highest rate of increase of the loss function
and the corresponding weight parameters on the x-axis.
This means that we have just used the gradient of the loss function to find
out which weight parameters would result in an even higher loss value. But
what we want to know is the exact opposite. We can get what we want, if
we multiply the gradient by minus 1 and this way obtain the opposite
direction of the gradient. This way we get the direction of the highest rate
of decrease of the loss function and the corresponding parameters on the x-
axis that cause this decrease:
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 34/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 35/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
As you can see, our weight w after the gradient descent is now 4.2 and
closer to the optimal weight than it was before the gradient step.
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 36/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
The value of the loss function for the new weight value is also smaller,
which means that the neural network is now capable to do a better
prediction. You can do the calculation in your head and see that the new
prediction is, in fact, closer to the label than before.
Each time we are performing the update of the weights, we move down the
negative gradient towards the optimal weights.
After each gradient descent step or weight update, the current weights of
the network get closer and closer to the optimal weights until we eventually
reach them and the neural network will be capable to do the predictions we
want to make.
Every Thursday, the Variable delivers the very best of Towards Data Science: from
hands-on tutorials and cutting-edge research to original features you don't want to
miss. Take a look.
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 37/38
4/7/2021 What is Deep Learning and How does it work? | Towards Data Science
By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information
about our privacy practices.
Deep Learning Artificial Intelligence Data Science Machine Learning Towards Data Science
https://towardsdatascience.com/what-is-deep-learning-and-how-does-it-work-2ce44bb692ac 38/38