♎
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
  • 解码器(Decoder)
  • 1. 解码器的输入与目标
  • 2. 解码器的隐藏状态更新
  • 3. 生成下一个单词
  • 4. 直观示例
  • 5. 总结
  1. GenAI
  2. Language Models Pre-training

Decoder Deep Dive

解码器(Decoder)

1. 解码器的输入与目标

解码器的作用:

  • 输入:编码器(Encoder)生成的 上下文向量 c,以及前一个时间步的输出。

  • 输出:一个新的序列 y1,y2,…,yUy_1, y_2, \dots, y_Uy1​,y2​,…,yU​(可能与输入长度不同),比如:

    • 机器翻译时,yyy 是目标语言的翻译句子。

    • 文本摘要时,yyy 是摘要文本。

    • 语音识别时,yyy 是转换后的文本。

公式:

y=(y1,y2,⋯ ,yU)y = (y_1, y_2, \cdots , y_U)y=(y1​,y2​,⋯,yU​)

其中 UUU 可能与输入序列长度 TTT 不同。


2. 解码器的隐藏状态更新

解码器在每个时间步 ttt 计算一个新的隐藏状态:

st′=g(st−1,yt′−1,c)s_t' = g(s_{t-1}, y_{t'-1}, c)st′​=g(st−1​,yt′−1​,c)

含义:

  • st′s_t'st′​:当前时间步 ttt 的隐藏状态。

  • st−1s_{t-1}st−1​:前一个时间步的隐藏状态(用来存储之前的信息)。

  • yt′−1y_{t'-1}yt′−1​:上一个时间步的输出(解码器是自回归的,即它使用自己生成的前一个 token 作为输入)。

  • ccc:来自编码器的上下文向量,表示整个输入序列的信息。

直观理解:

  • 解码器从 编码器的输出(context vector)+ 之前生成的 token 中学习信息。

  • 当前隐藏状态 st′s_t'st′​ 是基于过去的内容逐步更新的,类似于 RNN 的递归计算过程。


3. 生成下一个单词

解码器的隐藏状态 st′s_t'st′​ 会传递到输出层,计算当前时间步的 token yt′y_{t'}yt′​ 的条件概率:

P(yt′∣yt′−1,⋯ ,y1,c)=softmax(st−1,yt′−1,c)P(y_{t'} | y_{t'-1}, \cdots , y_1, c) = \text{softmax}(s_{t-1}, y_{t'-1}, c)P(yt′​∣yt′−1​,⋯,y1​,c)=softmax(st−1​,yt′−1​,c)

解释:

  • 解码器会预测下一个 token yt′y_{t'}yt′​ 的概率分布,这里使用了 softmax 归一化,使得所有可能的单词形成一个概率分布。

  • 例如,如果在机器翻译任务中:

    • P("Hello"∣"Bonjour",c)=0.8P(\text{"Hello"} | \text{"Bonjour"}, c) = 0.8P("Hello"∣"Bonjour",c)=0.8

    • P("Hi"∣"Bonjour",c)=0.2P(\text{"Hi"} | \text{"Bonjour"}, c) = 0.2P("Hi"∣"Bonjour",c)=0.2

    • 说明解码器认为 "Bonjour" 更可能翻译成 "Hello"。

直观理解:

  1. 解码器每一步预测下一个单词的概率分布。

  2. 通过 softmax 选择最有可能的单词。

  3. 这个新单词会成为下一步的输入,重复这个过程,直到生成完整句子。


4. 直观示例

例子 1:机器翻译(英文 → 法文)

输入(Encoder): "I love machine learning" 目标(Decoder): "J'aime l'apprentissage automatique"

  1. 编码器 读取 "I love machine learning",最终输出 context vector ( c )。

  2. 解码器 使用 context vector ccc,并从 "<start>" 令牌开始,逐步预测:

    • 预测 "J'aime",作为第一步输出。

    • 预测 "l'apprentissage",作为第二步输出。

    • 预测 "automatique",作为第三步输出。

    • 预测 "<end>",表示解码完成。

例子 2:文本摘要

输入(Encoder): "Neural networks are widely used in deep learning. They can model complex patterns and relationships in data." 目标摘要(Decoder): "Neural networks power deep learning."

  • 编码器 读取整段文本,提取其上下文信息。

  • 解码器 通过 context vector 逐步生成摘要。


5. 总结

  1. 解码器的任务 是从编码器得到的上下文向量 ccc,然后逐步生成新的序列 y1,y2,…,yUy_1, y_2, \dots, y_Uy1​,y2​,…,yU​。

  2. 解码器的隐藏状态 st′s_t'st′​依赖于:

    • 之前的隐藏状态 st−1s_{t-1}st−1​

    • 之前的输出 yt′−1y_{t'-1}yt′−1​

    • 上下文向量 cc c

  3. 解码器通过 softmax 计算下一个 token 的概率分布,然后选择最可能的 token 作为输出。

  4. 它是一个自回归(Autoregressive)过程,每个新生成的单词都会作为下一步的输入,直到生成完整的序列。

应用场景:

  • 机器翻译(NMT)

  • 文本摘要

  • 语音识别(ASR)

  • 聊天机器人(Chatbot)

PreviousEncoder Deep DiveNextEncoder VS Decoder

Last updated 2 months ago