Keras:如何记录验证损失 [英] Keras: how to record validation loss

查看:468
本文介绍了Keras:如何记录验证损失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:这是一个重复问题,但我不在寻找答案.相反,如何更好地自己找到答案.

Note: this is a duplicate question, but I'm not looking for the answer. Rather, how better to find the answer myself.

如何跨时期记录模型中的损失,训练准确性,测试损失和测试准确性?我想绘制一个图表,显示每个时期的验证损失.

How do I record loss, training accuracy, testing loss and testing accuracy, from a model, across epochs? I'd like to plot a graph that shows validation loss against each epoch.

我知道 callback 对象,可以在fit()中调用它,也可以在模型中调用它.历史与它有关,但是检查源代码和文档字符串对我来说只是一堵代码墙.例如,Numpy通常提供非常小的用例作为非常简单的实现的示例.但是我知道,答案只是单线,因为这实际上只是输入问题.

I know that the callback object, which can be called in fit(), or maybe model.history has something to do with it, but examining the source and docstrings are just a wall of code to me. Numpy, for instance, typically provides a very small use case as an example of very simple implementation. And yet I know that the answer to this is just a one-liner, because this really is just a question of input.

推荐答案

如文档 https中所详细说明: //keras.io/models/sequential/#fit ,当您调用model.fit时,它将返回一个callbacks.History对象.您可以从中获取损失和其他指标:

As detailed in the doc https://keras.io/models/sequential/#fit, when you call model.fit, it returns a callbacks.History object. You can get loss and other metrics from it:

...
train_history = model.fit(X_train, Y_train,
                    batch_size=batch_size, nb_epoch=nb_epoch,
                    verbose=1, validation_data=(X_test, Y_test))
loss = train_history.history['loss']
val_loss = train_history.history['val_loss']
plt.plot(loss)
plt.plot(val_loss)
plt.legend(['loss', 'val_loss'])
plt.show()

这篇关于Keras:如何记录验证损失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆