簡體   English   中英

使用 Gensim 從 Fasttext 的 .bin 文件重新訓練 FastText 模型時出現問題。 'FastTextTrainables' 對象沒有屬性 'syn1neg'

[英]Problem retraining a FastText model from .bin file from Fasttext using Gensim. 'FastTextTrainables' object has no attribute 'syn1neg'

我正在嘗試使用 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'

提前感謝您的幫助

感謝您提供詳細的代碼,顯示您嘗試過的內容以及遇到的錯誤。

您確定您使用的是最新的 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)不是絕對必要的,也不是做任何相關的事情。 模型的已知詞匯在前后是相同的。 (當然,如果使用帶有新單詞的實際新句子,那就不同了——但你的小例子還沒有真正測試詞匯擴展。)

並進一步:

這種將一些新數據或少量新詞訓練到現有模型中的方式充滿了艱難的權衡。

特別是,如果您的新數據僅包括您的新詞和原始模型詞的某些子集,則只有這些新數據詞將根據其用法接收訓練更新。 這會逐漸將新訓練數據中的所有單詞拉到新位置。 這些新位置可能會成為新文本的最佳位置,但可能與它們的舊位置相去甚遠——也許很遠——它們最初是在早期模型中訓練的。

因此,您的新詞和舊詞都不會與新數據中沒有的任何舊詞保持固有的可比性。 本質上,只有一起訓練的單詞才必須移動到有用的對比位置。

因此,如果您的新數據足夠大且變化多端,足以涵蓋您的應用程序所需的單詞,那么訓練一個全新的模型可能既簡單又好。 另一方面,如果您的新數據很薄,那么將一小部分單詞/示例訓練到舊模型中仍然有可能將這些單詞從有用的“對齊”中拉出與舊單詞的“對齊”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM