transformer的学习记录【完整代码+详细注释】(系列三)


第一节:transformer的架构介绍 + 输入部分的实现
链接: https://editor.csdn.net/md/?articleId=124648718


第二节 编码器部分实现(一)
链接: https://blog.csdn.net/weixin_42521185/article/details/124702949


第三节 编码器部分实现(二)
链接:https://blog.csdn.net/weixin_42521185/article/details/124724264


第四节 编码器部分实现(三)
链接:


1 前馈全连接层

  • 什么是前馈全连接层?
    在Transfomer中前馈全连接层就是,具有两层线性层的全连接网络。

  • 作用:
    考虑注意力机制,可能对复杂过程的拟合程度不够,通过增加两层网络来增强模型的拟合能力。

1.1 前馈全连接层的代码

  • 这里注意,利用上一层的输出作为输入,两层全连接层,作用是协助注意力机制拟合特征
  • 这里的输出保持和上一层一致!
class PositionwiseFeedForward(nn.Module):
    def __init__(self, d_model, d_ff, dropout=0.1):
        # d_model : 代表词嵌入的维度,同时也是两个线性层的输入维度和输出维度
        # d_ff : 代表第一个线性层的输出维度,和第二个线性层的输入维度
        # dropout : 经过Dropout层处理时,随机置零
        super(PositionwiseFeedForward, self).__init__()

        # 定义两层全连接的线性层
        self.w1 = nn.Linear(d_model, d_ff)
        self.w2 = nn.Linear(d_ff, d_model)
        self.dropout = nn.Dropout(p=dropout)

    def forward(self, x):
        # x: 来自上一层的输出
        # 首先将x送入第一个线性网络,然后relu  然后dropout
        # 然后送入第二个线性层
        return self.w2(self.dropout(F.relu((self.w1(x)))))

  • 设置传入的参数,这里注意,是要使用到前面多头注意力机制的输出的! mha_result
d_model = 512
d_ff = 64
dropout = 0.2

# 这个是上一层的输出,作为前馈连接的输入
x = mha_result
ff = PositionwiseFeedForward(d_model, d_ff, dropout)
ff_result = ff(x)
print(ff_result)
print(ff_result.shape)

1.2 包括前面学习内容的完整代码

import math
from torch.autograd import Variable
from torch import nn
import torch
from embedding_layer import Embedding
import copy

# 构建位置编码器的类
class PositionalEncoding(nn.Module):
    def __init__(self, d_model, dropout, max_len=5000):
        # d_model : 代表词嵌入的维度
        # dropout : 代表Dropout层的置零比率
        # max_len : 代表每个句子的最大长度
        super(PositionalEncoding, self).__init__()

        # 实例化 Dropout层
        self.dropout = nn.Dropout(p=dropout)

        # 初始化一个位置编码矩阵,大小是 max_len * d_model
        pe = torch.zeros(max_len, d_model)

        # 初始化一个绝对位置矩阵, max_len * 1
        position = torch.arange(0, max_len).unsqueeze(1)
        # print(position)

        # 定义一个变化矩阵,div_term, 跳跃式的初始化
        div_term = torch.exp(torch.arange(0, d_model, 2) * -(math.log(10000.0) / d_model))
        # print("ndiv_term", div_term)

        # 将前面定义的变化矩阵 进行技术,偶数分别赋值
        pe[:, 0::2] = torch.sin(position * div_term)  # 用正弦波给偶数部分赋值
        pe[:, 1::2] = torch.cos(position * div_term)  # 用余弦波给奇数部分赋值

        # 将二维张量,扩充为三维张量
        pe = pe.unsqueeze(0)  # 1 * max_len * d_model

        # 将位置编码矩阵,注册成模型的buffer,这个buffer不是模型中的参数,不跟随优化器同步更新
        # 注册成buffer后,就可以在模型保存后 重新加载的时候,将这个位置编码器和模型参数
        self.register_buffer('pe', pe)

    def forward(self, x):
        # x : 代表文本序列的词嵌入表示
        # 首先明确pe的编码太长了,将第二个维度,就是max_len对应的维度,缩小成x的句子的同等的长度
        x = x + Variable(self.pe[:, : x.size(1)], requires_grad=False)  # 表示位置编码是不参与更新的
        return self.dropout(x)


d_model = 512
dropout = 0.1
max_len = 60
vocab = 1000

x = Variable(torch.LongTensor([[100, 2, 421, 508], [491, 998, 1, 221]]))

emb = Embedding(vocab, d_model)
embr = emb(x)
x = embr  # shape: [2, 4, 512]
pe = PositionalEncoding(d_model, dropout, max_len)
pe_result = pe(x)
# print(pe_result)

import math
from torch.autograd import Variable
from torch import nn
import torch
import numpy as np
import matplotlib.pyplot as plt
import torch.nn.functional as F

def attention(query, key, value, mask=None, dropout=None):
    # query, key, value : 代表注意力的三个输入张量
    # mask : 掩码张量
    # dropout : 传入Dropout实例化对象
    # 首先,将query的最后一个维度提取出来,代表的是词嵌入的维度
    d_k = query.size(-1)

    # 按照注意力计算公式,将query和key 的转置进行矩阵乘法,然后除以缩放系数
    scores = torch.matmul(query, key.transpose(-2, -1)) / math.sqrt(d_k)

    # print("..", scores.shape)
    # 判断是否使用掩码张量
    if mask is not None:
        # 利用masked_fill 方法,将掩码张量和0进行位置的意义比较,如果等于0,就替换成 -1e9
        scores = scores.masked_fill(mask == 0, -1e9)

    # scores的最后一个维度上进行 softmax
    p_attn = F.softmax(scores, dim=-1)

    # 判断是否使用dropout
    if dropout is not None:
        p_attn = dropout(p_attn)

    # 最后一步完成p_attm 和 value 的乘法,并返回query的注意力表示
    return torch.matmul(p_attn, value), p_attn


query = key = value = pe_result
mask = Variable(torch.zeros(2, 4, 4))
attn, p_attn = attention(query, key, value, mask=mask)
# print('attn', attn)
# print('attn.shape', attn.shape)
# print("p_attn", p_attn)
# print(p_attn.shape)


# 实现克隆函数,因为在多头注意力机制下,要用到多个结果相同的线性层
# 需要使用clone 函数u,将他们统一 初始化到一个网络层列表对象中
def clones(module, N):
    # module : 代表要克隆的目标网络层
    # N : 将module几个
    return nn.ModuleList([copy.deepcopy(module) for _ in range(N)])


# 实现多头注意力机制的类
class MultiHeadAttention(nn.Module):
    def __init__(self, head, embedding_dim, dropout=0.1):
        # head : 代表几个头的函数
        # embedding_dim : 代表词嵌入的维度
        # dropout
        super(MultiHeadAttention, self).__init__()

        # 强调:多头的数量head 需要整除 词嵌入的维度 embedding_dim
        assert embedding_dim % head == 0

        # 得到每个头,所获得 的词向量的维度
        self.d_k = embedding_dim // head
        self.head = head
        self.embedding_dim = embedding_dim

        # 获得线性层,需要获得4个,分别是Q K V 以及最终输出的线性层
        self.linears = clones(nn.Linear(embedding_dim, embedding_dim), 4)

        # 初始化注意力张量
        self.attn = None

        # 初始化dropout对象
        self.drop = nn.Dropout(p=dropout)

    def forward(self, query, key, value, mask=None):
        # query,key,value 是注意力机制的三个输入张量,mask代表掩码张量
        # 首先判断是否使用掩码张量
        if mask is not None:
            # 使用squeeze将掩码张量进行围堵扩充,代表多头的第n个头
            mask = mask.unsqueeze(1)

        # 得到batch_size
        batch_size = query.size(0)

        # 首先使用 zip 将网络能和输入数据连接在一起,模型的输出 利用 view 和 transpose 进行维度和形状的
        query, key, value = \
            [model(x).view(batch_size, -1, self.head, self.d_k).transpose(1, 2)
             for model, x in zip(self.linears, (query, key, value))]

        # 将每个头的输出 传入到注意力层
        x, self.attn = attention(query, key, value, mask=mask, dropout=self.drop)

        # 得到每个头的计算结果,每个output都是4维的张量,需要进行维度转换
        # 前面已经将transpose(1, 2)
        # 注意,先transpose 然后 contiguous,否则无法使用view
        x = x.transpose(1, 2).contiguous().view(batch_size, -1, self.head*self.d_k)

        # 最后将x输入到线性层的最后一个线性层中进行处理,得到最终的多头注意力结构输出
        return self.linears[-1](x)

# 实例化若干个参数
head = 8
embedding_dim = 512
dropout = 0.2

# 若干输入参数的初始化
query = key = value = pe_result

mask = Variable(torch.zeros(2, 4, 4))

mha = MultiHeadAttention(head, embedding_dim, dropout)
mha_result = mha(query, key, value, mask)

# print(mha_result)
# print(mha_result.shape)


import math
from torch.autograd import Variable
from torch import nn
import torch
import numpy as np
import matplotlib.pyplot as plt
import torch.nn.functional as F


class PositionwiseFeedForward(nn.Module):
    def __init__(self, d_model, d_ff, dropout=0.1):
        # d_model : 代表词嵌入的维度,同时也是两个线性层的输入维度和输出维度
        # d_ff : 代表第一个线性层的输出维度,和第二个线性层的输入维度
        # dropout : 经过Dropout层处理时,随机置零
        super(PositionwiseFeedForward, self).__init__()

        # 定义两层全连接的线性层
        self.w1 = nn.Linear(d_model, d_ff)
        self.w2 = nn.Linear(d_ff, d_model)
        self.dropout = nn.Dropout(p=dropout)

    def forward(self, x):
        # x: 来自上一层的输出
        # 首先将x送入第一个线性网络,然后relu  然后dropout
        # 然后送入第二个线性层
        return self.w2(self.dropout(F.relu((self.w1(x)))))


d_model = 512
d_ff = 64
dropout = 0.2

# 这个是上一层的输出,作为前馈连接的输入
x = mha_result
ff = PositionwiseFeedForward(d_model, d_ff, dropout)
ff_result = ff(x)
print(ff_result)
print(ff_result.shape)

2 规范化层

2.1 规范化层的作用

  1. 可以当成是 神经网络的标配
  2. 因为随着网络层数增加,通过多层的计算后,参数可能出现或大或小的情况,这种情况是不利于神经网络收敛计算的。
  3. 经过了规范化,可以提高神经网络的计算速度

2.2 规范化层的讲解

  1. eps = 1e-6 : 是一个非常小的数,一般出现在分母,防止除零错误
  2. 两个辅助张量,ones 和zeros
  3. nn.Parameter 定义的参数,也是会随着模型一起训练的

2.3 实现规范化层的代码

  • 代码:
# 构架规范化层的类
class LayerNorm(nn.Module):
    def __init__(self, features, eps=1e-6):
        # features : 代表词嵌入的维度
        # eps :一个很小的数,防止在规范化公式 除以0
        super(LayerNorm, self).__init__()
        # 初始化两个参数张量 a2 b 2 用于对结果作规范化 操作计算
        # 用nn.Parameter  封装,代表他们也是模型中的参数,也要随着模型计算而计算
        self.a2 = nn.Parameter(torch.ones(features))
        self.b2 = nn.Parameter(torch.zeros(features))
        self.eps = eps  # 传入到模型中去

    def forward(self, x):
        # x : 是上一层网络的输出 (两层的前馈全连接层)
        # 首先对x进行 最后一个维度上的求均值操作,同时要求保持输出维度和输入维度一致
        mean = x.mean(-1, keepdim=True)
        # 接着对x最后一个维度上求标准差的操作,同时要求保持输出维度和输入维度一制
        std = x.std(-1, keepdim=True)
        # 按照规范化公式进行计算并返回
        return self.a2 * (x-mean) / (std + self.eps) + self.b2

2.4 完整代码

  • 可以直接运行!
import math
from torch.autograd import Variable
from torch import nn
import torch
import copy
import numpy as np
import matplotlib.pyplot as plt
import torch.nn.functional as F


# main作用:集成了整个Transformer代码
########################################################################################################################

########################################################################################################################
# 构建 Embedding 类来实现文本嵌入层
# vocab : 词表的长度, d_model : 词嵌入的维度
class Embedding(nn.Module):
    def __init__(self, vocab, d_model):
        super(Embedding, self).__init__()
        self.lut = nn.Embedding(vocab, d_model)
        self.d_model = d_model

    def forward(self, x):
        return self.lut(x) * math.sqrt(self.d_model)


# 词表: 1000*512, 共是1000个词,每一行是一个词,每个词是一个512d的向量表示
vocab = 1000
d_model = 512

x = Variable(torch.LongTensor([[100, 2, 421, 508], [491, 998, 1, 221]]))

emb = Embedding(vocab, d_model)
embr = emb(x)

########################################################################################################################
# 构建位置编码器的类
# d_model : 代表词嵌入的维度
# dropout : 代表Dropout层的置零比率
# max_len : 代表每个句子的最大长度
# 初始化一个位置编码矩阵pe,大小是 max_len * d_model
# 初始化一个绝对位置矩阵position, 大小是max_len * 1
class PositionalEncoding(nn.Module):
    def __init__(self, d_model, dropout, max_len=5000):
        super(PositionalEncoding, self).__init__()
        self.dropout = nn.Dropout(p=dropout)
        pe = torch.zeros(max_len, d_model)
        position = torch.arange(0, max_len).unsqueeze(1)
        # 定义一个变化矩阵,div_term, 跳跃式的初始化
        div_term = torch.exp(torch.arange(0, d_model, 2) * -(math.log(10000.0) / d_model))

        # 将前面定义的变化矩阵 进行技术,偶数分别赋值
        pe[:, 0::2] = torch.sin(position * div_term)  # 用正弦波给偶数部分赋值
        pe[:, 1::2] = torch.cos(position * div_term)  # 用余弦波给奇数部分赋值

        # 将二维张量,扩充为三维张量
        pe = pe.unsqueeze(0)  # 1 * max_len * d_model

        # 将位置编码矩阵,注册成模型的buffer,这个buffer不是模型中的参数,不跟随优化器同步更新
        # 注册成buffer后,就可以在模型保存后 重新加载的时候,将这个位置编码器和模型参数
        self.register_buffer('pe', pe)

    def forward(self, x):
        # x : 代表文本序列的词嵌入表示
        # 首先明确pe的编码太长了,将第二个维度,就是max_len对应的维度,缩小成x的句子的同等的长度
        x = x + Variable(self.pe[:, : x.size(1)], requires_grad=False)  # 表示位置编码是不参与更新的
        return self.dropout(x)


d_model = 512
dropout = 0.1
max_len = 60
vocab = 1000

x = Variable(torch.LongTensor([[100, 2, 421, 508], [491, 998, 1, 221]]))

emb = Embedding(vocab, d_model)
embr = emb(x)
x = embr  # shape: [2, 4, 512]
pe = PositionalEncoding(d_model, dropout, max_len)
pe_result = pe(x)
# print(pe_result)



def attention(query, key, value, mask=None, dropout=None):
    # query, key, value : 代表注意力的三个输入张量
    # mask : 掩码张量
    # dropout : 传入Dropout实例化对象
    # 首先,将query的最后一个维度提取出来,代表的是词嵌入的维度
    d_k = query.size(-1)

    # 按照注意力计算公式,将query和key 的转置进行矩阵乘法,然后除以缩放系数
    scores = torch.matmul(query, key.transpose(-2, -1)) / math.sqrt(d_k)

    # print("..", scores.shape)
    # 判断是否使用掩码张量
    if mask is not None:
        # 利用masked_fill 方法,将掩码张量和0进行位置的意义比较,如果等于0,就替换成 -1e9
        scores = scores.masked_fill(mask == 0, -1e9)

    # scores的最后一个维度上进行 softmax
    p_attn = F.softmax(scores, dim=-1)

    # 判断是否使用dropout
    if dropout is not None:
        p_attn = dropout(p_attn)

    # 最后一步完成p_attm 和 value 的乘法,并返回query的注意力表示
    return torch.matmul(p_attn, value), p_attn


query = key = value = pe_result
mask = Variable(torch.zeros(2, 4, 4))
attn, p_attn = attention(query, key, value, mask=mask)
# print('attn', attn)
# print('attn.shape', attn.shape)
# print("p_attn", p_attn)
# print(p_attn.shape)


# 实现克隆函数,因为在多头注意力机制下,要用到多个结果相同的线性层
# 需要使用clone 函数u,将他们统一 初始化到一个网络层列表对象中
def clones(module, N):
    # module : 代表要克隆的目标网络层
    # N : 将module几个
    return nn.ModuleList([copy.deepcopy(module) for _ in range(N)])


# 实现多头注意力机制的类
class MultiHeadAttention(nn.Module):
    def __init__(self, head, embedding_dim, dropout=0.1):
        # head : 代表几个头的函数
        # embedding_dim : 代表词嵌入的维度
        # dropout
        super(MultiHeadAttention, self).__init__()

        # 强调:多头的数量head 需要整除 词嵌入的维度 embedding_dim
        assert embedding_dim % head == 0

        # 得到每个头,所获得 的词向量的维度
        self.d_k = embedding_dim // head
        self.head = head
        self.embedding_dim = embedding_dim

        # 获得线性层,需要获得4个,分别是Q K V 以及最终输出的线性层
        self.linears = clones(nn.Linear(embedding_dim, embedding_dim), 4)

        # 初始化注意力张量
        self.attn = None

        # 初始化dropout对象
        self.drop = nn.Dropout(p=dropout)

    def forward(self, query, key, value, mask=None):
        # query,key,value 是注意力机制的三个输入张量,mask代表掩码张量
        # 首先判断是否使用掩码张量
        if mask is not None:
            # 使用squeeze将掩码张量进行围堵扩充,代表多头的第n个头
            mask = mask.unsqueeze(1)

        # 得到batch_size
        batch_size = query.size(0)

        # 首先使用 zip 将网络能和输入数据连接在一起,模型的输出 利用 view 和 transpose 进行维度和形状的
        query, key, value = \
            [model(x).view(batch_size, -1, self.head, self.d_k).transpose(1, 2)
             for model, x in zip(self.linears, (query, key, value))]

        # 将每个头的输出 传入到注意力层
        x, self.attn = attention(query, key, value, mask=mask, dropout=self.drop)

        # 得到每个头的计算结果,每个output都是4维的张量,需要进行维度转换
        # 前面已经将transpose(1, 2)
        # 注意,先transpose 然后 contiguous,否则无法使用view
        x = x.transpose(1, 2).contiguous().view(batch_size, -1, self.head*self.d_k)

        # 最后将x输入到线性层的最后一个线性层中进行处理,得到最终的多头注意力结构输出
        return self.linears[-1](x)

# 实例化若干个参数
head = 8
embedding_dim = 512
dropout = 0.2

# 若干输入参数的初始化
query = key = value = pe_result

mask = Variable(torch.zeros(2, 4, 4))

mha = MultiHeadAttention(head, embedding_dim, dropout)
mha_result = mha(query, key, value, mask)

# print(mha_result)
# print(mha_result.shape)


import math
from torch.autograd import Variable
from torch import nn
import torch
import numpy as np
import matplotlib.pyplot as plt
import torch.nn.functional as F


class PositionwiseFeedForward(nn.Module):
    def __init__(self, d_model, d_ff, dropout=0.1):
        # d_model : 代表词嵌入的维度,同时也是两个线性层的输入维度和输出维度
        # d_ff : 代表第一个线性层的输出维度,和第二个线性层的输入维度
        # dropout : 经过Dropout层处理时,随机置零
        super(PositionwiseFeedForward, self).__init__()

        # 定义两层全连接的线性层
        self.w1 = nn.Linear(d_model, d_ff)
        self.w2 = nn.Linear(d_ff, d_model)
        self.dropout = nn.Dropout(p=dropout)

    def forward(self, x):
        # x: 来自上一层的输出
        # 首先将x送入第一个线性网络,然后relu  然后dropout
        # 然后送入第二个线性层
        return self.w2(self.dropout(F.relu((self.w1(x)))))


d_model = 512
d_ff = 64
dropout = 0.2

# 这个是上一层的输出,作为前馈连接的输入
x = mha_result
ff = PositionwiseFeedForward(d_model, d_ff, dropout)
ff_result = ff(x)
# print(ff_result)
# print(ff_result.shape)


# 构架规范化层的类
class LayerNorm(nn.Module):
    def __init__(self, features, eps=1e-6):
        # features : 代表词嵌入的维度
        # eps :一个很小的数,防止在规范化公式 除以0
        super(LayerNorm, self).__init__()
        # 初始化两个参数张量 a2 b 2 用于对结果作规范化 操作计算
        # 用nn.Parameter  封装,代表他们也是模型中的参数,也要随着模型计算而计算
        self.a2 = nn.Parameter(torch.ones(features))
        self.b2 = nn.Parameter(torch.zeros(features))
        self.eps = eps  # 传入到模型中去

    def forward(self, x):
        # x : 是上一层网络的输出 (两层的前馈全连接层)
        # 首先对x进行 最后一个维度上的求均值操作,同时要求保持输出维度和输入维度一致
        mean = x.mean(-1, keepdim=True)
        # 接着对x最后一个维度上求标准差的操作,同时要求保持输出维度和输入维度一制
        std = x.std(-1, keepdim=True)
        # 按照规范化公式进行计算并返回
        return self.a2 * (x-mean) / (std + self.eps) + self.b2


features = d_model = 512
eps = 1e-6

x = ff_result
ln = LayerNorm(features, eps)
ln_result = ln(x)
print(ln_result)
print(ln_result.shape)


  • 输出结果:
tensor([[[ 7.5314e-01,  1.1069e+00,  1.6788e+00,  ...,  8.2092e-01,
           2.1026e+00, -1.6079e+00],
         [ 2.0356e+00,  2.2696e+00,  3.9820e-02,  ..., -3.3840e-03,
           4.2480e-01, -1.2518e+00],
         [ 1.7311e+00,  1.9941e+00,  4.4274e-01,  ..., -2.4831e-01,
           1.2970e+00, -1.5437e+00],
         [ 7.6818e-01,  1.3167e+00, -1.0743e-01,  ..., -4.9033e-01,
           8.1483e-01, -1.3569e-01]],

        [[ 5.7923e-01,  2.5550e-01, -1.4376e-01,  ..., -6.6552e-01,
          -3.5072e-01,  1.2394e-03],
         [ 4.9446e-01,  3.5806e-01,  3.7898e-01,  ..., -8.0877e-01,
          -1.2241e+00, -4.8933e-01],
         [ 1.1646e+00,  2.8150e-01,  1.3062e+00,  ..., -8.8959e-01,
          -1.5397e+00, -1.2664e-02],
         [ 4.1892e-01,  2.0376e-01,  3.4781e-01,  ..., -1.6625e+00,
          -2.5020e+00, -2.1855e-01]]], grad_fn=<AddBackward0>)
torch.Size([2, 4, 512])

  • 输出结果分析: 规范化层就是使得数据更加合理,在合理的范围内。
  • 所以说,输出结果是 [2, 4, 512]

猜你喜欢

转载自blog.csdn.net/weixin_42521185/article/details/124724264