当前位置:   article > 正文

Vision Transformers的注意力层概念解释和代码实现_vision transformer的多头注意力机制

vision transformer的多头注意力机制

2017年推出《Attention is All You Need》以来,transformers 已经成为自然语言处理(NLP)的最新技术。2021年,《An Image is Worth 16x16 Words》,成功地将transformers 用于计算机视觉任务。从那时起,许多基于transformers的计算机视觉体系结构被提出。

本文将深入探讨注意力层在计算机视觉环境中的工作原理。我们将讨论单头注意力和多头注意力。它包括注意力层的代码,以及基础数学的概念解释。

NLP应用中,注意力通常被描述为句子中单词(标记)之间的关系。而在计算机视觉应用程序中,注意力关注图像中patches (标记)之间的关系。

有多种方法可以将图像分解为一系列标记。原始的ViT²将图像分割成小块,然后将小块平摊成标记。《token -to- token ViT》³开发了一种更复杂的从图像创建标记的方法。

点积注意力

《Attention is All You Need》中定义的点积(相当于乘法)注意力是目前我们最常见也是最简单的一种中注意力机制,他的代码实现非常简单:

  1. classAttention(nn.Module):
  2. def__init__(self,
  3. dim: int,
  4. chan: int,
  5. num_heads: int=1,
  6. qkv_bias: bool=False,
  7. qk_scale: NoneFloat=None):
  8. """ Attention Module
  9. Args:
  10. dim (int): input size of a single token
  11. chan (int): resulting size of a single token (channels)
  12. num_heads(int): number of attention heads in MSA
  13. qkv_bias (bool): determines if the qkv layer learns an addative bias
  14. qk_scale (NoneFloat): value to scale the queries and keys by;
  15. if None, queries and keys are scaled by ``head_dim ** -0.5``
  16. """
  17. super().__init__()
  18. ## Define Constants
  19. self.num_heads=num_heads
  20. self.chan=chan
  21. self.head_dim=self.chan//self.num_heads
  22. self.scale=qk_scaleorself.head_dim**-0.5
  23. assertself.chan%self.num_heads==0, '"Chan" must be evenly divisible by "num_heads".'
  24. ## Define Layers
  25. self.qkv=nn.Linear(dim, chan*3, bias=qkv_bias)
  26. #### Each token gets projected from starting length (dim) to channel length (chan) 3 times (for each Q, K, V)
  27. self.proj=nn.Linear(chan, chan)
  28. defforward(self, x):
  29. B, N, C=x.shape
  30. ## Dimensions: (batch, num_tokens, token_len)
  31. ## Calcuate QKVs
  32. qkv=self.qkv(x).reshape(B, N, 3, self.num_heads, self.head_dim).permute(2, 0, 3, 1, 4)
  33. #### Dimensions: (3, batch, heads, num_tokens, chan/num_heads = head_dim)
  34. q, k, v=qkv[0], qkv[1], qkv[2]
  35. ## Calculate Attention
  36. attn= (q*self.scale) @k.transpose(-2, -1)
  37. attn=attn.softmax(dim=-1)
  38. #### Dimensions: (batch, heads, num_tokens, num_tokens)
  39. ## Attention Layer
  40. x= (attn@v).transpose(1, 2).reshape(B, N, self.chan)
  41. #### Dimensions: (batch, heads, num_tokens, chan)
  42. ## Projection Layers
  43. x=self.proj(x)
  44. ## Skip Connection Layer
  45. v=v.transpose(1, 2).reshape(B, N, self.chan)
  46. x=v+x
  47. #### Because the original x has different size with current x, use v to do skip connection
  48. returnx

 单头注意力

对于单个注意力头,让我们逐步了解向前传递每一个patch,使用7 * 7=49作为起始patch大小(因为这是T2T-ViT模型中的起始标记大小)。通道数64这也是T2T-ViT的默认值。然后假设有100标记,并且使用批大小为13进行前向传播(选择这两个数值是为了不会与任何其他参数混淆)。

  1. # Define an Input
  2. token_len=7*7
  3. channels=64
  4. num_tokens=100
  5. batch=13
  6. x=torch.rand(batch, num_tokens, token_len)
  7. B, N, C=x.shape
  8. print('Input dimensions are\n\tbatchsize:', x.shape[0], '\n\tnumber of tokens:', x.shape[1], '\n\ttoken size:', x.shape[2])
  9. # Define the Module
  10. A=Attention(dim=token_len, chan=channels, num_heads=1, qkv_bias=False, qk_scale=None)
  11. A.eval();

 输入的维度是这样的:

Input dimensions are

  batchsize: 13

  number of tokens: 100

  token size: 49

 根据查询、键和值矩阵定义的。第一步是通过一个可学习的线性层来计算这些。qkv_bias项表示这些线性层是否有偏置项。这一步还将标记的长度从输入49更改为chan参数(64)。

  1. qkv=A.qkv(x).reshape(B, N, 3, A.num_heads, A.head_dim).permute(2, 0, 3, 1, 4)
  2. q, k, v=qkv[0], qkv[1], qkv[2]
  3. print('Dimensions for Queries are\n\tbatchsize:', q.shape[0], '\n\tattention heads:', q.shape[1], '\n\tnumber of tokens:', q.shape[2], '\n\tnew length of tokens:', q.shape[3])
  4. print('See that the dimensions for queries, keys, and values are all the same:')
  5. print('\tShape of Q:', q.shape, '\n\tShape of K:', k.shape, '\n\tShape of V:', v.shape)

 可以看到 查询、键和值的维度是相同的,13代表批次,1是我们的注意力头数,100是我们输入的标记长度(序列长度),64是我们的通道数。

Dimensions for Queries are

  batchsize: 13

  attention heads: 1

  number of tokens: 100

  new length of tokens: 64

See that the dimensions for queries, keys, and values are all the same:

  Shape of Q: torch.Size([13, 1, 100, 64])

  Shape of K: torch.Size([13, 1, 100, 64])

  Shape of V: torch.Size([13, 1, 100, 64])

 我们看看可注意力是如何计算的,它被定义为:

 

Q、K、V分别为查询、键和值;dₖ是键的维数,它等于键标记的长度,也等于键的长度。

第一步是计算:

 

然后是

最后

Q·K的矩阵乘法看起来是这样的

 这些就是我们注意力的主要部分,代码是这样的

  1. attn= (q*A.scale) @k.transpose(-2, -1)
  2. print('Dimensions for Attn are\n\tbatchsize:', attn.shape[0], '\n\tattention heads:', attn.shape[1], '\n\tnumber of tokens:', attn.shape[2], '\n\tnumber of tokens:', attn.shape[3])

Dimensions for Attn are

  batchsize: 13

  attention heads: 1

  number of tokens: 100

  number of tokens: 100

 

 下一步就是计算A的softmax,这不会改变它的形状。

attn=attn.softmax(dim=-1)

 最后,我们计算出A·V=x:

  1. x=attn@v
  2. print('Dimensions for x are\n\tbatchsize:', x.shape[0], '\n\tattention heads:', x.shape[1], '\n\tnumber of tokens:', x.shape[2], '\n\tlength of tokens:', x.shape[3])

 就得到了我们最终的结果

Dimensions for x are

  batchsize: 13

  attention heads: 1

  number of tokens: 100

  length of tokens: 64

 因为只有一个头,所以我们去掉头数 1

x = x.transpose(1, 2).reshape(B, N, A.chan)

 然后我们将x输入一个可学习的线性层,这个线性层不会改变它的形状。

x=A.proj(x)

最后我们实现的跳过连接

  1. orig_shape= (batch, num_tokens, token_len)
  2. curr_shape= (x.shape[0], x.shape[1], x.shape[2])
  3. v=v.transpose(1, 2).reshape(B, N, A.chan)
  4. v_shape= (v.shape[0], v.shape[1], v.shape[2])
  5. print('Original shape of input x:', orig_shape)
  6. print('Current shape of x:', curr_shape)
  7. print('Shape of V:', v_shape)
  8. x=v+x
  9. print('After skip connection, dimensions for x are\n\tbatchsize:', x.shape[0], '\n\tnumber of tokens:', x.shape[1], '\n\tlength of tokens:', x.shape[2])

Original shape of input x: (13, 100, 49)

Current shape of x: (13, 100, 64)

Shape of V: (13, 100, 64)

After skip connection, dimensions for x are

  batchsize: 13

  number of tokens: 100

  length of tokens: 64

 

 多头注意力

我们可以扩展到多头注意。在计算机视觉中,这通常被称为多头自注意力(MSA)。我们不会详细介绍所有步骤,而是关注矩阵形状不同的地方。

对于多头的注意力,注意力头的数量必须可以整除以通道的数量,所以在这个例子中,我们将使用4个注意头。

  1. # Define an Input
  2. token_len=7*7
  3. channels=64
  4. num_tokens=100
  5. batch=13
  6. num_heads=4
  7. x=torch.rand(batch, num_tokens, token_len)
  8. B, N, C=x.shape
  9. print('Input dimensions are\n\tbatchsize:', x.shape[0], '\n\tnumber of tokens:', x.shape[1], '\n\ttoken size:', x.shape[2])
  10. # Define the Module
  11. MSA=Attention(dim=token_len, chan=channels, num_heads=num_heads, qkv_bias=False, qk_scale=None)
  12. MSA.eval();

 

Input dimensions are

  batchsize: 13

  number of tokens: 100

  token size: 49

计算查询、键和值的过程与单头的过程相同。但是可以看到标记的新长度是chan/num_heads。Q、K和V矩阵的总大小没有改变;它们的内容只是分布在头部维度上。你可以把它看作是将单个矩阵分割为多个:

 我们将子矩阵表示为Qₕ对于查询头i。

  1. qkv=MSA.qkv(x).reshape(B, N, 3, MSA.num_heads, MSA.head_dim).permute(2, 0, 3, 1, 4)
  2. q, k, v=qkv[0], qkv[1], qkv[2]
  3. print('Head Dimension = chan / num_heads =', MSA.chan, '/', MSA.num_heads, '=', MSA.head_dim)
  4. print('Dimensions for Queries are\n\tbatchsize:', q.shape[0], '\n\tattention heads:', q.shape[1], '\n\tnumber of tokens:', q.shape[2], '\n\tnew length of tokens:', q.shape[3])
  5. print('See that the dimensions for queries, keys, and values are all the same:')
  6. print('\tShape of Q:', q.shape, '\n\tShape of K:', k.shape, '\n\tShape of V:', v.shape)

 

Head Dimension = chan / num_heads = 64 / 4 = 16

Dimensions for Queries are

  batchsize: 13

  attention heads: 4

  number of tokens: 100

  new length of tokens: 16

See that the dimensions for queries, keys, and values are all the same:

  Shape of Q: torch.Size([13, 4, 100, 16])

  Shape of K: torch.Size([13, 4, 100, 16])

  Shape of V: torch.Size([13, 4, 100, 16])

 这里需要注意的是

 我们需要除以头数。num_heads = 4个不同的Attn矩阵,看起来像:

  1. attn= (q*MSA.scale) @k.transpose(-2, -1)
  2. print('Dimensions for Attn are\n\tbatchsize:', attn.shape[0], '\n\tattention heads:', attn.shape[1], '\n\tnumber of tokens:', attn.shape[2], '\n\tnumber of tokens:', attn.shape[3]

 

Dimensions for Attn are

  batchsize: 13

  attention heads: 4

  number of tokens: 100

  number of tokens: 100

 softmax 不会改变维度,我们略过,然后计算每一个头

 这在多个注意头中是这样的:

  1. attn = attn.softmax(dim=-1)
  2. x = attn @ v
  3. print('Dimensions for x are\n\tbatchsize:', x.shape[0], '\n\tattention heads:', x.shape[1], '\n\tnumber of tokens:', x.shape[2], '\n\tlength of tokens:', x.shape[3]

Dimensions for x are

  batchsize: 13

  attention heads: 4

  number of tokens: 100

  length of tokens: 16

 

 最后需要维度重塑并把把所有的xₕ` s连接在一起。这是第一步的逆操作:

  1. x=x.transpose(1, 2).reshape(B, N, MSA.chan)
  2. print('Dimensions for x are\n\tbatchsize:', x.shape[0], '\n\tnumber of tokens:', x.shape[1], '\n\tlength of tokens:', x.shape[2])

 

Dimensions for x are

  batchsize: 13

  number of tokens: 100

  length of tokens: 64

 我们已经将所有头的输出连接在一起,注意力模块的其余部分保持不变。

  1. x = MSA.proj(x)
  2. print('Dimensions for x are\n\tbatchsize:', x.shape[0], '\n\tnumber of tokens:', x.shape[1], '\n\tlength of tokens:', x.shape[2])
  3. orig_shape = (batch, num_tokens, token_len)
  4. curr_shape = (x.shape[0], x.shape[1], x.shape[2])
  5. v = v.transpose(1, 2).reshape(B, N, A.chan
  6. v_shape = (v.shape[0], v.shape[1], v.shape[2])
  7. print('Original shape of input x:', orig_shape)
  8. print('Current shape of x:', curr_shape)
  9. print('Shape of V:', v_shape)
  10. x = v + x
  11. print('After skip connection, dimensions for x are\n\tbatchsize:', x.shape[0], '\n\tnumber of tokens:', x.shape[1], '\n\tlength of tokens:', x.shape[2])

 

Dimensions for x are

  batchsize: 13

  number of tokens: 100

  length of tokens: 64

Original shape of input x: (13, 100, 49)

Current shape of x: (13, 100, 64)

Shape of V: (13, 100, 64)

After skip connection, dimensions for x are

  batchsize: 13

  number of tokens: 100

  length of tokens: 64

 总结

在这篇文章中我们完成了ViT中注意力层。为了更详细的说明我们进行了手动的代码编写,如果要实际的应用,可以使用PyTorch中的torch.nn. multiheadeattention(),因为他的实现要快的多。

最后参考文章:

[1] Vaswani et al (2017). Attention Is All You Need.https://doi.org/10.48550/arXiv.1706.03762

[2] Dosovitskiy et al (2020). An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale.https://doi.org/10.48550/arXiv.2010.11929

[3] Yuan et al (2021). Tokens-to-Token ViT: Training Vision Transformers from Scratch on ImageNet. https://doi.org/10.48550/arXiv.2101.11986GitHub code: https://github.com/yitu-opensource/T2T-ViT

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/841088
推荐阅读
相关标签
  

闽ICP备14008679号