专注于文本分类、关键词抽取、文本摘要、FAQ问答系统、对话系统语义理解NLU、知识图谱等。结合工业界具体案例和学术界最新研究成果实现NLP技术场景落地。更多精彩内容加入“NLP技术交流群” 学习。
文主要介绍 seq2seq框架和attention 在机器翻译中应用。
句子过长,很难记住以前的信息
输入和输出序列不同情况下,如何对齐
Fig. 0.1: seq2seq with an input sequence of length 4
Fig. 0.2: seq2seq with an input sequence of length 64
Fig. 1.0: Getting ready to pay attention
4个hidden state
encoder 最后一个hidden state 作为decoder输入
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)如下所示:
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
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]
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]
Fig. 1.5: Feed the context vector to decoder
动态演示attention 的整个过程