Machine Learning-L6-逻辑回归

1 逻辑回归

逻辑回归/对数几率回归(Logistic/Logit Regression)实际是一种分类学习方法,用于因变量是二分类(0/1,True/False,Yes/No)的情况。

这种方法直接对分类可能性进行建模,无需实现假设数据分布,用线性回归模型的预测结果逼近真实标记的对数几率,可得到近似概率预测。

对数几率函数(Logistic function)是一种Sigmoid函数(S形函数),将 z = w T x + b z=w^{T} x+b 值转化为一个接近 0 0 1 1 y y 值,并且输出值在 z = 0 z=0 附件变化很陡。

y = 1 1 + e ( w T x + b ) y=\frac {1}{1+e^{-(w^{T} x+b)}}
y = h θ ( x ) = 1 1 + e θ T x , θ = ( w , b ) (1) y = h_\theta(\boldsymbol x) = \frac {1}{1+e^{- \theta^{T} \boldsymbol x}}, \theta = (w,b) \tag 1
由于代价函数 J ( θ ) = 1 2 m i = 1 m ( h θ ( x ( i ) ) y ( i ) ) 2 J(\theta)=\frac {1}{2m} \sum\limits_{i=1}^{m}(h_\theta(x^{(i)})-y^{(i)})^2 为非凸函数,存在多个局部最小值


把逻辑回归看做用线性回归模型的预测结果逼近真实标记的对数几率,根据 ( 1 ) (1)

l n y 1 y = θ T x (2) ln \frac {y}{1-y} = \theta^{T} \boldsymbol x \tag 2 y y 看做类后验概率估 p ( y = 1 x ) p(y=1|\boldsymbol x) ,则 1 y 1-y 是其反例的概率估计: l n p ( y = 1 x ) p ( y = 0 x ) = θ T x (3) ln \frac {p(y=1 \mid \boldsymbol x)}{p(y=0 \mid \boldsymbol x)} = \theta^{T} \boldsymbol x \tag 3 p 1 = p ( y = 1 x ) , p 0 = p ( y = 0 x ) p_1 = p(y=1 \mid \boldsymbol x),p_0 = p(y=0 \mid \boldsymbol x)

p 1 = 1 p 0 p_1 = 1 - p_0 ,根据 ( 3 ) (3) 得:
p 1 = 1 1 + e θ T x (4) p_1 = \frac {1}{1+e^{- \theta^{T} \boldsymbol x}} \tag 4 p 0 = e θ T 1 + e θ T x (5) p_0 = \frac {e^{- \theta^{T}}}{1+e^{- \theta^{T} \boldsymbol x}} \tag 5
根据 ( 4 ) ( 5 ) (4)(5) p ( y x ) = p 1 y p 0 1 y , y { 0 , 1 } (6) p(y \mid \boldsymbol x) = p_1^yp_0^{1-y},y \in \{0,1\} \tag 6

因此,对于数据集 D = { ( x ( 1 ) , y ( 1 ) ) , ( x ( 2 ) , y ( 2 ) ) , . . . , ( x ( m ) , y ( m ) ) } D=\{(\boldsymbol x^{(1)},y^{(1)}), (\boldsymbol x^{(2)},y^{(2)}),...,(\boldsymbol x^{(m)},y^{(m)})\} ,优化目标为:

max θ L ( θ ) = i = 1 m p ( y ( i ) x ( i ) ; θ ) = i = 1 m p 1 y ( i ) p 0 1 y ( i ) (7) \max_\theta L(\theta) = \prod_{i=1}^m p(y^{(i)} \mid \boldsymbol x^{(i)}; \theta) = \prod_{i=1}^m p_1^{y^{(i)}}p_0^{1-y^{(i)}} \tag 7

其中 p ( y ( i ) x ( i ) ; θ ) p(y^{(i)} \mid \boldsymbol x^{(i)}; \theta) 表示给定 x ( i ) \boldsymbol x^{(i)} 和参数 θ \theta y ( i ) y^{(i)} 的分布,将其看作 θ \theta 的函数,即似然函数。

可通过极大似然法(Maximum likelihood method)来估计 θ \theta ,对 L ( θ ) L(\theta) 取对数:

l ( θ ) = i = 1 m y ( i ) log p 1 + ( 1 y ( i ) ) log p 0 = i = 1 m y ( i ) log ( h θ ( x ( i ) ) ) + ( 1 y ( i ) ) log ( 1 h θ ( x ( i ) ) ) (8) l(\theta) = \sum_{i=1}^m y^{(i)}\log p_1+(1-y^{(i)})\log p_0 = \sum_{i=1}^m y^{(i)} \log (h_\theta (\boldsymbol x^{(i)})) + (1-y^{(i)})\log(1- h_\theta (\boldsymbol x^{(i)})) \tag 8

l ( θ ) l(\theta) 是连续可导的凸函数,可使用梯度下降、牛顿法求其最优解。

根据 ( 8 ) (8) ,定义损失函数:
C o s t ( h θ ( x ) , y ) = { log ( h θ ( x ) ) i f y = 1 log ( 1 h θ ( x ) ) i f y = 0 (9) Cost(h_\theta(\boldsymbol x),y) = \begin{cases} - \log(h_{\theta} (\boldsymbol x)) & {if } y=1 \\ -\log(1-h_\theta (\boldsymbol x)) & {if } y=0 \end{cases} \tag 9

合并为下式:
C o s t ( h θ ( x ) , y ) = y log ( h θ ( x ) ) ( 1 y ) log ( 1 h θ ( x ) ) , y { 0 , 1 } (10) Cost(h_\theta(\boldsymbol x),y) = -y \log(h_\theta (\boldsymbol x))-(1-y) \log(1-h_\theta (\boldsymbol x)),y \in \{0,1\} \tag{10} 代价函数:
J ( θ ) = 1 m i = 1 m y ( i ) log ( h θ ( x ( i ) ) ) + ( 1 y ( i ) ) log ( 1 h θ ( x ( i ) ) ) (11) J(\theta) = -\frac {1}{m}\sum_{i=1}^m y^{(i)} \log(h_\theta (\boldsymbol x^{(i)})) + (1-y^{(i)})\log(1- h_\theta (\boldsymbol x^{(i)})) \tag{11}

2. 交叉熵损失函数

2.1 交叉熵

交叉熵可衡量在真实分布下使用非真实分布所指定的策略消除系统的不确定性所需要付出代价,定义如下:
H ( p , q ) = i = 1 n p ( x i ) l o g ( q ( x i ) ) H(p,q)=-\sum_{i=1}^np(x_i)log(q(x_i)) 其中 p ( x i ) p(x_i) 表示真实分布, q ( x i ) q(x_i) 表示非真实分布,即模型的预测分布。
交叉熵越低,策略就越好,最低的交叉熵对应使用真实分布的信息熵。
在机器学习中,通过最小化交叉熵,使得算法所产生的策略接近最优策略,即算法生成的非真实分布越接近真实分布。

2.2 相对熵

相对熵(relative entropy)又称KL散度(Kullback-Leibler divergence),用来衡量两个概率分布之间的差异,定义如下:

D K L ( p q ) = i = 1 n p ( x i ) l o g p ( x i ) q ( x i ) = i = 1 n p ( x i ) log ( p ( x i ) ) i = 1 n p ( x i ) log ( q ( x i ) ) = H ( p ) + H ( p , q ) \begin{aligned} D_{KL}(p||q) &= \sum_{i=1}^np(x_i)log\frac{p(x_i)}{q(x_i)} \\ &= \sum_{i=1}^np(x_i)\log (p(x_i))-\sum_{i=1}^np(x_i)\log (q(x_i))\\ &= -H(p)+ H(p,q) \end{aligned}

信息熵为完美编码,交叉熵不完美编码,相对熵(KL散度)是两者的差值(即差异),即交叉熵减去信息熵。

KL散度大于等于0,并且越接近0说明p与q这两个分布越接近,当且仅当 p p q q 相等时KL散度等于0。

机器学习的过程就是希望在训练数据上模型学到的分布 P m o d e l P_{model} 和真实数据的分布 P r e a l P_{real} 越接近越好,但由于没有真实数据的分布,只能希望模型学到的分布和训练数据的分布 P t r a i n P_{train} 尽量相同。

最小化模型分布 P m o d e l P_{model} 与训练数据上的分布 P t r a i n P_{train} 的差异等价于最小化这两个分布间的KL散度 K L ( P t r a i n i n g P m o d e l ) KL(P_{training}||P_{model})

2.3 交叉熵损失函数

在二分类中, p p 为实际值, q q 为预测值,用 p p 分布拟合 q q 分布,则
L c r o s s e n t r o p y ( p q ) = i = 1 n p ( x i ) log ( q ( x i ) ) ( 1 p ( x i ) ) log ( 1 q ( x i ) ) = i = 1 n p ( x i ) log ( q ( x i ) ) + ( 1 p ( x i ) ) log ( 1 q ( x i ) ) \begin{aligned}L_{cross-entropy}(p||q) &=& \sum_{i=1}^{n} -p(x_i)\log(q(x_i)) - (1-p(x_i))\log(1-q(x_i)) \\ &=& -\sum_{i=1}^{n} p(x_i)\log(q(x_i)) + (1-p(x_i))\log(1-q(x_i)) \end{aligned} 上式与逻辑回归的损失函数( 11 11 式)形式是一致的。

发布了21 篇原创文章 · 获赞 22 · 访问量 1016

猜你喜欢

转载自blog.csdn.net/apr15/article/details/105589247