♎
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
  • Encoder 和 Decoder 的数学区别
  • 1. 主要区别概览
  • 2. Encoder(编码器)的数学公式
  • 3. Decoder(解码器)的数学公式
  • 4. Encoder 和 Decoder 之间的核心数学区别
  • 5. 总结
  • h, x, s, y到底是啥
  • 例子:机器翻译
  • 2. 编码器(Encoder)
  • 3. 解码器(Decoder)
  • 4. 关键概念总结
  • 5. 图示
  1. GenAI
  2. Language Models Pre-training

Encoder VS Decoder

Decoder

Encoder 和 Decoder 的数学区别

1. 主要区别概览

组件
输入
计算方式
输出

Encoder

Decoder


2. Encoder(编码器)的数学公式

2.1 输入

编码器的输入是一个序列:

x=(x1,x2,…,xT)x = (x_1, x_2, \dots, x_T)x=(x1​,x2​,…,xT​)

其中,每个 xtx_txt​ 是输入的 token(例如单词的向量表示)。

2.2 递归计算

编码器使用递归公式计算隐藏状态:

ht=f(ht−1,xt)h_t = f(h_{t-1}, x_t)ht​=f(ht−1​,xt​)

其中:

  • hth_tht​ 是时间步 ttt 的隐藏状态。

  • fff 是 RNN/LSTM/GRU 的转换函数。

最简单的 RNN 计算方式:

ht=tanh⁡(Whht−1+Wxxt+b)h_t = \tanh(W_h h_{t-1} + W_x x_t + b)ht​=tanh(Wh​ht−1​+Wx​xt​+b)

LSTM 计算方式(包含遗忘门、输入门、输出门):

ft=σ(Wf[ht−1,xt]+bf)f_t = \sigma(W_f [h_{t-1}, x_t] + b_f)ft​=σ(Wf​[ht−1​,xt​]+bf​)
it=σ(Wi[ht−1,xt]+bi)i_t = \sigma(W_i [h_{t-1}, x_t] + b_i)it​=σ(Wi​[ht−1​,xt​]+bi​)
ot=σ(Wo[ht−1,xt]+bo)o_t = \sigma(W_o [h_{t-1}, x_t] + b_o)ot​=σ(Wo​[ht−1​,xt​]+bo​)
c~t=tanh⁡(Wc[ht−1,xt]+bc)\tilde{c}_t = \tanh(W_c [h_{t-1}, x_t] + b_c)c~t​=tanh(Wc​[ht−1​,xt​]+bc​)
ct=ft⊙ct−1+it⊙c~tc_t = f_t \odot c_{t-1} + i_t \odot \tilde{c}_tct​=ft​⊙ct−1​+it​⊙c~t​
ht=ot⊙tanh⁡(ct)h_t = o_t \odot \tanh(c_t)ht​=ot​⊙tanh(ct​)

最终,编码器的隐藏状态 hTh_ThT​ 形成上下文向量 ccc:

c=m(h1,h2,…,hT)c = m(h_1, h_2, \dots, h_T)c=m(h1​,h2​,…,hT​)

其中 mmm 可能是直接取最后一个隐藏状态(简单情况)或某种加权平均(如注意力机制)。


3. Decoder(解码器)的数学公式

3.1 输入

解码器的输入是:

  • 编码器的上下文向量 ccc。

  • 目标序列的前一个 token yt′−1y_{t'-1}yt′−1​。

3.2 递归计算

解码器使用自己的隐藏状态 st′s_t'st′​ 进行更新:

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′​ 是解码器的隐藏状态。

  • ggg 是解码器的转换函数(通常与编码器的 fff 结构类似)。

RNN 版本:

st′=tanh⁡(Wsst′−1+Wyyt′−1+Wcc+b)s_{t'} = \tanh(W_s s_{t'-1} + W_y y_{t'-1} + W_c c + b)st′​=tanh(Ws​st′−1​+Wy​yt′−1​+Wc​c+b)

LSTM 版本:

ft′=σ(Wf[st′−1,yt′−1,c]+bf)f_{t'} = \sigma(W_f [s_{t'-1}, y_{t'-1}, c] + b_f)ft′​=σ(Wf​[st′−1​,yt′−1​,c]+bf​)
it′=σ(Wi[st′−1,yt′−1,c]+bi)i_{t'} = \sigma(W_i [s_{t'-1}, y_{t'-1}, c] + b_i)it′​=σ(Wi​[st′−1​,yt′−1​,c]+bi​)
ot′=σ(Wo[st′−1,yt′−1,c]+bo)o_{t'} = \sigma(W_o [s_{t'-1}, y_{t'-1}, c] + b_o)ot′​=σ(Wo​[st′−1​,yt′−1​,c]+bo​)
c~t′=tanh⁡(Wc[st′−1,yt′−1,c]+bc)\tilde{c}_{t'} = \tanh(W_c [s_{t'-1}, y_{t'-1}, c] + b_c)c~t′​=tanh(Wc​[st′−1​,yt′−1​,c]+bc​)
ct′=ft′⊙ct′−1+it′⊙c~t′c_{t'} = f_{t'} \odot c_{t'-1} + i_{t'} \odot \tilde{c}_{t'}ct′​=ft′​⊙ct′−1​+it′​⊙c~t′​
st′=ot′⊙tanh⁡(ct′)s_{t'} = o_{t'} \odot \tanh(c_{t'})st′​=ot′​⊙tanh(ct′​)

3.3 输出生成

解码器的隐藏状态 st′s_t'st′​ 用于计算当前 token yt′y_t'yt′​ 的概率:

P(yt′∣yt′−1,…,y1,c)=softmax(Wost′+bo)P(y_{t'} | y_{t'-1}, \dots , y_1, c) = \text{softmax}(W_o s_{t'} + b_o)P(yt′​∣yt′−1​,…,y1​,c)=softmax(Wo​st′​+bo​)

softmax 计算每个可能单词的概率分布,并选择最可能的单词作为当前输出。


4. Encoder 和 Decoder 之间的核心数学区别

方面
编码器(Encoder)
解码器(Decoder)

输入

隐藏状态计算

输出

目标

压缩整个输入序列的信息

逐步生成目标序列

数学核心区别:

  1. 输入不同:

    • 编码器的输入是整个源语言句子 x1,x2,...,xTx_1, x_2, ..., x_Tx1​,x2​,...,xT​。

    • 解码器的输入是**编码器输出的上下文向量 ccc + 目标序列的前一个 token yt′−1y_{t'-1}yt′−1​。

  2. 隐藏状态计算方式不同:

    • 编码器:ht=f(ht−1,xt)h_t = f(h_{t-1}, x_t)ht​=f(ht−1​,xt​),仅依赖过去信息(单向)或前后信息(双向)。

    • 解码器:st′=g(st′−1,yt′−1,c)s_{t'} = g(s_{t'-1}, y_{t'-1}, c)st′​=g(st′−1​,yt′−1​,c),依赖于编码器输出 + 先前已生成的 token。

  3. 输出不同:

    • 编码器输出最终隐藏状态 hTh_ThT​(或经过注意力机制的上下文向量 ccc),用作整个输入的表示。

    • 解码器使用 softmax 生成目标序列中的 token,逐步预测下一个单词。


5. 总结

  1. 编码器 负责 处理整个输入序列,并生成一个固定长度的上下文向量 ccc,用来表示整个输入序列的信息。

  2. 解码器 逐步接收上下文向量 ccc,并依赖前一个时间步的输出,一步步生成目标序列。

  3. 数学层面上:

    • 编码器是一个标准的递归网络,输入的是 源语言序列,只依赖于 过去信息(或前后信息,若是双向)。

    • 解码器依赖编码器的输出 + 先前已生成的 token,是一个 自回归模型,用 softmax 生成下一个 token。

h, x, s, y到底是啥

在 序列到序列(seq2seq) 任务中,编码器的最终隐藏状态 hTh_ThT​ 被称为上下文变量或上下文向量(context vector)。它是整个输入序列的信息压缩,并传递给解码器,以生成目标输出。


例子:机器翻译

我们以 英语到法语翻译 为例:

  • 输入句子(英文):"I love you"

  • 目标句子(法语):"Je t'aime"

1. 词向量表示

假设:

  • "I" → x1x_1x1​

  • "love" → x2x_2x2​

  • "you" → x3x_3x3​

这些 xtx_txt​ 是输入单词的词向量(word embeddings)。


2. 编码器(Encoder)

编码器是一个 循环神经网络(RNN, LSTM, GRU),它逐步读取输入单词,并更新隐藏状态(hidden state) hth_tht​:

  1. 输入第一个单词 "I":

    h1=f(x1,h0)h_1 = f(x_1, h_0)h1​=f(x1​,h0​)

    其中 h0h_0h0​ 是初始隐藏状态(通常设为全零)。

  2. 输入第二个单词 "love":

    h2=f(x2,h1)h_2 = f(x_2, h_1)h2​=f(x2​,h1​)
  3. 输入第三个单词 "you":

    h3=f(x3,h2)h_3 = f(x_3, h_2)h3​=f(x3​,h2​)

最终,编码器的最后一个隐藏状态 hTh_ThT​ 作为上下文向量:

hT=h3h_T = h_3hT​=h3​

它浓缩了整个输入句子的意思,并作为解码器的输入。


3. 解码器(Decoder)

解码器是另一个 RNN,它接收 上下文向量 ( h_T ) 作为初始状态,并逐步生成目标序列("Je t'aime"):

  1. 生成第一个单词 "Je":

    s1=g(y0,hT)s_1 = g(y_0, h_T)s1​=g(y0​,hT​)

    其中 y0y_0y0​ 是起始标记 <SOS>。

  2. 生成第二个单词 "t'":

    s2=g(y1,s1)s_2 = g(y_1, s_1)s2​=g(y1​,s1​)

    其中 y1y_1y1​ 是 "Je"。

  3. 生成第三个单词 "aime":

    s3=g(y2,s2)s_3 = g(y_2, s_2)s3​=g(y2​,s2​)

    其中 y2y_2y2​ 是 "t'"。

最终,解码器输出 "Je t'aime",完成翻译。


4. 关键概念总结

符号
解释

输入单词的词向量(如 "I" → x_1)

隐藏状态,编码器在每个时间步更新它

上下文向量,编码器的最后隐藏状态

解码器的隐藏状态

解码器的输出单词


5. 图示

Encoder:
"I"   → x₁ → h₁ →
"love" → x₂ → h₂ →
"you"  → x₃ → h₃  (最终的上下文向量 h_T)

Decoder:
h_T → "Je"  → "t'" → "aime"
PreviousDecoder Deep DiveNextAttention Mechanism

Last updated 2 months ago

输入序列

逐步更新隐藏状态

最后一个隐藏状态 或上下文向量

上下文向量 + 目标序列的前一 token

逐步更新隐藏状态

生成的 token ,通过 softmax 计算概率分布

输入序列

编码器的上下文向量 + 目标 token

最终隐藏状态 (即上下文向量 )

生成的 token

x1,x2,…,xTx_1, x_2, \dots, x_Tx1​,x2​,…,xT​
ht=f(ht−1,xt)h_t = f(h_{t-1}, x_t)ht​=f(ht−1​,xt​)
hTh_ThT​
ccc
ccc
yt′−1y_{t'-1}yt′−1​
st′=g(st′−1,yt′−1,c)s_{t'} = g(s_{t'-1}, y_{t'-1}, c)st′​=g(st′−1​,yt′−1​,c)
yt′y_{t'}yt′​
x=(x1,x2,...,xT)x = (x_1, x_2, ..., x_T)x=(x1​,x2​,...,xT​)
ccc
yt′−1y_{t'-1}yt′−1​
ht=f(ht−1,xt)h_t = f(h_{t-1}, x_t)ht​=f(ht−1​,xt​)
st′=g(st′−1,yt′−1,c)s_{t'} = g(s_{t'-1}, y_{t'-1}, c)st′​=g(st′−1​,yt′−1​,c)
hTh_ThT​
ccc
yt′y_{t'}yt′​
xtx_txt​
hth_tht​
hTh_ThT​
sts_tst​
yty_tyt​