机器学习——EM算法

机器学习——EM算法


EM算法是一种迭代算法,用于含有隐变量(hidden variable)的概率模型参数的极大似然估计,或极大后验概率估计。EM算法的每次迭代由两步组成:E步,求期望(expcetation);M步,求极大(maximization)。所以这一算法称为期望极大算法(expectation maximization algorithm),简称EM算法。 EM算法与初值的选择有关,选择不同的初值可能得到不同的参数估计值。EM算法不能保证找到全局最优值。

EM算法

概率模型有时既含有观测变量(observation variable),又含有隐变量或潜在变量(latent variable)。当模型含有隐变量时,就不能简单地使用这些估计方法。EM算法就是含有隐变量模型参数的极大似然估计法,或极大后验概率估计法。

三硬币模型
假设有3枚硬币,分别记作A,B,C,出现的概率分别是 π \pi π p p p q q q。进行如下掷硬币实验:先掷硬币A,根据其结果选出硬币B或C,正面选硬币B,反面选硬币C,出现正面记作1,反面记作0。
三硬币模型可以写作
P ( y ∣ θ ) = ∑ z P ( y , z ∣ θ ) = ∑ z P ( z ∣ θ ) P ( y ∣ z , θ ) = π p y ( 1 − p ) 1 − y + ( 1 − π ) q y ( 1 − q ) 1 − y P ( Y ∣ θ ) = ∏ j = 1 n [ π p y j ( 1 − p ) 1 − y j + ( 1 − π ) q y j ( 1 − q ) 1 − y j ] θ ^ = arg ⁡ max ⁡ θ log ⁡ P ( Y ∣ θ ) \begin{aligned} P ( y \mid \theta ) & = \sum _ { z } P ( y , z \mid \theta ) = \sum _ { z } P ( z \mid \theta ) P ( y \mid z , \theta ) \\ & = \pi p ^ { y } ( 1 - p ) ^ { 1 - y } + ( 1 - \pi ) q ^ { y } ( 1 - q ) ^ { 1 - y } \end{aligned} \\ P ( Y \mid \theta ) = \prod _ { j = 1 } ^ { n } \left[ \pi p ^ { y _ { j } } ( 1 - p ) ^ { 1 - y _ { j } } + ( 1 - \pi ) q ^ { y _ { j } } ( 1 - q ) ^ { 1 - y _ { j } } \right] \\ \hat { \theta } = \arg \max _ { \theta } \log P ( Y \mid \theta ) P(yθ)=zP(y,zθ)=zP(zθ)P(yz,θ)=πpy(1p)1y+(1π)qy(1q)1yP(Yθ)=j=1n[πpyj(1p)1yj+(1π)qyj(1q)1yj]θ^=argθmaxlogP(Yθ)
EM算法首先选取参数的初值,记作 θ ( 0 ) = ( π ( 0 ) , p ( 0 ) , q ( 0 ) ) \theta ^ { ( 0 ) } = \left( \pi ^ { ( 0 ) } , p ^ { ( 0 ) } , q ^ { ( 0 ) } \right) θ(0)=(π(0),p(0),q(0)),第i次迭代的参数估计值为 θ ( i ) = ( π ( i ) , p ( i ) , q ( i ) ) \theta ^ { ( i ) } = \left( \pi ^ { ( i ) } , p ^ { ( i ) } , q ^ { ( i ) } \right) θ(i)=(π(i),p(i),q(i)),则第 i + 1 i+1 i+1次迭代如下:
E步:计算观测数据 y j y_j yj来自掷硬币B的概率
μ j ( i + 1 ) = π ( i ) ( p ( i ) ) y j ( 1 − p ( i ) ) 1 − y j π ( i ) ( p ( i ) ) y j ( 1 − p ( i ) ) 1 − y j + ( 1 − π ( i ) ) ( q ( i ) ) y j ( 1 − q ( i ) ) 1 − y j \mu _ { j } ^ { ( i + 1 ) } = \frac { \pi ^ { ( i ) } \left( p ^ { ( i ) } \right) ^ { y _ { j } } \left( 1 - p ^ { ( i ) } \right) ^ { 1 - y _ { j } } } { \pi ^ { ( i ) } \left( p ^ { ( i ) } \right) ^ { y _ { j } } \left( 1 - p ^ { ( i ) } \right) ^ { 1 - y _ { j } } + \left( 1 - \pi ^ { ( i ) } \right) \left( q ^ { ( i ) } \right) ^ { y _ { j } } \left( 1 - q ^ { ( i ) } \right) ^ { 1 - y _ { j } } } μj(i+1)=π(i)(p(i))yj(1p(i))1yj+(1π(i))(q(i))yj(1q(i))1yjπ(i)(p(i))yj(1p(i))1yj
M步:计算模型参数的新估计值
π ( i + 1 ) = 1 n ∑ j = 1 n μ j ( i + 1 ) \pi ^ { ( i + 1 ) } = \frac { 1 } { n } \sum _ { j = 1 } ^ { n } \mu _ { j } ^ { ( i + 1 ) } π(i+1)=n1j=1nμj(i+1)
p , q p, q p,q计算同理

EM算法与初值的选择有关,选择不同的初值可能得到不同的参数估计值。用 Y Y Y表示观测随机变量的数据, Z Z Z表示隐随机变量的数据。 Y Y Y Z Z Z连在一起称为完全数据(complete-data),观测数据 Y Y Y又称为不完全数据(incomplete-data)。

EM算法
(1)选择参数的初值 θ ( 0 ) \theta^{(0)} θ(0)
(2)E步:计算
Q ( θ , θ ( i ) ) = E Z [ log ⁡ P ( Y , Z ∣ θ ) ∣ Y , θ ( i ) ] = ∑ Z log ⁡ P ( Y , Z ∣ θ ) P ( Z ∣ Y , θ ( i ) ) \begin{aligned} Q \left( \theta , \theta ^ { ( i ) } \right) & = E _ { Z } \left[ \log P ( Y , Z \mid \theta ) \mid Y , \theta ^ { ( i ) } \right] \\ & = \sum _ { Z } \log P ( Y , Z \mid \theta ) P \left( Z \mid Y , \theta ^ { ( i ) } \right) \end{aligned} Q(θ,θ(i))=EZ[logP(Y,Zθ)Y,θ(i)]=ZlogP(Y,Zθ)P(ZY,θ(i))
(3)M步:求使 Q Q Q极大化的 θ \theta θ
θ ( i + 1 ) = arg ⁡ max ⁡ θ Q ( θ , θ ( i ) ) \theta ^ { ( i + 1 ) } = \arg \max _ { \theta } Q \left( \theta , \theta ^ { ( i ) } \right) θ(i+1)=argθmaxQ(θ,θ(i))
(4)重复第(2)步和第(3)步,直到收敛

注:(1)参数的初值可以任意选择,但需注意EM算法对初值是敏感的。(2)每次迭代实际在求 Q Q Q函数及其极大。(3)给出停止迭代的条件,若满足 ∥ θ ( i + 1 ) − θ ( i ) ∥ < ε 1  或  ∥ Q ( θ ( i + 1 ) , θ ( i ) ) − Q ( θ ( i ) , θ ( i ) ) ∥ < ε 2 \left\| \theta ^ { ( i + 1 ) } - \theta ^ { ( i ) } \right\| < \varepsilon _ { 1 } \quad \text { 或 } \left\| Q \left( \theta ^ { ( i + 1 ) } , \theta ^ { ( i ) } \right) - Q \left( \theta ^ { ( i ) } , \theta ^ { ( i ) } \right) \right\| < \varepsilon _ { 2 } θ(i+1)θ(i)<ε1  Q(θ(i+1),θ(i))Q(θ(i),θ(i))<ε2

EM算法推导

极大化
L ( θ ) = log ⁡ P ( Y ∣ θ ) = log ⁡ ∑ Z P ( Y , Z ∣ θ ) = log ⁡ ( ∑ Z P ( Y ∣ Z , θ ) P ( Z ∣ θ ) ) \begin{aligned} L ( \theta ) & = \log P ( Y \mid \theta ) = \log \sum _ { Z } P ( Y , Z \mid \theta ) \\ & = \log \left( \sum _ { Z } P ( Y \mid Z , \theta ) P ( Z \mid \theta ) \right) \end{aligned} L(θ)=logP(Yθ)=logZP(Y,Zθ)=log(ZP(YZ,θ)P(Zθ))
考虑新估计值 θ \theta θ能使 L ( θ ) L(\theta) L(θ)增加,即 L ( θ ) > L ( θ ( i ) ) L(\theta)>L(\theta^{(i)}) L(θ)>L(θ(i)),并逐步达到极大值,考虑两者的差
L ( θ ) − L ( θ ( i ) ) = log ⁡ ( ∑ Z P ( Y ∣ Z , θ ) P ( Z ∣ θ ) ) − log ⁡ P ( Y ∣ θ ( i ) ) L ( \theta ) - L \left( \theta ^ { ( i ) } \right) = \log \left( \sum _ { Z } P ( Y \mid Z , \theta ) P ( Z \mid \theta ) \right) - \log P \left( Y \mid \theta ^ { ( i ) } \right) L(θ)L(θ(i))=log(ZP(YZ,θ)P(Zθ))logP(Yθ(i))
利用Jensen不等式,得到其下界:
L ( θ ) − L ( θ ( i ) ) = log ⁡ ( ∑ Z P ( Z ∣ Y , θ ( i ) ) P ( Y ∣ Z , θ ) P ( Z ∣ θ ) P ( Z ∣ Y , θ ( i ) ) ) − log ⁡ P ( Y ∣ θ ( i ) ) ⩾ ∑ Z P ( Z ∣ Y , θ ( i ) ) log ⁡ P ( Y ∣ Z , θ ) P ( Z ∣ θ ) P ( Z ∣ Y , θ ( i ) ) − log ⁡ P ( Y ∣ θ ( i ) ) = ∑ Z P ( Z ∣ Y , θ ( i ) ) log ⁡ P ( Y ∣ Z , θ ) P ( Z ∣ θ ) P ( Z ∣ Y , θ ( i ) ) P ( Y ∣ θ ( i ) ) \begin{aligned} L ( \theta ) - L \left( \theta ^ { ( i ) } \right) & = \log \left( \sum _ { Z } P \left( Z \mid Y , \theta ^ { ( i ) } \right) \frac { P ( Y \mid Z , \theta ) P ( Z \mid \theta ) } { P \left( Z \mid Y , \theta ^ { ( i ) } \right) } \right) - \log P \left( Y \mid \theta ^ { ( i ) } \right) \\ & \geqslant \sum _ { Z } P \left( Z \mid Y , \theta ^ { ( i ) } \right) \log \frac { P ( Y \mid Z , \theta ) P ( Z \mid \theta ) } { P \left( Z \mid Y , \theta ^ { ( i ) } \right) } - \log P \left( Y \mid \theta ^ { ( i ) } \right) \\ & = \sum _ { Z } P \left( Z \mid Y , \theta ^ { ( i ) } \right) \log \frac { P ( Y \mid Z , \theta ) P ( Z \mid \theta ) } { P \left( Z \mid Y , \theta ^ { ( i ) } \right) P \left( Y \mid \theta ^ { ( i ) } \right) } \end{aligned} L(θ)L(θ(i))=log(ZP(ZY,θ(i))P(ZY,θ(i))P(YZ,θ)P(Zθ))logP(Yθ(i))ZP(ZY,θ(i))logP(ZY,θ(i))P(YZ,θ)P(Zθ)logP(Yθ(i))=ZP(ZY,θ(i))logP(ZY,θ(i))P(Yθ(i))P(YZ,θ)P(Zθ)

这里用到的是: log ⁡ ∑ j λ j y j ⩾ ∑ j λ j log ⁡ y j \log \sum _ { j } \lambda _ { j } y _ { j } \geqslant \sum _ { j } \lambda _ { j } \log y _ { j } logjλjyjjλjlogyj

B ( θ , θ ( i ) ) = ^ L ( θ ( i ) ) + ∑ Z P ( Z ∣ Y , θ ( i ) ) log ⁡ P ( Y ∣ Z , θ ) P ( Z ∣ θ ) P ( Z ∣ Y , θ ( i ) ) P ( Y ∣ θ ( i ) ) B \left( \theta , \theta ^ { ( i ) } \right) \hat { = } L \left( \theta ^ { ( i ) } \right) + \sum _ { Z } P \left( Z \mid Y , \theta ^ { ( i ) } \right) \log \frac { P ( Y \mid Z , \theta ) P ( Z \mid \theta ) } { P \left( Z \mid Y , \theta ^ { ( i ) } \right) P \left( Y \mid \theta ^ { ( i ) } \right) } B(θ,θ(i))=^L(θ(i))+ZP(ZY,θ(i))logP(ZY,θ(i))P(Yθ(i))P(YZ,θ)P(Zθ),则 L ( θ ) ⩾ B ( θ , θ ( i ) ) L ( \theta ) \geqslant B \left( \theta , \theta ^ { ( i ) } \right) L(θ)B(θ,θ(i)),即函数 B ( θ , θ ( i ) ) B \left( \theta , \theta ^ { ( i ) } \right) B(θ,θ(i)) L ( θ ) L(\theta) L(θ)的一个下界。由上式可知 L ( θ ( i ) ) = B ( θ ( i ) , θ ( i ) ) L \left( \theta ^ { ( i ) } \right) = B \left( \theta ^ { ( i ) } , \theta ^ { ( i ) } \right) L(θ(i))=B(θ(i),θ(i))

选择 θ ( i + 1 ) \theta^{(i+1)} θ(i+1)使 B ( θ , θ ( i ) ) B \left( \theta , \theta ^ { ( i ) } \right) B(θ,θ(i))达到极大,即
θ ( i + 1 ) = arg ⁡ max ⁡ θ B ( θ , θ ( i ) ) = arg ⁡ max ⁡ θ ( L ( θ ( i ) ) + ∑ Z P ( Z ∣ Y , θ ( i ) ) log ⁡ P ( Y ∣ Z , θ ) P ( Z ∣ θ ) P ( Z ∣ Y , θ ( i ) ) P ( Y ∣ θ ( i ) ) ) = arg ⁡ max ⁡ θ ( ∑ Z P ( Z ∣ Y , θ ( i ) ) log ⁡ ( P ( Y ∣ Z , θ ) P ( Z ∣ θ ) ) ) = arg ⁡ max ⁡ θ ( ∑ Z P ( Z ∣ Y , θ ( i ) ) log ⁡ P ( Y , Z ∣ θ ) ) = arg ⁡ max ⁡ θ Q ( θ , θ ( i ) ) \begin{aligned} \theta ^ { ( i + 1 ) } & = \arg \max _ { \theta } B \left( \theta , \theta ^ { ( i ) } \right) \\ & = \arg \max _ { \theta } \left( L \left( \theta ^ { ( i ) } \right) + \sum _ { Z } P \left( Z \mid Y , \theta ^ { ( i ) } \right) \log \frac { P ( Y \mid Z , \theta ) P ( Z \mid \theta ) } { P \left( Z \mid Y , \theta ^ { ( i ) } \right) P \left( Y \mid \theta ^ { ( i ) } \right) } \right) \\ & = \arg \max _ { \theta } \left( \sum _ { Z } P \left( Z \mid Y , \theta ^ { ( i ) } \right) \log ( P ( Y \mid Z , \theta ) P ( Z \mid \theta ) ) \right) \\ & = \arg \max _ { \theta } \left( \sum _ { Z } P \left( Z \mid Y , \theta ^ { ( i ) } \right) \log P ( Y , Z \mid \theta ) \right) \\ & = \arg \max _ { \theta } Q \left( \theta , \theta ^ { ( i ) } \right) \end{aligned} θ(i+1)=argθmaxB(θ,θ(i))=argθmax(L(θ(i))+ZP(ZY,θ(i))logP(ZY,θ(i))P(Yθ(i))P(YZ,θ)P(Zθ))=argθmax(ZP(ZY,θ(i))log(P(YZ,θ)P(Zθ)))=argθmax(ZP(ZY,θ(i))logP(Y,Zθ))=argθmaxQ(θ,θ(i))
即求 Q Q Q函数及其极大化。EM算法是通过不断求解下界的极大化逼近求解对数似然函数极大化的算法。

EM算法可以用于生成模型的无监督学习。生成模型由联合概率分布 P ( X , Y ) P(X,Y) P(X,Y)表示,可以认为无监督学习训练数据是联合概率分布产生的数据。 X X X为观测数据, Y Y Y为未观测数据。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
pytorch是一个开源的机器学习框架,它提供了丰富的工具和函数来实现机器学习算法。它支持多种机器学习算法的实现,包括但不限于线性回归、逻辑回归、支持向量机、决策树、随机森林、神经网络等。你可以使用pytorch的内置计算函数或基本的运算符号来进行计算操作,同时pytorch还提供了优化器和损失函数来帮助优化模型的训练过程。 在pytorch中,你可以使用tensor类型的数据进行机器学习算法的实现。如果有批处理的需求,你可以使用dataloader来加载和处理数据。你可以建立模型并完成训练过程,包括前向传播、损失反向传播和参数优化。你还可以使用matplotlib等库进行数据可视化,以便更好地理解和分析模型的结果。 总结起来,pytorch提供了丰富的工具和函数,使得机器学习算法的实现变得简单而高效。你可以根据自己的需求选择合适的算法,并使用pytorch来进行模型的训练和优化。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [机器学习学习源码包含几乎所有机器学习算法pytorch实现源码.zip](https://download.csdn.net/download/DeepLearning_/88195974)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [机器学习PyTorch——1.基本运算](https://blog.csdn.net/qq_52914337/article/details/122377696)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Pytorch机器学习经典算法——线性回归](https://blog.csdn.net/qq_42887663/article/details/125667340)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值