【发布时间】:2020-09-07 14:26:29
【问题描述】:
我想以一种增强的方式训练一个先前训练过的 word2vec 模型,如果在之前的训练过程中看到过这个词,则更新这个词的权重,并创建和更新在之前的训练过程中没有看到过的新词的权重之前的训练过程。例如:
from gensim.models import Word2Vec
# old corpus
corpus = [["0", "1", "2", "3"], ["2", "3", "1"]]
# first train on old corpus
model = Word2Vec(sentences=corpus, size=2, min_count=0, window=2)
# checkout the embedding weights for word "1"
print(model["1"])
# here comes a new corpus with new word "4" and "5"
newCorpus = [["4", "1", "2", "3"], ["1", "5", "2"]]
# update the previous trained model
model.build_vocab(newCorpus, update=True)
model.train(newCorpus, total_examples=model.corpus_count, epochs=1)
# check if new word has embedding weights:
print(model["4"]) # yes
# check if previous word's embedding weights are updated
print(model["1"]) # output the same as before
似乎前一个词的嵌入没有更新,即使前一个词的上下文在新语料库中发生了变化。有人可以告诉我如何更新以前的嵌入权重吗?
【问题讨论】:
标签: python nlp gensim word2vec