为什么梯度下降变成了随机的?
TL;DR · AI 摘要
梯度下降演变为随机梯度下降(SGD)的核心动因是:当数据规模增大时,传统批量梯度下降(BGD)计算成本过高,而SGD通过每次仅用单个样本或小批量更新参数,显著降低计算开销并引入噪声扰动,反而有助于跳出局部极小值;文章以线性回归为例,从MSE损失函数推导出解析解,并自然引出梯度下降的必要性。
核心要点
- 线性回归中β₀=27315.74、β₁=9020.66的解析解可通过MSE对β₀/β₁求偏导并令其为0推导得出
- 批量梯度下降(BGD)每轮需遍历全部n个样本计算梯度,时间复杂度O(n),而SGD仅需O(1)单样本更新
- SGD的随机性不仅加速收敛,其噪声特性还能帮助优化过程逃离平坦鞍点与局部极小值
结构提纲
按章节快速跳转。
通过均值与协方差公式直接计算斜率β₁=9020.66与截距β₀=27315.74,适用于小规模数据但缺乏可扩展性。
对MSE损失函数分别对β₀和β₁求偏导并令其为0, 可严格推导出线性回归的闭式解公式。
将MSE视为β₀-β₁平面上的三维碗状曲面,最小化目标等价于沿负梯度方向迭代下降至谷底。
当n极大时,BGD每步O(n)计算不可行;SGD用单样本梯度近似全量梯度,实现O(1)更新并提升泛化能力。
梯度估计中的随机噪声可打破对称性、避免停滞于鞍点,并在非凸优化中提升收敛鲁棒性。
思维导图
用一张图看清主题之间的关系。
查看大纲文本(无障碍 / 无 JS 友好)
- 梯度下降为何走向随机化
- 理论基础
- 线性回归闭式解
- MSE损失函数
- 偏导→极值条件
- 计算瓶颈
- BGD: O(n)每步
- 大数据下不可行
- 内存与算力限制
- SGD优势
- O(1)单样本更新
- 噪声促进跳出局部极小
- 更优泛化性能
金句 / Highlights
值得收藏与分享的关键句。
当数据集规模增大时,批量梯度下降每轮需计算n个样本的梯度,时间复杂度为O(n);而SGD每次仅用1个样本,复杂度降至O(1),使大规模训练成为可能。
对MSE损失函数求∂/∂β₀得:−2/n ∑(yᵢ − β₀ − β₁xᵢ) = 0,该方程解即为β₀ = ȳ − β₁x̄,揭示了截距公式的微分本质。
SGD的随机性并非缺陷而是优势:它引入的梯度噪声能有效帮助优化器逃离平坦区域与局部极小值,在深度学习非凸优化中至关重要。
Why Gradient Descent Became Stochastic
A step-by-step journey from calculus-based optimization to Stochastic Gradient Descent
Nikhil Dasari May 29, 2026 19 min read Share Photo by Sami TÜRK
In this blog post, we are going to discuss not only how but also why gradient descent and stochastic gradient descent are used.
We already know about linear regression, and recently I wrote about it in the context of vectors and projections.
Now, we will try to understand gradient descent with the help of a linear regression problem.
But before that, I just want to briefly recall what we already know about linear regression and the math behind it, so that anyone starting out finds it easy to follow.
If you already know the basic math behind linear regression, then you can directly start from the section titled Why Do We Need Gradient Descent?
Let’s say we started our machine learning journey, and the first thing we did was implementing a linear regression model using Python.
We implemented it successfully and got the best values for the slope and intercept.
Now we have a question: What’s actually happening behind this algorithm?
We want to understand the math behind it.
Linear Regression Recap
For that, let’s consider this data.
Image by Author
Now, we want to understand the math behind the algorithm.
Image by Author
We come across these formulas for the slope and intercept.
β 1 = ∑ n i=1 ( x i – x ¯ )( y i – y ¯ ) ∑ n i=1 ( x i – x ¯ ) 2
β 0 = y ¯ – β 1 x ¯
Now, by using these formulas we calculate the slope and intercept.
The Simple Linear Regression equation is:
y ^ = β 0 + β 1 x
The slope formula is:
β 1 = ∑ n i=1 ( x i − x ¯ )( y i − y ¯ ) ∑ n i=1 ( x i − x ¯ ) 2
The intercept formula is:
β 0 = y ¯ – β 1 x ¯
The dataset is:
x=[1.2,1.4,1.6,2.1,2.3,3.0,3.1,3.3,3.3,3.8] y=[39344,46206,37732,43526,39892,56643,60151,54446,64446,57190]
Compute the mean of x:
x ¯ = 1.2+1.4+1.6+2.1+2.3+3.0+3.1+3.3+3.3+3.8 10 x ¯ = 25.1 10 =2.51
Compute the mean of y:
y ¯ = 39344+46206+37732+43526+39892+56643+60151+54446+64446+57190 10 y ¯ = 499576 10 =49957.6
Now compute:
∑( x i − x ¯ )( y i − y ¯ )
After substitution and calculation:
∑( x i − x ¯ )( y i − y ¯ )=41663.44
Now compute:
∑( x i − x ¯ ) 2
After calculation:
∑( x i − x ¯ ) 2 =4.619
Now compute the slope:
β 1 = 41663.44 4.619 β 1 =9020.66
Now compute the intercept:
β 0 =49957.6−(9020.66)(2.51) β 0 =27315.74
Therefore:
β 0 =27315.74 β 1 =9020.66
Final regression equation:
y ^ =27315.74+9020.66x
We got the values using the formulas, but we are not satisfied and want to go deeper.
Now our goal is to learn how we got these formulas.
To understand that, we will now see a 3D bowl curve. We get that bowl curve when we plot all the possible combinations of β 0 , β 1 , and the mean squared error (MSE).
Image by Author
Now, by looking at the curve, we understand that we need the mean squared error to be as low as possible, and it reaches its minimum when the gradient becomes zero.
We already know that to find the slope of any curve, we need differentiation.
Next, we perform differentiation on the loss function, since the bowl curve is the 3D representation of it, and you realize that here we have two variables.
So, we perform partial differentiation and then solve further to get the formulas for the slope and intercept.
Deriving the Formulas for Slope and Intercept
Start with the Mean Squared Error (MSE) loss function:
MSE( β 0 , β
令导数等于零:
$$ -\frac{2}{n} \sum_{i=1}^{n} x_i (y_i - \beta_0 - \beta_1 x_i) = 0 $$
两边同乘 $-\frac{n}{2}$:
$$ \sum_{i=1}^{n} x_i (y_i - \beta_0 - \beta_1 x_i) = 0 $$
展开:
$$ \sum_{i=1}^{n} x_i y_i - \beta_0 \sum_{i=1}^{n} x_i - \beta_1 \sum_{i=1}^{n} x_i^2 = 0 $$
代入:
$$ \beta_0 = \bar{y} - \beta_1 \bar{x} $$
得:
$$ \sum_{i=1}^{n} x_i y_i - (\bar{y} - \beta_1 \bar{x}) \sum_{i=1}^{n} x_i - \beta_1 \sum_{i=1}^{n} x_i^2 = 0 $$
展开:
$$ \sum_{i=1}^{n} x_i y_i - \bar{y} \sum_{i=1}^{n} x_i + \beta_1 \bar{x} \sum_{i=1}^{n} x_i - \beta_1 \sum_{i=1}^{n} x_i^2 = 0 $$
由于:
$$ \sum_{i=1}^{n} x_i = n \bar{x} $$
代入得:
$$ \sum_{i=1}^{n} x_i y_i - n \bar{x} \bar{y} + \beta_1 n \bar{x}^2 - \beta_1 \sum_{i=1}^{n} x_i^2 = 0 $$
合并 $\beta_1$ 项:
$$ \beta_1 (n \bar{x}^2 - \sum_{i=1}^{n} x_i^2) = n \bar{x} \bar{y} - \sum_{i=1}^{n} x_i y_i $$
两边同乘 $-1$:
$$ \beta_1 \left( \sum_{i=1}^{n} x_i^2 - n \bar{x}^2 \right) = \sum_{i=1}^{n} x_i y_i - n \bar{x} \bar{y} $$
最终斜率公式为:
$$ \beta_1 = \frac{\sum_{i=1}^{n} x_i y_i - n \bar{x} \bar{y}}{\sum_{i=1}^{n} x_i^2 - n \bar{x}^2} $$
等价的协方差形式为:
$$ \beta_1 = \frac{\sum_{i=1}^{n} (x_i - \bar{x})(y_i - \bar{y})}{\sum_{i=1}^{n} (x_i - \bar{x})^2} $$
最后,将计算出的 $\beta_1$ 代入截距公式:
$$ \beta_0 = \bar{y} - \beta_1 \bar{x} $$
因此,最终的回归方程为:
$$ \hat{y} = \beta_0 + \beta_1 x $$
至此,我们已推导出斜率与截距的公式。
但需注意:上述推导仅适用于单特征情形;即便如此,数学过程已相当复杂。
若特征数量超过一个(如大多数真实数据集所示),情况将更为复杂。此时我们采用矩阵形式表示方程,并由此导出正规方程(Normal Equation),该方法可推广至任意数量的特征。
正规方程的推导
在简单线性回归中,我们推导出一个截距和一个斜率:
$$ \hat{y} = \beta_0 + \beta_1 x $$
然而,现实问题通常包含多个特征,例如:
- 工作年限
- 教育水平
- 年龄
此时线性回归模型变为:
$$ \hat{y} = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + \beta_3 x_3 + \cdots + \beta_p x_p $$
其中:
- $\beta_0$ 为截距项
- $\beta_1, \beta_2, \beta_3, \dots, \beta_p$ 分别为各特征对应的斜率
随着特征数量增加,为每个参数单独求解方程变得困难。为此,我们改用矩阵表示法简化问题。
假设有 $n$ 个观测样本和 $p$ 个特征。
首先定义目标向量:
$$ \mathbf{Y} = \begin{bmatrix} y_1 \\ y_2 \\ y_3 \\ \vdots \\ y_n \end{bmatrix} $$
再定义特征矩阵 $\mathbf{X}$:第一列为全 1 向量,用于表示截距项:
$$ \mathbf{X} = \begin{bmatrix} 1 & x_{11} & x_{12} & \cdots & x_{1p} \\ 1 & x_{21} & x_{22} & \cdots & x_{2p} \\ 1 & x_{31} & x_{32} & \cdots & x_{3p} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ 1 & x_{n1} & x_{n2} & \cdots & x_{np} \end{bmatrix} $$
定义参数向量:
$$ \boldsymbol{\beta} = \begin{bmatrix} \beta_0 \\ \beta_1 \\ \beta_2 \\ \vdots \\ \beta_p \end{bmatrix} $$
利用矩阵乘法:
$$ \mathbf{X} \boldsymbol{\beta} = \begin{bmatrix} 1 & x_{11} & x_{12} & \cdots & x_{1p} \\ 1 & x_{21} & x_{22} & \cdots & x_{2p} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ 1 & x_{n1} & x_{n2} & \cdots & x_{np} \end{bmatrix} \begin{bmatrix} \beta_0 \\ \beta_1 \\ \beta_2 \\ \vdots \\ \beta_p \end{bmatrix} = \begin{bmatrix} \beta_0 + \beta_1 x_{11} + \beta_2 x_{12} + \cdots + \beta_p x_{1p} \\ \beta_0 + \beta_1 x_{21} + \beta_2 x_{22} + \cdots + \beta_p x_{2p} \\ \vdots \\ \beta_0 + \beta_1 x_{n1} + \beta_2 x_{n2} + \cdots + \beta_p x_{np} \end{bmatrix} $$
即预测向量为:
$$ \hat{\mathbf{Y}} = \mathbf{X} \boldsymbol{\beta} $$
定义残差向量(实际值与预测值之差):
$$ \mathbf{Y} - \hat{\mathbf{Y}} = \mathbf{Y} - \mathbf{X} \boldsymbol{\beta} $$
均方误差(MSE)为:
$$ \text{MSE} = \frac{1}{n} (\mathbf{Y} - \mathbf{X} \boldsymbol{\beta})^\top (\mathbf{Y} - \mathbf{X} \boldsymbol{\beta}) $$
转置操作是必要的,因为 $(\mathbf{Y} - \mathbf{X} \boldsymbol{\beta})$ 是列向量,与其转置相乘后得到标量——即残差平方和。
展开表达式:
$$ \text{MSE} = \frac{1}{n} (\mathbf{Y}^\top \mathbf{Y} - \mathbf{Y}^\top \mathbf{X} \boldsymbol{\beta} - (\mathbf{X} \boldsymbol{\beta})^\top \mathbf{Y} + (\mathbf{X} \boldsymbol{\beta})^\top \mathbf{X} \boldsymbol{\beta}) $$
利用转置性质 $(\mathbf{X} \boldsymbol{\beta})^\top = \boldsymbol{\beta}^\top \mathbf{X}^\top$,代入得:
$$ \text{MSE} = \frac{1}{n} (\mathbf{Y}^\top \mathbf{Y} - \mathbf{Y}^\top \mathbf{X} \boldsymbol{\beta} - \boldsymbol{\beta}^\top \mathbf{X}^\top \mathbf{Y} + \boldsymbol{\beta}^\top \mathbf{X}^\top \mathbf{X} \boldsymbol{\beta}) $$
注意到 $\mathbf{Y}^\top \mathbf{X} \boldsymbol{\beta}$ 是标量,而标量等于其自身转置,故:
$$ \mathbf{Y}^\top \mathbf{X} \boldsymbol{\beta} = \boldsymbol{\beta}^\top \mathbf{X}^\top \mathbf{Y} $$
中间两项合并为:
$$ \text{MSE} = \frac{1}{n} (\mathbf{Y}^\top \mathbf{Y} - 2 \boldsymbol{\beta}^\top \mathbf{X}^\top \mathbf{Y} + \boldsymbol{\beta}^\top \mathbf{X}^\top \mathbf{X} \boldsymbol{\beta}) $$
为最小化 MSE,对 $\boldsymbol{\beta}$ 求导:
- $\mathbf{Y}^\top \mathbf{Y}$ 不含 $\boldsymbol{\beta}$,导数为 0
- $-2 \boldsymbol{\beta}^\top \mathbf{X}^\top \mathbf{Y}$ 的导数为 $-2 \mathbf{X}^\top \mathbf{Y}$
- $\boldsymbol{\beta}^\top \mathbf{X}^\top \mathbf{X} \boldsymbol{\beta}$ 的导数为 $2 \mathbf{X}^\top \mathbf{X} \boldsymbol{\beta}$
因此:
$$ \frac{\partial \text{MSE}}{\partial \boldsymbol{\beta}} = \frac{1}{n} (-2 \mathbf{X}^\top \mathbf{Y} + 2 \mathbf{X}^\top \mathbf{X} \boldsymbol{\beta}) = -\frac{2}{n} \mathbf{X}^\top \mathbf{Y} + \frac{2}{n} \mathbf{X}^\top \mathbf{X} \boldsymbol{\beta} $$
令导数为 0(极小值条件):
$$ -\frac{2}{n} \mathbf{X}^\top \mathbf{Y} + \frac{2}{n} \mathbf{X}^\top \mathbf{X} \boldsymbol{\beta} = 0 $$
两边同乘 $\frac{n}{2}$:
$$
- \mathbf{X}^\top \mathbf{Y} + \mathbf{X}^\top \mathbf{X} \boldsymbol{\beta} = 0
$$
整理得:
$$ \mathbf{X}^\top \mathbf{X} \boldsymbol{\beta} = \mathbf{X}^\top \mathbf{Y} $$
两边左乘 $(\mathbf{X}^\top \mathbf{X})^{-1}$:
$$ (\mathbf{X}^\top \mathbf{X})^{-1} \mathbf{X}^\top \mathbf{X} \boldsymbol{\beta} = (\mathbf{X}^\top \mathbf{X})^{-1} \mathbf{X}^\top \mathbf{Y} $$
利用单位矩阵性质 $(\mathbf{X}^\top \mathbf{X})^{-1} (\mathbf{X}^\top \mathbf{X}) = \mathbf{I}$,得:
$$ \mathbf{I} \boldsymbol{\beta} = (\mathbf{X}^\top \mathbf{X})^{-1} \mathbf{X}^\top \mathbf{Y} $$
由于 $\mathbf{I} \boldsymbol{\beta} = \boldsymbol{\beta}$,最终得到正规方程:
$$ \boldsymbol{\beta} = (\mathbf{X}^\top \mathbf{X})^{-1} \mathbf{X}^\top \mathbf{Y} $$
该方程一次性求解出:
- 截距项
- 所有斜率
- 最小化均方误差的最优参数
一般而言,正规方程由最小化 RSS(残差平方和)导出;由于 MSE = RSS / $n$,最小化 MSE 得到的解与最小化 RSS 相同。
利用正规方程重新求解斜率与截距
线性回归的矩阵形式为:
$$ \boldsymbol{\beta} = (\mathbf{X}^\top \mathbf{X})^{-1} \mathbf{X}^\top \mathbf{Y} $$
构造特征矩阵(首列为 1,代表截距项):
$$ \mathbf{X} = \begin{bmatrix} 1 & 1.2 \\ 1 & 1.4 \\ 1 & 1.6 \\ 1 & 2.1 \\ 1 & 2.3 \\ 1 & 3.0 \\ 1 & 3.1 \\ 1 & 3.3 \\ 1 & 3.3 \\ 1 & 3.8 \end{bmatrix} $$
构造目标向量:
$$ \mathbf{Y} = \begin{bmatrix} 39344 \\ 46206 \\ 37732 \\ 43526 \\ 39892 \\ 56643 \\ 60151 \\ 54446 \\ 64446 \\ 57190 \end{bmatrix} $$
参数向量为:
$$ \boldsymbol{\beta} = \begin{bmatrix} \beta_0 \\ \beta_1 \end{bmatrix} $$
计算转置:
$$ \mathbf{X}^\top = \begin{bmatrix} 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\ 1.2 & 1.4 & 1.6 & 2.1 & 2.3 & 3.0 & 3.1 & 3.3 & 3.3 & 3.8 \end{bmatrix} $$
计算:
$$ \mathbf{X}^\top \mathbf{X} = \begin{bmatrix} 10 & 25.1 \\ 25.1 & 67.89 \end{bmatrix} $$
计算逆矩阵:
$$ (\mathbf{X}^\top \mathbf{X})^{-1} = \begin{bmatrix} 1.4547 & -0.5378 \\ -0.5378 & 0.2142 \end{bmatrix} $$
计算:
$$ \mathbf{X}^\top \mathbf{Y} = \begin{bmatrix} 493576 \\ 1326200.7 \end{bmatrix} $$
代入正规方程:
$$ \boldsymbol{\beta} = \begin{bmatrix} 1.4547 & -0.5378 \\ -0.5378 & 0.2142 \end{bmatrix} \begin{bmatrix} 493576 \\ 1326200.7 \end{bmatrix} $$
矩阵乘法后得:
β = \[ \begin{matrix} 27315.02 \\ 9020.93 \\ \end{matrix} \]
因此:
β_0 = 27315.02 \\ β_1 = 9020.93
最终回归方程为:
\hat{y} = 27315.02 + 9020.93x
为什么我们需要梯度下降?
现在,在得到线性回归的正规方程后,我们可能会认为即使在特征很多的情况下,我们也可以直接求解最优参数。
但需要注意的是,这种方法仅适用于小规模或中等规模的数据集。当数据集非常大时,求解正规方程的计算成本变得非常高。
让我们看看正规方程:
β = (X^T X)^{-1} X^T y
从方程中,我们可以观察到逆运算,这就是线性回归使用正规方程求解斜率和截距变得计算密集的原因。
这在小数据集上效果很好,但在现实世界中,我们通常有成千上万的特征和数百万的数据点。
在这样的情况下,求解正规方程变得缓慢,并且需要大量的计算资源。
这就是梯度下降的用武之地,因为梯度下降不是直接求解最优解,而是逐步接近最优解。
现在,让我们了解梯度下降背后的数学原理。
梯度下降背后的数学原理
当我们推导正规方程时,我们得到了这个方程:
\frac{\partial MSE}{\partial β} = \frac{2}{n} X^T (Xβ - Y)
这个方程代表了碗形损失曲线的梯度(斜率)。
我们将其设置为零,然后进一步求解,得到用于找到最优解的正规方程。
但在梯度下降中,我们在得到这个方程后停止,并初始化一些随机值: β。使用这些值,我们计算梯度(斜率),并逐步接近最小损失。
假设我们初始化:
β_0 = 2 \\ β_1 = 5
β^{(0)} = \[ \begin{matrix} 2 \\ 5 \\ \end{matrix} \]
接下来,我们将这些值代入梯度方程来计算碗形曲线的斜率。
我们已经知道梯度方程是:
\frac{\partial MSE}{\partial β} = -\frac{2}{n} X^T y + \frac{2}{n} X^T Xβ
初始化的参数值是:
β^{(0)} = \[ \begin{matrix} 2 \\ 5 \\ \end{matrix} \]
这些只是梯度下降开始搜索最小损失的起始值。
现在让我们构造特征矩阵。
由于我们只有一个特征,矩阵 X 变为:
X = \[ \begin{matrix} 1 & 1.2 \\ 1 & 1.4 \\ 1 & 1.6 \\ 1 & 2.1 \\ 1 & 2.3 \\ 1 & 3.0 \\ 1 & 3.1 \\ 1 & 3.3 \\ 1 & 3.3 \\ 1 & 3.8 \\ \end{matrix} \]
第一列包含用于截距项的 1。
现在计算:
X^T = \[ \begin{matrix} 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\ 1.2 & 1.4 & 1.6 & 2.1 & 2.3 & 3.0 & 3.1 & 3.3 & 3.3 & 3.8 \\ \end{matrix} \]
现在计算:
X^T X = \[ \begin{matrix} 10 & 25.1 \\ 25.1 & 67.89 \\ \end{matrix} \]
接下来,设目标向量为:
y = \[ \begin{matrix} 39344 \\ 46206 \\ 37732 \\ 43526 \\ 39892 \\ 56643 \\ 60151 \\ 54446 \\ 64446 \\ 57190 \\ \end{matrix} \]
现在计算:
X^T y = \[ \begin{matrix} 493576 \\ 1326200.7 \\ \end{matrix} \]
由于我们的数据集包含:
n = 10
现在将所有值代入梯度方程:
\frac{\partial MSE}{\partial β} = -\frac{2}{10} \[ \begin{matrix} 493576 \\ 1326200.7 \\ \end{matrix} \] + \frac{2}{10} \[ \begin{matrix} 10 & 25.1 \\ 25.1 & 67.89 \\ \end{matrix} \] \[ \begin{matrix} 2 \\ 5 \\ \end{matrix} \]
首先计算矩阵乘法:
\[ \begin{matrix} 10 & 25.1 \\ 25.1 & 67.89 \\ \end{matrix} \] \[ \begin{matrix} 2 \\ 5 \\ \end{matrix} \] = \[ \begin{matrix} (10)(2) + (25.1)(5) \\ (25.1)(2) + (67.89)(5) \\ \end{matrix} \] = \[ \begin{matrix} 20 + 125.5 \\ 50.2 + 339.45 \\ \end{matrix} \] = \[ \begin{matrix} 145.5 \\ 389.65 \\ \end{matrix} \]
现在乘以:
\frac{2}{10} \[ \begin{matrix} 145.5 \\ 389.65 \\ \end{matrix} \] = \[ \begin{matrix} 29.1 \\ 77.93 \\ \end{matrix} \]
接下来计算:
-\frac{2}{10} \[ \begin{matrix} 493576 \\ 1326200.7 \\ \end{matrix} \] = \[ \begin{matrix} -98715.2 \\ -265240.14 \\ \end{matrix} \]
现在将一切代回:
\frac{\partial MSE}{\partial β} = \[ \begin{matrix} -98715.2 \\ -265240.14 \\ \end{matrix} \] + \[ \begin{matrix} 29.1 \\ 77.93 \\ \end{matrix} \]
最后:
\frac{\partial MSE}{\partial β} = \[ \begin{matrix} -98686.1 \\ -265162.21 \\ \end{matrix} \]
这个梯度代表了碗形 MSE 损失曲线在当前参数值处的斜率。
这里:
-98686.1
代表了 β_0 的斜率,
-265162.21
代表了 β_1 的斜率。
由于两个值都是负的,损失向右减少,因此梯度下降向右移动以减少损失。
现在,代替直接求解最优参数的数学方法,梯度下降逐步更新参数值,直到达到碗形损失曲线的最小点。
这个更新是通过梯度下降更新方程完成的:
β := β - α \frac{\partial MSE}{\partial β}
其中:
α
称为学习率,控制每次更新步长的大小。
更新方程可以逐步理解。
β
代表当前参数值。
\frac{\partial MSE}{\partial β}
代表碗形损失曲线在当前点的斜率(梯度)。
梯度告诉我们损失增加最快的方
预测值→残差→损失→梯度→参数更新
这个迭代过程持续进行,直到损失达到最小值,模型达到最优参数。
现在让我们理解为什么选择学习率非常重要。
如果学习率非常小:
α=0.000001
那么更新的幅度会变得非常小。
结果是:
学习非常缓慢
梯度下降可能需要成千上万次迭代才能达到最小点。
另一方面,如果学习率非常大:
α=10
那么更新的幅度会变得非常大。
结果是,梯度下降可能会反复 overshoot 最小点,并且无法达到解决方案。
因此,选择合适的 学习率对于高效的优化非常重要。
GIF 由作者提供 随机梯度下降
现在我们对梯度下降有了初步的了解。
在这种方法中,我们可以观察到,我们使用了整个数据集来计算梯度,然后更新参数。
对于非常大的数据集,这个过程可能会变得缓慢,这种方法称为 批梯度下降,因为它在每次更新步骤中使用整个数据集。
现在想象一个包含数百万个数据点的数据集。
对于每次单个更新步骤,梯度下降需要:
处理整个数据集 计算损失 计算梯度
然后 finally 更新参数。
这种重复的计算变得计算密集且耗时。
这就是随机梯度下降 (SGD) 进入视野的原因。
SGD 不是使用整个数据集来计算梯度,而是随机选择一个观察值,并立即更新参数。
更新方程保持不变:
β:=β−α ∂MSE ∂β
唯一的区别是梯度现在使用单个观察值而不是整个数据集来计算。
我们可以使用数据集中的一个数据点来理解这一点。
参数值为:
β (0) =[ 2 5 ]
学习率为:
α=0.01
现在假设 SGD 随机从数据集中选择以下训练样本:
(x,y)=(3.0,56643)
对于这个单个观察值:
X=[ 1 3.0 ]
和
y=[ 56643 ]
现在计算:
X T =[ 1 3.0 ]
接下来计算:
X T X =[ 1 3.0 ][ 1 3.0 ] =[ 1 3.0 3.0 9.0 ]
现在计算:
X T y =[ 1 3.0 ][ 56643 ] =[ 56643 169929 ]
由于 SGD 只使用一个观察值:
n=1
现在将一切代入梯度方程:
∂MSE ∂β = −2 n X T y+ 2 n X T Xβ
代入:
= −2 1 [ 56643 169929 ]+ 2 1 [ 1 3.0 3.0 9.0 ][ 2 5 ]
首先计算矩阵乘法:
[ 1 3.0 3.0 9.0 ][ 2 5 ] =[ (1)(2)+(3.0)(5) (3.0)(2)+(9.0)(5) ] =[ 2+15 6+45 ] =[ 17 51 ]
现在乘以:
2 1 =[ 34 102 ]
现在计算:
−2 1 [ 56643 169929 ]=[ −113286 −339858 ]
现在将一切代回:
∂MSE ∂β =[ −113286 −339858 ]+[ 34 102 ]
最后:
∂MSE ∂β =[ −113252 −339756 ]
这个梯度代表了这个单个训练样本的碗形损失曲线的斜率。
现在使用:
β:=β−α ∂MSE ∂β
代入值:
β=[ 2 5 ]–0.01[ −113252 −339756 ]
首先乘以学习率:
=[ 2 5 ]–[ −1132.52 −3397.56 ]
现在减去:
=[ 2+1132.52 5+3397.56 ]
最后:
β=[ 1134.52 3402.56 ]
在解决一个观察值后,参数立即得到更新。
现在 SGD 随机从数据集中选择另一个观察值并重复相同的过程。
与批梯度下降不同,后者等待处理整个数据集才更新参数,SGD 在每次单个训练样本后立即更新参数。
由于这些频繁的更新,SGD 达到解决方案的速度更快。
我们可以观察到,当使用单个观察值时,计算变得多么简单。
SGD 继续使用不同的训练样本重复更新参数,直到损失达到最小值或不再显著变化。
但是,朝向最小值的路径变得嘈杂且 zig-zag。
这使得 SGD 在涉及非常大数据集的现代机器学习和深度学习问题中非常有用。
结论
现在我们对梯度下降和随机梯度下降都有了了解。
首先,我们推导了正规方程,然后我们了解到,对于大型数据集,逆矩阵计算变得计算密集且内存使用量高。
为了解决这个问题,我们使用了梯度下降,它不仅适用于线性回归,还适用于许多机器学习和深度学习算法。
接下来,我们了解到,我们之前使用的第一个梯度下降方法,即批梯度下降,对于非常大的数据集,由于它在更新参数之前使用整个数据集,也可能变得缓慢。
这导致我们使用随机梯度下降 (SGD),它使用一个训练样本进行参数更新,并且在处理大数据集时比批梯度下降更快。
我们还有另一种梯度下降变体,称为小批量梯度下降,我们在其中使用数据集中的一个小批量训练样本,如 32 或 64 行,然后更新参数。
这样,它比批梯度下降更快,比随机梯度下降更稳定。
即使线性回归有一个封闭解,我们通常在处理包含数百万个观测值的大数据集时,更喜欢使用梯度下降,因为正规方程变得计算密集且不切实际。
在深度学习中,however,封闭解通常不存在,这使得优化算法如梯度下降变得更加重要。
数据集许可
本文使用的数据集是 Salary 数据集。
它在 Kaggle 上公开可用,并且under the Creative Commons Zero (CC0 Public Domain) license. 这意味着它可以自由地用于、修改和分享,用于商业和非商业目的,没有任何限制。
我希望能让你对梯度下降和随机梯度下降有更深入的理解。
如果你想阅读更多我的文章,你也可以在 Medium 和 LinkedIn 上找到。
我最近写了一篇关于 Lasso 回归的详细几何和直观解释。
你可以在这里阅读。
谢谢你的阅读!
WRITTEN BY
Nikhil Dasari 参阅 Nikhil Dasari 的所有文章
数据科学 深度探讨 梯度下降 数学 随机梯度
分享这篇文章
Towards Data Science 是一个社区出版物。提交你的见解,以达到我们的全球读者群,并通过 TDS 作者支付计划赚取收益。
为 TDS 写作 相关文章 在 TensorFlow 中实现卷积神经网络 人工智能
逐步代码指南,让你构建一个卷积神经网络
Shreya Rao 2024 年 8 月 20 日 6 分钟阅读 使用 Python 的自编码器进行时间序列异常检测 数据科学
用几行代码检测异常信号
Piero Paialunga 2024 年 8 月 21 日 12 分钟阅读 Back To Basics, Part Uno: 线性回归与成本函数 数据科学
一个关于essential机器学习概念的图解指南
Shreya Rao 2023 年 2 月 3 日 6 分钟阅读 统计必修课:二元正态投影解析 数据科学
推导和实际应用示例
Luigi Battistoni 2024 年 8 月 14 日 7 分钟阅读 我们的专栏 数据科学
TDS 的专栏是精心编排的帖子集合,涉及特定的思想或类别。
TDS 编辑 2020 年 11 月 14 日 4 分钟阅读 使用预算多臂赌博机优化营销活动 数据科学
附演示、我们的新解决方案和视频
Vadim Arzamasov 2024 年 8 月 16 日 10 分钟阅读 Back to Basics, Part Tres: 逻辑回归 数据科学
一个关于逻辑回归的图解指南
Shreya Rao 2023 年 3 月 2 日 8 分钟阅读 YouTube X LinkedIn Threads Bluesky
你的数据科学和 Al 家园。全球数据科学、数据 Analytics、数据工程、机器学习和人工智能专业人士的领先出版物。
© Insight Media Group, LLC 2026 订阅我们的时事通讯 为 TDS 写作 关于 广告 使用条款 隐私政策 cookies policy Some areas of this page may shift around if you resize the browser window. Be sure to check heading and document order.