Logistic Regression Cost Function (C1W2L03)

DeepLearningAI · Beginner ·📐 ML Fundamentals ·8y ago

Key Takeaways

Explains the cost function of logistic regression

Full Transcript

in the previous video you saw the logistic regression model to train the parameters W and B of your logistic regression model you need to define a cost function let's take a look at a cost function you can use to Train logistic regression to recap this is what we had defined from the previous slide so you output Y hat is sigmoid of W transpose X plus B where sigmoid of z is as defined here so to learn parameters for your model you're given a training set of M training examples and it seems natural that you want to find parameters W and B so that at least on the training set the outputs you have the predictions you have on the training set we're going to write this Y hat I that that would be close to the ground truth labels Y either you got in the training set so to fill in a little bit more detail for the equation on top we have said that Y has as defined at the top for a training example X and of course for each training example we're using these super scripts with round brackets with parentheses to index into different training examples your prediction on training example I which is y hat I is going to be obtained by taking the sigmoid function and applying it to W transpose X I the input for the training example plus B and you can also define Z I as follows where Zi is equal to you know W transpose X I plus B so throughout this course we're going to use this notational convention that the superscript parentheses I refers to data via X or Y or Z or something else associated with the i'f training example associated with the i'f example okay that's what the superscript I in parenthesis means now let's see what loss function or an error function we can use to measure how well our algorithm is doing one thing you could do is define the loss when your algorithm outputs Y hat and the true label is y to be maybe the squared error or one hover square it turns out that you could do this but in logistic regression people don't usually do this because when you come to learn the parameters you find that the optimization problem which we'll talk about later becomes non convex so you end up with ostentation problem it would not talk local optima so gradient descent may not find a global optimum if you didn't understand the last couple of comments don't worry about it we'll get to it in the later video but the intuition to take away is that this function L called the loss function is a function we need to define to measure how good our output Y hat is when the true label is y and squared error seems like it might be a reasonable choice except that it makes gradient descent not work well so in logistic regression we will actually define a different loss function that plays a similar role as squared error that will give us an optimization problem that is convex and so will see in a later video becomes much easier to optimize so what we use in logistic regression is actually the following loss function which ones with my rent out here is negative y log y hat plus 1 minus y log 1 minus y hat here's some intuition for why this loss function exists keep in mind that if we're using squared error then you want this squared error to be as small as possible and with this logistic regression loss function we'll also want this to be as small as possible to understand why this makes sense let's look at the two cases in the first case let's say Y is equal to 1 then the loss function Y has comma Y is just the stress term right under the negative sign so it's negative log Y hat if y is equal to 1 because if y equals 1 then the second term 1 minus y is equal to 0 so this says if y equals 1 you want negative Y hat to be as big as possible so that means you want log y hat to be large to be as big as possible and that means you want y hat to be large but because y hat is you know the sigmoid function it can never be bigger than one so this is saying that if Y is equal to one you once Y has to be as big as possible but it can't ever be bigger than one so saying you want Y hat to be close to one as well the other case is if y equals zero if y equals zero then this first term in the loss function is equal to zero because y 0 and in the second term defines the loss function so the loss becomes negative log 1 minus y hat and so if is a learning procedure you try to make the last function small what this means is that you want log 1 minus y hat to be large and because it's a negative sign there and then through a similar piece of reasoning you can conclude that this loss function is trying to make y hat as small as possible and again because Y hat has to be between 0 & 1 this is saying that if Y is equal to 0 then your loss function will push the parameters so make Y hat as close to zero as possible now there are a lot of functions with roughly this effect that if Y is equal to 1 you try to make Y hat knowledge and Y is equal to 0 or try to make Y hat small we just gave here in green a somewhat informal justification for this particular loss function will provide an optional video later to give a more formal justification for Y in logistic regression we like to use the loss function with this particular form finally the loss function was defined with respect to a single training example it measures how well you're doing on a single training example are now going to define something called the cost function which measures how are you doing on the entire training set so the cost function J which is applied to your parameter W NP is going to be the average ly 1 over m of the sum of the last function applied to each of the training examples in turn when here Y has is of course the prediction output by your logistic regression algorithm using you know a particular set of parameters W and B and so just to expand this out this is equal to negative 1 over m sum from I equals 1 through m of the definition of the last function above so this is why I log Y hat I plus 1 minus y I log 1 minus y hat I because it can put square brackets here so the minus sign goes outside everything else so the terminology I'm going to use is that the loss function is applied to just a single training example like so and the cost function is the cost of your parameters so in training your logistic regression model we're going to try to find parameters W and B that minimize the overall cost function J written at the bottom so you've just seen the set up for the logistic regression algorithm the loss function for a training example and the overall cost function for the parameters of your algorithm it turns out that logistic regression can be viewed as a very very small neural network in the next video we'll go over that so you can start gaining intuition about what neural networks do so that let's go on to the next video about how to view logistic regression as a very small neural network

Original Description

Take the Deep Learning Specialization: http://bit.ly/3cmtNgK 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

Related Reads

📰
“Los Movimientos”: The Routing Problem That Nearly Broke My Spirit
Solve complex pickup-and-delivery problems with time windows using mathematical optimization techniques
Towards Data Science
📰
How Guardoc transforms medical document processing with Amazon Nova models
Learn how Guardoc uses Amazon Nova models to transform medical document processing and improve accuracy
AWS Machine Learning
📰
The Reward Calibrator That Learns the Shape of Its Own Judgment
Learn how to build a reward calibrator that automatically adjusts weights for better judgment, eliminating manual tuning
Dev.to · Daniel Romitelli
📰
Stop Chasing the Perfect AI Model: Measure Before You Optimize
Learn to measure AI model performance before optimizing to avoid wasting time and resources
Dev.to · rushikeshpatil1007
Up next
Generative vs Discriminative Models - Explained
DataMListic
Watch →