自然语言处理(NLP):15 图解attention计算过程(02)

专注于文本分类、关键词抽取、文本摘要、FAQ问答系统、对话系统语义理解NLU、知识图谱等。结合工业界具体案例和学术界最新研究成果实现NLP技术场景落地。更多精彩内容加入“NLP技术交流群” 学习。


文主要介绍 seq2seq框架和attention 在机器翻译中应用。

目录

  • seq2seq 面临问题
  • Attention计算过程
    • [Step 0: Prepare hidden states.]
    • [Step 1: Obtain a score for every encoder hidden state]
    • [Step 2: Run all the scores through a softmax layer.]
    • [Step 3][Multiply each encoder hidden state by its softmaxed score.]
    • [Step 4][Sum up the alignment vectors.]
    • [Step 5][Feed the context vector into the decoder.]
    • [step: 求解attention 的整个过程汇总]

seq2seq 面临问题

  • 遗忘

句子过长,很难记住以前的信息

  • 对齐

输入和输出序列不同情况下,如何对齐

自然语言处理(NLP):15 图解attention计算过程(02)_第1张图片

Fig. 0.1: seq2seq with an input sequence of length 4

图片

Fig. 0.2: seq2seq with an input sequence of length 64

Attention计算过程

  • Attention为每个word计算出一个得分score
  • 对得分进行softmaxed
  • 对encoder 中每个hidden state 进行加权求和,得到当前时刻 context 向量

[Step 0: Prepare hidden states.]

自然语言处理(NLP):15 图解attention计算过程(02)_第2张图片

Fig. 1.0: Getting ready to pay attention

  • encoder 中全部的hidden state(绿色)

4个hidden state

  • decoder 的hidden state(红色)

encoder 最后一个hidden state 作为decoder输入

[Step 1: Obtain a score for every encoder hidden state]

自然语言处理(NLP):15 图解attention计算过程(02)_第3张图片

Fig. 1.1: Get the scores

计算encoder 的hidden state 和 decoder 的hidden state 的得分(相关性)

我们通过一个score函数来计算,本案例中我们使用 dot product 来完成。

decoder_hidden = [10, 5, 10]
encoder_hidden  score
---------------------
     [0, 1, 1]     15 (= 10×0 + 5×1 + 10×1, the dot product)
     [5, 0, 1]     60
     [1, 1, 0]     15
     [0, 5, 1]     35

更多的score function (也被称为 alignment score function 或者alignment model)如下所示:

自然语言处理(NLP):15 图解attention计算过程(02)_第4张图片

[Step 2: Run all the scores through a softmax layer.]

自然语言处理(NLP):15 图解attention计算过程(02)_第5张图片

Fig. 1.2: Get the softmaxed scores

计算出的score-> softmax 层-> 归一化的分值 softmaxed score (scalar)

encoder_hidden score score^


 [0, 1, 1]     15       0

 [5, 0, 1]     60       1

 [1, 1, 0]     15       0

 [0, 5, 1]     35       0

[Step 3][Multiply each encoder hidden state by its softmaxed score.]

自然语言处理(NLP):15 图解attention计算过程(02)_第6张图片

Fig. 1.3: Get the alignment vectors

softmaxed score 和 每个encoder 的hidden state 进行相乘-> 每个hidden state 得到新的向量(context vector

encoder score score^ alignment


[0, 1, 1] 15 0 [0, 0, 0]

[5, 0, 1] 60 1 [5, 0, 1]

[1, 1, 0] 15 0 [0, 0, 0]

[0, 5, 1] 35 0 [0, 0, 0]

[Step 4][Sum up the alignment vectors.]

自然语言处理(NLP):15 图解attention计算过程(02)_第7张图片

Fig. 1.4: Get the context vector

这里通过加权求和的方式获取最终 context vector.

encoder  score  score^  alignment
---------------------------------
[0, 1, 1]   15     0  [0, 0, 0]
[5, 0, 1]   60     1  [5, 0, 1]
[1, 1, 0]   15     0  [0, 0, 0]
[0, 5, 1]   35     0  [0, 0, 0]
context = [0+5+0+0, 0+0+0+0, 0+1+0+0] = [5, 0, 1]

[Step 5][Feed the context vector into the decoder.]

自然语言处理(NLP):15 图解attention计算过程(02)_第8张图片

Fig. 1.5: Feed the context vector to decoder

[step: 求解attention 的整个过程汇总]

动态演示attention 的整个过程

自然语言处理(NLP):15 图解attention计算过程(02)_第9张图片

图片

你可能感兴趣的:(自然语言处理,自然语言处理,Attention,PyTorch)