吴恩达机器学习课程代码笔记02
代码
在第四节课Linear Regression with Multiple Variables中,实现normal equation的一行代码。代码本身没啥难度,主要难点在于公式怎么推导出来的。
%----X = pinv(A),求矩阵 A 的广义逆矩阵
%----不使用 inv() 的原因在于不确定矩阵 A 是否为方阵
pinv(X'*x)*x'*y
normal equation的推导
在吴恩达老师的机器学习课程中,直接就给出了如下normal equation,无推导过程,像博主这种菜鸡直接懵逼,于是赶紧上网学习了一番,内化(自我感觉…)了三个推导方法后,分享于此,如有错误,还望指正。
θ=(XTX)−1XTy
方法一:矩阵求导法
这个方法来源于吴恩达老师的CS229 machine learning中的课程PDF资料,这门课程与Coursera上的同名课程(也就是大部分人看的公开课版本)有所不同,需要更高的数学水平,更注重公式的推导。
推导过程如下:
▽θJ(θ)=▽θ2m1i=1∑m(hθ(x(i))−y(i))2=▽θ21(Xθ−y)T(Xθ−y)
∵(A±B)T=AT+BTand(A×B)T=BT×AT∴▽θJ(θ)=21▽θ(θTXTXθ−θTXTy−yTXθ+yTy)∵如果α是一个实数,则tr(α)=α又∵θ是(n×1)的矩阵,X是(m×n)的矩阵,y是(m×1)的向量∴θTXTXθ的维数变换(1×n)×(n×m)×(m×n)×(n×1)得到一个(1×1)的方阵,即一个实数同理,θTXTy,yTXθ,yTy维数变换后得到的都是一个实数∵yTy是一个与θ无关的常量,对θ求偏导为0,则在式子中可以省略掉∴▽θJ(θ)=21▽θtr(θTXTXθ−θTXTy−yTXθ)∵tr(A±B)=A±Bandtr(A)=tr(AT)∴tr(θTXTy)=tr((Xθ)Ty)=tr(yXθ)∴▽θJ(θ)=21▽θ(tr(θTXTXθ)−2tr(yTXθ))∵tr内还是实数,则去掉tr对式子没影响∴▽θJ(θ)=21▽θ(θTXTXθ−2yTXθ)∵矩阵求导中,∂X∂bTAX=ATband∂X∂XTAX=(A+AT)X∴▽θJ(θ)=21(2XTXθ−2XTy)=XTXθ−XTy令▽θJ(θ)=0,则可以得到θ=(XTX)−1XTy
方法二:演绎法
这个方法比较简单易懂,整体思想就是从一条式子演绎展开为矩阵。先做如下假设:
损失函数为:
J(θ0,θ1,θ2...,θn)=2m1i=1∑m(hθ(x(i))−y(i))2
我们的目标是在不同的组合下,损失函数值能达到最小。所以通过对每个求偏导,求出对应偏导数等于0的每个值,这就是最终的组合。求偏导的过程如下:
以θ0的公式为例:m1i=1∑m(hθ(x(i))−y(i))x0(i)=0转换为矩阵运算:
将第二部分展开:
将(5)带入式子,得:
将(2)带入式子,得:
同理,
通过矩阵将这些式子结合在一起,
舍去m1,然后因为
所以上述(6)化简为:
XT(Xθ−y)=0XTXθ−XTy=0∴θ=(XTX)−1XTy
方法三:链式求导法
这个属于懒人必备的一个方法:
∂θ∂J(θ)=21∂θ∂(Xθ−y)T(Xθ−y)=21∂(Xθ−y)∂(Xθ−y)T(Xθ−y)∂θ∂(Xθ−y)=(Xθ−y)TX=0∴θTXTX=yTX又∵式子两边维数变换得到的是一个实数∴tr(θTXTX)=tr(yTX)tr(XTXθ)=tr(XTy)XTXθ=XTy∴θ=(XTX)−1XTy
Reference
https://www.cnblogs.com/AngelaSunny/p/6616712.html
https://www.cnblogs.com/crackpotisback/p/5545708.html
https://blog.****.net/zhangbaodan1/article/details/81013056
https://blog.****.net/daaikuaichuan/article/details/80620518