0

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.

today
  • 32,602
  • 8
  • 95
  • 115
Edamame
  • 23,718
  • 73
  • 186
  • 320
  • 2
    Possible duplicate of [How to return history of validation loss in Keras](https://stackoverflow.com/questions/36952763/how-to-return-history-of-validation-loss-in-keras) – c2huc2hu Aug 15 '19 at 19:48
  • If the answer resolved your issue, kindly *accept* it by clicking on the checkmark (✔) next to the answer to mark it as "answered" - see [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – today Apr 25 '20 at 05:37

1 Answers1

2

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(...)).

today
  • 32,602
  • 8
  • 95
  • 115