注意力

torchhd.memory.attention(query: Tensor, keys: Tensor, values: Tensor, beta: float | None = None) Tensor[来源]

注意力机制

Parameters:
  • query (Tensor) – 用于与键比较相似性的查询向量。

  • keys (Tensor) – 用于与查询进行比较的键向量。

  • values (Tensor) – 包含可从内存中检索的值向量的值向量。

  • beta (float, optional) – 在softmax之前的注意力权重的温度标量。默认值:1/sqrt(d)

Shapes:
  • 查询: \((*, f)\)

  • 键值: \((n, f)\)

  • 值:\((n, g)\)

  • 结果: \((*, g)\)

Examples::
>>> items = torchhd.random(6, 512)
>>> read = torchhd.memory.attention(items, items, items).sign()
>>> torchhd.cosine_similarity(read, items)
tensor([[ 1.0000,  0.0625,  0.0117, -0.0625, -0.0078, -0.0430],
        [ 0.0625,  1.0000, -0.0195,  0.0703,  0.0469,  0.0508],
        [ 0.0117, -0.0195,  1.0000,  0.0820,  0.0195,  0.0156],
        [-0.0625,  0.0703,  0.0820,  1.0000, -0.0547, -0.0195],
        [-0.0078,  0.0469,  0.0195, -0.0547,  1.0000, -0.0898],
        [-0.0430,  0.0508,  0.0156, -0.0195, -0.0898,  1.0000]])