如何将 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
,它是每个 epoch 之后注册的所有指标的字典.因此,要在每个时期之后获取损失函数值列表,您可以轻松做到:
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 Callbacks DocumentationKeras Callbacks Documentation 在 创建回调 段落中.
这篇关于如何将 Keras 丢失输出记录到文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!