赞
踩
论文链接: https://arxiv.org/abs/2012.12556
本文是transformer的综述。
Transformer 一开始是用在NLP领域的。它主要是由相同结构的多个encoder, decoder模块组成。每一个encoder和decoder都是由 self-attention layer和feed-forward neural network 组成。而decoder还包括 encoder-decoder attention layer 。
接下来分别拆开每个模块来看以下。
简单的self-attention layer输入是三个不同的vectors: query vector q, key vector k, value vector v, 各自维度为:
d
q
=
d
k
=
d
v
=
d
m
o
d
e
l
=
512
d_q = d_k = d_v = d_{model}=512
dq=dk=dv=dmodel=512。
来自不同的向量组合成三个不同的矩阵,Q, K, V。然后 attention function 根据下面步骤来进行计算:
上面的步骤可以融合成一个公式:
Attention
(
Q
,
K
,
V
)
=
softmax
(
Q
⋅
K
T
d
k
)
⋅
V
\text{Attention}(Q,K,V) = \text{softmax}(\frac{Q \cdot K^T}{\sqrt{d_k}})\cdot V
Attention(Q,K,V)=softmax(dk
Q⋅KT)⋅V
而对于在Decoder中的 encoder-decoder attention layer, 它的attention function 和上面大致上一样,除了以下一点:
key matrix K 和 value matrix V 是来自于encoder 模块的,query matrix Q是来自于前面一层。
前面的步骤是位置无关的,意味着self-attention layer 没有能力去捕捉位置信息。为了解决这个问题,在原始的input embedding上添加一个维度为 d m o d e l d_{model} dmodel的positional encoding。具体如下:
PE
(
p
o
s
,
2
i
)
=
sin
(
p
o
s
1000
0
2
i
d
m
o
d
e
l
)
\text{PE}(pos, 2i) = \sin(\frac{pos}{10000^{\frac{2i}{d_{model}}}})
PE(pos,2i)=sin(10000dmodel2ipos)
PE
(
p
o
s
,
2
i
+
1
)
=
cos
(
p
o
s
10000
2
i
d
m
o
d
e
l
)
\text{PE}(pos, 2i+1)=\cos(\frac{pos}{10000\frac{2i}{d_{model}}})
PE(pos,2i+1)=cos(10000dmodel2ipos)
其中,pos 是词在句子中的位置, i 表示positional encoding的当前维度。
Multi-head attention 是一个提升普通self-attentioin layer的一个机制。一个简单的single-head self-attention layer 限制了我们专注于一个或多个特定位置的能力,而不会同时影响对其他同样重要位置的注意。具体实施就是,给定一个输入向量和heads的数量, h h h,输入向量首先回变成三个不同的向量组: the query group, the key group, 和the value group。在每一个group里面,分别有 h h h个vecotors,维度为 d q ′ = d k ′ = d v ′ = d m o d e l / h = 64 d_{q'}=d_{k'}=d_{v'}=d_{model}/h=64 dq′=dk′=dv′=dmodel/h=64。来自于不同输入的向量会形成三个不同的groups of matrixs { Q i } i = 1 h , { K i } i = 1 h \{Q_i\}^h_{i=1}, \{K_i\}^h_{i=1} {Qi}i=1h,{Ki}i=1h和 { V i } i = 1 h \{V_i\}^h_{i=1} {Vi}i=1h。Multi-head attention的步骤可以总结为:
MultiHead
(
Q
′
,
K
′
,
V
′
)
=
Concat
(
h
e
a
d
1
,
.
.
.
,
h
e
a
d
h
)
W
∘
\text{MultiHead}(Q',K',V')=\text{Concat}(head_1, ..., head_h)W^{\circ}
MultiHead(Q′,K′,V′)=Concat(head1,...,headh)W∘
其中,
h
e
a
d
i
=
A
t
t
e
n
t
i
o
n
(
Q
i
,
K
i
,
V
i
)
head_i = Attention(Q_i, K_i, V_i)
headi=Attention(Qi,Ki,Vi)
Q ′ , K ′ , V ′ Q', K', V' Q′,K′,V′分别是 { Q i } i = 1 h , { K i } i = 1 h , { V i } i = 1 h \{Q_i\}^h_{i=1},\{K_i\}^h_{i=1},\{V_i\}^h_{i=1} {Qi}i=1h,{Ki}i=1h,{Vi}i=1h的连接。 W ∘ W^{\circ} W∘是维度为 R d m o d e l × d m o d e l R^{d_{model} \times d_{model}} Rdmodel×dmodel的linear projection matrix。
在每一个encoder和decoder的sub-layer之间添加了residual connection。加强了信息的流动。在residual connection之后还添加了layer-normalization。这些操作可以归结为:
LayerNorm(X+Attention(X))
\text{LayerNorm(X+Attention(X))}
LayerNorm(X+Attention(X))
其中,
X
X
X是self-attention layer的输入。这是因为query, key和value matrices Q, K, V是来自于同样的输入matrix X。
在self-attention layers 之后都会有Feed-Forward Network(FFN)。其包含两个linear transformation layer 和 一个 nonlinear activation function。
FFN ( X ) = W 2 σ ( W 1 X ) \text{FFN}(X) = W_2 \sigma(W_1X) FFN(X)=W2σ(W1X)
其中 σ ( ) \sigma() σ()是GELU。
final layer是用来把向量转化回词汇。由一个 linear layer 和 softmax layer组成。linear layer 把向量映射回维度为 d w o r d d_{word} dword的逻辑向量, d w o r d d_{word} dword是字典里面词汇的数量。softmax layer把逻辑向量转换为probabilities。
而对于CV的任务,大多说transformers会使用original transformer’s encoder module。这样的transformers会被认为是一个新的feature selector。相比于只关注local characteristics的CNNs, transformer可以捕捉long-distance characteristics,意味着它可以更好地获得全局信息。
相比于hidden state必须逐个计算的RNNs, transformer更有效因为其self-attention layer的输出和FCN可以 parallel computation。
视觉任务也越来越多使用transformer。 我们从Classification, objection detection等等的任务进行大概的了解。
Transformer可以作为网络的Backbone。对于visual tranformer, 它们使用了 tokenizer 来把像素group成一个小的 visual tokens 。然后这些 visual tokens直接用于图像分类,transformers用于构建tokens之间的联系。最近使用transformer作分类任务的有 iGPT, ViT, 和 DeiT。
Generative pre-training methods 包含了两个阶段: pre-training stage和 fine-tunning stage。
Vision Transformer 是直接对sequences of image patches进行classification。
首先,图像
x
∈
R
H
×
W
×
C
x \in R^{H \times W \times C}
x∈RH×W×C 变为一系列的 flattened 2D patches
x
p
∈
R
N
×
(
P
2
⋅
C
)
x_p \in R^{N \times (P^2 \cdot C)}
xp∈RN×(P2⋅C),
(
P
,
P
)
(P, P)
(P,P)是每一个image patch 的大小。 sequence length 是
N
=
H
W
/
P
2
N = HW/P^2
N=HW/P2。因为transformer要求在所有的层上输入固定的长度,因此需要一个linear projection layer生成patch 和 position 的embedding输入进encoder。随后使用了transformer的标准encoder,和MLP head进行分类。
Data-efficient image Transformer 是一个 convolution-free transformer。与ViT-B有同样的结构但参数比较少。如果使用CNN 蒸馏会提高性能。
Transformer-based object detection methods 大致上分为两类: transformer-based set prediction 和 transformer-based backbone。
著名的DETR就是基于这个方法。它是一个end-to-end的目标检测器,能够把目标检测任务当成简单的set prediction 问题,消除了传统人工设计的Anchor 和 NMS。具体框架如下:
DETR一开始使用CNN backbone提取特征,然后和position encoding结合,形成一个flattened feature输入到transformer encoder。Decoder 会把encoder的 embeddings和 positional encodings(object queries)当成输入,产生
N
N
N个输出embeddings。 其中这里的
N
N
N是一个预先设定的值,通常大于图像的目标数。最后简单的Feed-Forward Networks会用来计算最后的预测结果,包括bounding boxes 和 class labels。 DETR采用 bipartite matching algorithm来比较ground-truth和prediction。 Loss fuinction为 Hungarian loss
L
Hungarian
(
y
,
y
^
)
∑
i
=
1
N
[
−
l
o
g
P
^
σ
^
(
i
)
(
c
i
)
+
1
{
c
i
≠
ϕ
}
L
b
o
x
(
b
i
,
b
^
σ
^
(
i
)
)
]
L_{\text{Hungarian} (y, \hat{y})} \sum_{i=1}^N [-log\hat{P}_{\hat{\sigma}_{(i)}}(c_i) + 1_{\{c_i \neq \phi\}}L_{box}(b_i, \hat{b}_{\hat{\sigma}}(i))]
LHungarian(y,y^)i=1∑N[−logP^σ^(i)(ci)+1{ci=ϕ}Lbox(bi,b^σ^(i))]
其中,
y
,
y
^
y, \hat{y}
y,y^是ground-truth和prediction。
σ
^
\hat{\sigma}
σ^是 optimal assignment,
C
i
C_i
Ci是ground-truth label和
P
^
σ
^
(
i
)
(
c
i
)
\hat{P}_{\hat{\sigma}_{(i)}}(c_i)
P^σ^(i)(ci)是预测的label。
b
i
b_i
bi是ground-truth bounding boxes,
b
^
σ
^
(
i
)
\hat{b}_{\hat{\sigma}}(i)
b^σ^(i)是预测的bounding boxes。
但是DETR有它的局限性,比如说训练时间长和对小目标的检测效果较差。因此,有学者进一步提出了Deformable DETR, Adaptive Clustering Transformer 等等。
Transformer也可以作为backbone。输入的图像分割成数个patches,然后输入到vision transformer,输出enbedding features,最后通过dection head进行预测。ViT-FRCNN是其中的一个例子。
目前先关注这么多transformer知识,后续补充。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。