我正在使用预先训练的谷歌新闻数据集,通过在 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)
注意事项:从实质性的角度来看,一个可能非常小的语料库是否真的可以“改进”在一个庞大的语料库上训练的谷歌词向量当然是非常值得商榷的......
关于python - 是否可以从 python 中的句子语料库重新训练 word2vec 模型(例如 GoogleNews-vectors-negative300.bin)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35117491/