gpt4 book ai didi

python - MultiHeadAttention 中的 att_mask 和 key_padding_mask 有什么区别

转载 作者:行者123 更新时间:2023-12-03 17:06:09 36 4
gpt4 key购买 nike

att_mask有什么区别和 key_padding_maskMultiHeadAttnetion pytorch:

key_padding_mask – if provided, specified padding elements in the key will be ignored by the attention. When given a binary mask and a value is True, the corresponding value on the attention layer will be ignored. When given a byte mask and a value is non-zero, the corresponding value on the attention layer will be ignored


attn_mask – 2D or 3D mask that prevents attention to certain positions. A 2D mask will be broadcasted for all the batches while a 3D mask allows to specify a different mask for the entries of each batch.


提前致谢。

最佳答案

key_padding_mask用于屏蔽填充的位置,即在输入序列结束之后。这始终特定于输入批次,并取决于批次中的序列与最长的序列相比有多长。它是形状批量大小 × 输入长度的二维张量。
另一方面,attn_mask说明哪些键值对是有效的。在 Transformer 解码器中,三角形掩码用于模拟推理时间并防止关注“ future ”位置。这是什么att_mask通常用于。如果是二维张量,则形状为输入长度×输入长度。您还可以拥有一个特定于批次中每个项目的掩码。在这种情况下,您可以使用形状为(批量大小 × 头数)× 输入长度 × 输入长度的 3D 张量。 (因此,理论上,您可以使用 3D key_padding_mask 模拟 att_mask 。)

关于python - MultiHeadAttention 中的 att_mask 和 key_padding_mask 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62629644/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com