Transformer Notes takes3

Comment: 15 pages, 5 figures

Decoder和Encoder是什么?

Decoder把symbolic的特征,转换到continuous的特征;Encoder把continuous的特征,转换到symbolic的特征

Attention是什么?

Attention计算了 s o f t W e i g h t = M e t h o d ( q u e r y , k e y ) softWeight = Method(query, key) softWeight=Method(query,key) ,Method中有可以有多种选择,可以认为是query在key中的位置信息。Output等于 ∑ s o f t W e i g h t ∗ v a l u e \sum{softWeight * value} softWeightvalue。attention中weight是动态计算的,卷积网络中weight是提前训练的,所以attention在不同的输入中的计算过程也是不一样的(动态网络weight)。

Scaled Dot-Product Attention

Transformer中的Attention为Scaled Dot-Product Attention,其使用矩阵实现了GPU加速。query表示为Matrix Q Q Q ; key 表示为Matrix K K K ,维度为 d k d_k dk ; values表示为Matrix V V V,维度为 d v d_v dv d k = d v d_k = d_v dk=dv? maybe 。

A t t e n t i o n ( Q , K , V ) = S o f t m a x ( Q K T d k ) V Attention(Q, K, V) = Softmax(\frac{QK^T}{\sqrt{d_k}}) V Attention(Q,K,V)=Softmax(dk QKT)V

1 d k \frac{1}{\sqrt{d_k}} dk 1 的引入是因为在 S o f t m a x ( Q K T ) Softmax(QK^T) Softmax(QKT) 中,大的数值会导致梯度变得很小。所以引入这个常量。文章在角注中说明了为什么选取这个数值。

Feed-Forward Networks

线性变换+ReLU+线性变换。或者说,1x1卷积+ReLu+1x1卷积

F F N ( x ) = m a x ( 0 , x W 1 + b 1 ) W 2 + b 2 FFN(x)=max(0, xW_1 +b_1)W_2 +b_2 FFN(x)=max(0,xW1+b1)W2+b2

Embeddings and Softmax

就是MLP+Softmax。两个embedding layers和pre-softmax linear transformation共享权重。权重乘上了 d m o d e l \sqrt{d_{model}} dmodel

Positional Encoding

因为Self-Attention没有包含位置信息,为了使用到位置信息,我们需要手动添加位置编码。Transformer提出的位置编码

P E ( p o s , 2 i ) = s i n ( p o s / 1000 0 2 i / d m o d e l ) PE_{(pos, 2i)} = sin(pos/10000^{2i/d_{model}}) PE(pos,2i)=sin(pos/100002i/dmodel)

P E p o s , 2 i + 1 = c o s ( p o s / 1000 0 2 i / d m o d e l ) PE_{pos, 2i+1} = cos(pos/10000^{2i/d_{model}}) PEpos,2i+1=cos(pos/100002i/dmodel)

有如下特性

  • 每一个dimension有独立的位置编码,且相邻的dimension相差很大。我推测是因为相邻的差别要很大,不然会被淹没在输入中,因为相位变化本身很小。

  • 位置编码是不符合函数定义的,sin和cos。这是因为如下两点

  • 如果是线性或者非线性函数,其可以被视为线性函数和非线性函数的叠加。其和网络自身尝试拟合的函数重叠。同时,我推测:输入数据中可能会出现类似的线性或非线性变化,其会导致位置编码实效。

  • 如果是普通函数,当输入尺寸比训练的时候的更长。网络无法去处理这种情况。而sin和cos因为是重复的,所以任何输入长度大小,都可以正常工作。

Jargon

  • Token: basic units of text/code for LLM AI models to process/generate language.

  • Tokenization: splitting input/output texts into smaller units for LLM AI models.

  • Vocabulary size: the number of tokens each model uses, which varies among different GPT models.

To further investigate:

  • Adam是如何实现优化的

  • beam search是什么

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值