Basics of Neural Network Programming - Logistic Regression cost function


For logistic regression, to train the parameters w and b, we need to define a cost function.

Loss function

The loss function measures the discrepancy between the prediction \hat{y}^{(i)} and the desired output y^{(i)}. In other words, the loss function computes the error for a single training example. It's defined as:

\pounds(\hat{y}^{(i)},y^{(i)})=-y^{(i)}log\: \hat{y}^{(i)}-(1-y^{(i)})log\: (1-\hat{y}^{(i)})

Let's take a look how the loss function is derived:

Given following training examples:

\left \{ (x^{(1)}, y^{(1)}),...,(x^{(m)},y^{(m)}) \right \}

For specific training example,

z^{(i)}=w^{T}x^{(i)}+b

\hat{y}^{(i)}=\sigma (z^{(i)})=\frac{1}{1+e^{-z^{(i)}}}

And we want: \hat{y}^{(i)}\approx y^{(i)}

If y^{(i)}=1, then \hat{y}^{(i)}= p(y^{(i)}=1|x^{(i)}). It means given x^{(i)}, what's the probability of y^{(i)}=1

If y^{(i)}=0, then 1-\hat{y}^{(i)}= p(y^{(i)}=0|x^{(i)}). It means given x^{(i)}, what's the probability of y^{(i)}=0

Above two can be merged into one as below:

p(y^{(i)}|x^{(i)})=\left \{ \hat{y}^{(i)} \right \}^{y^{(i)}}\times \left \{1- \hat{y}^{(i)} \right \}^{1-y^{(i)}}

So, p(y^{(i)}|x^{(i)}) just means the probability of y^{(i)} (=0 OR =1) for given x^{(i)}. We want this probability as high as possible.

Next, we define following equation since log function is a strictly monotonically increasing function, and maximizing \log p(y^{(i)}|x^{(i)}) gives you the same result as maximizing p(y^{(i)}|x^{(i)}):

log\: p(y^{(i)}|x^{(i)})\\ =log\: \left \{ \hat{y}^{(i)} \right \}^{y^{(i)}}\times \left \{1- \hat{y}^{(i)} \right \}^{1-y^{(i)}}\\ =log\: \left \{ \hat{y}^{(i)} \right \}^{y^{(i)}}+log\: \left \{1- \hat{y}^{(i)} \right \}^{1-y^{(i)}}\\ =y^{(i)}log\: \hat{y}^{(i)}+(1-y^{(i)})log\: (1-\hat{y}^{(i)})

Because p(y^{(i)}| x^{(i)})\in (0,1), \log (p(y^{(i)}|x^{(i)}) )\in (- \infty , 0).  So, we define the loss function as the following negavie of p(y^{(i)}|x^{(i)}). And minimizing the loss corresponds to maximizing the log of the probability:

\pounds(\hat{y}^{(i)},y^{(i)})\\= -log\: p(y^{(i)}|x^{(i)}) \\=-y^{(i)}log\: \hat{y}^{(i)}-(1-y^{(i)})log\: (1-\hat{y}^{(i)})

And \pounds \in (0, \infty)

Let's try to understand this loss function:

  • When y^{(i)}=1, then \pounds =-log\: \hat{y}^{​{(i)}}
    • If \hat{y}^{(i)}=1, then \pounds =0
    • If \hat{y}^{(i)}=0, then \pounds =-(-\infty )=\infty
  • When y^{(i)}=0, then \pounds =-log\: (1-\hat{y}^{​{(i)}})
    • If \hat{y}^{(i)}=0, then \pounds =0
    • If \hat{y}^{(i)}=1, then \pounds =-(-\infty )=\infty

Cost function

It's defined as:

J(w,b)\\ =\frac{1}{m}\sum _{i=1}^{m}\pounds (\hat{y}^{(i)},y^{(i)})\\ =-\frac{1}{m}\sum_{i=1}^{m}\left [ y^{(i)}\log \hat{y}^{(i)}+(1-y^{(i)})log(1-\hat{y}^{(i)})\right ]

It's the average of loss function over all m training examples.

Actually, this definition can also be derived as below based on Maximum Likelihood Estimation (最大似然估计):

If assuming that the training examples are IID (Identically Independently Distributed, 独立同分布), the probability of all the training examples is the product of probabilities:

\prod _{i=1}^{m}p(y^{(i)}|x^{(i)})

To maximize the above that is the chance of your observations for all examples in the training set, is the same as miximizing the following:

\log \prod _{i=1}^{m}p(y^{(i)}|x^{(i)})\\ =\sum_{i=1}^{m}\log p(y^{(i)}|x^{(i)})\\ =\sum_{i=1}^{m}\left [ -\pounds (\hat{y}^{(i)},y^{(i)}) \right ]\\ =-\sum_{i=1}^{m}\left [ \pounds (\hat{y}^{(i)},y^{(i)}) \right ]

Per Maximum Likelihood Estimation, we need choose the parameters w and b that maximizes above equation. This justifies the cost we had for logistic regression which is J(w,b). But because now we want to minimize the cost instead of maximize the likelihood, we'll get rid of the minus sign. And also for convenience, we make sure that our quantities are better scale, we just add a 1/m scaling factor:

J(w,b)\\ =\frac{1}{m}\sum _{i=1}^{m}\pounds (\hat{y}^{(i)},y^{(i)})\\ =-\frac{1}{m}\sum_{i=1}^{m}\left [ y^{(i)}\log \hat{y}^{(i)}+(1-y^{(i)})log(1-\hat{y}^{(i)})\right ]

So, by minimizing the cost function J(w,b), we're really carrying out maximum likelihood estimation with the logistic regression model under the assumption that our training examples are IID.

<end>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值