1-模型介绍
Hypothesis:
h θ ( x ) = θ 0 + θ 1 x 1 + . . . + θ n x n \displaystyle h_\theta(x)=\theta_0 + \theta_1x_1 + ...+\theta_nx_{n} hθ(x)=θ0+θ1x1+...+θnxn
Parameters:
θ 0 , θ 1 , . . . , θ n \theta_0,\theta_1,...,\theta_n θ0,θ1,...,θn
Cost Function:
J ( θ 0 , θ 1 , . . . , θ n ) = 1 2 m ∑ i = 1 m ( h θ ( x ( i ) − y ( i ) ) 2 \displaystyle J(\theta_0,\theta_1,...,\theta_n)=\frac{1}{2m}\sum_{i=1}^{m}(h_\theta(x^{(i)}-y^{(i)})^2 J(θ0,θ1,...,θn)=2m1i=1∑m(hθ(x(i)−y(i))2
Goal:
m i n i m i z e θ 0 , θ 1 , . . . , θ n J ( θ 0 , θ 1 , . . . , θ n ) \displaystyle minimize_{_{\theta_0,\theta_1,...,\theta_n}}J(\theta_0,\theta_1,...,\theta_n) minimizeθ0,θ1,...,θnJ(θ0,θ1,...,θn)
Hypothesis函数
是我们需要拟合的函数,一般称之为假设函数,我需要做的是通过给定的数据来找到参数 θ 0 ,   θ 1 \theta_0,\:\theta_1 θ0,θ1来确定一条直线,使数据中尽可能多的点落在直线周围上面的描述只是一个简单的只有两个参数的问题.
Cost函数
cost函数,计算实际值和观测值之间的误差的平方,用这个值来表示拟合函数的代价,代价越小,则说明拟合的函数越贴合数据.
Goal
因此我们需要做的是使代价函数最小.
实现方式
实际运用中我们通常写成向量形式,方便计算.通常我们可以表示为矩阵形式
假设函数:
h θ ( x ) = θ T X , 其 中 θ = [ θ 0 , θ 1 , . . . , θ n ] T , X = [ 1 , x 1 , x 2 , . . . , x n ] \displaystyle h_\theta(x)=\theta^TX,其中\theta = [\theta_0, \theta_1,...,\theta_n]^T ,X=[1,x_1,x_2,...,x_n] hθ(x)=θTX,其中θ=[θ0,θ1,...,θn]T,X=[1,x1,x2,...,xn]
Cost 函数计算
J
(
θ
0
,
θ
1
,
.
.
.
,
θ
n
)
=
1
2
m
e
r
r
o
r
T
∗
e
r
r
o
r
J(\theta_0,\theta_1,...,\theta_n)=\frac{1}{2m}error^T*error
J(θ0,θ1,...,θn)=2m1errorT∗error
e
r
r
o
r
=
θ
T
X
−
Y
error=\theta^TX-Y
error=θTX−Y
Y
=
[
y
0
,
y
1
,
.
.
.
,
y
n
]
Y = [y_0,y_1,...,y_n]
Y=[y0,y1,...,yn]
2- 梯度下降算法
接下来就是如何寻找一组合适的 [ θ 0 , θ 1 , . . . , θ n ] [\theta_0,\theta_1,...,\theta_n] [θ0,θ1,...,θn]来确定我们的假设函数的值.梯度下降算法是一种求解局部最优值的方法.对于 f ( x ) f(x) f(x),在 a a a点的梯度是 f ( x ) f(x) f(x)增长最快的方向,那么相反的方向则是该点下降最快的方向.具体公式如下
∂ ∂ θ j J ( θ 0 , . . . , θ n ) = ∂ ∂ θ j 1 2 ∑ j = 1 m ( h θ ( x ) − y ) 2 = ( h θ ( x ) − y ) x ( i ) \frac{\partial}{\partial\theta_j}J(\theta_0,...,\theta_n)=\frac{\partial}{\partial\theta_j} \frac{1}{2}\sum_{j=1}^{m}(h_\theta(x)-y)^2=(h_\theta(x)-y)x^{(i)} ∂θj∂J(θ0,...,θn)=∂θj∂21j=1∑m(hθ(x)−y)2=(hθ(x)−y)x(i)
下面是参数更新过程
θ j = θ j − α ∂ ∂ θ J ( θ ) = θ j − α ( h θ ( x ) − y ) x ( i ) \theta_j = \theta_j-\alpha\frac{\partial}{\partial\theta }J(\theta)=\theta_j-\alpha(h_\theta(x)-y)x^{(i)} θj=θj−α∂θ∂J(θ)=θj−α(hθ(x)−y)x(i)
对于线性回归问题,我们的假设函数是线性函数,因此我们的参数更新过程可以写成向量形式:
∂ ∂ θ j J ( θ 0 , . . . , θ n ) = 1 m X T ( θ T − Y ) \frac{\partial}{\partial\theta_j}J(\theta_0,...,\theta_n)=\frac{1}{m}X^T(\theta^T-Y) ∂θj∂J(θ0,...,θn)=m1XT(θT−Y)