自己注意と交差注意

Self-Attention が線形変換によって QKV を計算する理由、その背後にある原理または直感的な説明は何ですか? - Zhihu が主な質問に答える主な質問: 注意のマッピングの後、モデリングの類似性は無意味ですか? 個人的にはこれを感じます... https://www.zhihu.com/question/592626839/answer/2965200007トランスフォーマー アーキテクチャのクロスアテンションモダリティに関係なく 2 つの埋め込みシーケンスをマージします。たとえば、Stable Diffusion U-Net のテキスト付きの画像です。 :/ /vaclavkosar.com/ml/cross-attention-in-transformer-architecture一言で言えば、クロスアテンションはシーケンス間の異なる位置を入力して注意を行うことであり、セルフアテンションはシーケンス内で注意を行うことです。自己注意と相互注意の違いは、q と kv のソースだけです. 自己注意 Q(uery)K(ey)V(alue) はすべてシーケンスから来ますが、相互注意の Q は別のシーケンスから来ます.多くのクロスモーダル シーケンスです。

交差注意は、2 つの異なるシーケンスを混合します。

class CrossAttention(nn.Module):
    r"""
    A cross attention layer.

    Parameters:
        query_dim (`int`): The number of channels in the query.
        cross_attention_dim (`int`, *optional*):
            The number of channels in the encoder_hidden_states. If not given, defaults to `query_dim`.
        heads (`int`,  *optional*, defaults to 8): The number of heads to use for multi-head attention.
        dim_head (`int`,  *optional*, defaults to 64): The number of channels in each head.
        dropout (`float`, *optional*, defaults to 0.0): The dropout probability to use.
        bias (`bool`, *optional*, defaults to False):
            Set to `True` for the query, key, and value linear layers to contain a bias parameter.
    """
    query = attn.to_q(hidden_states)
    query = attn.head_to_batch_dim(query)

    encoder_hidden_states = encoder_hidden_states if encoder_hidden_states is not None else hidden_states
    key = attn.to_k(encoder_hidden_states)
    value = attn.to_v(encoder_hidden_states)
    key = attn.head_to_batch_dim(key)
    value = attn.head_to_batch_dim(value)

    attention_probs = attn.get_attention_scores(query, key, attention_mask)
    hidden_states = torch.bmm(attention_probs, value)

応用:

トランスフォーマーに記載されていますが、同じモードでクロスアテンションとは呼べません

安定拡散中:

 知覚者io:

 

おすすめ

転載: blog.csdn.net/u012193416/article/details/130261040