Word2vec模型的大小非常小,无法识别单词

时间:2018-07-07 11:47:14

标签: python python-3.x word2vec gensim word-embedding

我使用word2vec软件包对 6.4 GB文本数据训练了gensim模型,该软件包使用以下代码段进行了预处理:

def read_input(input_file):
  with open(input_file, "r") as inp:
    inp_str = inp.read()

  inp_str = inp_str.strip('\n')
  inp_str = re.sub(r'\n', ' ', inp_str)
  lowercase = inp_str.lower()
  punc = lowercase.translate(str.maketrans('', '', string.punctuation))

  return (punc.translate(str.maketrans('','','1234567890')))

def read_(input_file):
  return( gensim.utils.simple_preprocess(input_file, deacc=True, min_len=3))          

doc = read_input('../train1.txt')
documents = read_(doc)
logging.info ("Done reading data file")

但是每次我训练模型时,它的大小都是 147 Kb ,这似乎不合适,当我尝试从训练后的模型中生成矢量时,它会说:

KeyError: "word 'type' not in vocabulary"

以下是我用于训练word2vec模型的代码:

old_model = Word2Vec.load('../word2vec_model')
old_model.train(documents, total_examples=old_model.corpus_count, epochs=7)

old_model.save('../word2vec_model1')

logging.info ("Saved the new word2vec model")

请帮助我解决此问题。

1 个答案:

答案 0 :(得分:1)

大小很小,可能是因为在设置模型参数时,您可能为 min_count 参数使用了较大的值。 尝试减少min_count中的值并重新训练模型