# 2.2 理解 mini-batch 梯度下降法（Understanding mini-batch gradient descent）

Batch gradient descent和Mini-batch gradient descent的cost曲线：

![](https://2314428465-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Le0cHhI0S0DK8pwlrmD%2F-Le0cKOp1vaxoORIi4ak%2F-Le0cglVOFUGd6Qlb-FR%2F5import.png?generation=1556953114245727\&alt=media)

对于一般的神经网络模型，使用Batch gradient descent，随着迭代次数增加，cost是不断减小的。而使用Mini-batch gradient descent，随着在不同的mini-batch上迭代训练，其cost不是单调下降，而是受类似noise的影响，出现振荡。但整体的趋势是下降的，最终也能得到较低的cost值

出现细微振荡的原因是不同的mini-batch之间是有差异的。可能第一个子集$$(X^{{1}},Y^{{1}})$$是好的子集，而第二个子集$$(X^{{2}},Y^{{2}})$$包含了一些噪声noise。出现细微振荡是正常的

如果mini-batch size=m，即为Batch gradient descent，只包含一个子集为$$(X^{{1}},Y^{{1}})=(X,Y)$$；

如果mini-batch size=1，即为Stachastic gradient descent，每个样本就是一个子集$$(X^{{1}},Y^{{1}})=(x^{(i)},y^{(i)})$$，共有m个子集

蓝色的线代表Batch gradient descent，紫色的线代表Stachastic gradient descent。Batch gradient descent会比较平稳地接近全局最小值，但因为使用了所有m个样本，每次前进的速度有些慢。Stachastic gradient descent每次前进速度很快，但路线曲折，有较大的振荡，最终会在最小值附近来回波动，难达到最小值。而且在数值处理上不能使用向量化的方法来提高运算速度

![](https://2314428465-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Le0cHhI0S0DK8pwlrmD%2F-Le0cKOp1vaxoORIi4ak%2F-Le0cglZrO-GKuXiEHlD%2F6import.png?generation=1556953117279083\&alt=media)

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

mini-batch size不能设置得太大（Batch gradient descent），也不能设置得太小（Stachastic gradient descent）。相当于结合了Batch gradient descent和Stachastic gradient descent各自的优点，既能使用向量化优化算法，又能较快速地找到最小值。mini-batch gradient descent的梯度下降曲线如下图绿色所示，每次前进速度较快，且振荡较小，基本能接近全局最小值。

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

![](https://2314428465-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Le0cHhI0S0DK8pwlrmD%2F-Le0cKOp1vaxoORIi4ak%2F-Le0cgleAqmjABP-Fhde%2F14import.png?generation=1556953134058860\&alt=media)

* 总体样本数量m不太大时，例如$$m\leq2000$$，建议直接使用Batch gradient descent
* 总体样本数量m很大时，建议将样本分成许多mini-batches。推荐常用的mini-batch size为64,128,256,512。都是2的幂。原因是计算机存储数据一般是2的幂，这样设置可以提高运算速度
* &#x20;mini-batch 中确保 $$X{{t}}$$ 和$$Y{{t}}$$要符合 CPU/GPU 内存，取决于应用方向以及训练集的大小。如果处理的 mini-batch 和 CPU/GPU 内存不相符，不管用什么方法处理数据，算法的表现都急转直下变得惨不忍睹

## 从训练集（X，Y）中构建小批量

* 随机洗牌（**Shuffle**）：创建训练集（X，Y）的混洗版本，X和Y的每一列代表一个训练示例。随机混洗是在X和Y之间同步完成的。这样在混洗之后第$$i$$列的X对应的例子就是Y第$$i$$列中的标签。混洗步骤可确保将示例随机分成不同的小批次

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

* 分区（**Partition**）：将混洗（X，Y）分区为小批量mini\_batch\_size（此处为64）。训练示例的数量并非总是可以被mini\_batch\_size整除。最后一个小批量可能会更小

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


---

# 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/22-li-jie-mini-batch-ti-du-xia-jiang-fa-ff08-understanding-mini-batch-gradient-descent.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.
