1
votes

I am training a model with keras for one epoch only:

history = model.fit([x], y,
                    validation_split=0.2, epochs=1, batch_size=2)
print(history.history['accuracy'])

The history now obviously only contains one value from the end of the epoch. How can I evaluate the training accuracy or loss during the epoch? I expect these to be the values that are shown in the console during training.

To be clear: I want a history to be written after every batch (not after every epoch, as per usual).

1
I think you can write your custom callback then every batch it print out the accuracy.Đinh Anh Vũ
You rightly expect. What causes you to disappoint?M.Innat
Please see my edit. I am interested in history after every batch, not every epochChristian

1 Answers

1
votes

I assume you want to save the accuracy and loss at the end of each batch. To do that you need to create a custom callback as shown below

class BCP(keras.callbacks.Callback):
    batch_accuracy = [] # accuracy at given batch
    batch_loss = [] # loss at given batch    
    def __init__(self):
        super(BCP,self).__init__() 
    def on_train_batch_end(self, batch, logs=None):                
        BCP.batch_accuracy.append(logs.get('accuracy'))
        BCP.batch_loss.append(logs.get('loss'))

now in model.fit include

callbacks = [BCP()]

now train for 1 epoch. at the end of the epoch the values of the accuracy and loss for each batch is stored in BCP.batch_accuracy and BCP.batch_loss. You can print them out as follows:

print('{0:^4s}{1:^22s}{2:^10s}'.format('Batch', 'Loss', 'Accuracy'))
for i in range (len(BCP.batch_accuracy)):
    print('{0:^4s}{1:15.5f}{2:15.2f}'.format(str(i), BCP.batch_loss[i], BCP.batch_accuracy[i]* 100))