URL
- paper: https://arxiv.org/pdf/1910.10683
- code: https://github.com/huggingface/transformers/tree/main/src/transformers/models/t5
TL;DR
T5名字的由来是:Text-to-Text Transfer Transformer(文本到文本转换的 Transformer)T5使用了《Attention is all you need》中提出的标准Transformer网络,没有任何改变
Algorithm
example
T5是一个encoder-decoder架构的模型,可以用来做文本翻译,本例子使用Hello, world!英语翻译法语为例
0. prompt
prompt的作用是在输入之前加上对任务的描述- 比如
english_to_franch("Hello, world!") API会被prompt为"translate English to French: Hello, world!"纯文本输入到模型
1. encoder input tokenize
T5使用的分词算法是unigram,词表可以在 https://huggingface.co/google-t5/t5-base/blob/main/tokenizer.json 这里找到"translate English to French: Hello, world!"会被tokenize为:[13959, 1566, 12, 2379, 10, 8774, 6, 296, 55, 1]
2. encoder input token embedding
- 和
GPT系列没有区别,需要把encoder input token id查表变成token embedding
3. encoder input position encoding
- 与
GPT系列使用可学习的position embedding不同,T5使用的是position encoding - 且使用的是相对位置编码,而不是绝对位置编码
- 与
GPT系列只在模型casual decoder第一层输入加入position embedding不同,T5的position encoding是在encoder以及decoder的每一层都是使用了
4. encoder
- 与
GPT系列直接使用token embedding+position embedding直接得到hidden state来输入decoder不同,T5有encoder结构 T5的encoder结构采用标准transformer encoder结构,每个token可以看到所有token(双向注意力机制)encoder一共12层,每一层包括如下顺序结构为:self attention blocklayer normself attentiondropout
FFNlayer normMLPdropout
encoder最终输出一个shape = (batch, input_token_len, encoder_dim)的encoder hidden state
5. decoder input token embedding and position encoding
- 与
GPT系列不同之处在于T5在decoder阶段需要decoder input - 通常情况下
decoder input是<BOS>或<S>等特殊标记,token长度仅仅为1,用于表示序列开始 decoder input token embedding和position encoding过程和encoder input token embedding和position encoding并无区别token embedding+position encoding得到decoder hidden state,其shape = (batch, 1, decoder_dim)
6. decoder
decoder一共12层,每一层包括如下顺序结构为:self attention blocklayer normself attentiondropout
cross attention blocklayer normself attentiondropout
FFNlayer normMLPdropout
- 其中
self attention的输入是decoder hidden state(注意不是encoder hidden state),在self attention中,和GPT类似,采用 单向注意力 decoder hidden state和encoder hidden state输入到cross attention中,Cross attention和Self attention实际上只有一个区别:self attention的query / key / value都由同一个hidden state得到,因此称为selfcross attention的key / value由同一个hidden state得到,query由另一个hidden state得到,因此称为cross- 在
encoder-decoder架构的transformer中,decoder中的cross attention的key / value通常由encoder output hidden state得到,query通常由decoder hidden state得到 Cross attention中每个decoder hidden state可以查询到所有的encoder hidden state
- 重复跑完
12层,最终输出shape = (batch, 1, decoder_dim)的decoder output hidden state
7. decoder output hidden state to token
- 需要将
docoder output hidden state用一层MLP转化到vocabulary空间,找到最可能的一个token - 此
token对应的单词即为模型最终输出的第一个词。 - 如果这个词是词表中的结束符,则停止输出。如果不是,则用此词替代前一个词,重复上述的 5. decoder input token embedding and position encoding 和 6. decoder 和 7. decoder output hidden state to token 过程,直到达到最长输出长度限制或出现停止符。
总结
标准 transformer 的行为
encoder输入所有文本,双向注意力,得到encoder hidden statedecoder输入初始化为<BOS>,长度为1decoder每一层包含:self attention,单向注意力cross attention,双向注意力,decoder hidden state做query,encoder hidden states做key and valueFFN