首页 > 解决方案 > 将 gensim KeyedVector 转换为模型的过程是什么?

问题描述

我想加载一个 gensim 预训练模型并继续训练它。

这个例子失败了......我尝试了许多变体和 gensim API,但我试图做的似乎不可能。

import gensim, logging, os
from gensim.models import KeyedVectors
from gensim.models import Word2Vec

import gensim.downloader as api
wv = api.load('word2vec-google-news-300')

# this fails - TypeError: 'int' object is not iterable
model = Word2Vec(wv)

class MySentences(object):
    def __init__(self, dirname):
        self.dirname = dirname

    def __iter__(self):
        for fname in os.listdir(self.dirname):
            for line in open(os.path.join(self.dirname, fname)):
                yield line.split()

sentences = MySentences('all-tokenized-sentences.txt')

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

标签: pythonmodelgensimword2vec

解决方案


推荐阅读