Machine Learning|吴恩达 公式总结(1)【白话公式推导版】

Machine Learning|吴恩达 公式总结(1)

多元线性回归(Multivariate Linear Regression)

【原课程地址:https://www.coursera.org/learn/machine-learning/home/welcome。本系列笔记只关注课程中涉及的算法公式。因为课程中对许多算法只提及结果不进行过程推导。而本人数学较差,微积分、概率统计这些当初让人伤感的课程现在感觉就仿佛从没学过一样。但通过自己推导过的相关算法过程,尽量在理解清晰的情况下展现记录下来,以供类似我这样数学水平的后来者可以参考。】

申明:
x j ( i ) =第 i t h 个训练样本的第j个特征(value of feature j in the i t h training)
x ( i ) =第 i t h 训练样本,含其所有特征(the input (features) of the i t h training )
m = 训练样本的总数(the number of training examples)
n = 特征总数(the number of features)

x ( i ) R ( n ) ; 1 < j n ; 1 < i m
方程式:

h θ x ( i ) = θ 0 + θ 1 x 1 ( i ) + θ 2 x 2 ( i ) + θ 3 x 3 ( i ) + + θ n x n ( i )

假设(Assume): x 0 i = 1 ; x ( i ) R ( n + 1 )
方程式的向量表述(vector presentation):
h θ ( x ) = [ θ 0 θ 1 θ n ] [ x 0 x 1 x n ] = θ T x

损失函数(cost function):

J ( θ 0 , θ 1 , , θ m ) = 1 2 m i = 1 m ( h θ ( x ( i ) ) y ( i ) ) 2

梯度下降公式(Gradient Descent):

( x 0 i = 1 )

θ j := θ j α 1 m i = 1 m ( h θ ( x ( i ) ) y ( i ) ) x j ( i )

偏导推导过程(partial derivative work processes):

e . g . , [ ( a x + b ) n ] = n ( a x + b ) ( a x + b ) = a n ( a x + b )

扫描二维码关注公众号,回复: 1585344 查看本文章

θ j [ ( h θ ( x ( i ) ) y ( i ) ) 2 ] = 2 ( h θ ( x ( i ) ) y ( i ) ) θ j ( h θ ( x ( i ) ) y ( i ) )
= 2 ( h θ ( x ( i ) ) y ( i ) ) θ j ( θ 0 x 0 ( i ) + θ 1 x 1 ( i ) + + θ j x j ( i ) + + θ n x n ( i ) y ( i ) )
(all the θ 0 x 0 ( i ) + θ 1 x 1 ( i ) + + θ j x j ( i ) + + θ n x n ( i ) y ( i ) are constants otherwise θ j x j ( i ) ,and derivative constant will be zero)
= 2 ( h θ ( x ( i ) ) y ( i ) ) θ j ( θ j x j ( i ) + c o n s t a n t )
= 2 ( h θ ( x ( i ) ) y ( i ) ) x j ( i )

θ j [ 1 2 m i = 1 m ( h θ ( x ( i ) ) y ( i ) ) 2 ] = 1 2 m 2 i = 1 m ( h θ ( x ( i ) ) y ( i ) ) x j ( i )

正规化方程(Normal Equation)

(即函数求解法)在“正规化方程”方法里,我们最小化损失函数J,通过设定 θ j = 0 .这将使我们可以直接求得最优 θ ,而无需进行迭代式的训练过程。方程如下:
In the “Normal Equation” method, we will minimize J by explicitly taking its derivatives with respect to the θj ’s, and setting them to zero. This allows us to find the optimum theta without iteration. The normal equation formula is given below:

θ = ( X T X ) 1 X T y

“正规化方程”法,无需提前对特征进行缩放,下面我们对梯度下降法与正规化方程法的优缺点进行对比:
There is no need to do feature scaling with the normal equation.
The following is a comparison of gradient descent and the normal equation:

梯度下降Gradient Descent 正规化方程Normal Equation
需要选择一个学习率 α Need to choose alpha 无需设定学习率No need to choose alpha
需要进行迭代训练 Needs many iterations 没有训练过程 No need to iterate
计算复杂度: O ( k n 2 ) O ( n 3 ) , need to calculate inverse of X T X (需要计算 X T X 的逆举证,计算复杂度高)
当特征量n很大时仍能很好的工作Works well when n is large n很大时运算量指数增长,运算变得十分缓慢Slow if n is very large

Atention: Sometimes, X T X will be noninvertible,the common causes might be having :
X T X 有可能不可逆,可能会是以下原因:
- 重复的特征,比如两个特征的特征值存在线性依赖(i.e. x2=x1*3+2,x2=x1*5.5 但x2=x1^2属于非线性而不包含在内)
- 特征量太多 (e.g. m ≤ n特征量大于等于样本数量). 这种情况删除一些特征或使用“正则化”(regularization) (to be explained in a later lesson).
【删除多余特征,或者说叫特征降维,可以采用PCA主成分分析法,后面的课程有介绍】

逻辑回归(Logistic Regression)

h θ ( x ) = g ( θ T x )
z = θ T x
g ( z ) = 1 1 + e z
合并后公式 : h θ ( x ) = 1 1 + e θ T x
The following image shows us what the sigmoid function looks like:
sigmoid的函数图像,注意它的上下限:
The following image shows us what the sigmoid function looks like:

Cost Function:

在逻辑回归中,我们无法使用相同的cost函数就像我们在线性回归中做的那样。因为,逻辑回归函数会产生输出是波浪曲线,产生大量的局部最优区。换句话说,它是一个非凸函数。因此,我们设定逻辑回归的损失函数如下:

J ( θ ) = 1 m i = 1 m [ y ( i ) l o g ( h θ ( x ( i ) ) ) + ( 1 y ( i ) ) l o g ( 1 h θ ( x ( i ) ) ) ]

i f y ( i ) = 0 : J ( θ ) = 1 m i = 1 m [ ( 1 y ( i ) ) l o g ( 1 h θ ( x ( i ) ) ) ]
i f y ( i ) = 1 : J ( θ ) = 1 m i = 1 m [ y ( i ) l o g ( h θ ( x ( i ) ) ) ]

公式的向量化表达式:
h = g ( X θ )
J ( θ ) = 1 m   ( y T l o g ( h ) ( 1 y ) T l o g ( 1 h ) )

Gradient Descent:

α = 学习率(learning rate)

R e p e a t {

θ j := θ j α m i = 1 m ( h θ ( x ( i ) ) y ( i ) ) x j ( i )

}

偏导计算过程:

注意:课程公式中所有的log(x),都是指ln(x)或 ‘ l o g e ( x )
u = f ( x )
[ g ( u ) ] = g ( u ) f ) 根据复合函数求导公式,以后的推导会用到以下公式
[ a f ( x ) ] = a f ( x )
[ l o g ( f ( x ) ) ] = 1 f ( x ) f ( x )
[ a f ( x ) ] = f ( x ) a ( f ( x ) 1 ) f ( x )
[ e f ( x ) ] = e f ( x ) f ( x )
[ a + b f ( x ) ] = b f ( x )

θ j [ h θ ( x ( i ) ) ] = ( 1 1 + e θ T x ( i ) ) = [ ( 1 + e θ T x ( i ) ) 1 ] = 1 ( 1 + e θ T x ( i ) ) 2 ( 1 + e θ T x ( i ) )
= 1 ( 1 + e θ T x ( i ) ) 2 ( e θ T x ( i ) ) = 1 ( 1 + e θ T x ( i ) ) 2 ( e θ T x ( i ) ) ( θ T x ( i ) )
(回顾线性回归损失函数,式子 θ 0 x 0 ( i ) + θ 1 x 1 ( i ) + + θ j x j ( i ) + + θ n x n ( i ) y ( i ) 的所有项都是常数,除了 θ j x j ( i ) ,常数的导数是0)
= 1 ( 1 + e θ T x ( i ) ) 2 ( e θ T x ( i ) ) ( x j ( i ) )
= 1 ( 1 + e θ T x ( i ) ) e θ T x ( i ) ( 1 + e θ T x ( i ) ) x j ( i )
= 1 ( 1 + e θ T x ( i ) ) ( e θ T x ( i ) + 1 ) 1 ( 1 + e θ T x ( i ) ) x j ( i )
( r e c a l l : h θ ( x ) = 1 1 + e θ T x )
= h θ ( x ) ( 1 h θ ( x ) ) x j ( i )

合并(combin):

θ j [ y ( i ) l o g ( h θ ( x ( i ) ) ) + ( 1 y ( i ) ) l o g ( 1 h θ ( x ( i ) ) ) ]

= y ( i ) ( h θ ( x ( i ) ) ) h θ ( x ( i ) ) + ( 1 y ( i ) ) 1 1 h θ ( x ( i ) ) ( 1 h θ ( x ( i ) ) )
= y ( i ) ( h θ ( x ( i ) ) ) h θ ( x ( i ) ) + ( 1 y ( i ) ) ( h θ ( x ( i ) ) ) 1 h θ ( x ( i ) )
= ( h θ ( x ( i ) ) ) y ( i ) h θ ( x ( i ) ) h θ ( x ( i ) ) ( 1 h θ ( x ( i ) ) )
= ( h θ ( x ( i ) ) y ( i ) ) x j ( i )

θ j := θ j α [ θ j J ( θ ) ]
θ j := θ j α [ 1 m i = 1 m [ y ( i ) l o g ( h θ ( x ( i ) ) ) + ( 1 y ( i ) ) l o g ( 1 h θ ( x ( i ) ) ) ] ]
θ j := θ j α m i = 1 m ( h θ ( x ( i ) ) y ( i ) ) x j ( i )


中英对照:
Multivariate Linear Regression:多元线性回归
cost function:损失函数
Gradient Descent:梯度下降
Logistic Regression:逻辑回归

猜你喜欢

转载自blog.csdn.net/weixin_40920228/article/details/80633411