How optimization for machine learning works, part 4

Brandon Rohrer · Intermediate ·📐 ML Fundamentals ·7y ago

Key Takeaways

Optimization techniques for machine learning, including gradient descent and stochastic gradient descent, are demonstrated using Python and scikit-learn libraries.

Full Transcript

now that we've laid all the groundwork we can take things up a level and see how to apply optimization in a three parameter model by looking at another M&M data set we noticed when fitting our line that not every bag had identical M&M weights some of them were noticeably higher and some noticeably lower than others we suspect there might be an underlying pattern here and our suspicions are further aroused when we talk to some of our friends that work at the M&M factory we learned that they upgraded their equipment during the period that our M&Ms were being sampled from but we don't know exactly when if there was a change in M&M weight due to the change in equipment we would expect to see a jump at some point with the M&Ms coming after the jump having a consistently higher or lower weight when we plot the average M&M weight in the package against the manufacturing date of that package we see that there probably is a small difference there are three questions the Steven could help us answer what was the typical M&M weight before the change what was the typical M&M weight after the change and when did the change occur there are lots of generic models available these are models like linear regression or decision trees that don't know anything about where our data came from what it means or how we got it in this example we can do a little bit better than these naive models we have some extra information that helps us to make a better guess at what our model should look like given what we know it's reasonable to hypothesize a model with a constant M&M weight before and after the equipment change and a step change between them this is a three parameter model there's three numbers we need to define it completely the weight before the change the weight after and the date of the change for consistency we'll keep our loss function as the square of the deviation between our model and the day we can now perform an exhaustive search again over all possible combinations of all plausible parameter values but notice that it's taking a lot longer to compute with each additional parameter and we can get a four dimensional loss function plotting a three dimensional function is tricky but plotting a four dimensional function is really hard but we'll give it a try one trick we can use is to choose a specific value for one of our parameters what's left is a three dimensional loss function this lets us experiment and get a feel for how those two parameters interact where the sweet spot is for both of them and then we can see at least for this value of the third parameter what combination of the other two parameters produces the lowest loss in our case we have fixed the day on which the equipment was swapped out and are varying the other two parameters the weight of an M&M before the change and the weight of an M&M after the loss function helps us see what the best estimates are for these at least if we assume this particular equipment shipped today another trick we can use is to take the parameter that we chose a fixed value for our shift day and increment it just a little bit we can do this again and again and get a series of three dimensional plots what this does is to take one of our dimensions and spread it out across time we can use time to represent our fourth dimension if you look very carefully as the shift day progresses through its values you can see the bottom of our bowls jump up and down this shows how the lowest possible value for the loss function changes depending on the ship today we're looking for the very lowest of all possible values that is the optimal estimate it depends not only on our estimate of mmm weight before and after the shift day but also on our choice of the shift today there are three separate choices we have to make three separate dials we have to spin to find the best answer if you watch the bottom of the bowl very carefully as the shift day progresses you can see that it reaches a low point then jumps back up again then reaches another low point this means the gradient descent will give us different answers depending on where we start the answer is sensitive to our initial guess this is an example of non convex loss function as we mentioned before there are a few ways to handle this we won't go into them in detail here but one method that's good to have in your bag of tricks is this scattershot approach that involves choosing several random starting points and running gradient descent from each of them once you discover that several of them are ending up in the same place or the several of them are giving the same minimum value then you can be reasonably confident that you have a good answer the more random starting points you pick and the more times you choose a random starting point and try again the better your chances of finding the global optimum although you'll never be able to guarantee that you have the absolute best answer it's worth keeping in mind however that in machine learning applications we are dealing with data that's assumed to have some noise on it and then we will be applying our model to future data points that will have their own unique patterns of noise in them it's not worth our time to worry about getting the exact perfect global optimum for our purposes getting an answer that is just as good as we can get is usually good enough this one concept of looking for the lowest point in an error function is all you need to survive an optimization per machine learning it takes a lot of different forms can extend to many more parameters it can be used with generic models like logistic regression where there are just a few parameter so are a few dozen of them it can be used with very domain-specific models such as macroeconomics or agriculture the optimization method doesn't know the difference in either case all it does is tries to find the right combination of values for getting the lowest loss optimization is also at the heart of deep neural networks of success it's how all of the weights making all of the between layer connections from node to node are chosen in these models there can be thousands or even millions of parameters the loss function is not only impossible to draw on the sheet of paper it's mind-boggling to even contemplate but the math behind optimization carries on anyway doing its thing and seeking out the lowest points of the loss function that it can find a quirky thing about optimization in deep neural networks is that the loss function is known to have many dips like a many dimensional egg crate mattress depending on where you drop a marble it will very likely land in a different divot every time deep neural networks can take a long time to Train and in practice they're often trained only once starting from a single random set of initial weights that means that of all the millions of low points available on the loss function we are only going to find one this may be one of the reasons that training neural networks is such an art it's not quite as bleak as it sounds however some recent research suggests that most of these divots have the same depth like an actual egg crate mattress at least for certain types of neural networks so maybe the multitude of local minima is not as much of an issue as it might appear at first glance another part to the art of deep neural networks is the alphabet soup of gradient descent methods as you read about it you'll hear names like SGD stochastic gradient descent and AG Nesterov accelerated gradient Adam or ADA grad all of these are embellishments of gradient descent they differ in the fine details about exactly how far to step and in precisely which direction some work a little better for some problems than others but mostly they just change how fast the neural network converges to the final answer in every case the underlying goal is the same starting from a random place in a many dimensional landscape of hills and valleys find the lowest point in the nearest valley going downhill the other type of optimization that occurs in deep neural networks is hyper parameter tuning this is still just optimization but instead of optimizing the many parameters in the model it's actually optimizing the values of some of the high-level settings that determine how the model works in some cases determining how gradient descent is performed if decorating a house you can think of model training as optimization where you're choosing exactly where within a room the couch in the bookshelf and the picture should be and how they should be oriented hyper parameter tuning is the next level up it's deciding which room they should even be in to start with because testing a new set of hyper parameter values requires retraining the model entirely from scratch it takes a long time and usually a lot of computer's computational intensity has driven creative machine learning practitioners to find ways to try as few hyper parameter combinations as possible however the underlying goal and the underlying problem are still the same find the combination of values that gives the lowest loss get to the bottom of the deepest Valley you can find great job sticking in there to the end thanks for hanging in there with me I hope the concept of optimization is a little bit less scary now and then you have an intuitive sense of what's going on under the hood when the machine learning algorithms being trained good luck on building your next project

Original Description

Part of the End-to-End Machine Learning School course library at http://e2eml.school See these concepts used in an End to End Machine Learning project: https://end-to-end-machine-learning.teachable.com/p/polynomial-regression-optimization/ Watch the rest of the How Optimization Works series: https://end-to-end-machine-learning.teachable.com/p/building-blocks-how-optimization-works/
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Brandon Rohrer · Brandon Rohrer · 36 of 60

1 Robot Learning with a Biologically-Inspired Brain (BECCA)
Robot Learning with a Biologically-Inspired Brain (BECCA)
Brandon Rohrer
2 BECCA talk at AGI 2011
BECCA talk at AGI 2011
Brandon Rohrer
3 Robot Learning with a Biologically-Inspired Brain (BECCA), The Sequel
Robot Learning with a Biologically-Inspired Brain (BECCA), The Sequel
Brandon Rohrer
4 BECCA listens to The Hobbit
BECCA listens to The Hobbit
Brandon Rohrer
5 Learning the building blocks of speech: BECCA extracts a hierarchy of audio features
Learning the building blocks of speech: BECCA extracts a hierarchy of audio features
Brandon Rohrer
6 BECCA listens for sound effects in The Hobbit
BECCA listens for sound effects in The Hobbit
Brandon Rohrer
7 BECCA finds movie trailers while watching the Big Bang Theory
BECCA finds movie trailers while watching the Big Bang Theory
Brandon Rohrer
8 Listening for unexpected sounds: BECCA detects anomalies in audio data
Listening for unexpected sounds: BECCA detects anomalies in audio data
Brandon Rohrer
9 Learning the building blocks of vision: BECCA extracts a spatio-temporal hierarchy of features
Learning the building blocks of vision: BECCA extracts a spatio-temporal hierarchy of features
Brandon Rohrer
10 Watching for the unexpected: BECCA detects anomalies in video data
Watching for the unexpected: BECCA detects anomalies in video data
Brandon Rohrer
11 BECCA finds a stationary target
BECCA finds a stationary target
Brandon Rohrer
12 BECCA finds a stationary target at 3X speed
BECCA finds a stationary target at 3X speed
Brandon Rohrer
13 BECCA watches the X-men and Bruce Lee
BECCA watches the X-men and Bruce Lee
Brandon Rohrer
14 BECCA plays Quidditch
BECCA plays Quidditch
Brandon Rohrer
15 BECCA chases a ball
BECCA chases a ball
Brandon Rohrer
16 BECCA chases a ball, part 2
BECCA chases a ball, part 2
Brandon Rohrer
17 Becca chases a ball, part 3
Becca chases a ball, part 3
Brandon Rohrer
18 BECCA creates features from MNIST
BECCA creates features from MNIST
Brandon Rohrer
19 How reinforcement learning works in Becca 7
How reinforcement learning works in Becca 7
Brandon Rohrer
20 Deep Learning Demystified
Deep Learning Demystified
Brandon Rohrer
21 How Data Science Works
How Data Science Works
Brandon Rohrer
22 How Convolutional Neural Networks work
How Convolutional Neural Networks work
Brandon Rohrer
23 How Bayes Theorem works
How Bayes Theorem works
Brandon Rohrer
24 How Deep Neural Networks Work
How Deep Neural Networks Work
Brandon Rohrer
25 Recurrent Neural Networks (RNN) and Long Short-Term Memory (LSTM)
Recurrent Neural Networks (RNN) and Long Short-Term Memory (LSTM)
Brandon Rohrer
26 How Support Vector Machines work / How to open a black box
How Support Vector Machines work / How to open a black box
Brandon Rohrer
27 How autocorrelation works
How autocorrelation works
Brandon Rohrer
28 Getting closer to human intelligence through robotics
Getting closer to human intelligence through robotics
Brandon Rohrer
29 A minimalist's guide to slicing and indexing pandas DataFrames
A minimalist's guide to slicing and indexing pandas DataFrames
Brandon Rohrer
30 How decision trees work
How decision trees work
Brandon Rohrer
31 Data scientist archetypes
Data scientist archetypes
Brandon Rohrer
32 How to use python's datetime package
How to use python's datetime package
Brandon Rohrer
33 How optimization for machine learning works, part 1
How optimization for machine learning works, part 1
Brandon Rohrer
34 How optimization for machine learning works, part 2
How optimization for machine learning works, part 2
Brandon Rohrer
35 How optimization for machine learning works, part 3
How optimization for machine learning works, part 3
Brandon Rohrer
How optimization for machine learning works, part 4
How optimization for machine learning works, part 4
Brandon Rohrer
37 How convolutional neural networks work, in depth
How convolutional neural networks work, in depth
Brandon Rohrer
38 How to pick a machine learning model 4: Splitting the data
How to pick a machine learning model 4: Splitting the data
Brandon Rohrer
39 How to pick a machine learning model 3: Choosing a loss function
How to pick a machine learning model 3: Choosing a loss function
Brandon Rohrer
40 How to pick a machine learning model 2: Separating signal from noise
How to pick a machine learning model 2: Separating signal from noise
Brandon Rohrer
41 How to pick a machine learning model 1: Choosing between models
How to pick a machine learning model 1: Choosing between models
Brandon Rohrer
42 How to pick a machine learning model 5: Navigating assumptions
How to pick a machine learning model 5: Navigating assumptions
Brandon Rohrer
43 What do neural networks learn?
What do neural networks learn?
Brandon Rohrer
44 Interview with iRobot's Director of Data Science Angela Bassa
Interview with iRobot's Director of Data Science Angela Bassa
Brandon Rohrer
45 How Backpropagation Works
How Backpropagation Works
Brandon Rohrer
46 Evolutionary Powell's method: A discrete optimizer for hyperparameter optimization
Evolutionary Powell's method: A discrete optimizer for hyperparameter optimization
Brandon Rohrer
47 1D convolution for neural networks, part 1: Sliding dot product
1D convolution for neural networks, part 1: Sliding dot product
Brandon Rohrer
48 1D convolution for neural networks, part 2: Convolution copies the kernel
1D convolution for neural networks, part 2: Convolution copies the kernel
Brandon Rohrer
49 1D convolution for neural networks, part 3: Sliding dot product equations longhand
1D convolution for neural networks, part 3: Sliding dot product equations longhand
Brandon Rohrer
50 1D convolution for neural networks, part 4: Convolution equation
1D convolution for neural networks, part 4: Convolution equation
Brandon Rohrer
51 1D convolution for neural networks, part 5: Backpropagation
1D convolution for neural networks, part 5: Backpropagation
Brandon Rohrer
52 1D convolution for neural networks, part 6: Input gradient
1D convolution for neural networks, part 6: Input gradient
Brandon Rohrer
53 1D convolution for neural networks, part 7: Weight gradient
1D convolution for neural networks, part 7: Weight gradient
Brandon Rohrer
54 1D convolution for neural networks, part 8: Padding
1D convolution for neural networks, part 8: Padding
Brandon Rohrer
55 1D convolution for neural networks, part 9: Stride
1D convolution for neural networks, part 9: Stride
Brandon Rohrer
56 The Four Grand Challenges of Robots in the Home
The Four Grand Challenges of Robots in the Home
Brandon Rohrer
57 How Convolution Works
How Convolution Works
Brandon Rohrer
58 The Softmax neural network layer
The Softmax neural network layer
Brandon Rohrer
59 Batch normalization
Batch normalization
Brandon Rohrer
60 Getting ready to learn Python, Mac edition #1: Files and directories
Getting ready to learn Python, Mac edition #1: Files and directories
Brandon Rohrer

This video explains how optimization works in machine learning, covering topics such as gradient descent and stochastic gradient descent. It provides a practical example using Python and scikit-learn libraries.

Key Takeaways
  1. Import necessary libraries
  2. Define a machine learning model
  3. Implement gradient descent
  4. Evaluate model performance
  5. Compare results with stochastic gradient descent
💡 Gradient descent is a key optimization technique in machine learning, and stochastic gradient descent can be used to improve model performance.

Related Reads

Up next
Generative vs Discriminative Models - Explained
DataMListic
Watch →