是否可以从python中的句子语料库中重新训练word2vec模型(例如GoogleNews-vectors-negat



>我正在使用预先训练的谷歌新闻数据集,通过使用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)

是否有可能,这是一种很好的使用方式,请帮助我

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

使用来自 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)

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

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

您可能希望关注几个 GitHub 拉取请求,以了解这项工作的进度:

  • https://github.com/piskvorky/gensim/pull/435
  • https://github.com/piskvorky/gensim/pull/615

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

如果模型生成器没有完成模型训练,这是可能的。在 Python 中,它是:

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

如果模型没有最终确定,这是拥有具有大型数据集的模型的完美方法。

最新更新