♎
Limited AI
  • Machine Learning
    • Linear Model Cheating Sheet
    • Nonlinear Model Cheating Sheet
    • General Linear Model 1
    • General Linear Model 2
    • General Linear Model 3
    • Tree Based Methods
    • Tree Based Methods Supplement
    • XG,Cat,Light__Boosting
    • KNN&PCA
    • Model Performance
    • Model Evaluation
    • Code Practice
      • KNN
      • Decision Tree Python Code
    • Data and Feature Engineering
      • Handle Bias Data
      • Cold Start Problem
  • Deep Learning
    • Summary v2
    • Basic Neural Network
      • From Linear to Deep
      • Perceptron and Activation Function
      • NN network Details
      • Backpropagation Details
      • Gradient Vanishing vs Gradient Exploding
    • Basic CNN
      • Why CNN
      • Filter/ Convolution Kernel and Its Operation
      • Padding& Stride
      • Layers
      • Extra:From Fully Connected Layers to Convolutions
      • Extra: Multiple Input and Multiple Output Channels
    • Advance CNN
      • Convolutional Neural Networks(LeNet)
      • Deep Convolution Neural Networks(AlexNet)
      • Networks Using Blocks (VGG)
      • Network in Network(NiN)
      • Multi-Branch Networks(GoogLeNet&I mageNet)
      • Residual Networks(ResNet) and ResNeXt
      • Densely Connected Networks(DenseNet)
      • Batch Normalization
    • Basic RNN
      • Seq Model
      • Raw Text to Seq
      • Language Models
      • Recurrent Neural Networks(RNN)
      • Backpropagation Through Time
    • Advance RNN
      • Gated Recurrent Units(GRU)
      • Long Short-Term Memory(LSTM)
      • Bidirectional Recurrent Neural Networks(BRNN)
      • Encoder-Decoder Architecture
      • Seuqence to Sequence Learning(Seq2Seq)
    • Attention Mechanisms and Transformers
      • Queries, Keys, and Values
      • Attention is all you need
        • Attention and Kernel
        • Attention Scoring Functions
        • The Bahdanau Attention Mechanism
        • Multi-Head Attention
        • Self-Attention
        • Attention的实现
      • The Transformer Architecture
        • Extra Reading
        • 最短的最大路径长度
      • Large-Scaling Pretraning with Transformers
        • BERT vs OpenAI GPT vs ELMo
        • Decoder Model框架
        • Bert vs XLNet
        • T5& GPT& Bert比较
        • 编码器-解码器架构 vs GPT 模型
        • Encoder vs Decoder Reference
      • Transformers for Vision
      • Transformer for Multiomodal
    • NLP Pretraining
      • Word Embedding(word2vec)
        • Extra Reading
      • Approximate Training
      • Word Embedding with Global Vectors(GloVe)
        • Extra Reading
        • Supplement
      • Encoder(BERT)
        • BERT
        • Extra Reading
      • Decoder(GPT&XLNet&Lamma)
        • GPT
        • XLNet
          • XLNet架构
          • XLNet特点与其他比较
      • Encoder-Decoder(BART& T5)
        • BART
        • T5
  • GenAI
    • Introduction
      • GenAI Paper Must Read
      • GenAI六个阶段
    • Language Models Pre-training
      • Encoder-Decoder Architecture
      • Encoder Deep Dive
      • Decoder Deep Dive
      • Encoder VS Decoder
      • Attention Mechanism
      • Transformers
    • Example: Llama 3 8B架构
    • Fine-Tuning Generation Models
    • RAG and Adavance RAG
    • AI Agent
  • Statistics and Optimization
    • A/B testing
    • Sampling/ABtesting/GradientMethod
    • Gradient Decent Deep Dive
  • Machine Learning System Design
    • Extra Reading
    • Introduction
  • Responsible AI
    • AI Risk and Uncertainty
      • What is AI risk
      • General Intro for Uncertainty Quantification
      • Calibration
      • Conformal Prediction
        • Review the linear regression
        • Exchangeability
        • Split Conformal Prediction
        • Conformalized Quantile Regression
        • Beyond marginal coverage
        • Split Conformal Classification
        • Full Conformal Coverage
        • Cross-Validation +
        • Conformal Histgram Regression
    • xAI
      • SHAP value
  • Extra Research
    • Paper Reading
    • Reference
Powered by GitBook
On this page
  • 反向传播(Backpropagation)
  • 如何计算
  1. Deep Learning
  2. Basic Neural Network

Backpropagation Details

反向传播(Backpropagation)

反向传播是一种用于训练人工神经网络的算法,特别是在深度学习中很常用。它通过计算神经网络的预测结果与实际结果之间的误差,进而调整网络中的权重和偏置,以最小化预测误差。

工作原理:

  1. 前向传播:将输入数据传递到网络的每一层,经过一系列的线性变换和非线性激活函数,最终输出预测结果。

  2. 计算误差:将预测结果与实际结果进行比较,计算损失函数(如均方误差或交叉熵)的值,即误差。

  3. 反向传播误差:通过链式法则(链式求导),将误差从输出层逐层向回传播,计算每个神经元对最终误差的贡献(即偏导数)。

  4. 更新权重:根据计算出的偏导数,使用梯度下降算法或其他优化方法调整网络的权重和偏置,以减少误差。

例子:

假设我们有一个简单的三层神经网络用于分类任务:

  • 输入层:有两个节点,表示两个输入特征。

  • 隐藏层:有两个节点。

  • 输出层:有一个节点,表示二分类问题的预测结果。

步骤:

  1. 前向传播:

    • 输入两个特征 (x1,x2x_1, x_2x1​,x2​)。

    • 权重 w1,w2w_1, w_2w1​,w2​ 和偏置 (b) 将输入线性组合后传递给隐藏层节点,并应用激活函数(如 ReLU 或 Sigmoid)。

    • 隐藏层输出传递给输出层,再次经过线性组合和激活函数后,输出预测值 y^\hat{y}y^​。

  2. 计算误差:

    • 使用损失函数计算实际值 yyy 和预测值 y^\hat{y}y^​ 之间的误差。例如,如果损失函数是均方误差(MSE),则误差为: Loss=12(y^−y)2\text{Loss} = \frac{1}{2}(\hat{y} - y)^2Loss=21​(y^​−y)2

  3. 反向传播误差:

    • 通过链式求导,计算输出层到隐藏层、再到输入层的各个权重的偏导数。这些偏导数表示每个权重对最终误差的贡献。

  4. 更新权重:

    • 使用梯度下降法调整权重: wnew=wold−η∂Loss∂ww_{\text{new}} = w_{\text{old}} - \eta \frac{\partial \text{Loss}}{\partial w}wnew​=wold​−η∂w∂Loss​ 其中,η\etaη 是学习率。

通过不断进行多次前向传播和反向传播,神经网络会逐步学会减少误差,从而提高预测的准确性。

这个过程可以扩展到更深的神经网络和更多的复杂问题。

如何计算 ∂Loss∂w\frac{\partial \text{Loss}}{\partial w}∂w∂Loss​

在反向传播中,∂Loss∂w\frac{\partial \text{Loss}}{\partial w}∂w∂Loss​(即损失函数对权重 www 的偏导数)是通过链式法则计算得出的。这个偏导数告诉我们权重的变化会对损失函数产生多大的影响,从而帮助我们更新权重,减小误差。

1. 损失函数对输出的导数

假设我们使用均方误差(MSE)作为损失函数,定义为:

Loss=12(y^−y)2\text{Loss} = \frac{1}{2}(\hat{y} - y)^2Loss=21​(y^​−y)2

其中,y^\hat{y}y^​ 是神经网络的预测值,yyy 是真实值。

对 y^\hat{y}y^​ 求导数,得到:

∂Loss∂y^=y^−y\frac{\partial \text{Loss}}{\partial \hat{y}} = \hat{y} - y∂y^​∂Loss​=y^​−y

2. 输出对权重的导数

假设神经网络输出 y^\hat{y}y^​ 是通过线性组合输入和权重得到的,即:

y^=w⋅x+b\hat{y} = w \cdot x + by^​=w⋅x+b

其中,www 是权重,xxx 是输入,bbb 是偏置。

现在,我们对 www 求导数:

∂y^∂w=x\frac{\partial \hat{y}}{\partial w} = x∂w∂y^​​=x

这意味着输出 y^\hat{y}y^​ 对权重的导数等于输入 xxx。

3. 链式法则

现在,我们可以通过链式法则,将损失函数对权重的偏导数计算出来:

∂Loss∂w=∂Loss∂y^⋅∂y^∂w\frac{\partial \text{Loss}}{\partial w} = \frac{\partial \text{Loss}}{\partial \hat{y}} \cdot \frac{\partial \hat{y}}{\partial w}∂w∂Loss​=∂y^​∂Loss​⋅∂w∂y^​​

代入之前的公式,得到:

∂Loss∂w=(y^−y)⋅x\frac{\partial \text{Loss}}{\partial w} = (\hat{y} - y) \cdot x∂w∂Loss​=(y^​−y)⋅x

这表示,权重的更新是基于预测误差 (y^−y)(\hat{y} - y)(y^​−y) 和输入 xxx 之间的乘积。

4. 权重更新

最后,使用梯度下降法更新权重:

wnew=wold−η⋅∂Loss∂ww_{\text{new}} = w_{\text{old}} - \eta \cdot \frac{\partial \text{Loss}}{\partial w}wnew​=wold​−η⋅∂w∂Loss​

其中,η\etaη 是学习率,决定了权重更新的步长。

总结

∂Loss∂w\frac{\partial \text{Loss}}{\partial w}∂w∂Loss​ 可以通过以下步骤得出:

  1. 先计算损失函数对输出的导数 ∂Loss∂y^\frac{\partial \text{Loss}}{\partial \hat{y}}∂y^​∂Loss​。

  2. 再计算输出对权重的导数 ∂y^∂w\frac{\partial \hat{y}}{\partial w}∂w∂y^​​。

  3. 最后通过链式法则将这两个导数相乘,得到损失函数对权重的偏导数。

这个过程可以扩展到更加复杂的神经网络,每一层的权重都可以通过类似的方式进行更新。

PreviousNN network DetailsNextGradient Vanishing vs Gradient Exploding

Last updated 8 months ago