# 2.6 动量梯度下降法（Gradient descent with Momentum ）

动量梯度下降算法速度比传统的梯度下降算法快很多。做法是在每次训练时，对梯度进行指数加权平均处理，然后用得到的梯度值更新权重$$W$$和常数项$$b$$

![](https://2314428465-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Le0cHhI0S0DK8pwlrmD%2F-Le0cKOp1vaxoORIi4ak%2F-Le0cuyxsT71VRM0qe7F%2F7.bmp?generation=1556953150615628\&alt=media)

原始的梯度下降算法如上图蓝色折线所示。在梯度下降过程中，梯度下降的振荡较大，尤其对于$$W$$、$$b$$之间数值范围差别较大的情况。此时每一点处的梯度只与当前方向有关，产生类似折线的效果，前进缓慢。而如果对梯度进行指数加权平均，使当前梯度不仅与当前方向有关，还与之前的方向有关，在纵轴方向，\
平均过程中，正负数相互抵消，所以平均值接近于零。但在横轴方向，所\
有的微分都指向横轴方向，因此横轴方向的平均值仍然较大，用算法几次迭代后，最终纵轴方向的摆动变小了，横轴方向运动更快，因此算法走了一\
条更加直接的路径，在抵达最小值的路上减少了摆动，这样处理让梯度前进方向更加平滑，减少振荡，能够更快地到达最小值处

权重$$W$$和常数项$$b$$的指数加权平均表达式如下：

$$
V\_{dW}=\beta\cdot V\_{dW}+(1-\beta)\cdot dW
$$

$$
V\_{db}=\beta\cdot V\_{db}+(1-\beta)\cdot db
$$

动量的角度来看，以权重$$W$$为例，$$V\_{dW}$$可以理解成速度$$V$$，$$dW$$可以看成是加速度$$a$$。指数加权平均实际上是计算当前的速度，当前速度由之前的速度和现在的加速度共同影响。而$$\beta<1$$又能限制速度$$V\_{dW}$$过大。即当前的速度是渐变的，而不是瞬变的，是动量的过程。保证了梯度下降的平稳性和准确性，减少振荡，较快地达到最小值处

动量梯度下降算法的过程如下：

$$On\ iteration\ t:$$

$$
\ \ \ \ Compute\ dW,\ db\ on\ the\ current\ mini-batch
$$

$$
\ \ \ \ V\_{dW}=\beta V\_{dW}+(1-\beta)dW
$$

$$
\ \ \ \ V\_{db}=\beta V\_{db}+(1-\beta)db
$$

$$
\ \ \ \ W=W-\alpha V\_{dW},\ b=b-\alpha V\_{db}
$$

初始时，令$$V\_{dW}=0,V\_{db}=0$$。一般设置$$\beta=0.9$$，即指数加权平均前10次的数据，实际应用效果较好。

偏移校正可以不使用。因为经过10次迭代后，随着滑动平均的过程，偏移情况会逐渐消失


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://baozoulin.gitbook.io/neural-networks-and-deep-learning/di-er-men-ke-gai-shan-shen-ceng-shen-jing-wang-luo-chao-can-shu-tiao-shi-zheng-ze-hua-yi-ji-you-hua/improving-deep-neural-networks/optimization-algorithms/26-dong-liang-tidu-xia-jiang-fa-ff08-gradient-descent-with-momentum.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
