【问题标题】:Is it possible to re-train a word2vec model (e.g. GoogleNews-vectors-negative300.bin) from a corpus of sentences in python?是否可以从 python 中的句子语料库重新训练 word2vec 模型(例如 GoogleNews-vectors-negative300.bin)?
【发布时间】:2016-05-09 03:13:32
【问题描述】:

我正在使用预训练的 Google 新闻数据集通过 Python 中的 Gensim 库获取词向量

model = Word2Vec.load_word2vec_format('GoogleNews-vectors-negative300.bin', binary=True)

加载模型后,我将训练评论句子单词转换为向量

#reading all sentences from training file
with open('restaurantSentences', 'r') as infile:
x_train = infile.readlines()
#cleaning sentences
x_train = [review_to_wordlist(review,remove_stopwords=True) for review in x_train]
train_vecs = np.concatenate([buildWordVector(z, n_dim) for z in x_train])

在 word2Vec 过程中,我的语料库中的单词出现了很多错误,这些错误不在模型中。问题是我如何重新训练已经预训练的模型(例如 GoogleNews-vectors-negative300.bin'),以便为那些缺失的单词获取词向量。

以下是我尝试过的: 从我训练的句子中训练了一个新模型

# Set values for various parameters
num_features = 300    # Word vector dimensionality                      
min_word_count = 10   # Minimum word count                        
num_workers = 4       # Number of threads to run in parallel
context = 10          # Context window    size                                                                                    
downsampling = 1e-3   # Downsample setting for frequent words

sentences = gensim.models.word2vec.LineSentence("restaurantSentences")
# Initialize and train the model (this will take some time)
print "Training model..."
model = gensim.models.Word2Vec(sentences, workers=num_workers,size=num_features, min_count = min_word_count, 
                      window = context, sample = downsampling)


model.build_vocab(sentences)
model.train(sentences)
model.n_similarity(["food"], ["rice"])

成功了!但问题是我的数据集非常小,训练大型模型的资源也很少。

我正在研究的第二种方法是扩展已经训练好的模型,例如 GoogleNews-vectors-negative300.bin。

model = Word2Vec.load_word2vec_format('GoogleNews-vectors-negative300.bin', binary=True)
sentences = gensim.models.word2vec.LineSentence("restaurantSentences")
model.train(sentences)

有没有可能,是不是一个好用的方法,请帮帮我

【问题讨论】:

标签: python nlp gensim word2vec


【解决方案1】:

这就是我在技术上解决问题的方法:

使用 Radim Rehurek 的可迭代句子准备数据输入:https://rare-technologies.com/word2vec-tutorial/

sentences = MySentences('newcorpus')  

设置模型

model = gensim.models.Word2Vec(sentences)

将词汇与谷歌词向量相交

model.intersect_word2vec_format('GoogleNews-vectors-negative300.bin',
                                lockf=1.0,
                                binary=True)

最终执行模型并更新

model.train(sentences)

警告说明:从实质性的角度来看,一个可能非常小的语料库能否真正“改进”在海量语料库上训练的 Google 词向量当然是值得商榷的......

【讨论】:

  • 您的评论表明此方法旨在“改进”Google 的词向量。 The documentation 建议它实际上使用 Google 的向量来改进您的模型,而不是相反。 (现有词汇表中没有添加任何单词,但相交的单词采用文件的权重,不相交的单词被单独保留。) 我尝试了您的方法并检查了我的模型的语料库大小。它反映的是新的训练数据,而不是 Google 新闻。
  • 你是对的——也许改善这个词在这里有误导性。代码的作用是更新新语料库中的单词并返回给你。
  • Google 的词向量的大小(词汇)大约是 3,000,000 个词,所以如果你的语料库的大小要小得多,比如大约 10,000 个词,在相交时,你的模型的大小将继续仅 10,000 个,但您模型中的词将直接分配来自 Google 词向量的权重,完全忽略原始模型中先前的权重。所以除非你也有一个非常大的语料库可以训练,否则它不会有任何区别。
  • 我试过这个:model.intersect_word2vec_format('tweets_cbow_300',lockf=1.0,binary=False, encoding='utf8') 它返回一个错误:UnicodeDecodeError: 'utf-8' codec can' t 解码位置 0 中的字节 0x80:无效的起始字节添加此:unicode_errors='ignore' 并不能解决问题。
【解决方案2】:

有些人一直在努力扩展 gensim 以允许在线培训。

您可能希望关注几个 GitHub 拉取请求以了解该工作的进展:

看起来这项改进可以允许更新 GoogleNews-vectors-negative300.bin 模型。

【讨论】:

  • 那是个好消息,....我希望我可以进行在线培训,我只是在等待:)
【解决方案3】:

如果模型构建器没有完成模型训练,这是可能的。 在python中是:

model.sims(replace=True) #finalize the model

如果模型没有最终确定,那么它是拥有大型数据集的模型的完美方式。

【讨论】:

    猜你喜欢
    • 2018-03-08
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 2020-06-04
    • 1970-01-01
    • 2016-07-28
    • 1970-01-01
    • 2021-10-22
    相关资源
    最近更新 更多