在spacy中,如何使用自己在gensim中创建的word2vec模型? [英] In spacy, how to use your own word2vec model created in gensim?

查看:65
本文介绍了在spacy中,如何使用自己在gensim中创建的word2vec模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 gensim 中训练了我自己的 word2vec 模型,我正在尝试在 spacy 中加载该模型.首先,我需要将它保存在我的磁盘中,然后尝试在 spacy 中加载一个初始化模型,但无法弄清楚究竟是如何加载的.

I have trained my own word2vec model in gensim and I am trying to load that model in spacy. First, I need to save it in my disk and then try to load an init-model in spacy but unable to figure out exactly how.

gensimmodel
Out[252]:
<gensim.models.word2vec.Word2Vec at 0x110b24b70>

import spacy
spacy.load(gensimmodel)

OSError: [E050] Can't find model 'Word2Vec(vocab=250, size=1000, alpha=0.025)'. It doesn't seem to be a shortcut link, a Python package or a valid path to a data directory.

推荐答案

以纯文本格式训练和保存模型:

Train and save your model in plain-text format:

from gensim.test.utils import common_texts, get_tmpfile
from gensim.models import Word2Vec

path = get_tmpfile("./data/word2vec.model")

model = Word2Vec(common_texts, size=100, window=5, min_count=1, workers=4)
model.wv.save_word2vec_format("./data/word2vec.txt")

Gzip 文本文件:

gzip word2vec.txt

生成一个 word2vec.txt.gz 文件.

运行以下命令:

python -m spacy init-model en ./data/spacy.word2vec.model --vectors-loc word2vec.txt.gz

使用以下方法加载向量:

Load the vectors using:

nlp = spacy.load('./data/spacy.word2vec.model/')

这篇关于在spacy中,如何使用自己在gensim中创建的word2vec模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆