zoukankan      html  css  js  c++  java
  • PyTorch在NLP任务中使用预训练词向量

    在使用pytorch或tensorflow等神经网络框架进行nlp任务的处理时,可以通过对应的Embedding层做词向量的处理,更多的时候,使用预训练好的词向量会带来更优的性能。下面分别介绍使用gensim和torchtext两种加载预训练词向量的方法。

    1.使用gensim加载预训练词向量
        对于如下这样一段语料

    test_sentence = """When forty winters shall besiege thy brow,
    And dig deep trenches in thy beauty's field,
    Thy youth's proud livery so gazed on now,
    Will be a totter'd weed of small worth held:
    Then being asked, where all thy beauty lies,
    Where all the treasure of thy lusty days;
    To say, within thine own deep sunken eyes,
    Were an all-eating shame, and thriftless praise.
    How much more praise deserv'd thy beauty's use,
    If thou couldst answer 'This fair child of mine
    Shall sum my count, and make my old excuse,'
    Proving his beauty by succession thine!
    This were to be new made when thou art old,
    And see thy blood warm when thou feel'st it cold.""".split()
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
        构建词表,此过程也可使用Keras或torchtext来简化完成,完整代码见文末仓库。

    # 给每个单词编码,也就是用数字来表示每个单词,这样才能够传入word embeding得到词向量。
    vocab = set(test_sentence) # 通过set将重复的单词去掉
    word_to_idx = {word: i+1 for i, word in enumerate(vocab)}
    # 定义了一个unknown的词,也就是说没有出现在训练集里的词,我们都叫做unknown,词向量就定义为0。
    word_to_idx['<unk>'] = 0
    idx_to_word = {i+1: word for i, word in enumerate(vocab)}
    idx_to_word[0] = '<unk>'
    1
    2
    3
    4
    5
    6
    7
        使用gensim加载已训练好的word2vec词向量,此处用的是glove已训练好的词向量,下载链接:https://pan.baidu.com/s/1i5XmTA9 因为glove词向量和word2vec词向量格式略有不同,先使用gensim的scripts.glove2word2vec方法将glove词向量转化为word2vec词向量的格式。转化方式很简单,如下:

    from gensim.test.utils import datapath, get_tmpfile
    from gensim.models import KeyedVectors
    # 已有的glove词向量
    glove_file = datapath('test_glove.txt')
    # 指定转化为word2vec格式后文件的位置
    tmp_file = get_tmpfile("test_word2vec.txt")
    from gensim.scripts.glove2word2vec import glove2word2vec
    glove2word2vec(glove_file, tmp_file)
    1
    2
    3
    4
    5
    6
    7
    8
        去词向量文件中查表,得到词表中单词对应的权重weight。在词向量文件中没匹配到的单词则继续保留全0向量。

    # 使用gensim载入word2vec词向量
    wvmodel = gensim.models.KeyedVectors.load_word2vec_format('/Users/wyw/Documents/vectors/word2vec/word2vec.6B.100d.txt', binary=False, encoding='utf-8')
    vocab_size = len(vocab) + 1
    embed_size = 100
    weight = torch.zeros(vocab_size, embed_size)

    for i in range(len(wvmodel.index2word)):
    try:
    index = word_to_idx[wvmodel.index2word[i]]
    except:
    continue
    weight[index, :] = torch.from_numpy(wvmodel.get_vector(
    idx_to_word[word_to_idx[wvmodel.index2word[i]]]))
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
        得到weight权重后,即可在PyTorch的Embedding层中就可以指定预训练的词向量。

    embedding = nn.Embedding.from_pretrained(weight)
    # requires_grad指定是否在训练过程中对词向量的权重进行微调
    self.embedding.weight.requires_grad = True
    1
    2
    3
        完整代码见我的github仓库:https://github.com/atnlp/torchtext-summary 下的Language-Model.ipynb文件

    2.使用torchtext加载预训练的词向量
    下面介绍如何在torchtext中使用预训练的词向量,进而传送给神经网络模型进行训练。关于torchtext更完整的用法见我另一篇博客:TorchText用法示例及完整代码

    使用torchtext默认支持的预训练词向量
        默认情况下,会自动下载对应的预训练词向量文件到当前文件夹下的.vector_cache目录下,.vector_cache为默认的词向量文件和缓存文件的目录。

    from torchtext.vocab import GloVe
    from torchtext import data
    TEXT = data.Field(sequential=True)
    # 以下两种指定预训练词向量的方式等效
    # TEXT.build_vocab(train, vectors="glove.6B.200d")
    TEXT.build_vocab(train, vectors=GloVe(name='6B', dim=300))
    # 在这种情况下,会默认下载glove.6B.zip文件,进而解压出glove.6B.50d.txt, glove.6B.100d.txt, glove.6B.200d.txt, glove.6B.300d.txt这四个文件,因此我们可以事先将glove.6B.zip或glove.6B.200d.txt放在.vector_cache文件夹下(若不存在,则手动创建)。
    1
    2
    3
    4
    5
    6
    7
    指定预训练词向量和缓存文件所在目录
        上述使用预训练词向量文件的方式存在一大问题,即我们每做一个nlp任务时,建立词表时都需要在对应的.vector_cache文件夹中下载预训练词向量文件,如何解决这一问题?我们可以使用torchtext.vocab.Vectors中的name和cachae参数指定预训练的词向量文件和缓存文件的所在目录。因此我们也可以使用自己用word2vec等工具训练出的词向量文件,只需将词向量文件放在name指定的目录中即可。

    通过name参数可以指定预训练的词向量文件所在的目录
        默认情况下预训练词向量文件和缓存文件的目录位置都为当前目录下的 .vector_cache目录,虽然通过name参数指定了预训练词向量文件存在的目录,但是因为缓存文件的目录没有特殊指定,此时在当前目录下仍然需要存在 .vector_cache 目录。

    # glove.6B.200d.txt为预先下载好的预训练词向量文件
    if not os.path.exists(.vector_cache):
    os.mkdir(.vector_cache)
    vectors = Vectors(name='myvector/glove/glove.6B.200d.txt')
    TEXT.build_vocab(train, vectors=vectors)
    1
    2
    3
    4
    5
    通过cache参数指定缓存目录
    # 更进一步的,可以在指定name的同时同时指定缓存文件所在目录,而不是使用默认的.vector_cache目录
    cache = '.vector_cache'
    if not os.path.exists(cache):
    os.mkdir(cache)
    vectors = Vectors(name='myvector/glove/glove.6B.200d.txt', cache=cache)
    TEXT.build_vocab(train, vectors=vectors)
    1
    2
    3
    4
    5
    6
    在模型中指定Embedding层的权重
    在使用预训练好的词向量时,我们需要在神经网络模型的Embedding层中明确地传递嵌入矩阵的初始权重。权重包含在词汇表的vectors属性中。以Pytorch搭建的Embedding层为例:

    # 通过pytorch创建的Embedding层
    embedding = nn.Embedding(2000, 256)
    # 指定嵌入矩阵的初始权重
    weight_matrix = TEXT.vocab.vectors
    embedding.weight.data.copy_(weight_matrix )
    1
    2
    3
    4
    5
    一个比较完整的示例
    import torch
    from torchtext import data
    from torchtext import datasets
    from torchtext.vocab import GloVe
    import numpy as np

    def load_data(opt):
    # use torchtext to load data, no need to download dataset
    print("loading {} dataset".format(opt.dataset))
    # set up fields
    text = data.Field(lower=True, include_lengths=True, batch_first=True, fix_length=opt.max_seq_len)
    label = data.Field(sequential=False)

    # make splits for data
    train, test = datasets.IMDB.splits(text, label)

    # build the vocabulary
    text.build_vocab(train, vectors=GloVe(name='6B', dim=300))
    label.build_vocab(train)

    # print vocab information
    print('len(TEXT.vocab)', len(text.vocab))
    print('TEXT.vocab.vectors.size()', text.vocab.vectors.size())
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    完整代码见我的GitHub仓库:https://github.com/atnlp/torchtext-summary
    关于torchtext的其他用法见我的博客:http://www.nlpuser.com/pytorch/2018/10/30/useTorchText/
    个人原创,未经允许不得转载。
    ---------------------
    作者:nlpuser
    来源:CSDN
    原文:https://blog.csdn.net/nlpuser/article/details/83627709
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    css 负边距 小记
    javascript Array 方法学习
    使用自定义字体 @font-face 小试
    mongodb 基本指令学习 (2)
    mongodb 基本指令学习
    ASP.NET MVC AJAX调用JsonResult方法并返回自定义错误信息
    MVC MVVM Knockout 常遇问题总结
    关于 mvc 中 连字符
    在一般处理程序中,把Form Post过来的表单集合转换成对象 ,仿 MVC post,反射原理
    EF经验分享_jimmyzzc
  • 原文地址:https://www.cnblogs.com/jfdwd/p/11083750.html
Copyright © 2011-2022 走看看