學習筆記-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的綜述。馬克一下~
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章