Keras: how to record validation loss
<p>Note: this is a <a href="http://stackoverflow.com/questions/37664783/how-to-plot-a-learning-curve-for-a-keras-experiment">duplicate question</a>, but I'm not looking for the answer. Rather, how better to find the answer myself. </p>

<p>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.</p>

<p>I know that the <a href="https://keras.io/callbacks/" rel="nofollow">callback</a> 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.</p>

<p>As detailed in the doc <a href="https://keras.io/models/sequential/#fit" rel="nofollow">https://keras.io/models/sequential/#fit</a>, when you call <code>model.fit</code>, it returns a <code>callbacks.History</code> object. You can get loss and other metrics from it:</p>

<pre><code>...
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()
</code></pre>

python keras