学习笔记-Transformer中注意力机制

transformer框架

之前对《Attention is all your need》中的框架结构一直不理解,读了很多相关的介绍也是迷迷糊糊的。今天又理了一遍相关的知识点,主要是各个涉及了注意力机制部分的Q,K,V,又看了一部分相关的TensorFlow实现代码,感觉比之前稍微清楚了一些。

在这里插入图片描述

相关链接

  • 谷歌官方的一份代码models/mtf_transformer.py/_layer_stack函数,里面有Self-attention,Encoder-Decoder attention,Local attention, Compressed attention几种。但是核心部分都被封装起来了,需要安装mesh-tensorflow,查看相关的函数
import mesh-tensorflow as mtf

# Self attention layer
y, new_k, new_v = mtf.layers.multihead_self_attention_incremental(some_argvs)

# Encoder-Decoder attention layer
y, new_k, new_v =  mtf.layers.multihead_encdec_attention_incremental(some_argvs)

# Local attebtion          
y, new_k, new_v = mtf.layers.masked_local_attention_1d_incremental(some_argvs)

# Compressed attention
mtf.layers.multihead_self_attention_memory_compressed(some_argvs)
  • 在谷歌官方出来之前,有很多人自己实现了Transformer的逻辑,这里有一份我觉得写得很清楚的代码,而且也有相关的博主进行了代码解析
  • 我也看了一些让人直观理解整个过程的博客,这篇动图把decoder过程解释的很清楚
  • 也是在阅读代码的时候,我发现还有很多attention,所以打算日后慢慢了解。这里有一份多种attention的综述。马克一下~
发布了120 篇原创文章 · 获赞 35 · 访问量 17万+

猜你喜欢

转载自blog.csdn.net/u012328476/article/details/104637423