• 李沐67_自注意力——自学笔记


    !pip install --upgrade d2l
    
    • 1
    import math
    import torch
    from torch import nn
    from d2l import torch as d2l
    
    • 1
    • 2
    • 3
    • 4

    自注意力

    代码片段是基于多头注意力对一个张量完成自注意力的计算

    num_hiddens, num_heads = 100, 5
    attention = d2l.MultiHeadAttention(num_hiddens, num_heads, 0.5) ## 此处修改,只剩下一个num_hiddens
    attention.eval()
    
    • 1
    • 2
    • 3
    /usr/local/lib/python3.10/dist-packages/torch/nn/modules/lazy.py:181: UserWarning: Lazy modules are a new feature under heavy development so changes to the API or functionality can happen at any moment.
      warnings.warn('Lazy modules are a new feature under heavy development '
    
    
    
    
    
    MultiHeadAttention(
      (attention): DotProductAttention(
        (dropout): Dropout(p=0.5, inplace=False)
      )
      (W_q): LazyLinear(in_features=0, out_features=100, bias=False)
      (W_k): LazyLinear(in_features=0, out_features=100, bias=False)
      (W_v): LazyLinear(in_features=0, out_features=100, bias=False)
      (W_o): LazyLinear(in_features=0, out_features=100, bias=False)
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    batch_size, num_queries, valid_lens = 2, 4, torch.tensor([3, 2])
    X = torch.ones((batch_size, num_queries, num_hiddens))
    attention(X, X, X, valid_lens).shape
    
    • 1
    • 2
    • 3
    torch.Size([2, 4, 100])
    
    • 1

    位置编码

    PositionalEncoding, 为了使用序列的顺序信息,通过在输入表示中添加 位置编码(positional encoding)来注入绝对的或相对的位置信息。 位置编码可以通过学习得到也可以直接固定得到。

    
    class PositionalEncoding(nn.Module):
        """位置编码"""
        def __init__(self, num_hiddens, dropout, max_len=1000):
            super(PositionalEncoding, self).__init__()
            self.dropout = nn.Dropout(dropout)
            # 创建一个足够长的P
            self.P = torch.zeros((1, max_len, num_hiddens))
            X = torch.arange(max_len, dtype=torch.float32).reshape(
                -1, 1) / torch.pow(10000, torch.arange(
                0, num_hiddens, 2, dtype=torch.float32) / num_hiddens)
            self.P[:, :, 0::2] = torch.sin(X)
            self.P[:, :, 1::2] = torch.cos(X)
    
        def forward(self, X):
            X = X + self.P[:, :X.shape[1], :].to(X.device)
            return self.dropout(X)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    在位置嵌入矩阵
    中, 行代表词元在序列中的位置,列代表位置编码的不同维度。 从下面的例子中可以看到位置嵌入矩阵的第6列和第7列的频率高于第8列和第9列。 第6列和第7列之间的偏移量(第8列和第9列相同)是由于正弦函数和余弦函数的交替。

    encoding_dim, num_steps = 32, 60
    pos_encoding = PositionalEncoding(encoding_dim, 0)
    pos_encoding.eval()
    X = pos_encoding(torch.zeros((1, num_steps, encoding_dim)))
    P = pos_encoding.P[:, :X.shape[1], :]
    d2l.plot(torch.arange(num_steps), P[0, :, 6:10].T, xlabel='Row (position)',
             figsize=(6, 2.5), legend=["Col %d" % d for d in torch.arange(6, 10)])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    绝对位置信息

    for i in range(8):
        print(f'{i}的二进制是:{i:>03b}')
    
    • 1
    • 2
    0的二进制是:000
    1的二进制是:001
    2的二进制是:010
    3的二进制是:011
    4的二进制是:100
    5的二进制是:101
    6的二进制是:110
    7的二进制是:111
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    二进制表示中,较高比特位的交替频率低于较低比特位, 与下面的热图所示相似,只是位置编码通过使用三角函数在编码维度上降低频率。 由于输出是浮点数,因此此类连续表示比二进制表示法更节省空间。

    P = P[0, :, :].unsqueeze(0).unsqueeze(0)
    d2l.show_heatmaps(P, xlabel='Column (encoding dimension)',
                      ylabel='Row (position)', figsize=(3.5, 4), cmap='Blues')
    
    • 1
    • 2
    • 3

    在这里插入图片描述

  • 相关阅读:
    RNN在图像压缩领域的应用-Variable rate image compression with recurrent neural networks
    C语言系统化精讲(五):C语言格式化输入和运算符与表达式
    zookeeper基础学习之六: zookeeper java客户端curator
    mysql 的 localhost 连接与 IP 地址连接有什么区别
    MindFusion Crack版,以围绕根的同心圆排列层
    SQL关于日期的计算合集
    字符串函数和内存函数(strlen,strcpy ,strcat ,strcmp,strstr,memcpy,memmove,memcmp,memset)
    Java单例模式的几种写法及其优缺点,用Enum枚举实现被认为是最好的方式?
    迪克森电荷泵
    Nginx模块开发之http handler实现流量统计(入门篇)
  • 原文地址:https://blog.csdn.net/Rrrrrr900/article/details/138195737