Vectorizing Logistic Regression's Gradient Computation (C1W2L14)

DeepLearningAI · Beginner ·🔢 Mathematical Foundations ·8y ago

Key Takeaways

Vectorizing logistic regression's gradient computation using NumPy, implementing a highly efficient version of gradient descent without explicit for loops over training examples, and deriving the equations for the vectorized implementation of DB and DW.

Full Transcript

in the previous video you saw how you can use vectorization to compute the predictions the lowercase A's for an entire training set all sort of at the same time in this video you see how you can use vectorization to also perform the gradient computations for all M training examples again all sort of at the same time and then at the end of this video we'll pull all together and show how you can derive a very efficient implementation of logistic regression so you remember that for the gradient computation what we did was we computed DZ one for the first example riskily a one minus y one and then DZ 2 equals a 2 minus y 2 and so on so on and so on for all my training examples so what we're going to do is define a new variable D capital Z is going to be DZ 1 DZ 2 right DZ m again all the D lowercase D variables stacked horizontally so this would be a 1 by M matrix or alternative VA M dimensional real vector now recall that from the previous slide we'd already figured out how to compute capital a which was this a 1 through am and we had defined Capital y as y 1 through ym also you know stacked horizontally so based on these definitions um maybe you could see for yourself that DZ can be computed as just a minus y because this is going to be equal to now a 1 minus y1 through the first element a2 minus y2 in the second element and so on and so and so this first elements a 1 minus y1 is exactly the definition of DZ 1 the second element is exactly definitely Z 2 and so on so with just one line of code you can compute all of this at the same time now in the previous implementation we've gotten rid of one for loop already but we still had this second for loop over training examples so we initialize DW to zero to the vector of zeros but then we still have to loop over training examples where we have PW plus equals x1 times d z1 for the first training example DW plus equals x2 DZ 2 and so on so we do the M times and then you know DW 2y equals by M and similarly for B write DP was initialized as 0 and then DB plus equals V Z 1 DP plus equals D Z 2 down to you know DZ m and then D be divided equally M so that's what we had in the previous implementation we'd already gotten rid of one for loop so le is now DW is a vector and we went separately you know updating DW 1 DW 2 and so on so we've gotten rid of that already but we still have a for loop over the m examples in the training set so let's think these operations in vectorize them here's what we can do for the vectorized implementation of DB what is doing is basically summing up all of these disease and then dividing by M so DB is basically 1 over m sum from I equals 1 through m of DZ I and well all the disease are in that a row vector and so in Python what you do is in Flynn you know 1 over m times n P dot sum of D Z right so just take the variable and call the NP but some function on it and that would give you DB how about DW I'll just write out the correct equations you can verify the right thing to do DW x will be one of M times the matrix x times DZ transpose and to kind of see why that's the case this is equal to one of M then the matrix X is X 1 through X M right stacked up in columns like that and DZ transpose is going to be DZ one down to DZ m like so and so if you figure out what this matrix times this vector works out to be this turns out to be one over m times X 1 DZ 1 plus dot dot dot plus XM DZ m right and so this is a n by 1 vector and this is what you actually end up with what DW because DW was taking these you know x ID zi and adding them up and so that's what exactly this matrix vector multiplication is doing and so again with one line of code you can compute G W so the vectorized implementation of the difference calculations is just this you use this line to implement DB and use this line to influence DW and notice that without a for loop over the training sites you can now compute the updates you want to your parameters so now let's put all together into how you would actually implement logistic regression so this is our original highly inefficient non vectorized implementation where um so the first thing we've done in the previous video was get rid of this volume right so instead of looping over DW 1 DW 2 and so on we have replaced this with a vector value DW and just say this is DW + equals X I which is now a vector times DZ I but now we'll see that we can also get rid of not just a for loop below but also get rid of this volume so here's how you do it so using what we had from the previous slides you would say Capital Z is equal to W transpose X plus B and code you write is capital Z equals NP dot W transpose X plus B and then a equals sigmoid of capital T so you've now computes it all of this and all of this for all the values of iron mix on the previous line we said you would compute DZ equals capital A minus capital y so now you compute all of this for all the values of I then finally DW equals 1 over M X D Z transpose and DB equals one of em you know and P dot some DZ so you've just done for propagation and back propagation really computing the predictions and computing the derivatives on all M training examples without using a for loop and so the gradient descent update then would be you know W gets updated as W minus the learning rate times DW is just computed above and B is updated as B minus a learning rate times DB so sometimes it's really cool inserted it notices on assignment but I guess I haven't been totally consistent to that notation but with this you have just implemented a single iteration of gradient descent for logistic regression now I know I said that we should get rid of explicit folders whenever you can but if you want to implement multiple iterations of gradient descent then you still need a for loop over the number of iterations so you if you want to have a thousand iterations of gradient you might still need a full loop over the iteration number because and also most folders like that and I don't think there's any way to get rid of that for loop but I do think it's incredibly cool that you can implement at least one iteration of gradient descent without needing to use a full loop so that's it you now have a highly vectorize and highly efficient implementation of gradient descent or register direction there's just some one more detail that I want to talk about in the next video which is in our description here I briefly alluded to this technique called broadcasting broadcasting turns out to be a technique that pison an umpire allows you to use to make certain parts of your code also much more efficient so let's see what some more details of broadcasting in the next video

Original Description

Take the Deep Learning Specialization: http://bit.ly/3crP5ti Check out all our courses: https://www.deeplearning.ai Subscribe to The Batch, our weekly newsletter: https://www.deeplearning.ai/thebatch Follow us: Twitter: https://twitter.com/deeplearningai_ Facebook: https://www.facebook.com/deeplearningHQ/ Linkedin: https://www.linkedin.com/company/deeplearningai
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from DeepLearningAI · DeepLearningAI · 0 of 60

← Previous Next →
1 Forward and Backward Propagation (C1W4L06)
Forward and Backward Propagation (C1W4L06)
DeepLearningAI
2 deeplearning.ai's Heroes of Deep Learning: Yuanqing Lin
deeplearning.ai's Heroes of Deep Learning: Yuanqing Lin
DeepLearningAI
3 deeplearning.ai's Heroes of Deep Learning: Ruslan Salakhutdinov
deeplearning.ai's Heroes of Deep Learning: Ruslan Salakhutdinov
DeepLearningAI
4 deeplearning.ai's Heroes of Deep Learning: Yoshua Bengio
deeplearning.ai's Heroes of Deep Learning: Yoshua Bengio
DeepLearningAI
5 deeplearning.ai's Heroes of Deep Learning: Pieter Abbeel
deeplearning.ai's Heroes of Deep Learning: Pieter Abbeel
DeepLearningAI
6 deeplearning.ai's Heroes of Deep Learning: Ian Goodfellow
deeplearning.ai's Heroes of Deep Learning: Ian Goodfellow
DeepLearningAI
7 deeplearning.ai's Heroes of Deep Learning: Andrej Karpathy
deeplearning.ai's Heroes of Deep Learning: Andrej Karpathy
DeepLearningAI
8 Using an Appropriate Scale (C2W3L02)
Using an Appropriate Scale (C2W3L02)
DeepLearningAI
9 Gradient Checking (C2W1L13)
Gradient Checking (C2W1L13)
DeepLearningAI
10 Gradient Checking Implementation Notes (C2W1L14)
Gradient Checking Implementation Notes (C2W1L14)
DeepLearningAI
11 Learning Rate Decay (C2W2L09)
Learning Rate Decay (C2W2L09)
DeepLearningAI
12 Understanding Mini-Batch Gradient Dexcent (C2W2L02)
Understanding Mini-Batch Gradient Dexcent (C2W2L02)
DeepLearningAI
13 Mini Batch Gradient Descent (C2W2L01)
Mini Batch Gradient Descent (C2W2L01)
DeepLearningAI
14 The Problem of Local Optima (C2W3L10)
The Problem of Local Optima (C2W3L10)
DeepLearningAI
15 Exponentially Weighted Averages (C2W2L03)
Exponentially Weighted Averages (C2W2L03)
DeepLearningAI
16 Tuning Process (C2W3L01)
Tuning Process (C2W3L01)
DeepLearningAI
17 Understanding Exponentially Weighted Averages (C2W2L04)
Understanding Exponentially Weighted Averages (C2W2L04)
DeepLearningAI
18 Bias Correction of Exponentially Weighted Averages (C2W2L05)
Bias Correction of Exponentially Weighted Averages (C2W2L05)
DeepLearningAI
19 Gradient Descent With Momentum (C2W2L06)
Gradient Descent With Momentum (C2W2L06)
DeepLearningAI
20 Normalizing Activations in a Network (C2W3L04)
Normalizing Activations in a Network (C2W3L04)
DeepLearningAI
21 Hyperparameter Tuning in Practice (C2W3L03)
Hyperparameter Tuning in Practice (C2W3L03)
DeepLearningAI
22 Adam Optimization Algorithm (C2W2L08)
Adam Optimization Algorithm (C2W2L08)
DeepLearningAI
23 RMSProp (C2W2L07)
RMSProp (C2W2L07)
DeepLearningAI
24 Fitting Batch Norm Into Neural Networks (C2W3L05)
Fitting Batch Norm Into Neural Networks (C2W3L05)
DeepLearningAI
25 Why Does Batch Norm Work? (C2W3L06)
Why Does Batch Norm Work? (C2W3L06)
DeepLearningAI
26 Batch Norm At Test Time (C2W3L07)
Batch Norm At Test Time (C2W3L07)
DeepLearningAI
27 Softmax Regression (C2W3L08)
Softmax Regression (C2W3L08)
DeepLearningAI
28 Deep Learning Frameworks (C2W3L10)
Deep Learning Frameworks (C2W3L10)
DeepLearningAI
29 Neural Network Overview (C1W3L01)
Neural Network Overview (C1W3L01)
DeepLearningAI
30 Training Softmax Classifier (C2W3L09)
Training Softmax Classifier (C2W3L09)
DeepLearningAI
31 Why Deep Representations? (C1W4L04)
Why Deep Representations? (C1W4L04)
DeepLearningAI
32 Gradient Descent For Neural Networks (C1W3L09)
Gradient Descent For Neural Networks (C1W3L09)
DeepLearningAI
33 Neural Network Representations (C1W3L02)
Neural Network Representations (C1W3L02)
DeepLearningAI
34 TensorFlow (C2W3L11)
TensorFlow (C2W3L11)
DeepLearningAI
35 Activation Functions (C1W3L06)
Activation Functions (C1W3L06)
DeepLearningAI
36 Explanation For Vectorized Implementation (C1W3L05)
Explanation For Vectorized Implementation (C1W3L05)
DeepLearningAI
37 Getting Matrix Dimensions Right (C1W4L03)
Getting Matrix Dimensions Right (C1W4L03)
DeepLearningAI
38 Understanding Dropout (C2W1L07)
Understanding Dropout (C2W1L07)
DeepLearningAI
39 Building Blocks of a Deep Neural Network (C1W4L05)
Building Blocks of a Deep Neural Network (C1W4L05)
DeepLearningAI
40 Why Non-linear Activation Functions (C1W3L07)
Why Non-linear Activation Functions (C1W3L07)
DeepLearningAI
41 Computing Neural Network Output (C1W3L03)
Computing Neural Network Output (C1W3L03)
DeepLearningAI
42 Backpropagation Intuition (C1W3L10)
Backpropagation Intuition (C1W3L10)
DeepLearningAI
43 Train/Dev/Test Sets (C2W1L01)
Train/Dev/Test Sets (C2W1L01)
DeepLearningAI
44 Deep L-Layer Neural Network (C1W4L01)
Deep L-Layer Neural Network (C1W4L01)
DeepLearningAI
45 Random Initialization (C1W3L11)
Random Initialization (C1W3L11)
DeepLearningAI
46 Other Regularization Methods (C2W1L08)
Other Regularization Methods (C2W1L08)
DeepLearningAI
47 Normalizing Inputs (C2W1L09)
Normalizing Inputs (C2W1L09)
DeepLearningAI
48 Derivatives Of Activation Functions (C1W3L08)
Derivatives Of Activation Functions (C1W3L08)
DeepLearningAI
49 Parameters vs Hyperparameters (C1W4L07)
Parameters vs Hyperparameters (C1W4L07)
DeepLearningAI
50 Vectorizing Across Multiple Examples (C1W3L04)
Vectorizing Across Multiple Examples (C1W3L04)
DeepLearningAI
51 What does this have to do with the brain? (C1W4L08)
What does this have to do with the brain? (C1W4L08)
DeepLearningAI
52 Dropout Regularization (C2W1L06)
Dropout Regularization (C2W1L06)
DeepLearningAI
53 Vanishing/Exploding Gradients (C2W1L10)
Vanishing/Exploding Gradients (C2W1L10)
DeepLearningAI
54 Basic Recipe for Machine Learning (C2W1L03)
Basic Recipe for Machine Learning (C2W1L03)
DeepLearningAI
55 Bias/Variance (C2W1L02)
Bias/Variance (C2W1L02)
DeepLearningAI
56 Forward Propagation in a Deep Network (C1W4L02)
Forward Propagation in a Deep Network (C1W4L02)
DeepLearningAI
57 Weight Initialization in a Deep Network (C2W1L11)
Weight Initialization in a Deep Network (C2W1L11)
DeepLearningAI
58 Numerical Approximations of Gradients (C2W1L12)
Numerical Approximations of Gradients (C2W1L12)
DeepLearningAI
59 Regularization (C2W1L04)
Regularization (C2W1L04)
DeepLearningAI
60 Why Regularization Reduces Overfitting (C2W1L05)
Why Regularization Reduces Overfitting (C2W1L05)
DeepLearningAI

This video teaches how to vectorize logistic regression's gradient computation, allowing for a highly efficient implementation of gradient descent without explicit for loops over training examples. The equations for the vectorized implementation of DB and DW are derived, and the importance of vectorization in machine learning is emphasized.

Key Takeaways
  1. Define the variables and equations for logistic regression
  2. Vectorize the gradient computation using NumPy
  3. Derive the equations for the vectorized implementation of DB and DW
  4. Implement the vectorized gradient descent update
💡 Vectorization is a crucial technique in machine learning that allows for efficient computation and optimization of models.

Related Reads

Up next
Solve Any Math Problem Step by Step — Free (Type or Snap a Photo)
Zariga Tongy
Watch →