Edamame
Edamame

Reputation: 25366

Keras: export the loss and accuracy as an array for plotting

I am using the following code to train a model in Keras:

model_A.fit(train_X, train_Y, epochs=20)

The code works fine and the outputs are like below:

Epoch 1/20
1800/1800 [==============================] - 0s 34us/step - loss: 0.2764 - acc: 0.9033
Epoch 2/20
1800/1800 [==============================] - 0s 32us/step - loss: 0.2704 - acc: 0.9083
Epoch 3/20
1800/1800 [==============================] - 0s 32us/step - loss: 0.2687 - acc: 0.9094
Epoch 4/20
1800/1800 [==============================] - 0s 32us/step - loss: 0.2748 - acc: 0.9089
Epoch 5/20
1800/1800 [==============================] - 0s 32us/step - loss: 0.2902 - acc: 0.8922
Epoch 6/20
1800/1800 [==============================] - 0s 32us/step - loss: 0.2357 - acc: 0.9183
Epoch 7/20
1800/1800 [==============================] - 0s 32us/step - loss: 0.2499 - acc: 0.9183
Epoch 8/20
1800/1800 [==============================] - 0s 33us/step - loss: 0.2286 - acc: 0.9228
Epoch 9/20
1800/1800 [==============================] - 0s 32us/step - loss: 0.2325 - acc: 0.9194
Epoch 10/20
1800/1800 [==============================] - 0s 33us/step - loss: 0.2053 - acc: 0.9261
Epoch 11/20
1800/1800 [==============================] - 0s 33us/step - loss: 0.2256 - acc: 0.9161
Epoch 12/20
1800/1800 [==============================] - 0s 33us/step - loss: 0.2120 - acc: 0.9261
Epoch 13/20
1800/1800 [==============================] - 0s 32us/step - loss: 0.2085 - acc: 0.9328
Epoch 14/20
1800/1800 [==============================] - 0s 32us/step - loss: 0.1881 - acc: 0.9328
Epoch 15/20
1800/1800 [==============================] - 0s 31us/step - loss: 0.1835 - acc: 0.9344
Epoch 16/20
1800/1800 [==============================] - 0s 34us/step - loss: 0.1812 - acc: 0.9356
Epoch 17/20
1800/1800 [==============================] - 0s 32us/step - loss: 0.1704 - acc: 0.9361
Epoch 18/20
1800/1800 [==============================] - 0s 32us/step - loss: 0.1929 - acc: 0.9272
Epoch 19/20
1800/1800 [==============================] - 0s 32us/step - loss: 0.1822 - acc: 0.9317
Epoch 20/20
1800/1800 [==============================] - 0s 32us/step - loss: 0.1713 - acc: 0.9417

I am wondering if there is a way to save the loss/accuracy values in an array, so I could plot them over epochs later.

Upvotes: 0

Views: 1370

Answers (1)

today
today

Reputation: 33410

The fit method returns a History object which contains information about the training process. For example:

# train the model
h = model.fit(...)

# loss values at the end of each epoch
h.history['loss']

# validation loss values per epoch (if you have used validation data)
h.history['val_loss']

# accuracy values at the end of each epoch (if you have used `acc` metric)
h.history['acc']

# validation accuracy values per epoch (if you have used `acc` metric and validation data)
h.history['val_acc']

# list of epochs number
h.epoch

Further, it's not necessary to store the History object in a variable (like h = model.fit(...)) because it could also be accessed using model.history.history (however, note that this history attribute would not be persisted when model is saved using model.save(...)).

Upvotes: 2

Related Questions