Second Order Optimization - The Math of Intelligence #2

Siraj Raval · Beginner ·🛠️ AI Tools & Apps ·9y ago

Key Takeaways

The video covers second order optimization techniques, specifically Newton's method, and its application in machine learning, with a focus on the mathematical foundations and implementation in Python. It also discusses the differences between first and second order optimization methods, including gradient descent and its variants.

Full Transcript

[Music] hello world it's SJ and let's talk about [Music] optimization there are thousands of languages spoken across the world each one unique its ability to represent Concepts and convey ideas but there is one language that is shared by All Humans regardless of where you come from mathematics no matter your culture or your age you possess the ability to understand this language of numbers that connects us all across continents and time like all languages fluency requires practice but unlike any other language the more fluent you become in math the more Unstoppable you'll be in anything you want to do in life math is happening all around us to a degree that most people don't realize we can think of everything as a set of variables as metrics and there exists relations between all of these variables in math we call these relations functions it's our way of representing a set of patterns a mapping a relationship between many variables variables no matter what machine learning model we use no matter what data set we use the goal of machine learning is to optimize for an objective and by doing so we are approximating a function the process of optimization helps us iteratively discover the functions hidden in the depths of data last week we talked about a popular optimization technique called gradient descent this can be broken down in into a fstep process first we Define some machine learning model with a set of initial weight values these act as the coefficients of the function that the model represents the mapping between input data and output predictions these values are naive we have no idea what they should actually be but we're trying to discover the optimal ones we'll Define an error function and when we plot the graph of the relationship between all the possible error values and all the possible weight values for our function we'll see that there exists a valley a Minima we'll use our error to help us compute the partial derivative with respect to each weight value we have and this gives us our gradient the gradient represents the change in the error when the weights are changed by a very small value Val from their original value we use the gradient to update the values of our weights in a direction such that the error is minimized iteratively coming closer and closer to the Minima of the function we step our solution in the negative direction of the gradient repeatedly when we reach it we have learned the optimal weight values for our model where our gradient is equal to zero our model will then be able to make predictions for input data it's never seen before most optimization problems can be solved using gradient descent and its variance they all fall into a category called first order optimization methods we call them first order because they only require us to compute the first derivative but there's another class of techniques that aren't as widely used called second order optimization methods that require us to compute the second derivative the first derivative tells us if the function is increasing or decreasing at a certain point and the second derivative tells us if the first derivative is increasing or decreasing which hints at its curvature first order methods provide us with a line that is tangential to a point on an air surface and second order methods provide us with a quadratic surface that kisses the curvature of the error surface H get a room you two the advantage then of second order methods is that they don't ignore the curvature of the eror surface and in terms of stepwise performance they are better let's look at a popular second order optimization technique called Newton's method named after the dude who invented calculus whose name [Music] was there are actually two versions of Newton's method the first version is for find the roots of a polinomial all those points where it intersects with the xais so if you threw a ball and recorded its trajectory finding the root of the equation would tell you exactly what time it hits the ground the second version is for optimization and it's what we use in machine learning but let's code the root finding version first to develop some basic intuition hey you must be Eloise ofl p and you must be S the data scientist yeah hi good to meet you good to meet you too any Revelations on how to predict our neuron network not yet this is going to be quite a challenge we've got to perform anomaly detection on a 500 tab data set I was thinking we just use Newton's method to perform logistic regression first to see if we can predict particles nun method yeah it's a form of second order optimization I already built a prototype it's been training for the past few hours I'm impressed machine learner you should be let's say we have a function F ofx and some initial guessed solution Newton's method says that we first find the slope of the tangent line at our guess point then find the point at which the tangent line intersects the xaxis we'll use that point to find its projection in the original function then we iterate again from our first step this time replacing our first point with this one we keep iterating and eventually we'll stop when our current value X is less than or equal to a threshold so that's the root finding version of Newton's method where we're trying to find where the function equals zero but in the optimization version we're trying to find where the derivative of the function equals z it's minimum at a high level given a random starting location we construct a quadratic approximation to the objective function that matches the first and second derivative values at the that point and then we minimize that quadratic function instead of the original function the minimizer of the quadratic function is used as the starting point in the next step and we repeat this process iteratively okay so let's go over two cases of Newton's method for optimization to learn more a 1D case and a 2d case in the first case we've got a 1dimensional function we can obtain a quadratic approximation at a given point of the function using what's called a tailor series expansion neglecting terms of order three or higher a tailor series is a representation of a function as an infinite sum of terms that are calculated from the values of the function's derivatives at a single point it was invented by an English mathematician named Brooke Taylor Swift just kidding so we take the second order tailor series for our initial point x and minimize it by finding the first derivative and second derivative and equating them to zero in order to find the minimum x value we iterate this process in the second case let's say we've got a function of multiple Dimensions we can find the minimum of it using the same approach except for two changes we replace the first derivatives with a gradient and the second derivatives with a hessen a hessen is a matrix of the second order partial derivatives of a scalar and it describes the local curvature of a multivariable function check this out derivatives help us compute gradients which we can represent using a Jacobian matrix for first order optimization and we can use the hesen for second order optimization these are four of the five derivative operators used in all of calculus they're the ways that we organize and represent change numerically so when should you use a second order method first order methods are usually less computationally expensive to compute and less time expensive converging pretty fast on large data sets second order methods are faster when the second derivative is known and easy to compute but the second derivative is often intractable to compute requiring lots of computation for certain problems gradient descent can get stuck along paths of slow convergence around saddle points whereas second order methods won't trying out different optimization techniques for your specific problem is the best way to see what works best here are the key points to remember from this video first order optimization techniques use the first derivative of a function to minimize it second order optimization techniques use a second derivative the Jacobian is a matrix of first partial derivatives and the hessen is a matrix of second partial derivatives and Newton's method is a popular second order optimization technique that can sometimes outperform gradient descent last week's coding challenge winner is Alberto gares Alberto used gradient descent to find the line of best fit his Jupiter notebook is insanely detailed you could learn gradient descent just from reading it alone very well thought out that was dope Alberto wizard of the week and the runner up is Ivan gusev who implemented gradient descent from scratch for polinomial of any order this week's challenge is to implement Newton's method for optimizations from scratch details are in the read me post your GitHub Link in the comments and winners will be announced next week please subscribe for more programming videos and for now I've got to invent the six derivative so thanks for watching

Original Description

Gradient Descent and its variants are very useful, but there exists an entire other class of optimization techniques that aren't as widely understood. We'll learn about second order method variants, how they compare to first order methods, and implement our own in Python. Code for this video (with challenge): https://github.com/llSourcell/Second_Order_Optimization_Newtons_Method Alberto's Winning Code: https://github.com/alberduris Ivan's Runner up Code: https://github.com/PiaFraus Please Subscribe! And like. And comment. That's what keeps me going. Course Syllabus: https://github.com/llSourcell/The_Math_of_Intelligence More learning resources: https://web.stanford.edu/class/msande311/lecture13.pdf https://www.cs.toronto.edu/~hinton/csc2515/notes/lec6tutorial.pdf https://www.quora.com/In-mathematical-optimization-problems-the-first-derivative-is-often-used-Why-not-the-second-or-higher-order-derivatives https://en.wikipedia.org/wiki/Newton%27s_method_in_optimization https://www.youtube.com/watch?v=28BMpgxn_Ec&t=444s https://www.youtube.com/watch?v=42zJ5xrdOqo&t=438s Join us in the Wizards Slack channel: http://wizards.herokuapp.com/ And please support me on Patreon: https://www.patreon.com/user?u=3191693 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 second order optimization techniques, including Newton's method, and its application in machine learning. It covers the mathematical foundations of optimization and provides a practical implementation in Python. By watching this video, viewers will gain a deeper understanding of optimization techniques and how to apply them to real-world problems.

Key Takeaways
  1. Find the slope of the tangent line at the guess point
  2. Find the point at which the tangent line intersects the x-axis
  3. Use the point to find its projection in the original function
  4. Iterate again from the first step, replacing the first point with the new point
  5. Minimize the quadratic function instead of the original function
  6. Implement Newton's method in Python
💡 Second order optimization techniques, such as Newton's method, can be more efficient and effective than first order methods, such as gradient descent, for certain types of problems.

Related Reads

📰
AI Interview Copilot vs AI Mock Interview Tool: Which One Do Developers Need?
Learn the difference between AI Interview Copilot and AI Mock Interview Tool to boost developer interview performance
Dev.to AI
📰
I Automated My Developer Portfolio (and You Can Too)
Automate your developer portfolio with DevCard AI, which generates a styled and interactive portfolio based on your GitHub profile or resume
Dev.to AI
📰
How to Check If ChatGPT and Perplexity Actually Recommend Your Website (I Wrote a Script to Find…
Learn to verify if AI search engines like ChatGPT recommend your website using a Python script
Medium · Python
📰
Building a voice-first database: why we chose speech over spreadsheets
Learn how to build a voice-first database using AI, and why it's a game-changer for non-technical users who hate spreadsheets
Dev.to AI
Up next
AI Periodic Table Explained | Complete AI Tools & Concepts Map
Pavithra’s Podcast
Watch →