Stochastic Gradient Descent/zh

    From Marovi AI
    Revision as of 03:40, 27 April 2026 by DeployBot (talk | contribs) (Batch translate Stochastic Gradient Descent unit 8 → zh)
    (diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
    Other languages:
    ArticlesMachine LearningOptimization algorithmsStochastic Gradient Descent/zh
    Stochastic Gradient Descent

    Topics {{#arraymap:Optimization, Neural Networks, Gradient Methods|,|@@item@@|@@item@@|}}

    Summary
    Stochastic gradient descent (SGD) is the core optimization algorithm behind modern machine learning. Instead of computing gradients over an entire dataset, it estimates them from small random samples, making training feasible on large-scale data. Nearly all deep learning models are trained using SGD or one of its variants (Adam, RMSProp, etc.).
      {{#arraymap:Estimates gradients from random mini-batches instead of the full dataset; Learning rate schedule is critical for convergence; Variants like Adam and AdamW add adaptive per-parameter rates; Converges to global minimum for convex problems under Robbins–Monro conditions|;|@@item@@|
    • @@item@@
    • |}}

    隨機梯度下降(通常縮寫為 SGD)是一種迭代優化算法,用於最小化以可微子函數之和形式表示的目標函數。它是現代機器學習訓練的主力,驅動着從邏輯回歸到深度神經網絡的各種模型。

    動機

    在經典的梯度下降中,每次參數更新前都要在整個訓練集上計算損失函數的完整梯度。當數據集很大時,這種做法的代價高得難以承受。SGD 通過在每一步從單個隨機選取的樣本(或一個小的 mini-batch)估計梯度來解決該問題,以較高噪聲的估計換取每次迭代成本的大幅降低。

    算法

    給定一個參數化的損失函數

    $ L(\theta) = \frac{1}{N}\sum_{i=1}^{N} \ell(\theta;\, x_i,\, y_i) $

    SGD 在第 $ t $ 步的更新規則為:

    $ \theta_{t+1} = \theta_t - \eta_t \,\nabla_\theta \ell(\theta_t;\, x_{i_t},\, y_{i_t}) $

    其中 $ \eta_t $學習率(步長),$ i_t $ 是隨機選取的索引。

    小批量變體

    在實踐中通常使用包含 $ B $ 個樣本的 mini-batch

    $ \theta_{t+1} = \theta_t - \frac{\eta_t}{B}\sum_{j=1}^{B} \nabla_\theta \ell(\theta_t;\, x_{i_j},\, y_{i_j}) $

    常見的批大小在 32 到 512 之間。較大的批次能降低梯度方差,但會增加內存占用。

    偽代碼

    initialise parameters θ
    for epoch = 1, 2, … do
        shuffle training set
        for each mini-batch B ⊂ training set do
            g ← (1/|B|) Σ ∇ℓ(θ; xᵢ, yᵢ)   # estimate gradient
            θ ← θ − η · g                     # update parameters
        end for
    end for
    

    學習率調度

    學習率 $ \eta_t $收斂有重要影響。常見的策略包括:

    • 常數 —— 簡單,但可能產生超調或停滯。
    • 階梯衰減 —— 每 $ k $ 個 epoch 將 $ \eta $ 乘以一個因子(如 0.1)。
    • 指數衰減 —— $ \eta_t = \eta_0 \, e^{-\lambda t} $
    • 餘弦退火 —— 沿餘弦曲線平滑降低學習率,常配合熱重啟使用。
    • 線性 warm-up —— 在最初若干次迭代中從較小的 $ \eta $ 逐步增大,以穩定早期訓練。

    收斂性質

    對於具有 Lipschitz 連續梯度的目標,使用滿足以下條件的衰減學習率的 SGD

    $ \sum_{t=1}^{\infty} \eta_t = \infty, \qquad \sum_{t=1}^{\infty} \eta_t^2 < \infty $

    幾乎必然收斂到全局最小值(Robbins–Monro 條件)。對於非凸問題——深度學習中的典型情形——SGD 收斂到一個駐點,經驗證據表明它常常能找到良好的局部極小值。

    常見變體

    若干擴展方法可降低梯度估計的方差,或為每個參數自適應地調整步長:

    方法 核心思想 文獻
    Momentum 對歷史梯度累積指數衰減的移動平均 Polyak, 1964
    Nesterov 加速梯度 在「前瞻」位置上計算梯度 Nesterov, 1983
    Adagrad 為每個參數設置學習率,對頻繁更新的特徵逐步減小 Duchi et al., 2011
    RMSProp 利用平方梯度的移動平均修正 Adagrad 學習率不斷衰減的問題 Hinton(講義),2012
    Adam momentum 與 RMSProp 風格的自適應學習率結合 Kingma 與 Ba, 2015
    AdamW 將權重衰減與自適應梯度更新解耦 Loshchilov 與 Hutter, 2019

    實踐注意事項

    • 數據洗牌 —— 在每個 epoch 重新打亂數據集,避免出現循環模式。
    • 梯度裁剪 —— 對梯度範數進行截斷,以防止更新爆炸,尤其是在循環神經網絡中。
    • 批歸一化 —— 對層輸入進行歸一化可降低對學習率的敏感度。
    • 混合精度訓練 —— 使用半精度浮點數能在現代 GPU 上加速 SGD,同時幾乎不損失精度。

    應用

    SGD 及其變體幾乎在機器學習的所有領域都有應用:

    • 訓練深度神經網絡(計算機視覺、NLP、語音識別)
    • 大規模線性模型(邏輯回歸、通過 SGD 訓練的 SVM)
    • 強化學習中的策略優化
    • 推薦系統與協同過濾
    • 數據以流式方式到達的在線學習場景

    參見

    參考文獻

    • Robbins, H. 與 Monro, S. (1951). "A Stochastic Approximation Method". Annals of Mathematical Statistics.
    • Bottou, L. (2010). "Large-Scale Machine Learning with Stochastic Gradient Descent". COMPSTAT.
    • Kingma, D. P. 與 Ba, J. (2015). "Adam: A Method for Stochastic Optimization". ICLR.
    • Ruder, S. (2016). "An overview of gradient descent optimization algorithms". arXiv:1609.04747.