【NLP】Transformer(seq2seq model with Self-Attention)

【一】RNN & CNN

  • RNN 难以 平行运算,于是,有人想用 CNN 取代 RNNkernels 可以同时计算,但需要 迭很多层 才能扩展 感受野

【二】用 Self-Attention 取代 RNN

  • Self-Attention 可以同时计算 b1 b2 b3 b4
  • 通过 embeddinginput 搞成 vector(词向量) 的形式
  • 将对应的 vector(词向量) 乘上3个不同的 matrix(Wq,Wk,Wv),此过程称为 transformation
  1. q:query(to match others)
  2. k:key(to be matched)
  3. v:value(information to be extracted)
  • 拿每个 query q 去对每个 key kattention,吃两个 vector,抛出一个分数 。(d is dim of q and k

  • 将得到的 attention 的值跑一下 Soft-max,得到各个概率

  • 将得到的 各个概率 乘上 各个 value相加 后得到对应的最终的值

【三】纵观 Self-Attention

  • transformer(计算 QKV
  • attention(计算 K * Q,省略 根号d 以简化)
  • soft-max
  • value

【四】Multi-head Self-Attention

  • Q K V 的基础上继续分化
  • 其实这个流程下来跟一开始的 inputsequence 无关,怎么办呢,给 ai 加个 ei,判断出当前的 ai 是在什么位置
          

【五】Seq2Seq with Attention

  • 原始的 Seq2SeqEncoderDecoder
  • 现在用 Attention 来取代 RNN 的结构
  • 谷歌官方给出的动画示意图
  • Encoding 的时候做 Attention,图示做了 3次 Attention,此时是平行运算
  • Decoding 的时候,Encode 出来的结果作为输入,先做 Attention,然后根据其结果,结合 Encode 的结果做 Attention
     

【六】Transformer

  • 图示,左半部分是 Encoding,又半部分是 Decoding
发布了57 篇原创文章 · 获赞 5 · 访问量 2880

猜你喜欢

转载自blog.csdn.net/qq_34330456/article/details/100922649