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)