> For the complete documentation index, see [llms.txt](https://baozoulin.gitbook.io/neural-networks-and-deep-learning/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](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/21-mini-batch-ti-du-xia-jiang-ff08-mini-batch-gradient-descent.md).

# 2.1 Mini-batch 梯度下降（Mini-batch gradient descent）

神经网络训练过程是同时对所有m个样本（称为batch）通过向量化计算方式进行的。如果m很大，训练速度会很慢，因为每次迭代都要对所有样本进进行求和运算和矩阵运算。这种梯度下降算法称为Batch Gradient Descent

解决：

把m个训练样本分成若干个子集，称为mini-batches，然后每次在单一子集上进行神经网络训练，这种梯度下降算法叫做Mini-batch Gradient Descent

假设总的训练样本个数$$m=5000000$$，其维度为$$(n\_x,m)$$。将其分成5000个子集，每个mini-batch含有1000个样本。将每个mini-batch记为$$X^{{t}}$$，其维度为$$(n\_x,1000)$$。相应的每个mini-batch的输出记为$$Y^{{t}}$$，其维度为(1,1000)，且$$t=1,2,\cdots,5000$$

* $$X^{(i)}$$**：第i个样本**
* $$Z^{\[l]}$$：**神经网络第**$$l$$**层网络的线性输出**
* $$X^{{t}},Y^{{t}}$$**：第t组mini-batch**

Mini-batches Gradient Descent是先将总的训练样本分成T个子集（mini-batches），然后对每个mini-batch进行神经网络训练，包括Forward Propagation，Compute Cost Function，Backward Propagation，循环至T个mini-batch都训练完毕

$$
for\ \ t=1,\cdots,T\ \ {
$$

$$
\ \ \ \ Forward\ Propagation
$$

$$
\ \ \ \ Compute\ Cost\ Function
$$

$$
\ \ \ \ Backward\ Propagation
$$

$$
\ \ \ \ W:=W-\alpha\cdot dW
$$

$$
\ \ \ \ b:=b-\alpha\cdot db
$$

$$
}
$$

经过T次循环之后，所有m个训练样本都进行了梯度下降计算。这个过程称之为经历了一个epoch。对于Batch Gradient Descent而言，一个epoch只进行一次梯度下降算法；而Mini-Batches Gradient Descent，一个epoch会进行T次梯度下降算法

对于Mini-Batches Gradient Descent，可以进行多次epoch训练。每次epoch，最好是将总体训练数据打乱、重新分成T组mini-batches，这样有利于训练出最佳的神经网络模型
