【问题标题】:AttentionQKV from Trax来自 Trax 的 AttentionQKV
【发布时间】:2020-09-30 00:36:05
【问题描述】:

Trax实现的AttentionQKV层如下:AttentionQKV

def AttentionQKV(d_feature, n_heads=1, dropout=0.0, mode='train'):
  """Returns a layer that maps (q, k, v, mask) to (activations, mask).
  See `Attention` above for further context/details.
  Args:
    d_feature: Depth/dimensionality of feature embedding.
    n_heads: Number of attention heads.
    dropout: Probababilistic rate for internal dropout applied to attention
        activations (based on query-key pairs) before dotting them with values.
    mode: One of `'train'`, `'eval'`, or `'predict'`.
  """
  return cb.Serial(
      cb.Parallel(
          core.Dense(d_feature),
          core.Dense(d_feature),
          core.Dense(d_feature),
      ),
      PureAttention(  # pylint: disable=no-value-for-parameter
          n_heads=n_heads, dropout=dropout, mode=mode),
      core.Dense(d_feature),
  )

特别是三个平行的密集层的目的是什么?该层的输入是 q, k, v, mask。为什么q、k、v要经过一个dense layer?

【问题讨论】:

    标签: attention-model trax


    【解决方案1】:

    这段代码 sn-p 是 Attention is all you need 论文第 5 页顶部的方程的实现,该论文在 2017 年介绍了 Transformer 模型。计算如图 2 所示:

    隐藏状态被投影到 h 注意力头中,这些注意力头并行地进行缩放的点积注意力。投影可以解释为提取与头部相关的信息。然后每个头部根据不同的(学习的)标准进行概率检索。

    【讨论】:

    • 那么上图中的Q、K、V其实就是隐藏状态?不知何故,我认为 Q、K、V 是隐藏状态乘以查询、键和值矩阵的结果。
    • 是的,在 self-attention 中它们三个是一样的。在encoder-decoder attention中,Q来自decoder,V和K是encoder states。
    猜你喜欢
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    • 2023-02-12
    • 1970-01-01
    • 2022-07-28
    • 2020-12-08
    • 2023-02-08
    • 1970-01-01
    相关资源
    最近更新 更多