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

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

问题描述

我已经在gensim中训练了自己的word2vec模型,并且试图以spacy加载该模型.首先,我需要将其保存在磁盘中,然后尝试以零散的方式加载init模型,但无法确切地知道该怎么做.

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 the text file:

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/')

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

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