How to log Keras loss output to a file

You can use CSVLogger callback.

as example:

from keras.callbacks import CSVLogger

csv_logger = CSVLogger('log.csv', append=True, separator=';')
model.fit(X_train, Y_train, callbacks=[csv_logger])

Look at: Keras Callbacks


There is a simple solution to your problem. Every time any of the fit methods are used - as a result the special callback called History Callback is returned. It has a field history which is a dictionary of all metrics registered after every epoch. So to get list of loss function values after every epoch you can easly do:

history_callback = model.fit(params...)
loss_history = history_callback.history["loss"]

It's easy to save such list to a file (e.g. by converting it to numpy array and using savetxt method).

UPDATE:

Try:

import numpy
numpy_loss_history = numpy.array(loss_history)
numpy.savetxt("loss_history.txt", numpy_loss_history, delimiter=",")

UPDATE 2:

The solution to the problem of recording a loss after every batch is written in Keras Callbacks Documentation in a Create a Callback paragraph.