【发布时间】:2021-01-19 02:38:55
【问题描述】:
我有兴趣在 Gensim word2vec 模型上放置一个回调,以在每批之后触发一些函数。根据documentation,可以在批处理结束或纪元结束时放置回调。但是,如下面的 MVE 所示,实际上只触发了 epoch 回调。
要运行示例,让corpus_filepath 指向一个由不带标点的句子组成的行分隔文件(给定一行的句子中的单词应该用空格分隔)。您可能还需要在 Word2Vec 实例化中更改 workers。
from gensim.models import Word2Vec
from gensim.models.callbacks import CallbackAny2Vec
corpus_filepath = 'train.txt'
out_filepath = 'out.txt'
class MyCallback(CallbackAny2Vec):
def __init__(self):
pass
def on_batch_end(self, model):
print('batch end')
def on_epoch_end(self, model):
print('epoch end')
callback = MyCallback()
model = Word2Vec(size=300, window=5, min_count=0, workers=64)
print('Making vocabulary...')
model.build_vocab(corpus_file=corpus_filepath)
print('Beginning training...')
model.train(corpus_file=corpus_filepath, epochs=5, total_words=model.corpus_total_words, callbacks=[callback])
输出不正确(缺少批量打印输出):
Making vocabulary...
Beginning training...
epoch end
epoch end
epoch end
epoch end
epoch end
我做错了什么?
【问题讨论】:
标签: python machine-learning gensim word2vec