文章目录
第15章:大规模机器学习
15.1 随机梯度下降(Stochastic Gradient Descent)
已知梯度下降算法 θ j : = θ j − α 1 m ∑ i = 1 m ( h θ ( x ( i ) ) − y ( i ) ) x j ( i ) \theta_{j}:=\theta_{j}-\alpha\frac{1}{m}\sum_{i=1}^{m}(h_{\theta}(x^{(i)})-y^{(i)})x_{j}^{(i)} θj:=θj−αm1∑i=1m(hθ(x(i))−y(i))xj(i),当数据集 m m m 很大时,每一次更新 θ \theta θ 所需的时间很长,何况需多次迭代,耗费时间太长。所以对此进行优化,有以下几种优化方法:
- Stochastic gradient descent
- Mini-batch gradient descent
- MapReduce
Stochastic gradient descent(随机梯度下降)的算法流程如下:
算法先根据
(
x
(
1
)
,
y
(
1
)
)
(x^{(1)},y^{(1)})
(x(1),y(1))对
θ
\theta
θ 优化,然后根据
(
x
(
2
)
,
y
(
2
)
)
(x^{(2)},y^{(2)})
(x(2),y(2)) 对
θ
\theta
θ 优化,依次优化至
(
x
(
m
)
,
y
(
m
)
)
(x^{(m)},y^{(m)})
(x(m),y(m)) ,与 Batch gradient descent 每次优化使用全部数据集不同。
注:算法不像Batch gradient descent每次优化都朝向梯度下降最大的地方移动,而是迂回的移动至一个接近全局最小值的值。
α
=
c
o
n
s
t
1
i
t
e
r
a
t
i
o
n
N
u
m
b
e
r
+
c
o
n
s
t
2
\alpha=\frac{const1}{iterationNumber+const2}
α=iterationNumber+const2const1,可使得学习率随时间的增加而减少,即每次移动的步长越来越小,尽可能接近最小值。
15.2 小批量梯度下降(Mini-batch Gradient Descent)
小批量梯度下降算法是介于批量梯度下降算法和随机梯度下降算法之间的算法,每计算 b b b 个数据,便更新一次参数 θ \theta θ 。
- Batch gradient descent:每次优化时,使用 m m m 个数据
- Stochastic gradient descent:每次优化时,使用 1 1 1 个数据
- Mini-batch gradient descent:每次优化时,使用 b b b 个数据( b b b 为 mini-batch size),一般取2-10
Mini-batch gradient descent 的算法表示为:
15.3 MapReduce
MapReduce 算法的思想是,将数据分布式计算,然后再汇总起来。
例如, m = 400 m=400 m=400 , θ j : = θ j − α 1 400 ∑ i = 1 400 ( h θ ( x ( i ) ) − y ( i ) ) x j ( i ) \theta_{j}:=\theta_{j}-\alpha\frac{1}{400}\sum_{i=1}^{400}(h_{\theta}(x^{(i)})-y^{(i)})x_{j}^{(i)} θj:=θj−α4001∑i=1400(hθ(x(i))−y(i))xj(i) ,有四台计算机,可将数据集划分为四份:
- Machine 1: ( x ( 1 ) , y ( 1 ) ) , . . . , ( x ( 100 ) , y ( 100 ) ) → t e m p j ( 1 ) = ∑ i = 1 100 ( h θ ( x ( i ) ) − y ( i ) ) x j ( i ) (x^{(1)},y^{(1)}),...,(x^{(100)},y^{(100)})\rightarrow temp_{j}^{(1)}=\sum_{i=1}^{100}(h_{\theta}(x^{(i)})-y^{(i)})x_{j}^{(i)} (x(1),y(1)),...,(x(100),y(100))→tempj(1)=∑i=1100(hθ(x(i))−y(i))xj(i)
- Machine 2: ( x ( 101 ) , y ( 101 ) ) , . . . , ( x ( 200 ) , y ( 200 ) ) → t e m p j ( 2 ) = ∑ i = 101 200 ( h θ ( x ( i ) ) − y ( i ) ) x j ( i ) (x^{(101)},y^{(101)}),...,(x^{(200)},y^{(200)})\rightarrow temp_{j}^{(2)}=\sum_{i=101}^{200}(h_{\theta}(x^{(i)})-y^{(i)})x_{j}^{(i)} (x(101),y(101)),...,(x(200),y(200))→tempj(2)=∑i=101200(hθ(x(i))−y(i))xj(i)
- Machine 3: ( x ( 201 ) , y ( 201 ) ) , . . . , ( x ( 300 ) , y ( 300 ) ) → t e m p j ( 3 ) = ∑ i = 201 300 ( h θ ( x ( i ) ) − y ( i ) ) x j ( i ) (x^{(201)},y^{(201)}),...,(x^{(300)},y^{(300)})\rightarrow temp_{j}^{(3)}=\sum_{i=201}^{300}(h_{\theta}(x^{(i)})-y^{(i)})x_{j}^{(i)} (x(201),y(201)),...,(x(300),y(300))→tempj(3)=∑i=201300(hθ(x(i))−y(i))xj(i)
- Machine 4: ( x ( 301 ) , y ( 301 ) ) , . . . , ( x ( 400 ) , y ( 400 ) ) → t e m p j ( 4 ) = ∑ i = 301 400 ( h θ ( x ( i ) ) − y ( i ) ) x j ( i ) (x^{(301)},y^{(301)}),...,(x^{(400)},y^{(400)})\rightarrow temp_{j}^{(4)}=\sum_{i=301}^{400}(h_{\theta}(x^{(i)})-y^{(i)})x_{j}^{(i)} (x(301),y(301)),...,(x(400),y(400))→tempj(4)=∑i=301400(hθ(x(i))−y(i))xj(i)
四台计算机分别计算,然后汇总 θ j : = θ j − α 1 400 ( t e m p j ( 1 ) + t e m p j ( 2 ) + t e m p j ( 3 ) + t e m p j ( 4 ) ) \theta_{j}:=\theta_{j}-\alpha\frac{1}{400}(temp_{j}^{(1)}+temp_{j}^{(2)}+temp_{j}^{(3)}+temp_{j}^{(4)}) θj:=θj−α4001(tempj(1)+tempj(2)+tempj(3)+tempj(4))
15.4 在线学习(online learning)
在线学习算法是指,对于一个成型的模型,每次有新的数据来临时,对参数进行更新,更新之后丢弃这个数据,因为不断有新的数据来临,所以不是用整个数据集一次性进行更新参数。