如何加载预训练的 Word2vec 模型文件并重新使用它?

新手上路,请多包涵

我想使用预训练的 word2vec 模型,但我不知道如何在 python 中加载它。

该文件是一个模型文件 (703 MB)。它可以在这里下载:

http://devmount.github.io/GermanWordEmbeddings/

原文由 Vahid SJ 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.1k
2 个回答

只是为了加载

import gensim

# Load pre-trained Word2Vec model.
model = gensim.models.Word2Vec.load("modelName.model")

现在你可以像往常一样训练模型了。另外,如果你想保存它并多次重新训练它,这就是你应该做的

model.train(//insert proper parameters here//)
"""
If you don't plan to train the model any further, calling
init_sims will make the model much more memory-efficient
If `replace` is set, forget the original vectors and only keep the normalized
ones = saves lots of memory!
replace=True if you want to reuse the model
"""
model.init_sims(replace=True)

# save the model for later use
# for loading, call Word2Vec.load()

model.save("modelName.model")

原文由 AbtPst 发布,翻译遵循 CC BY-SA 3.0 许可协议

使用 KeyedVectors 加载预训练模型。

 from gensim.models import KeyedVectors
from gensim import models

word2vec_path = 'path/GoogleNews-vectors-negative300.bin.gz'
w2v_model = models.KeyedVectors.load_word2vec_format(word2vec_path, binary=True)

原文由 Nilani Algiriyage 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题