如何将Keras损失输出记录到文件中 [英] How to log Keras loss output to a file
问题描述
运行Keras神经网络模型时,您可能会在控制台中看到以下内容:
When you run a Keras neural network model you might see something like this in the console:
Epoch 1/3
6/1000 [..............................] - ETA: 7994s - loss: 5111.7661
随着时间的流逝,损失有望改善.我想将这些损失随时间记录到文件中,以便我可以从中学习.我尝试过:
As time goes on the loss hopefully improves. I want to log these losses to a file over time so that I can learn from them. I have tried:
logging.basicConfig(filename='example.log', filemode='w', level=logging.DEBUG)
但这是行不通的.我不确定在这种情况下我需要什么级别的日志记录.
but this doesn't work. I am not sure what level of logging I need in this situation.
我也尝试过使用如下回调:
I have also tried using a callback like in:
def generate_train_batch():
while 1:
for i in xrange(0,dset_X.shape[0],3):
yield dset_X[i:i+3,:,:,:],dset_y[i:i+3,:,:]
class LossHistory(keras.callbacks.Callback):
def on_train_begin(self, logs={}):
self.losses = []
def on_batch_end(self, batch, logs={}):
self.losses.append(logs.get('loss'))
logloss=LossHistory()
colorize.fit_generator(generate_train_batch(),samples_per_epoch=1000,nb_epoch=3,callbacks=['logloss'])
,但是显然这不是在写文件.无论采用哪种方法,通过回调或日志记录模块或其他任何方法,我都想听听您的解决方案,用于将keras神经网络丢失记录到文件中.谢谢!
but obviously this isn't writing to a file. Whatever the method, through a callback or the logging module or anything else, I would love to hear your solutions for logging loss of a keras neural network to a file. Thanks!
推荐答案
有一个简单的解决方案可以解决您的问题.每次使用任何fit
方法-结果都会返回称为 History Callback 的特殊回调.它具有字段history
,该字段是每个时期之后注册的所有度量的字典.因此,要在每个时期之后轻松获得损失函数值的列表,您可以轻松地做到:
There is a simple solution to your problem. Every time any of the fit
methods are used - as a result the special callback called History Callback is returned. It has a field history
which is a dictionary of all metrics registered after every epoch. So to get list of loss function values after every epoch you can easly do:
history_callback = model.fit(params...)
loss_history = history_callback.history["loss"]
将此类列表保存到文件很容易(例如,将其转换为numpy
数组并使用savetxt
方法).
It's easy to save such list to a file (e.g. by converting it to numpy
array and using savetxt
method).
更新:
尝试:
import numpy
numpy_loss_history = numpy.array(loss_history)
numpy.savetxt("loss_history.txt", numpy_loss_history, delimiter=",")
更新2:
在 Keras回调文档在创建回调段落中.
这篇关于如何将Keras损失输出记录到文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!