机器学习算法第二篇
主要内容:多项式回归模型之梯度下降法算法展开
通俗的讲:
算法目的:
- 求通过训练集计算出一个最合适的线\面\超平面
h
θ
(
x
)
h_\theta(x)
hθ(x),使所有点到它的距离(误差)之和最小,
将测试的特征向量带入该 h θ ( x ) h_\theta(x) hθ(x)所得的值即为预测值
步骤:
-
设一个目标函数(线\面\超平面) h θ ( x ) = θ 1 x i ( 1 ) + θ 2 x i ( 2 ) + . . . + θ n x i ( n ) h_\theta(x) = \theta_1x_i^{(1)}+\theta_2x_i^{(2)} +...+\theta_nx_i^{(n)} hθ(x)=θ1xi(1)+θ2xi(2)+...+θnxi(n) ,一个n维特征带入后可得到预测值 y ^ i \hat y_i y^i
-
设一个损失函数 J ( θ 1 , θ 2 . . . θ n ) = 1 2 m ∑ i = 1 m ( y ^ i − y i ) 2 J(\theta_1,\theta_2...\theta_n)=\frac{1}{2m}\sum_{i=1}^m (\hat y_i-y_i)^2 J(θ1,θ2...θn)=2m1∑i=1m(y^i−yi)2,它表示所有点预测值 y ^ i \hat y_i y^i到真实值y之间的误差的平方和的平均,数学上可以代表目标函数的好坏,即拟合度的高低
-
所以我们目标可以理解为在学习阶段:求合适的 ( θ 1 , θ 2 . . . θ n ) (\theta_1,\theta_2...\theta_n) (θ1,θ2...θn)使损失函数最小
-
带入训练集数据,通过梯度下降法或标准方程法解出最合适的 ( θ 1 , θ 2 . . . θ n ) (\theta_1,\theta_2...\theta_n) (θ1,θ2...θn),然后带入线\面\超平面函数 h θ ( x ) = θ 1 x i ( 1 ) + θ 2 x i ( 2 ) + . . . + θ n x i ( n ) h_\theta(x) = \theta_1x_i^{(1)}+\theta_2x_i^{(2)} +...+\theta_nx_i^{(n)} hθ(x)=θ1xi(1)+θ2xi(2)+...+θnxi(n) 得到目标函数(回归函数)
-
把需要测试的数据输入该回归函数,可得到预测值(也可测试回归函数的拟合度,即参数的好坏)
展开来说:
-
有训练集 T = { ( x 1 , y 1 ) , ( x 2 , y 2 ) , ( x 3 , y 3 ) . . . ( x m , y m ) } 由 P ( X , Y ) 独 立 同 分 布 产 生 T=\{(x_1,y_1),(x_2,y_2),(x_3,y_3)...(x_m,y_m) \}由P(X,Y)独立同分布产生 T={(x1,y1),(x2,y2),(x3,y3)...(xm,ym)}由P(X,Y)独立同分布产生
T : [ x 1 ( 1 ) x 1 ( 2 ) . . . x 1 ( n ) y 1 x 2 ( 1 ) x 2 ( 2 ) . . . x 2 ( n ) y 2 x 3 ( 1 ) x 3 ( 2 ) . . . x 3 ( n ) y 3 . . . . . . . . . . . . . . x m ( 1 ) x m ( 2 ) . . . x m ( n ) y m ] T:\begin{bmatrix} x_1^{(1)} & x_1^{(2)} \quad ...&x_1^{(n)}& y_1 \\ x_2^{(1)} & x_2^{(2)} \quad...&x_2^{(n)}& y_2\\ x_3^{(1)} & x_3^{(2)} \quad...&x_3^{(n)}& y_3 \\ . & . \quad...& .& . \\ . & . \quad...& .& . \\ x_m^{(1)} & x_m^{(2)} \quad...&x_m^{(n)}& y_m \\ \end{bmatrix} T:⎣⎢⎢⎢⎢⎢⎢⎢⎡x1(1)x2(1)x3(1)..xm(1)x1(2)...x2(2)...x3(2)...........xm(2)...x1(n)x2(n)x3(n)..xm(n)y1y2y3..ym⎦⎥⎥⎥⎥⎥⎥⎥⎤ -
输入空间 X ⊊ R ∗ \mathscr {X}\subsetneq R^* X⊊R∗为n维向量的集合
-
输出空间为标记集合 Y = R \mathscr{Y}=R Y=R,为全体实数标量集合
-
输入为特征向量 x i ( x i ( 1 ) , x i ( 2 ) . . . x i ( n ) ) ∈ X x_i(x_i^{(1)},x_i^{(2)}...x_i^{(n)})\in\mathscr {X} xi(xi(1),xi(2)...xi(n))∈X
-
输出为类标记 y ∈ Y y\in\mathscr{Y} y∈Y
-
X是定义在输入空间 X \mathscr {X} X上的随机向量
-
Y是定义在输入空间 Y \mathscr {Y} Y上的随机变量
-
设回归函数 h θ ( x ) = θ 1 x i ( 1 ) + θ 2 x i ( 2 ) + . . . + θ n x i ( n ) h_\theta(x)=\theta_1x_i^{(1)}+\theta_2x_i^{(2)} +...+\theta_nx_i^{(n)} hθ(x)=θ1xi(1)+θ2xi(2)+...+θnxi(n)
任意点 P i ( x i ( 1 ) , x i ( 2 ) . . . x i ( n ) ) P_i(x_i^{(1)},x_i^{(2)}...x_i^{(n)}) Pi(xi(1),xi(2)...xi(n))带入该式可得到点 P i P_i Pi到预测值 y ^ i \hat y_i y^i -
带入多项式模型的损失函数 J ( θ 1 , θ 2 . . . θ n ) = 1 2 m ∑ i = 1 m ( y ^ i − y i ) 2 J(\theta_1,\theta_2...\theta_n)=\frac{1}{2m}\sum_{i=1}^m (\hat y_i-y_i)^2 J(θ1,θ2...θn)=2m1∑i=1m(y^i−yi)2
-
即 (1) J ( θ 1 , θ 2 . . . θ n ) = 1 2 m ∑ i = 1 m ( h θ ( x ) − y i ) 2 J(\theta_1,\theta_2...\theta_n)=\frac{1}{2m}\sum_{i=1}^m (h_\theta(x)-y_i)^2\tag 1 J(θ1,θ2...θn)=2m1i=1∑m(hθ(x)−yi)2(1)
-
在训练阶段, 代价函数的x 与y 都是已知量 θ \theta θ为变量量
因此算法所述的目标等价于求损失函数 J ( θ 1 , θ 2 . . . θ n ) J(\theta_1,\theta_2...\theta_n) J(θ1,θ2...θn)的值取最小值时候的变量 ( θ 1 , θ 2 . . . θ n ) (\theta_1,\theta_2...\theta_n) (θ1,θ2...θn) -
用梯度下降法求出最适合的 ( θ 1 , θ 2 . . . θ n ) (\theta_1,\theta_2...\theta_n) (θ1,θ2...θn)
梯度下降法求解过程:
- 概念:
通过将各变量 ( θ 1 , θ 2 . . . θ n ) (\theta_1,\theta_2...\theta_n) (θ1,θ2...θn)不断朝函数取得极值时的变量 ( ω 1 , ω 2 . . . ω n ) (\omega_1,\omega_2...\omega_n) (ω1,ω2...ωn)方向靠拢,从而获得代价函数取最小值时候的各 ( θ 1 , θ 2 . . . θ n ) (\theta_1,\theta_2...\theta_n) (θ1,θ2...θn)参数
方法一:
方法一更易于理解,但计算复杂度较高,求导难度大
\
- 对变量
(
θ
1
,
θ
2
.
.
.
θ
n
)
(\theta_1,\theta_2...\theta_n)
(θ1,θ2...θn)进行初始随机赋值
θ 1 = 1 , θ 1 = 5 , θ 3 = 0.5 , θ n = 10 \theta_1=1, \\ \theta_1=5,\\ \theta_3=0.5,\\ \theta_n=10 θ1=1,θ1=5,θ3=0.5,θn=10
\
2. 对函数的表达式的逐个变量
(
θ
1
,
θ
2
.
.
.
θ
n
)
(\theta_1,\theta_2...\theta_n)
(θ1,θ2...θn)求偏导,得到
∇
J
(
θ
1
,
θ
2
.
.
.
θ
n
)
=
(
∂
f
∂
θ
1
,
∂
f
∂
θ
2
,
∂
f
∂
θ
3
.
.
.
∂
f
∂
θ
i
)
∇J(\theta_1,\theta_2...\theta_n)=\left(\frac{\partial f}{\partial\theta_1},\frac{\partial f}{\partial\theta_2},\frac{\partial f}{\partial\theta_3}...\frac{\partial f}{\partial\theta_i}\right)
∇J(θ1,θ2...θn)=(∂θ1∂f,∂θ2∂f,∂θ3∂f...∂θi∂f)
∂
f
∂
θ
1
=
1
m
∑
i
=
1
m
(
h
θ
(
x
i
)
−
y
i
)
x
i
(
1
)
∂
f
∂
θ
2
=
1
m
∑
i
=
1
m
(
h
θ
(
x
i
)
−
y
i
)
x
i
(
2
)
.
.
.
.
.
.
∂
f
∂
θ
n
=
1
m
∑
i
=
1
m
(
h
θ
(
x
i
)
−
y
i
)
x
i
(
n
)
\frac{\partial f}{\partial\theta_1}=\frac{1}{m}\sum_{i=1}^m\left(h_\theta(x_i)-y_i\right)x_i^{(1)} \\ \frac{\partial f}{\partial\theta_2}=\frac{1}{m}\sum_{i=1}^m\left(h_\theta(x_i)-y_i\right)x_i^{(2)}\\ ...\\...\\ \frac{\partial f}{\partial\theta_n}=\frac{1}{m}\sum_{i=1}^m\left(h_\theta(x_i)-y_i\right)x_i^{(n)}
∂θ1∂f=m1i=1∑m(hθ(xi)−yi)xi(1)∂θ2∂f=m1i=1∑m(hθ(xi)−yi)xi(2)......∂θn∂f=m1i=1∑m(hθ(xi)−yi)xi(n)
\
-
设定学习率 控制每次迭代 变量的移动距离
l R = 0.001 , 该 参 数 为 示 例 , 应 按 照 实 际 情 况 调 整 lR=0.001,该参数为示例,应按照实际情况调整 lR=0.001,该参数为示例,应按照实际情况调整
\ -
分别对每个变量进行迭代
θ 1 = θ 1 − l R ∂ f ∂ θ 1 θ 2 = θ 2 − l R ∂ f ∂ θ 2 θ 3 = θ 3 − l R ∂ f ∂ θ 3 . . . . . . θ m = θ m − l R ∂ f ∂ θ m 1 \theta_1=\theta_1-lR\frac{\partial f}{\partial\theta_1}\\\theta_2=\theta_2-lR\frac{\partial f}{\partial\theta_2}\\\theta_3=\theta_3-lR\frac{\partial f}{\partial\theta_3}\\...\\...\\\theta_m=\theta_m-lR\frac{\partial f}{\partial\theta_m1} θ1=θ1−lR∂θ1∂fθ2=θ2−lR∂θ2∂fθ3=θ3−lR∂θ3∂f......θm=θm−lR∂θm1∂f
\
5.重复第四步到足够次数,可得到距离代价函数取最小值时候的变量 ( ω 1 , ω 2 . . . ω n ) (\omega_1,\omega_2...\omega_n) (ω1,ω2...ωn)极为接近的变量 ( θ 1 , θ 2 . . . θ n ) (\theta_1,\theta_2...\theta_n) (θ1,θ2...θn)
方法二
方法二:将代价函数矩阵化,从而极大化简计算复杂度和求导复杂度
- 子式矩阵化
-
θ = [ θ 1 θ 2 θ 3 . . θ m ] \theta= \begin{bmatrix} \theta_1 \\ \theta_2\\ \theta_3 \\ . \\ . \\ \theta_m \\ \end{bmatrix} θ=⎣⎢⎢⎢⎢⎢⎢⎡θ1θ2θ3..θm⎦⎥⎥⎥⎥⎥⎥⎤
-
J ( θ 1 , θ 2 . . . θ n ) ⇒ J ( θ ) J(\theta_1,\theta_2...\theta_n) \Rightarrow J(\theta) J(θ1,θ2...θn)⇒J(θ)
-
y i ⇒ Y = y _ d a t a = [ y 1 y 2 y 3 . . y m ] y_i\Rightarrow Y=y\_data=\begin{bmatrix} y_1 \\ y_2\\ y_3 \\ . \\ . \\ y_m \\ \end{bmatrix} yi⇒Y=y_data=⎣⎢⎢⎢⎢⎢⎢⎡y1y2y3..ym⎦⎥⎥⎥⎥⎥⎥⎤
-
x i ⇒ X = x _ d a t a = [ x 1 ( 1 ) x 1 ( 2 ) . . . x 1 ( n ) x 2 ( 1 ) x 2 ( 2 ) . . . x 2 ( n ) x 3 ( 1 ) x 3 ( 2 ) . . . x 3 ( n ) . . . . . . . . . . . . x m ( 1 ) x m ( 2 ) . . . x m ( n ) ] x_i\Rightarrow X=x\_data=\begin{bmatrix} x_1^{(1)} & x_1^{(2)} \quad ...&x_1^{(n)} \\ x_2^{(1)} & x_2^{(2)} \quad...&x_2^{(n)}\\ x_3^{(1)} & x_3^{(2)} \quad...&x_3^{(n)} \\ . & . \quad...& . \\ . & . \quad...& . \\ x_m^{(1)} & x_m^{(2)} \quad...&x_m^{(n)} \\ \end{bmatrix} xi⇒X=x_data=⎣⎢⎢⎢⎢⎢⎢⎢⎡x1(1)x2(1)x3(1)..xm(1)x1(2)...x2(2)...x3(2)...........xm(2)...x1(n)x2(n)x3(n)..xm(n)⎦⎥⎥⎥⎥⎥⎥⎥⎤
-
h θ ( x i ) ⇒ [ h θ ( x 1 ) h θ ( x 2 ) h θ ( x 3 ) . . h θ ( x m ) ] = [ θ 1 x 1 ( 1 ) + θ 2 x 1 ( 2 ) + . . . + θ n x 1 ( n ) θ 1 x 2 ( 1 ) + θ 2 x 2 2 ) + . . . + θ n x 2 ( n ) θ 1 x 3 ( 1 ) + θ 2 x 3 ( 2 ) + . . . + θ n x 3 ( n ) . . . . . . . . θ 1 x m ( 1 ) + θ 2 x m ( 2 ) + . . . + θ n x m ( n ) ] = X ⋅ θ h_\theta(x_i)\Rightarrow \begin{bmatrix} h_\theta (x_1) \\ h_\theta (x_2) \\ h_\theta (x_3 ) \\ . \\ . \\ h_\theta (x_m) \\ \end{bmatrix}=\begin{bmatrix} \theta_1x_1^{(1)}+\theta_2x_1^{(2)} +...+\theta_nx_1^{(n)}\\ \theta_1x_2^{(1)}+\theta_2x_2 ^{2)}+...+\theta_nx_2^{(n)}\\ \theta_1x_3^{(1)}+\theta_2x_3 ^{(2)}+...+\theta_nx_3^{(n)}\\ \quad... . \\ \quad... .\\ \theta_1x_m^{(1)}+\theta_2x_m^{(2)} +...+\theta_nx_m^{(n)}\\ \end{bmatrix}=X \cdot \theta hθ(xi)⇒⎣⎢⎢⎢⎢⎢⎢⎡hθ(x1)hθ(x2)hθ(x3)..hθ(xm)⎦⎥⎥⎥⎥⎥⎥⎤=⎣⎢⎢⎢⎢⎢⎢⎢⎡θ1x1(1)+θ2x1(2)+...+θnx1(n)θ1x2(1)+θ2x22)+...+θnx2(n)θ1x3(1)+θ2x3(2)+...+θnx3(n)........θ1xm(1)+θ2xm(2)+...+θnxm(n)⎦⎥⎥⎥⎥⎥⎥⎥⎤=X⋅θ
-
∂ f ∂ θ i ⇒ ∇ J ( θ ) = [ ∂ f ∂ θ 1 ∂ f ∂ θ 2 ∂ f ∂ θ 3 . . ∂ f ∂ θ n ] \frac{\partial f}{\partial\theta_i}\Rightarrow ∇J(\theta)=\begin{bmatrix} \frac{\partial f}{\partial\theta_1}\\ \frac{\partial f}{\partial\theta_2}\\ \frac{\partial f}{\partial\theta_3}\\ . \\ . \\ \frac{\partial f}{\partial\theta_n}\\ \end{bmatrix} ∂θi∂f⇒∇J(θ)=⎣⎢⎢⎢⎢⎢⎢⎢⎡∂θ1∂f∂θ2∂f∂θ3∂f..∂θn∂f⎦⎥⎥⎥⎥⎥⎥⎥⎤
2.变换流程
因
为
因为
因为
∂
f
∂
θ
1
=
1
m
∑
i
=
1
m
(
h
θ
(
x
i
)
−
y
i
)
x
i
(
1
)
∂
f
∂
θ
2
=
1
m
∑
i
=
1
m
(
h
θ
(
x
i
)
−
y
i
)
x
i
(
2
)
.
.
.
.
.
.
∂
f
∂
θ
n
=
1
m
∑
i
=
1
m
(
h
θ
(
x
i
)
−
y
i
)
x
i
(
n
)
\frac{\partial f}{\partial\theta_1}=\frac{1}{m}\sum_{i=1}^m\left(h_\theta(x_i)-y_i\right)x_i^{(1)} \\ \frac{\partial f}{\partial\theta_2}=\frac{1}{m}\sum_{i=1}^m\left(h_\theta(x_i)-y_i\right)x_i^{(2)}\\ ...\\...\\ \frac{\partial f}{\partial\theta_n}=\frac{1}{m}\sum_{i=1}^m\left(h_\theta(x_i)-y_i\right)x_i^{(n)}
∂θ1∂f=m1i=1∑m(hθ(xi)−yi)xi(1)∂θ2∂f=m1i=1∑m(hθ(xi)−yi)xi(2)......∂θn∂f=m1i=1∑m(hθ(xi)−yi)xi(n)
\
所
以
所以
所以
∇
J
(
θ
)
=
[
∂
f
∂
θ
1
∂
f
∂
θ
2
∂
f
∂
θ
3
.
.
∂
f
∂
θ
n
]
=
[
x
1
(
1
)
(
h
θ
(
x
1
)
−
y
1
)
+
x
2
(
1
)
(
h
θ
(
x
2
)
−
y
2
)
+
.
.
.
+
x
m
(
1
)
(
h
θ
(
x
m
)
−
y
m
)
x
1
(
2
)
(
h
θ
(
x
1
)
−
y
1
)
+
x
2
(
2
)
(
h
θ
(
x
2
)
−
y
2
)
+
.
.
.
+
x
m
(
2
)
(
h
θ
(
x
m
)
−
y
m
)
x
1
(
3
)
(
h
θ
(
x
1
)
−
y
1
)
+
x
2
(
3
)
(
h
θ
(
x
2
)
−
y
2
)
+
.
.
.
+
x
m
(
3
)
(
h
θ
(
x
m
)
−
y
m
)
.
.
.
.
.
.
.
.
x
1
(
n
)
(
h
θ
(
x
1
)
−
y
1
)
+
x
2
(
n
)
(
h
θ
(
x
2
)
−
y
2
)
+
.
.
.
+
x
m
(
n
)
(
h
θ
(
x
m
)
−
y
m
)
]
∇J(\theta)=\begin{bmatrix} \frac{\partial f}{\partial\theta_1}\\ \frac{\partial f}{\partial\theta_2}\\ \frac{\partial f}{\partial\theta_3}\\ . \\ . \\ \frac{\partial f}{\partial\theta_n}\\ \end{bmatrix}=\begin{bmatrix} x_1^{(1)}(h_\theta(x_1)-y_1)+x_2^{(1)}(h_\theta(x_2)-y_2)+...+x_m^{(1)}(h_\theta(x_m)-y_m)\\ x_1^{(2)}(h_\theta(x_1)-y_1)+x_2^{(2)}(h_\theta(x_2)-y_2)+...+x_m^{(2)}(h_\theta(x_m)-y_m)\\ x_1^{(3)}(h_\theta(x_1)-y_1)+x_2^{(3)}(h_\theta(x_2)-y_2)+...+x_m^{(3)}(h_\theta(x_m)-y_m)\\ \quad... . \\ \quad... .\\ x_1^{(n)}(h_\theta(x_1)-y_1)+x_2^{(n)}(h_\theta(x_2)-y_2)+...+x_m^{(n)}(h_\theta(x_m)-y_m)\\ \end{bmatrix}
∇J(θ)=⎣⎢⎢⎢⎢⎢⎢⎢⎡∂θ1∂f∂θ2∂f∂θ3∂f..∂θn∂f⎦⎥⎥⎥⎥⎥⎥⎥⎤=⎣⎢⎢⎢⎢⎢⎢⎢⎡x1(1)(hθ(x1)−y1)+x2(1)(hθ(x2)−y2)+...+xm(1)(hθ(xm)−ym)x1(2)(hθ(x1)−y1)+x2(2)(hθ(x2)−y2)+...+xm(2)(hθ(xm)−ym)x1(3)(hθ(x1)−y1)+x2(3)(hθ(x2)−y2)+...+xm(3)(hθ(xm)−ym)........x1(n)(hθ(x1)−y1)+x2(n)(hθ(x2)−y2)+...+xm(n)(hθ(xm)−ym)⎦⎥⎥⎥⎥⎥⎥⎥⎤
\
套矩阵乘法公式
[
a
b
c
d
]
[
x
y
]
=
[
a
x
+
b
y
c
x
+
d
y
]
得
:
\begin{bmatrix}a& b\\ c& d \end{bmatrix}\begin{bmatrix}x\\ y \end{bmatrix}=\begin{bmatrix}ax+by\\cx+dy \end{bmatrix}得:
[acbd][xy]=[ax+bycx+dy]得:
\
∇
J
(
θ
)
=
[
∂
f
∂
θ
1
∂
f
∂
θ
2
∂
f
∂
θ
3
.
.
∂
f
∂
θ
n
]
=
=
[
x
1
(
1
)
x
1
(
2
)
.
.
.
x
1
(
n
)
x
2
(
1
)
x
2
(
2
)
.
.
.
x
2
(
n
)
x
3
(
1
)
x
3
(
2
)
.
.
.
x
3
(
n
)
.
.
.
.
.
.
.
.
.
.
.
.
x
m
(
1
)
x
m
(
2
)
.
.
.
x
m
(
n
)
]
T
[
h
θ
(
x
1
)
−
y
1
h
θ
(
x
2
)
−
y
2
h
θ
(
x
3
−
y
3
.
.
.
h
θ
(
x
m
)
−
y
m
]
∇J(\theta)=\begin{bmatrix} \frac{\partial f}{\partial\theta_1}\\ \frac{\partial f}{\partial\theta_2}\\ \frac{\partial f}{\partial\theta_3}\\ . \\ . \\ \frac{\partial f}{\partial\theta_n}\\ \end{bmatrix}==\begin{bmatrix} x_1^{(1)} & x_1^{(2)} \quad ...&x_1^{(n)} \\ x_2^{(1)} & x_2^{(2)} \quad...&x_2^{(n)}\\ x_3^{(1)} & x_3^{(2)} \quad...&x_3^{(n)} \\ . & . \quad...& . \\ . & . \quad...& . \\ x_m^{(1)} & x_m^{(2)} \quad...&x_m^{(n)} \\ \end{bmatrix}^T\begin{bmatrix} h_\theta(x_1)-y_1\\ h_\theta(x_2)-y_2\\ h_\theta(x_3-y_3\\ ...\\ h_\theta(x_m)-y_m\\ \end{bmatrix}
∇J(θ)=⎣⎢⎢⎢⎢⎢⎢⎢⎡∂θ1∂f∂θ2∂f∂θ3∂f..∂θn∂f⎦⎥⎥⎥⎥⎥⎥⎥⎤==⎣⎢⎢⎢⎢⎢⎢⎢⎡x1(1)x2(1)x3(1)..xm(1)x1(2)...x2(2)...x3(2)...........xm(2)...x1(n)x2(n)x3(n)..xm(n)⎦⎥⎥⎥⎥⎥⎥⎥⎤T⎣⎢⎢⎢⎢⎡hθ(x1)−y1hθ(x2)−y2hθ(x3−y3...hθ(xm)−ym⎦⎥⎥⎥⎥⎤
右 式 分 解 得 右式分解得 右式分解得
∇ J ( θ ) = [ ∂ f ∂ θ 1 ∂ f ∂ θ 2 ∂ f ∂ θ 3 . . ∂ f ∂ θ n ] = = [ x 1 ( 1 ) x 1 ( 2 ) . . . x 1 ( n ) x 2 ( 1 ) x 2 ( 2 ) . . . x 2 ( n ) x 3 ( 1 ) x 3 ( 2 ) . . . x 3 ( n ) . . . . . . . . . . . . x m ( 1 ) x m ( 2 ) . . . x m ( n ) ] T ( [ h θ ( x 1 ) h θ ( x 2 ) h θ ( x 3 . . . h θ ( x m ) ] − [ y 1 y 2 y 3 . . . y m ] ) ∇J(\theta)=\begin{bmatrix} \frac{\partial f}{\partial\theta_1}\\ \frac{\partial f}{\partial\theta_2}\\ \frac{\partial f}{\partial\theta_3}\\ . \\ . \\ \frac{\partial f}{\partial\theta_n}\\ \end{bmatrix}==\begin{bmatrix} x_1^{(1)} & x_1^{(2)} \quad ...&x_1^{(n)} \\ x_2^{(1)} & x_2^{(2)} \quad...&x_2^{(n)}\\ x_3^{(1)} & x_3^{(2)} \quad...&x_3^{(n)} \\ . & . \quad...& . \\ . & . \quad...& . \\ x_m^{(1)} & x_m^{(2)} \quad...&x_m^{(n)} \\ \end{bmatrix}^T \left( \begin{bmatrix} h_\theta(x^1)\\ h_\theta(x^2)\\ h_\theta(x^3\\ ...\\ h_\theta(x^m)\\ \end{bmatrix}-\begin{bmatrix} y^1\\ y^2\\ y^3\\ ...\\ y^m\\ \end{bmatrix} \right) ∇J(θ)=⎣⎢⎢⎢⎢⎢⎢⎢⎡∂θ1∂f∂θ2∂f∂θ3∂f..∂θn∂f⎦⎥⎥⎥⎥⎥⎥⎥⎤==⎣⎢⎢⎢⎢⎢⎢⎢⎡x1(1)x2(1)x3(1)..xm(1)x1(2)...x2(2)...x3(2)...........xm(2)...x1(n)x2(n)x3(n)..xm(n)⎦⎥⎥⎥⎥⎥⎥⎥⎤T⎝⎜⎜⎜⎜⎛⎣⎢⎢⎢⎢⎡hθ(x1)hθ(x2)hθ(x3...hθ(xm)⎦⎥⎥⎥⎥⎤−⎣⎢⎢⎢⎢⎡y1y2y3...ym⎦⎥⎥⎥⎥⎤⎠⎟⎟⎟⎟⎞
代
入
子
式
们
得
到
:
代入子式们得到:
代入子式们得到:
∇
J
(
θ
)
=
X
T
(
X
θ
−
Y
)
∇J(\theta)=X^T(X\theta-Y)
∇J(θ)=XT(Xθ−Y)
\
又
因
为
又因为
又因为
θ
1
=
θ
1
−
l
R
∂
f
∂
θ
1
θ
2
=
θ
2
−
l
R
∂
f
∂
θ
2
θ
3
=
θ
3
−
l
R
∂
f
∂
θ
3
.
.
.
.
.
.
θ
m
=
θ
m
−
l
R
∂
f
∂
θ
m
1
\theta_1=\theta_1-lR\frac{\partial f}{\partial\theta_1}\\\theta_2=\theta_2-lR\frac{\partial f}{\partial\theta_2}\\\theta_3=\theta_3-lR\frac{\partial f}{\partial\theta_3}\\...\\...\\\theta_m=\theta_m-lR\frac{\partial f}{\partial\theta_m1}
θ1=θ1−lR∂θ1∂fθ2=θ2−lR∂θ2∂fθ3=θ3−lR∂θ3∂f......θm=θm−lR∂θm1∂f
代
入
子
式
与
∇
J
(
θ
)
得
到
最
终
式
子
:
代入子式与∇J(\theta)得到最终式子:
代入子式与∇J(θ)得到最终式子:
θ
=
θ
−
L
R
(
∇
J
(
θ
)
)
\theta=\theta-LR(∇J(\theta))
θ=θ−LR(∇J(θ))
-
式
子
里
除
L
R
外
都
是
矩
阵
式子里除LR外都是矩阵
式子里除LR外都是矩阵
重 复 上 式 足 够 次 数 , 可 得 到 距 离 代 价 函 数 取 最 小 值 时 候 的 变 量 ( ω 1 , ω 2 . . . ω n ) 极 为 接 近 的 变 量 ( θ 1 , θ 2 . . . θ n ) 重复上式足够次数,可得到距离代价函数取最小值时候的变量(\omega_1,\omega_2...\omega_n)极为接近的变量(\theta_1,\theta_2...\theta_n) 重复上式足够次数,可得到距离代价函数取最小值时候的变量(ω1,ω2...ωn)极为接近的变量(θ1,θ2...θn)