How to save epochs progress to log file?

9,011 views
Skip to first unread message

Lior Magen

unread,
Jan 1, 2017, 6:05:51 AM1/1/17
to Keras-users
I'm using Keras for classification task and now want to deploy it in production. Since now when I was training I simply used verbose=1 or other value but I don't want the progress to be printed, I want it to be saved as logs. I'm using 'logging' library on Python and want the progress to be saved there.
I believe that I should use some Callback and tried using:

self.classifier.fit(x_tr, y_tr, batch_size=self.batch_size, nb_epoch=self.nb_epoch,
                    verbose
=self.print_status, validation_data=(x_te, y_te), callbacks=[keras.callbacks.ProgbarLogger()])

It doesn't change anything and I was looking at Keras documentation but still no clue. 

Can anyone help me with how to use this callback properly please?

jeffer...@gmail.com

unread,
Jan 2, 2017, 9:10:41 AM1/2/17
to Keras-users
You can remove the `calback` argument altogether because the `History` callback "is applied automatically to every Keras model" (per https://keras.io/callbacks/). What I've done is just something like this:

import cPickle as pickle

# ...

results
= model.fit(...) # results.history is dict of loss, acc, val_loss, and val_acc

# ...
with open('results.pickle', 'wb') as f:
    pickle
.dump(results.history, f)

Lior Magen

unread,
Jan 2, 2017, 9:12:15 AM1/2/17
to Keras-users, jeffer...@gmail.com
This will keep the results after the training is done, right? I want something to print the progress in real time. Any optional Callback for this task maybe?

Benjamin Striner

unread,
Jan 17, 2017, 2:18:31 AM1/17/17
to Keras-users, jeffer...@gmail.com
Hi Lior,

Here is the callback that I use. Haven't PRed it in to keras yet. Just use model.fit(...,callbacks=[LoggingCallback(logging.info)])

Cheers,
Ben

class LoggingCallback(Callback):
"""Callback that logs message at end of epoch.
"""

def __init__(self, print_fcn=print):
Callback.__init__(self)
self.print_fcn = print_fcn

def on_epoch_end(self, epoch, logs={}):
msg = "Epoch: %i, %s" % (epoch, ", ".join("%s: %f" % (k, v) for k, v in logs.iteritems()))
self.print_fcn(msg)

Benjamin Striner

unread,
Jan 17, 2017, 2:19:47 AM1/17/17
to Keras-users, jeffer...@gmail.com
BTW, for saving results after training, I like to use pandas. Write all of the training history straight to CSV in a one-liner.

history= model.fit(...)
pandas.DataFrame(history.history).to_csv("history.csv")

Benjamin Striner

unread,
Jan 17, 2017, 3:42:49 AM1/17/17
to Keras-users, jeffer...@gmail.com
Just made a PR with the callback.

model.fit(...,callbacks=[LoggingCallback(logging.info)])

If you want to pull it in locally for now:
git remote add bstriner https://github.com/bstriner/keras.git
git fetch bstriner
git merge bstriner/logging_callback

Klemen Grm

unread,
Jan 17, 2017, 3:52:26 AM1/17/17
to Keras-users, jeffer...@gmail.com
You can also use the built-in CSVLogger callback.

https://keras.io/callbacks/#csvlogger

jeffer...@gmail.com

unread,
Jan 17, 2017, 7:58:34 AM1/17/17
to Keras-users, jeffer...@gmail.com
Yes, `keras.callback.CSVLogger` is the way to go. I hadn't stumbled across that until after my original reply. :-)

Lior Magen

unread,
Jan 23, 2017, 10:34:15 AM1/23/17
to Keras-users, jeffer...@gmail.com
Thank you so much for your help, this is exactly what I needed.

In the end Iv'e used your code with minor changes (really minor, just so it will work with Python 3.*)

class LoggingCallback(Callback):
   
"""Callback that logs message at end of epoch.
    """



   
def __init__(self, print_fcn=print):
       
Callback.__init__(self)
       
self.print_fcn = print_fcn


   
def on_epoch_end(self, epoch, logs={}):

        msg
= "{Epoch: %i} %s" % (epoch, ", ".join("%s: %f" % (k, v) for k, v in logs.items()))
       
self.print_fcn(msg)


Again, I really appreciate your help. 
Reply all
Reply to author
Forward
0 new messages