加载了gensim的Fasttext模型不会继续使用新句子进行训练

2024-06-28 06:12:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试加载一个西班牙语的fasttext.bin模型,从https://fasttext.cc/docs/en/crawl-vectors.html加载,并继续使用我感兴趣的特定领域的新句子对其进行训练

系统:Anaconda、Jupyter笔记本、Python 3.6、升级的Gensim

我的代码(玩具示例):

from gensim.models.fasttext import load_facebook_model
import os
os.chdir('path/to/directory')
model = load_facebook_model('cc.es.300.bin')

'enmadrarse' in model.wv.vocab
>>> False
old_vector = np.copy(model.wv['enmadrarse'])

new_sentences = [['complexidad', 'cataratas', 'enmadrarse'],
['enmadrarse', 'cataratas', 'increibles'], 
['unidad','enmadrarse','complexa']]

model.build_vocab(new_sentences, update = True)
model.train(new_sentences, total_examples = len(new_sentences), epochs=model.epochs)

new_vector = np.copy(model.wv['enmadrarse'])
np.allclose(old_vector, new_vector, atol=1e-4)
>>> True

'enmadrarse' in model.wv.vocab
>>> False (still)

单词的新旧向量是相等的,它不在词汇表中,因此模型什么也学不到。我做错了什么


Tags: 模型importnewmodelfacebookbinnpsentences