【问题标题】:gensim - Word2vec continue training on existing model - AttributeError: 'Word2Vec' object has no attribute 'compute_loss'gensim - Word2vec 继续对现有模型进行训练 - AttributeError:“Word2Vec”对象没有属性“compute_loss”
【发布时间】:2018-01-25 13:28:06
【问题描述】:

我正在尝试继续对现有模型进行训练,

model = gensim.models.Word2Vec.load('model/corpus.zhwiki.word.model')
more_sentences = [['Advanced', 'users', 'can', 'load', 'a', 'model', 'and', 'continue', 'training', 'it', 'with', 'more', 'sentences']]    
model.build_vocab(more_sentences, update=True)
model.train(more_sentences, total_examples=model.corpus_count, epochs=model.iter)

但最后一行出现错误:

AttributeError: 'Word2Vec' 对象没有属性 'compute_loss'

一些帖子说这是由于使用了早期版本的 gensim 造成的,我尝试在加载现有模型之后和 train() 之前添加它。

model.compute_loss = False

之后,它没有给我AttributeError,但是model.train()的输出是0,并且模型没有用新的句子训练。

如何解决这个问题?

【问题讨论】:

    标签: python nlp word2vec gensim


    【解决方案1】:

    这是我继续训练模型的方法

    # training_data: initial training data. contain list of tokenized sentences
    model = Word2Vec(training_data, size=50, window=5, min_count=10, workers=4)
    
    # datasmall: more sentences
    # total_examples: number of additional sentence
    # epochs: provide your current epochs. model.epochs is ok 
    model.train(datasmall, total_examples=len(datasmall), epochs=model.epochs)
    

    【讨论】:

      【解决方案2】:

      train()total_examples(和 epochs)参数应该与您当前在 more_sentences 中提供的参数相匹配,而不是之前训练的剩余值。

      例如,假设您的代码仅显示一个附加句子,您可以指定total_examples=1

      如果这不是问题的根源,请仔细检查 more_sentences 是否是您在调用 train() 时所期望的。

      【讨论】:

      • 我已经更改了 total_examples=1 和 epochs=1,但它也给了我相同的结果。 more_sentences 也具有我希望放入 train() 方法的值
      • 您确定新文本中的所有单词都比min_count 出现的频率更高吗? (所有因频率低而被跳过的单词可能是0 训练单词计数的一种解释。)
      • 是的,这是可能的,但是在 'train()' 方法中我不能给出 'min_count' 参数。也许原始模型中的“min_count”太低了?有什么方法可以设置/更改“min_count”?
      • 如果您想保留的不常用词丢失,则原始min_count 太高您可以在执行build_vocab(..., update=True) 之前直接重新分配model.min_count。但是,请注意,消除低频词通常有利于向量质量和训练时间——添加单个新句子的过程不太可能很好地工作。 (通过对许多不同示例的交错训练,模型变得自洽良好;玩具大小的语料库或玩具大小的更新不太可能很好地工作。)
      猜你喜欢
      • 1970-01-01
      • 2021-10-11
      • 1970-01-01
      • 1970-01-01
      • 2018-10-18
      • 1970-01-01
      • 1970-01-01
      • 2018-04-10
      • 1970-01-01
      相关资源
      最近更新 更多