Backpropagation in 5 Minutes (tutorial)
Key Takeaways
This video tutorial by Siraj Raval explains the concept of backpropagation in neural networks, covering the necessary calculus terms such as derivatives, partial derivatives, and the chain rule, and demonstrates how to implement it programmatically using Python.
Full Transcript
Hello world. It's Suriraj and today we're going to learn how back propagation works in five minutes. Gradient descent is a popular optimization technique and can be used in many different types of machine learning models. It's used to optimize or improve the accuracy of our models predictions. One implementation of it that is particularly popular is for neural networks. A neural network is a learning model that can make predictions. We give it some input data. The x values represent the input and the y values represent the expected output labels. Our network's job is to learn this mapping so that given some arbitrary input, it can correctly predict its output label. So our three-layer neural network will first have an input layer. Each neuron represents a different row from our input data. Then it has a hidden layer. Data will flow in one direction from our input layer to our output. And the way it does this is by having weights that connect each neuron in one layer to every neuron in the next layer. We can initialize these weights as matrices with random values to start off with. We'll multiply each row of the input by each column of our weight matrix. The resulting values from this operation results in our hidden neuron values. We'll take each of those values and convert them to a probability value. That is a value between 0 and 1 by applying an activation function to it. The type we'll use in this example is a sigmoid. So each neuron receives a set of inputs, performs a dotproduct, and then applies an activation function to it. We'll just repeat this same process again to calculate the output prediction. We compute the dotproduct of the hidden layer neurons and the next weight matrix between the hidden layer and the output layer. Then we once again apply our activation function to it. This resulting value is our prediction. And this process that we just completed is called forward propagation. If we compare this to our expected output, we'll see that our prediction is incorrect. We want to find the absolute best weight values that given any input, they would help calculate the correct output. To do this, we'll first want to calculate an error value. We want to minimize this error. If we were to create a simple graph of the error value versus some random weight from our network, it would look like this. Too small a weight value and our error is high, but too big and our error becomes high. Again, we want an optimal value for each weight in our network where our error is smallest. Starting at some random weight value, we want to take a step in the direction towards the minimum error. This direction is the opposite to the gradient. If we take many steps descending down the gradient, eventually the weight will find the minimum of the error. We call this process gradient descent. So, how do we do this? Well, we'll need to use calculus. Let's do a little refresher on three terms from calculus we'll need to know. The derivative is a term that means the slope of the tangent line to a curve at a specific point. It measures the rate of change of a function. A derivative of a function f ofx gives you another function that returns the slope of f ofx at a point x. For example, the derivative of x^2 is 2x. So at x= 2, the slope is 4. A partial derivative of a function of several variables is its derivative with respect to one of those variables with the others held constant. And the chain rule is the process we can use to compute derivatives of composite functions. A composite function is a function of other functions. That is we might have one function that is composed of multiple inner or nested functions. Say you have some function f ofx and another function g ofx. Using them you form some composite function f of g ofx. The chain rule states that the derivative of f of g ofx is equal to the derivative of g ofx times the derivative of f ofx. A neural network is essentially a massive nested composite function. Each layer of a feed forward neural network can be represented as a single function whose inputs are a weight vector and the outputs of the previous layer. The purpose of back propagation is to figure out the partial derivatives of our error function with respect to each individual weight in the network. So we can use those in gradient descent. It gives us a way of computing the error for every layer and then relating those errors to the quantity of real interest, the partial derivative with respect to any weight in the network. We can use the chain rule to compute the partial derivatives that is the gradient of the error with respect to each weight. Back propagation at its core simply consists of repeatedly applying the chain rule through all the possible paths in our network. Our ultimate goal in training a neural network is to find the gradient of each weight with respect to the output. We do this so that we can update the weights incrementally using gradient descent. We reuse multiple values as we compute the updates for weights that appear earlier and earlier in the network. After we have the error for the output layer, we calculate an error for each neuron in the hidden layers going backwards layer by layer. The error for a neuron in a hidden layer is the sum of the products between the errors of the neurons in the next layer and the weights of the connections to those neurons multiplied by the derivative of the activation function. We will use those errors to calculate the variation of the weights as a result of the current input pattern and ideal outputs. The variation or delta of a weight is the product of the input neuron output value with the error of the output neuron for that connection. This process is repeated for all the input patterns and the deltas are accumulated. At the end of a learning iteration, we change the actual weights with the accumulated deltas for all the training patterns and we multiply it with a learning rate which states how fast a network converges to a result. When we run our code, we can see this process in action as our prediction gradually increases in accuracy. Please subscribe. And for now, I've got to go derive the meaning of life.
Original Description
Let's discuss the math behind back-propagation. We'll go over the 3 terms from Calculus you need to understand it (derivatives, partial derivatives, and the chain rule and implement it programmatically.
Code for this video:
https://github.com/llSourcell/how_to_do_math_for_deep_learning
Please Subscribe! And like. And comment. That's what keeps me going.
I've used this code in a previous video. I had to keep the code as simple as possible in order to add on these mathematical explanations and keep it at around 5 minutes.
More Learning resources:
https://mihaiv.wordpress.com/2010/02/08/backpropagation-algorithm/
http://outlace.com/Computational-Graph/
http://briandolhansky.com/blog/2013/9/27/artificial-neural-networks-backpropagation-part-4
https://jeremykun.com/2012/12/09/neural-networks-and-backpropagation/
https://mattmazur.com/2015/03/17/a-step-by-step-backpropagation-example/
Join us in the Wizards Slack channel:
http://wizards.herokuapp.com/
And please support me on Patreon:
https://www.patreon.com/user?u=3191693
Forgot to add my patron shoutout at the end so special thanks to Patrons Tim Jiang, HG Oh, Hoang, Advait Shinde, Vijay Daniel & Umesh Rangasamy
Follow me:
Twitter: https://twitter.com/sirajraval
Facebook: https://www.facebook.com/sirajology Instagram: https://www.instagram.com/sirajraval/ Instagram: https://www.instagram.com/sirajraval/
Signup for my newsletter for exciting updates in the field of AI:
https://goo.gl/FZzJ5w
Hit the Join button above to sign up to become a member of my channel for access to exclusive content! Join my AI community: http://chatgptschool.io/ Sign up for my AI Sports betting Bot, WagerGPT! (500 spots available): https://www.wagergpt.xyz
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Siraj Raval · Siraj Raval · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
What is Bitcoin?
Siraj Raval
5 Ways to Use Bitcoin
Siraj Raval
BTC Fever - Siraj [Music Video]
Siraj Raval
5 Reasons to Build Decentralized Apps
Siraj Raval
The Interplanetary File System
Siraj Raval
How to Build a Dapp in 3 min
Siraj Raval
Life Before Smartphones
Siraj Raval
4 Ways to Use Smart Contracts
Siraj Raval
3 Dapps You HAVE to See
Siraj Raval
Char's Life as a BitTorrent Engineer
Siraj Raval
4 Reasons AlphaGo is a Huge Deal
Siraj Raval
Build a Neural Net in 4 Minutes
Siraj Raval
Sentiment Analysis in 4 Minutes
Siraj Raval
The Hackathon Life
Siraj Raval
Your First ML App - Machine Learning for Hackers #1
Siraj Raval
Build an AI Composer - Machine Learning for Hackers #2
Siraj Raval
Build a Game AI - Machine Learning for Hackers #3
Siraj Raval
Build a Movie Recommender - Machine Learning for Hackers #4
Siraj Raval
Build an AI Artist - Machine Learning for Hackers #5
Siraj Raval
Build a Chatbot - ML for Hackers #6
Siraj Raval
Build an AI Reader - Machine Learning for Hackers #7
Siraj Raval
Build an AI Writer - Machine Learning for Hackers #8
Siraj Raval
Build a Chatbot w/ an API - ML for Hackers #9
Siraj Raval
One-Shot Learning - Fresh Machine Learning #1
Siraj Raval
Generative Adversarial Nets - Fresh Machine Learning #2
Siraj Raval
Tone Analysis - Fresh Machine Learning #3
Siraj Raval
Generate Rap Lyrics - Fresh Machine Learning #4
Siraj Raval
Build an Autoencoder in 5 Min - Fresh Machine Learning #5
Siraj Raval
Build a Self Driving Car in 5 Min - Fresh Machine Learning #6
Siraj Raval
Build an Antivirus in 5 Min - Fresh Machine Learning #7
Siraj Raval
TensorFlow in 5 Minutes (tutorial)
Siraj Raval
Build a Recurrent Neural Net in 5 Min
Siraj Raval
Build a Simulation in 5 Min
Siraj Raval
Build a TensorFlow Image Classifier in 5 Min
Siraj Raval
Tensorboard Explained in 5 Min
Siraj Raval
Generate Music in TensorFlow
Siraj Raval
Build a Game Bot (LIVE)
Siraj Raval
Deep Learning Frameworks Compared
Siraj Raval
Introduction - Learn Python for Data Science #1
Siraj Raval
Build a Neural Network (LIVE)
Siraj Raval
Twitter Sentiment Analysis - Learn Python for Data Science #2
Siraj Raval
Recommendation Systems - Learn Python for Data Science #3
Siraj Raval
Predicting Stock Prices - Learn Python for Data Science #4
Siraj Raval
Pong Neural Network (LIVE)
Siraj Raval
Deep Dream in TensorFlow - Learn Python for Data Science #5
Siraj Raval
Visualizing Data with D3.js (LIVE)
Siraj Raval
Genetic Algorithms - Learn Python for Data Science #6
Siraj Raval
Enter Siraj [Music Video]
Siraj Raval
Build a Web Scraper (LIVE)
Siraj Raval
Why is P vs NP Important?
Siraj Raval
How to Make a Neural Network (LIVE)
Siraj Raval
How to Make an Amazing Tensorflow Chatbot Easily
Siraj Raval
How to Make an Amazing Video Game Bot Easily
Siraj Raval
How to Make a Tensorflow Neural Network (LIVE)
Siraj Raval
How to Make a Simple Tensorflow Speech Recognizer
Siraj Raval
Joel Shor - Really Quick Questions with an Awesome Google Engineer
Siraj Raval
How to Make a Path Planning Algorithm Easily (LIVE)
Siraj Raval
The Best Way to Prepare a Dataset Easily
Siraj Raval
Catherine Olsson - Really Quick Questions with an OpenAI Engineer
Siraj Raval
How to Make a Tic Tac Toe Neural Network Easily (LIVE)
Siraj Raval
More on: ML Maths Basics
View skill →
🎓
Tutor Explanation
DeepCamp AI