【问题标题】:How to add missing words vectors in GoogleNews-vectors-negative300.bin pre-trained model?如何在 GoogleNews-vectors-negative300.bin 预训练模型中添加缺失词向量?
【发布时间】:2016-03-02 19:27:48
【问题描述】:

我在 python 中使用 gensim word2vec 库并使用预训练的 GoogleNews-vectors-negative300.bin 模型。但是,

我的语料库中有没有词向量的词,我是 得到 keyError 我该如何解决这个问题?

这是我迄今为止尝试过的,

1: 加载GoogleNews-vectors-negative300.bin per-trained 模型:

model = Word2Vec.load_word2vec_format('GoogleNews-vectors-negative300.bin', binary=True)
print "model loaded..."

2:利用推文中所有词向量的平均值构建训练集词向量,然后进行缩放

def buildWordVector(text, size):
vec = np.zeros(size).reshape((1, size))
count = 0.
for word in text:
    try:
        vec += model[word].reshape((1, size))
        count += 1.
        #print "found! ",  word
    except KeyError:
        print "not found! ",  word #missing words
        continue
if count != 0:
    vec /= count
return vec

trained_vecs = np.concatenate([buildWordVector(z, n_dim) for z in x_train])

请说明如何在预训练的 Word2vec 模型中添加新词?

【问题讨论】:

  • 发布到目前为止您尝试过的代码。 Gensim 关于 word2vec (radimrehurek.com/gensim/models/word2vec.html) 的文档涵盖了您想要实现的目标。如果您正在寻找预训练的向量,您可以在此处获取它们 - code.google.com/p/word2vec
  • 我遵循了相同的教程。但我认为我并没有完全执行每一步。我刚刚从预训练模型中获得了词向量。现在,我想用一些单词或句子来扩展预训练模型。

标签: python nlp gensim word2vec word-embedding


【解决方案1】:

编辑 2019/06/07

正如@Oleg Melnikov 和https://rare-technologies.com/word2vec-tutorial/#online_training__resuming 所指出的,没有词汇树是不可能恢复训练的(用C 代码训练完成后不会保存)

请注意,无法使用 C 工具 load_word2vec_format() 生成的模型恢复训练。您仍然可以将它们用于查询/相似性,但那里缺少对训练至关重要的信息(词汇树)。


  1. 获取预训练的向量 - 例如。 Google News

  2. 在gensim中加载模型

  3. 继续在 gensim 中训练模型

这些命令可能会派上用场

# Loading pre-trained vectors
model = Word2Vec.load_word2vec_format('/tmp/vectors.bin', binary=True)

# Training the model with list of sentences (with 4 CPU cores)
model.train(sentences, workers=4)

【讨论】:

  • 嘿,谢谢,我想问一下,我们需要在“model.train(sentences, workers=4)”之前运行“model.build_vocab(sentences)”吗?
  • 我不确定model.build_vocab是否重新初始化了词向量,但是在训练时你可以提供trim_rule来选择你想要的词汇。
  • 那么有没有办法重新训练已经训练好的模型呢?或者对于 Googlenews 预训练模型中不存在的缺失词应该有什么解决方法?请帮忙
  • 您的语料库中的单词不存在于 GoogleNews 模型中?您能否详细说明此类词在语料库中的比例?
  • 我正在使用谷歌预训练的词向量模型。但是,对于某些词,我无法获得词向量。现在,我正在寻找一些方法来添加这些词。这怎么可能?
猜你喜欢
  • 2018-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-04
  • 2016-05-09
  • 1970-01-01
  • 2019-11-21
  • 2018-11-02
相关资源
最近更新 更多