是否有可能从python的句子语料库中重新训练word2vec模型(例如GoogleNews-vectors-negative300.bin)?

Nom*_*uks 10 python nlp gensim word2vec

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

model = Word2Vec.load_word2vec_format('GoogleNews-vectors-negative300.bin', binary=True)
Run Code Online (Sandbox Code Playgroud)

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

#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])
Run Code Online (Sandbox Code Playgroud)

在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"])
Run Code Online (Sandbox Code Playgroud)

有效!但问题是我有一个非常小的数据集和较少的资源来训练一个大型模型.

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

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

它是否可能并且是一种好的使用方式,请帮助我

小智 6

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

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

sentences = MySentences('newcorpus')  
Run Code Online (Sandbox Code Playgroud)

设置模型

model = gensim.models.Word2Vec(sentences)
Run Code Online (Sandbox Code Playgroud)

将词汇表与谷歌词向量相交

model.intersect_word2vec_format('GoogleNews-vectors-negative300.bin',
                                lockf=1.0,
                                binary=True)
Run Code Online (Sandbox Code Playgroud)

最后执行模型并更新

model.train(sentences)
Run Code Online (Sandbox Code Playgroud)

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

  • 您的评论表明此方法旨在“改进”谷歌的词向量。[文档](https://radimrehurek.com/gensim/models/word2vec.html) 会建议它实际上使用谷歌的向量来改进你的模型,而不是相反。*(现有词汇中没有添加词,但交叉词采用文件的权重,非交叉词单独保留。)* 我尝试了您的方法并检查了我的模型的语料库大小。它反映了新的训练数据,而不是 Google 新闻。 (2认同)

orl*_*uke 2

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

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

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