回想一下,在逻辑回归中,需要预测的结果 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+e−z1。
我们约定 y ^ = P ( y = 1 ∣ x ) \hat{y}=P(y=1|x) y^=P(y=1∣x),即算法的输出 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} 1−y^。因此 y ^ \hat{y} y^表示的是y=1的概率, 1 − y ^ 1-\hat{y} 1−y^表示的就是y=0的概率。 i f y = 1 : p ( y ∣ x ) = y ^ if \space y=1: p(y|x)=\hat{y} if y=1:p(y∣x)=y^ i f y = 0 : p ( y ∣ x ) = 1 − y ^ if \space y=0: p(y|x)=1-\hat{y} if y=0:p(y∣x)=1−y^对于这两个式子,在y=1和y=0条件下定义了 p ( y ∣ x ) p(y|x) p(y∣x),我们可以将这两个公式合并成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(y∣x)=y^y(1−y^)(1−y)由于log函数是严格单调递增的函数,最大化
l
o
g
(
p
(
y
∣
x
)
)
log(p(y|x))
log(p(y∣x))等价于最大化
p
(
y
∣
x
)
p(y|x)
p(y∣x)
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(y∣x)=log y^y(1−y^)(1−y)=ylog y^+(1−y)log (1−y^)=−Loss(y^,y)最后一个公式中有一个负号是因为当训练学习算法时希望算法输出值的概率是最大的,然而在逻辑回归中,我们需要最小化损失函数,因此最小化损失函数就是最大化
l
o
g
P
(
y
∣
x
)
log\space P(y|x)
log P(y∣x)。这就是单个训练样本的损失函数表达式。
整个训练集的成本函数是怎样表示的?探讨一下整个训练集中标签的概率
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=1∏mp(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=1∏mp(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=1∑mlog 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=1∑mLoss(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=1∑mLoss(y^(i),y(i))总结一下,为了最小化成本函数
J
(
w
,
b
)
J(w,b)
J(w,b),我们从逻辑回归模型的最大似然角度出发,假设训练集中的样本都是独立同分布的。