【问题标题】:Problem retraining a FastText model from .bin file from Fasttext using Gensim. 'FastTextTrainables' object has no attribute 'syn1neg'使用 Gensim 从 Fasttext 的 .bin 文件重新训练 FastText 模型时出现问题。 “FastTextTrainables”对象没有属性“syn1neg”
【发布时间】:2020-09-15 14:51:04
【问题描述】:

我正在尝试使用 gensim 包装器为我的问题微调一个 FastText 预训练模型,但我遇到了问题。 我从 .bin 文件中成功加载模型嵌入,如下所示:

from gensim.models.fasttext import FastText
model=FastText.load_fasttext_format(r_bin)

尽管如此,当我想使用这 3 行代码重新训练模型时,我很挣扎:

sent = [['i', 'am ', 'interested', 'on', 'SPGB'], ['SPGB' 'is', 'a', 'good', 'choice']]
model.build_vocab(sent, update=True)
model.train(sentences=sent, total_examples = len(sent), epochs=5)

无论我如何更改,我都会一遍又一遍地收到此错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-91-6456730b1919> in <module>
      1 sent = [['i', 'am', 'interested', 'on', 'SPGB'], ['SPGB' 'is', 'a', 'good', 'choice']]
----> 2 model.build_vocab(sent, update=True)
      3 model.train(sentences=sent, total_examples = len(sent), epochs=5)

/opt/.../fasttext.py in build_vocab(self, sentences, update, progress_per, keep_raw_vocab, trim_rule, **kwargs)
    380         return super(FastText, self).build_vocab(
    381             sentences, update=update, progress_per=progress_per,
--> 382             keep_raw_vocab=keep_raw_vocab, trim_rule=trim_rule, **kwargs)
    383 
    384     def _set_train_params(self, **kwargs):

/opt/.../base_any2vec.py in build_vocab(self, sentences, update, progress_per, keep_raw_vocab, trim_rule, **kwargs)
    484             trim_rule=trim_rule, **kwargs)
    485         report_values['memory'] = self.estimate_memory(vocab_size=report_values['num_retained_words'])
--> 486         self.trainables.prepare_weights(self.hs, self.negative, self.wv, update=update, vocabulary=self.vocabulary)
    487 
    488     def build_vocab_from_freq(self, word_freq, keep_raw_vocab=False, corpus_count=None, trim_rule=None, update=False):

/opt/.../fasttext.py in prepare_weights(self, hs, negative, wv, update, vocabulary)
    752 
    753     def prepare_weights(self, hs, negative, wv, update=False, vocabulary=None):
--> 754         super(FastTextTrainables, self).prepare_weights(hs, negative, wv, update=update, vocabulary=vocabulary)
    755         self.init_ngrams_weights(wv, update=update, vocabulary=vocabulary)
    756 

/opt/.../word2vec.py in prepare_weights(self, hs, negative, wv, update, vocabulary)
   1402             self.reset_weights(hs, negative, wv)
   1403         else:
-> 1404             self.update_weights(hs, negative, wv)
   1405 
   1406     def seeded_vector(self, seed_string, vector_size):

/opt/.../word2vec.py in update_weights(self, hs, negative, wv)
   1452             self.syn1 = vstack([self.syn1, zeros((gained_vocab, self.layer1_size), dtype=REAL)])
   1453         if negative:
-> 1454             self.syn1neg = vstack([self.syn1neg, zeros((gained_vocab, self.layer1_size), dtype=REAL)])
   1455         wv.vectors_norm = None
   1456 

AttributeError: 'FastTextTrainables' object has no attribute 'syn1neg'

提前感谢您的帮助

【问题讨论】:

  • 您正在加载的模型的来源是什么? (它是如何训练的,使用什么参数等?)而且,您可以编辑您的问题以显示您看到的完整错误 - 使用回溯堆栈和文件/行吗?
  • 我从 FastText 网页下载了可用的英文 .bin 文件:fasttext.cc/docs/en/crawl-vectors.html 我还用我得到的完整错误编辑了这个问题。提前感谢@gojomo

标签: python nlp gensim pre-trained-model fasttext


【解决方案1】:

感谢您提供详细的代码,显示您尝试过的内容和遇到的错误。

您确定您使用的是最新版本的 Gensim,gensim-3.8.3?我无法使用您的代码和 Gensim 重现错误。

另外:在gensim-3.8.3 中,您会看到一个警告:

DeprecationWarning: Call to deprecated 'load_fasttext_format' (use load_facebook_vectors (to use pretrained embeddings) or load_facebook_model (to continue training with the loaded full model, more RAM) instead).

(不推荐使用的方法只会为您调用 load_facebook_model(),因此使用旧方法不会单独导致您的问题 - 但您的环境应该使用最新的 Gensim,并且您的代码应该更新以调用首选方法.)

请注意:

由于您的小测试文本中没有新词,build_vocab(..., update=True) 不是绝对必要的,也没有做任何相关的事情。您的模型的已知词汇在之前和之后是相同的。 (当然,如果使用带有新单词的实际新句子,那会有所不同——但你的小例子还没有真正测试词汇扩展。)

还有:

这种将一些新数据或少量新词训练到现有模型中的方式充满了艰难的权衡。

特别是,如果您的新数据仅包含您的新词和原始模型词的某些子集,则只有这些新数据词会根据它们的用法接收训练更新.这会逐渐将新训练数据中的所有单词拉到新位置。这些新位置可能会成为新文本的最佳位置,但可能与它们最初在早期模型中训练的旧位置相距甚远(也许非常远)。

因此,无论是您的新词还是旧词that-have-received-new-trainined 都不会与新数据中没有的任何旧词具有内在的可比性。从本质上讲,只有一起训练的单词才会被移动到有用的对比位置。

因此,如果您的新数据足够大且变化多样,足以涵盖您的应用程序所需的单词,那么训练一个全新的模型可能既简单又更好。另一方面,如果您的新数据很薄,仅将那一小部分单词/示例训练到旧模型中仍然存在将那部分单词从与旧单词有用的“对齐”中拉出的风险。

【讨论】:

  • 那么,您建议作为一种可能的解决方案来转到您提到的版本?
  • 如果您没有看到 DeprecationWarning,您可能不在 Gensim 的最新正式发布版本 gensim-3.8.3。对于任何错误,在尝试其他调试之前检查它是否被更新的版本修复总是好的。但更进一步,当我使用 3.8.3 版本时,我无法使用您提供的代码重现您的错误。
猜你喜欢
  • 2020-06-26
  • 2021-03-29
  • 1970-01-01
  • 2018-11-11
  • 2019-02-04
  • 1970-01-01
  • 2019-04-08
  • 1970-01-01
  • 2022-11-04
相关资源
最近更新 更多