Backpropagation in 5 Minutes (tutorial)

Siraj Raval · Beginner ·📐 ML Fundamentals ·9y ago

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 What is Bitcoin?
What is Bitcoin?
Siraj Raval
2 5 Ways to Use Bitcoin
5 Ways to Use Bitcoin
Siraj Raval
3 BTC Fever - Siraj [Music Video]
BTC Fever - Siraj [Music Video]
Siraj Raval
4 5 Reasons to Build Decentralized Apps
5 Reasons to Build Decentralized Apps
Siraj Raval
5 The Interplanetary File System
The Interplanetary File System
Siraj Raval
6 How to Build a Dapp in 3 min
How to Build a Dapp in 3 min
Siraj Raval
7 Life Before Smartphones
Life Before Smartphones
Siraj Raval
8 4 Ways to Use Smart Contracts
4 Ways to Use Smart Contracts
Siraj Raval
9 3 Dapps You HAVE to See
3 Dapps You HAVE to See
Siraj Raval
10 Char's Life as a BitTorrent Engineer
Char's Life as a BitTorrent Engineer
Siraj Raval
11 4 Reasons AlphaGo is a Huge Deal
4 Reasons AlphaGo is a Huge Deal
Siraj Raval
12 Build a Neural Net in 4 Minutes
Build a Neural Net in 4 Minutes
Siraj Raval
13 Sentiment Analysis in 4 Minutes
Sentiment Analysis in 4 Minutes
Siraj Raval
14 The Hackathon Life
The Hackathon Life
Siraj Raval
15 Your First ML App - Machine Learning for Hackers #1
Your First ML App - Machine Learning for Hackers #1
Siraj Raval
16 Build an AI Composer - Machine Learning for Hackers #2
Build an AI Composer - Machine Learning for Hackers #2
Siraj Raval
17 Build a Game AI - Machine Learning for Hackers #3
Build a Game AI - Machine Learning for Hackers #3
Siraj Raval
18 Build a Movie Recommender - Machine Learning for Hackers #4
Build a Movie Recommender - Machine Learning for Hackers #4
Siraj Raval
19 Build an AI Artist - Machine Learning for Hackers #5
Build an AI Artist - Machine Learning for Hackers #5
Siraj Raval
20 Build a Chatbot - ML for Hackers #6
Build a Chatbot - ML for Hackers #6
Siraj Raval
21 Build an AI Reader - Machine Learning for Hackers #7
Build an AI Reader - Machine Learning for Hackers #7
Siraj Raval
22 Build an AI Writer - Machine Learning for Hackers #8
Build an AI Writer - Machine Learning for Hackers #8
Siraj Raval
23 Build a Chatbot w/ an API - ML for Hackers #9
Build a Chatbot w/ an API - ML for Hackers #9
Siraj Raval
24 One-Shot Learning - Fresh Machine Learning #1
One-Shot Learning - Fresh Machine Learning #1
Siraj Raval
25 Generative Adversarial Nets - Fresh Machine Learning #2
Generative Adversarial Nets - Fresh Machine Learning #2
Siraj Raval
26 Tone Analysis - Fresh Machine Learning #3
Tone Analysis - Fresh Machine Learning #3
Siraj Raval
27 Generate Rap Lyrics - Fresh Machine Learning #4
Generate Rap Lyrics - Fresh Machine Learning #4
Siraj Raval
28 Build an Autoencoder in 5 Min - Fresh Machine Learning #5
Build an Autoencoder in 5 Min - Fresh Machine Learning #5
Siraj Raval
29 Build a Self Driving Car in 5 Min - Fresh Machine Learning #6
Build a Self Driving Car in 5 Min - Fresh Machine Learning #6
Siraj Raval
30 Build an Antivirus in 5 Min - Fresh Machine Learning #7
Build an Antivirus in 5 Min - Fresh Machine Learning #7
Siraj Raval
31 TensorFlow in 5 Minutes (tutorial)
TensorFlow in 5 Minutes (tutorial)
Siraj Raval
32 Build a Recurrent Neural Net in 5 Min
Build a Recurrent Neural Net in 5 Min
Siraj Raval
33 Build a Simulation in 5 Min
Build a Simulation in 5 Min
Siraj Raval
34 Build a TensorFlow Image Classifier in 5 Min
Build a TensorFlow Image Classifier in 5 Min
Siraj Raval
35 Tensorboard Explained in 5 Min
Tensorboard Explained in 5 Min
Siraj Raval
36 Generate Music in TensorFlow
Generate Music in TensorFlow
Siraj Raval
37 Build a Game Bot (LIVE)
Build a Game Bot (LIVE)
Siraj Raval
38 Deep Learning Frameworks Compared
Deep Learning Frameworks Compared
Siraj Raval
39 Introduction - Learn Python for Data Science #1
Introduction - Learn Python for Data Science #1
Siraj Raval
40 Build a Neural Network (LIVE)
Build a Neural Network (LIVE)
Siraj Raval
41 Twitter Sentiment Analysis - Learn Python for Data Science #2
Twitter Sentiment Analysis - Learn Python for Data Science #2
Siraj Raval
42 Recommendation Systems - Learn Python for Data Science #3
Recommendation Systems - Learn Python for Data Science #3
Siraj Raval
43 Predicting Stock Prices - Learn Python for Data Science #4
Predicting Stock Prices - Learn Python for Data Science #4
Siraj Raval
44 Pong Neural Network (LIVE)
Pong Neural Network (LIVE)
Siraj Raval
45 Deep Dream in TensorFlow - Learn Python for Data Science #5
Deep Dream in TensorFlow - Learn Python for Data Science #5
Siraj Raval
46 Visualizing Data with D3.js (LIVE)
Visualizing Data with D3.js (LIVE)
Siraj Raval
47 Genetic Algorithms - Learn Python for Data Science #6
Genetic Algorithms - Learn Python for Data Science #6
Siraj Raval
48 Enter Siraj [Music Video]
Enter Siraj [Music Video]
Siraj Raval
49 Build a Web Scraper (LIVE)
Build a Web Scraper (LIVE)
Siraj Raval
50 Why is P vs NP Important?
Why is P vs NP Important?
Siraj Raval
51 How to Make a Neural Network (LIVE)
How to Make a Neural Network (LIVE)
Siraj Raval
52 How to Make an Amazing Tensorflow Chatbot Easily
How to Make an Amazing Tensorflow Chatbot Easily
Siraj Raval
53 How to Make an Amazing Video Game Bot Easily
How to Make an Amazing Video Game Bot Easily
Siraj Raval
54 How to Make a Tensorflow Neural Network (LIVE)
How to Make a Tensorflow Neural Network (LIVE)
Siraj Raval
55 How to Make a Simple Tensorflow Speech Recognizer
How to Make a Simple Tensorflow Speech Recognizer
Siraj Raval
56 Joel Shor - Really Quick Questions with an Awesome Google Engineer
Joel Shor - Really Quick Questions with an Awesome Google Engineer
Siraj Raval
57 How to Make a Path Planning Algorithm Easily (LIVE)
How to Make a Path Planning Algorithm Easily (LIVE)
Siraj Raval
58 The Best Way to Prepare a Dataset Easily
The Best Way to Prepare a Dataset Easily
Siraj Raval
59 Catherine Olsson - Really Quick Questions with an OpenAI Engineer
Catherine Olsson - Really Quick Questions with an OpenAI Engineer
Siraj Raval
60 How to Make a Tic Tac Toe Neural Network Easily (LIVE)
How to Make a Tic Tac Toe Neural Network Easily (LIVE)
Siraj Raval

This video teaches the basics of backpropagation in neural networks, including the necessary calculus concepts, and demonstrates how to implement it programmatically. By watching this video, viewers will gain a deeper understanding of how neural networks learn and make predictions.

Key Takeaways
  1. Initialize weights as matrices with random values
  2. Compute the dot product of the input and weight matrix
  3. Apply an activation function to the resulting values
  4. Compute the error value by comparing the prediction to the expected output
  5. Use calculus to compute the partial derivatives of the error function with respect to each weight
  6. Apply the chain rule to compute the gradient of the error with respect to each weight
  7. Update the weights incrementally using gradient descent
💡 Backpropagation is an essential concept in neural networks, and understanding the calculus behind it is crucial for building and training effective models.

Related Reads

📰
Understanding Transformers (Part 2): Why Backpropagation Broke Recurrent Neural Networks
Learn why backpropagation through time broke recurrent neural networks and how it led to the development of transformers
Medium · Data Science
📰
CS-NRRM™: A Practical Implementation of AI-Readable Longitudinal Data Infrastructure
Learn how to implement a practical AI-readable longitudinal data infrastructure using CS-NRRM, a framework for preserving continuity in large datasets
Medium · Data Science
📰
Learn why basic statistics knowledge is not enough for probability in machine learning and what you need to know instead
Medium · Machine Learning
📰
Learn why basic statistics knowledge is not enough for probability in machine learning and what you need to know instead
Medium · Data Science
Up next
Bloom Filters: Probably Yes, Definitely No
DataMListic
Watch →