gensim word2vec 打印日志丢失 [英] gensim word2vec print log loss
问题描述
在使用 gensim word2vec 模型时,如何打印以记录(文件或粗壮)训练阶段每个时期的损失.
how to print to log (file or stout) the loss of each epoch in the training phase, when using gensim word2vec model.
我试过了:
logging.basicConfig(format='%(asctime)s: %(levelname)s: %(message)s')
logging.root.setLevel(level=logging.INFO)
但我没有看到任何损失打印.
But I didn't saw any loss printing.
推荐答案
您可以使用 get_latest_training_loss()
方法获取 word2vec 模型的最新训练损失.如果您想在每个 epoch 之后打印损失,您可以添加一个回调来执行此操作.例如:
You can get the latest training loss of a word2vec model with the method get_latest_training_loss()
. If you want to print the loss after every epoch you can add a callback that does this. For example:
from gensim.test.utils import common_texts, get_tmpfile
from gensim.models import Word2Vec
from gensim.models.callbacks import CallbackAny2Vec
class callback(CallbackAny2Vec):
'''Callback to print loss after each epoch.'''
def __init__(self):
self.epoch = 0
def on_epoch_end(self, model):
loss = model.get_latest_training_loss()
print('Loss after epoch {}: {}'.format(self.epoch, loss))
self.epoch += 1
model = Word2Vec(common_texts, size=100, window=5, min_count=1,
compute_loss=True, callbacks=[callback()])
然而,损失是以累积方式计算的(即,在每个 epoch 之后打印的损失是到目前为止所有 epoch 的总损失).请参阅gojomo 在此处的回答了解更多说明.
However, the loss is computed in a cumulative way (i.e. the loss that gets printed after each epoch is the total loss of all epochs so far). See gojomo's answer here for more explanation.
这篇关于gensim word2vec 打印日志丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!