Gensim word2vec 增强或合并预训练向量
Gensim word2vec augment or merge pre-trained vectors
我正在从 word2vec C 代码生成的二进制文件中加载预训练向量,其中包含以下内容:
model_1 = Word2Vec.load_word2vec_format('vectors.bin', binary=True)
我正在使用这些向量生成句子的向量表示,这些句子包含 vectors.bin
中可能不存在的向量的单词。例如,如果 vectors.bin
没有与单词 "yogurt" 相关联的向量,我尝试
yogurt_vector = model_1['yogurt']
我得到 KeyError: 'yogurt'
,这很有道理。我想要的是能够将没有对应向量的句子单词添加到 model_1
中。我从 this post 得知您无法继续训练 C 向量。那么有没有一种方法可以训练一个新模型,比如 model_2
,对于没有向量的单词,并将 model_2
与 model_1
合并?
或者,有没有办法在我实际尝试检索单词之前测试模型是否包含单词,这样我至少可以避免 KeyError?
避免按键错误很容易:
[x for x in 'this model hus everything'.split() if x in model_1.vocab]
更难的问题是将新词合并到现有模型中。问题是 word2vec 计算了 2 个单词彼此相邻的可能性,如果单词 'yogurt' 不在模型训练的第一个 body 中,它就不会与任何一个相邻单词,所以第二个模型与第一个模型不相关。
您可以在保存模型时查看内部结构(使用 numpy.save),我有兴趣与您一起编写允许添加词汇表的代码。
这是一个很好的问题,不幸的是,如果不更改代码的内部结构,就无法添加到词汇表中。查看此讨论:https://groups.google.com/forum/#!searchin/word2vec-toolkit/online$20word2vec/word2vec-toolkit/L9zoczopPUQ/_Zmy57TzxUQJ
我的建议是忽略词汇表中没有的词,只使用词汇表中的词。如果您使用的是 python,您可以通过以下方式执行此操作:
for word in wordlist:
if word in model.vocab:
present.append(word)
else:
# this is all the words that are absent for your model
# might be useful for debugging. Ignore if you dont need this info
absent.append(word)
<Do whatever you want with the words in the list 'present'>
YoonKim 在 "Convolutional Neural Networks for Sentence Classification"
中提出了处理 absent/missing 个单词的可能替代方法
它的代码:https://github.com/yoonkim/CNN_sentence/blob/master/process_data.py#L88
def add_unknown_words(word_vecs, vocab, min_df=1, k=300):
"""
For words that occur in at least min_df documents, create a separate word vector.
0.25 is chosen so the unknown vectors have (approximately) same variance as pre-trained ones
"""
for word in vocab:
if word not in word_vecs and vocab[word] >= min_df:
word_vecs[word] = np.random.uniform(-0.25,0.25,k)
但这行得通,你可以使用模型查找来找到相应的向量。相似性等功能丢失
您可以继续向模型词汇表添加新的words/sentences并训练扩充模型,使用gensim在线训练算法(https://rutumulkar.com/blog/2015/word2vec/),
https://radimrehurek.com/gensim/auto_examples/tutorials/run_word2vec.html
model = gensim.models.Word2Vec.load(temporary_filepath)
more_sentences = [
['Advanced', 'users', 'can', 'load', 'a', 'model',
'and', 'continue', 'training', 'it', 'with', 'more', 'sentences'],
]
model.build_vocab(more_sentences, update=True)
model.train(more_sentences, total_examples=model.corpus_count, epochs=model.epochs)
相关:
- Update gensim word2vec model
- Is it possible to re-train a word2vec model (e.g. GoogleNews-vectors-negative300.bin) from a corpus of sentences in python?
我正在从 word2vec C 代码生成的二进制文件中加载预训练向量,其中包含以下内容:
model_1 = Word2Vec.load_word2vec_format('vectors.bin', binary=True)
我正在使用这些向量生成句子的向量表示,这些句子包含 vectors.bin
中可能不存在的向量的单词。例如,如果 vectors.bin
没有与单词 "yogurt" 相关联的向量,我尝试
yogurt_vector = model_1['yogurt']
我得到 KeyError: 'yogurt'
,这很有道理。我想要的是能够将没有对应向量的句子单词添加到 model_1
中。我从 this post 得知您无法继续训练 C 向量。那么有没有一种方法可以训练一个新模型,比如 model_2
,对于没有向量的单词,并将 model_2
与 model_1
合并?
或者,有没有办法在我实际尝试检索单词之前测试模型是否包含单词,这样我至少可以避免 KeyError?
避免按键错误很容易:
[x for x in 'this model hus everything'.split() if x in model_1.vocab]
更难的问题是将新词合并到现有模型中。问题是 word2vec 计算了 2 个单词彼此相邻的可能性,如果单词 'yogurt' 不在模型训练的第一个 body 中,它就不会与任何一个相邻单词,所以第二个模型与第一个模型不相关。
您可以在保存模型时查看内部结构(使用 numpy.save),我有兴趣与您一起编写允许添加词汇表的代码。
这是一个很好的问题,不幸的是,如果不更改代码的内部结构,就无法添加到词汇表中。查看此讨论:https://groups.google.com/forum/#!searchin/word2vec-toolkit/online$20word2vec/word2vec-toolkit/L9zoczopPUQ/_Zmy57TzxUQJ
我的建议是忽略词汇表中没有的词,只使用词汇表中的词。如果您使用的是 python,您可以通过以下方式执行此操作:
for word in wordlist:
if word in model.vocab:
present.append(word)
else:
# this is all the words that are absent for your model
# might be useful for debugging. Ignore if you dont need this info
absent.append(word)
<Do whatever you want with the words in the list 'present'>
YoonKim 在 "Convolutional Neural Networks for Sentence Classification"
中提出了处理 absent/missing 个单词的可能替代方法它的代码:https://github.com/yoonkim/CNN_sentence/blob/master/process_data.py#L88
def add_unknown_words(word_vecs, vocab, min_df=1, k=300):
"""
For words that occur in at least min_df documents, create a separate word vector.
0.25 is chosen so the unknown vectors have (approximately) same variance as pre-trained ones
"""
for word in vocab:
if word not in word_vecs and vocab[word] >= min_df:
word_vecs[word] = np.random.uniform(-0.25,0.25,k)
但这行得通,你可以使用模型查找来找到相应的向量。相似性等功能丢失
您可以继续向模型词汇表添加新的words/sentences并训练扩充模型,使用gensim在线训练算法(https://rutumulkar.com/blog/2015/word2vec/),
https://radimrehurek.com/gensim/auto_examples/tutorials/run_word2vec.html
model = gensim.models.Word2Vec.load(temporary_filepath)
more_sentences = [
['Advanced', 'users', 'can', 'load', 'a', 'model',
'and', 'continue', 'training', 'it', 'with', 'more', 'sentences'],
]
model.build_vocab(more_sentences, update=True)
model.train(more_sentences, total_examples=model.corpus_count, epochs=model.epochs)
相关:
- Update gensim word2vec model
- Is it possible to re-train a word2vec model (e.g. GoogleNews-vectors-negative300.bin) from a corpus of sentences in python?