• 预训练word2vec


    预训练word2vec

    我们将实现跳元语法模型,然后,我们将在PTB数据集上使用负采样预训练word2vec。首先,让我们通过调用d2l.load_data_ptb函数来获得该数据集的数据迭代器和词表

    import math
    import torch
    from torch import nn
    from d2l import torch as d2l
    
    batch_size, max_window_size, num_noise_words = 512, 5, 5
    data_iter, vocab = d2l.load_data_ptb(batch_size, max_window_size,num_noise_words)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    1 - 跳元模型

    我们通过嵌入层和批量矩阵乘法实现了跳元模型。首先,我们回顾一下嵌入层是如何工作的

    嵌入层

    嵌入层将词元的索引映射到其特征向量。该层的权重是一个矩阵,其行数等于字典大小(input_dim),列数等于每个标记的向量维数(output_dim)。词嵌入模型训练之后,这个权重就是我们所需要的

    embed = nn.Embedding(num_embeddings=20, embedding_dim=4)
    print(f'Parameter embedding_weight ({embed.weight.shape}, '
        f'dtype={embed.weight.dtype})')
    
    • 1
    • 2
    • 3
    Parameter embedding_weight (torch.Size([20, 4]), dtype=torch.float32)
    
    • 1

    嵌入层的输入是词元(词)的索引。对于任何词元索引i,其向量表示可以从嵌入层中的权重矩阵的第i行获得。由于向量维度(output_dim)被设置为4,因此当小批量词元索引的形状为(2,3)时,嵌入层返回具有形状(2,3,4)的向量

    x = torch.tensor([[1, 2, 3], [4, 5, 6]])
    embed(x)
    
    • 1
    • 2
    tensor([[[ 2.6863, -0.1858,  1.2145,  0.9756],
             [ 0.9973,  0.1903, -0.4698, -0.1412],
             [ 1.6520, -0.8778, -0.9541,  0.4742]],
    
            [[-0.0514,  1.0495, -0.3649, -0.1095],
             [ 1.1095, -0.6687, -0.4921, -1.0560],
             [-1.1306,  0.3363, -0.2100, -1.4195]]], grad_fn=)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    定义前向传播

    在前向传播中,跳元语法模型的输入包括形状为(批量大小,1)的中心词索引cenetr和形状为(批量大小,max_len)的上下文与噪声词索引contexts_and_negatives。这两个变量首先通过嵌入层从词元索引转换成向量,然后它们的批量矩阵相乘返回形状为(批量大小,1,max_len)的输出

    输出中的每个元素时中心词向量和上下文或噪声词向量的点积

    def skip_gram(center, contexts_and_negatives, embed_v, embed_u):
        v = embed_v(center)
        u = embed_u(contexts_and_negatives)
        pred = torch.bmm(v, u.permute(0, 2, 1))
        return pred
    
    • 1
    • 2
    • 3
    • 4
    • 5

    让我们为一些样例输入打印此skip_gram函数的输出形状

    skip_gram(torch.ones((2, 1), dtype=torch.long),
            torch.ones((2, 4), dtype=torch.long), embed, embed).shape
    
    • 1
    • 2
    torch.Size([2, 1, 4])
    
    • 1

    2 - 训练

    在训练带负采样的跳元模型之前,我们先定义它的损失函数

    二元交叉熵损失

    class SigmoidBCELoss(nn.Module):
        # 带掩码的⼆元交叉熵损失
        def __init__(self):
            super().__init__()
            
        def forward(self, inputs, target, mask=None):
            out = nn.functional.binary_cross_entropy_with_logits(
                inputs, target, weight=mask, reduction="none")
            return out.mean(dim=1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    loss = SigmoidBCELoss()
    
    • 1

    下面计算给定变量的二进制交叉熵损失

    pred = torch.tensor([[1.1, -2.2, 3.3, -4.4]] * 2)
    label = torch.tensor([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0]])
    mask = torch.tensor([[1, 1, 1, 1], [1, 1, 0, 0]])
    loss(pred, label, mask) * mask.shape[1] / mask.sum(axis=1)
    
    • 1
    • 2
    • 3
    • 4
    tensor([0.9352, 1.8462])
    
    • 1

    下面显示了如何使用二元交叉熵损失中的Sigmoid激活函数(以较低效率的方式)计算上述结果。我们可以将这两个输出视为两个规范化的损失,在非掩码预测上进行平均

    def sigmd(x):
        return -math.log(1 / (1 + math.exp(-x)))
    
    print(f'{(sigmd(1.1) + sigmd(2.2) + sigmd(-3.3) + sigmd(4.4)) / 4:.4f}')
    print(f'{(sigmd(-1.1) + sigmd(-2.2)) / 2:.4f}')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    0.9352
    1.8462
    
    • 1
    • 2

    初始化模型参数

    我们定义了两个嵌入层,将词表中的所有单词分别作为中心词和上下文词使用。子向量维度embed_size被设置为100

    embed_size = 100
    net = nn.Sequential(nn.Embedding(num_embeddings=len(vocab),
                        embedding_dim=embed_size),
                        nn.Embedding(num_embeddings=len(vocab),
                        embedding_dim=embed_size))
    
    • 1
    • 2
    • 3
    • 4
    • 5

    定义训练阶段代码

    训练阶段代码实现定义如下,由于填充存在,损失函数的计算与以前的训练函数略有不同

    def train(net, data_iter, lr, num_epochs, device=d2l.try_gpu()):
        def init_weights(m):
            if type(m) == nn.Embedding:
                nn.init.xavier_uniform_(m.weight)
        
        net.apply(init_weights)
        net = net.to(device)
        optimizer = torch.optim.Adam(net.parameters(), lr=lr)
        animator = d2l.Animator(xlabel='epoch', ylabel='loss',
                                xlim=[1, num_epochs])
        # 规范化的损失之和,规范化的损失数
        metric = d2l.Accumulator(2)
        for epoch in range(num_epochs):
            timer, num_batches = d2l.Timer(), len(data_iter)
            for i, batch in enumerate(data_iter):
                optimizer.zero_grad()
                center, context_negative, mask, label = [data.to(device) for data in batch]
                pred = skip_gram(center, context_negative, net[0], net[1])
                l = (loss(pred.reshape(label.shape).float(), label.float(), mask) / mask.sum(axis=1) * mask.shape[1])
                l.sum().backward()
                optimizer.step()
                metric.add(l.sum(), l.numel())
                if (i + 1) % (num_batches // 5) == 0 or i == num_batches - 1:
                    animator.add(epoch + (i + 1) / num_batches,(metric[0] / metric[1],))
        print(f'loss {metric[0] / metric[1]:.3f}, '
            f'{metric[1] / timer.stop():.1f} tokens/sec on {str(device)}')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    我们现在可以使用负采样来训练跳元模型

    lr, num_epochs = 0.002, 5
    train(net, data_iter, lr, num_epochs)
    
    • 1
    • 2
    loss 0.410, 160366.1 tokens/sec on cuda:0
    
    • 1

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7hQbWUND-1662561501425)(https://yingziimage.oss-cn-beijing.aliyuncs.com/img/202209072237458.svg)]

    3 - 应用词嵌入

    在训练word2vec模型之后,我们可以使⽤训练好模型中词向量的余弦相似度来从词表中找到与输⼊单词语义最相似的单词

    def get_similar_tokens(query_token, k, embed):
        W = embed.weight.data
        x = W[vocab[query_token]]
        # 计算余弦相似性。增加1e-9以获得数值稳定性
        cos = torch.mv(W, x) / torch.sqrt(torch.sum(W * W, dim=1) *torch.sum(x * x) + 1e-9)
        topk = torch.topk(cos, k=k+1)[1].cpu().numpy().astype('int32')
        for i in topk[1:]: # 删除输⼊词
            print(f'cosine sim={float(cos[i]):.3f}: {vocab.to_tokens(i)}')
    
    get_similar_tokens('chip', 3, net[0])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    cosine sim=0.761: microprocessor
    cosine sim=0.718: intel
    cosine sim=0.655: chips
    
    • 1
    • 2
    • 3

    4 - 小结

    • 使用嵌入层和二元交叉熵损失来训练带负采样的跳远模型
    • 词嵌入的应用包括基于词向量的余弦相似度为给定词找到语义相似的词
  • 相关阅读:
    Spark Windos模式
    Anaconda prompt中使用conda下载pytorch,一直卡在solving environment解决方案
    【EasyRL学习笔记】第四章 Policy Gradient 策略梯度
    Python面试题:如何在 Python 中实现一个简单的 Web 服务器?
    【ReactCli】开发模式脚手架配置
    证件照尺寸修改、图片背景换色、照片大小压缩…几个在线图片编辑、处理网站推荐
    对话腾讯天琴赵伟峰:当音乐与科技结合,会碰撞出怎样的火花?
    python sqlalchemy(ORM)- 01 简单使用
    一种非线性权重的自适应鲸鱼优化算法IMWOA附matlab代码
    如何在CubeIDE环境下查看或生成汇编文件
  • 原文地址:https://blog.csdn.net/mynameisgt/article/details/126755473