吴恩达深度学习 —— 2.18(选修)逻辑回归损失函数的解释

回想一下,在逻辑回归中,需要预测的结果 y ^ \hat{y} y^可以表示为 y ^ = σ ( w T x + b ) \hat{y}=\sigma(w^Tx+b) y^=σ(wTx+b),其中 σ ( z ) = 1 1 + e − z \sigma(z)=\frac{1}{1+e^{-z}} σ(z)=1+ez1

我们约定 y ^ = P ( y = 1 ∣ x ) \hat{y}=P(y=1|x) y^=P(y=1x),即算法的输出 y ^ \hat{y} y^是给定训练样本x条件下y等于1的概率。换句话说,如果y=1,那么在给定x得到y=1的概率等于 y ^ \hat{y} y^。反过来说,如果y=0,那么在给定x得到y=0的概率等于 1 − y ^ 1-\hat{y} 1y^。因此 y ^ \hat{y} y^表示的是y=1的概率, 1 − y ^ 1-\hat{y} 1y^表示的就是y=0的概率。 i f   y = 1 : p ( y ∣ x ) = y ^ if \space y=1: p(y|x)=\hat{y} if y=1:p(yx)=y^ i f   y = 0 : p ( y ∣ x ) = 1 − y ^ if \space y=0: p(y|x)=1-\hat{y} if y=0:p(yx)=1y^对于这两个式子,在y=1和y=0条件下定义了 p ( y ∣ x ) p(y|x) p(yx),我们可以将这两个公式合并成y一个公式。

需要指出的是,我们讨论的是二分分类问题的成本函数,因此y的取值只能是0或者1,上述两个条件概率公式可以合并成下面这样: p ( y ∣ x ) = y ^ y ( 1 − y ^ ) ( 1 − y ) p(y|x)=\hat{y}^y(1-\hat{y})^{(1-y)} p(yx)=y^y(1y^)(1y)由于log函数是严格单调递增的函数,最大化 l o g ( p ( y ∣ x ) ) log(p(y|x)) log(p(yx))等价于最大化 p ( y ∣ x ) p(y|x) p(yx)
l o g   p ( y ∣ x ) = l o g   y ^ y ( 1 − y ^ ) ( 1 − y ) = y l o g   y ^ + ( 1 − y ) l o g   ( 1 − y ^ ) = − L o s s ( y ^ , y ) log\space p(y|x)=log\space \hat{y}^y(1-\hat{y})^{(1-y)}=ylog\space \hat{y}+(1-y)log\space (1-\hat{y})=-Loss(\hat{y},y) log p(yx)=log y^y(1y^)(1y)=ylog y^+(1y)log (1y^)=Loss(y^,y)最后一个公式中有一个负号是因为当训练学习算法时希望算法输出值的概率是最大的,然而在逻辑回归中,我们需要最小化损失函数,因此最小化损失函数就是最大化 l o g   P ( y ∣ x ) log\space P(y|x) log P(yx)。这就是单个训练样本的损失函数表达式。

整个训练集的成本函数是怎样表示的?探讨一下整个训练集中标签的概率 P ( l a b e l s i n t a r g e t s e t ) P(labels in target set) P(labelsintargetset),假设所有的训练样本服从同一分布且相互独立,也就是独立同分布,所有这些样本的联合概率,就是每个样本概率的乘积,即从1到m的 p ( y ( i ) ∣ x ( i ) ) p(y^{(i)}|x^{(i)}) p(y(i)x(i))的概率乘积,可以表示为 p ( l a b e l s   i n   t a r g e t   s e t ) = ∏ i = 1 m p ( y ( i ) ∣ x ( i ) ) p(labels\space in\space target\space set)=\prod_{i=1}^mp(y^{(i)}|x^{(i)}) p(labels in target set)=i=1mp(y(i)x(i))如果你想做最大似然估计,需要寻找一组参数使得给定样本的观测值概率最大,令这个概率最大化,等价于令其对数最大化,在等式两边取对数 l o g   p ( l a b e l s   i n   t a r g e t   s e t ) = l o g ∏ i = 1 m p ( y ( i ) ∣ x ( i ) ) log\space p(labels\space in\space target\space set)=log\prod_{i=1}^mp(y^{(i)}|x^{(i)}) log p(labels in target set)=logi=1mp(y(i)x(i))通过转化得到
l o g   p ( l a b e l s   i n   t a r g e t   s e t ) = ∑ i = 1 m l o g   p ( y ( i ) ∣ x ( i ) ) log\space p(labels\space in\space target\space set)=\sum_{i=1}^mlog\space p(y^{(i)}|x^{(i)}) log p(labels in target set)=i=1mlog p(y(i)x(i))前面讲到 l o g   p ( y ( i ) ∣ x ( i ) ) = − L o s s ( y ^ ( i ) , y ( i ) ) log\space p(y^{(i)}|x^{(i)})=-Loss(\hat{y}^{(i)},y^{(i)}) log p(y(i)x(i))=Loss(y^(i),y(i))在统计学里面,有一个方法叫最大似然估计,即求出一组参数,使这个式子取得最大值,公式可以写为 l o g   p ( l a b e l s   i n   t a r g e t   s e t ) = − ∑ i = 1 m L o s s ( y ^ ( i ) , y ( i ) ) log\space p(labels\space in\space target\space set)=-\sum_{i=1}^mLoss(\hat{y}^{(i)},y^{(i)}) log p(labels in target set)=i=1mLoss(y^(i),y(i))这样我们就推导出了前面给出的逻辑回顾的成本函数 J ( w , b ) J(w,b) J(w,b),由于训练模型时,目标是让成本函数最小化,所以我们不直接使用极大似然估计,因此把公式中的负号去掉。最后为了方便,可以对成本函数进行适当的缩放,在公式前面加一个额外的常数因子 ( 1 m ) (\frac{1}{m}) (m1)。其公式表示为 J ( w , b ) = 1 m ∑ i = 1 m L o s s ( y ^ ( i ) , y ( i ) ) J(w,b)=\frac{1}{m}\sum_{i=1}^mLoss(\hat{y}^{(i)},y^{(i)}) J(w,b)=m1i=1mLoss(y^(i),y(i))总结一下,为了最小化成本函数 J ( w , b ) J(w,b) J(w,b),我们从逻辑回归模型的最大似然角度出发,假设训练集中的样本都是独立同分布的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值