Search code examples
kerastensorboard

Keras: use Tensorboard with train_on_batch()


For the keras functions fit() and fit_generator() there is the possibility of tensorboard visualization by passing a keras.callbacks.TensorBoard object to the functions. For the train_on_batch() function there obviously are no callback available. Are there other options in keras to create a Tensorboard in this case?


Solution

  • I think that currently, the only option is to use TensorFlow code. In this stackoverflow answer I found a way to create a TensorBoard log manually.
    Thus a code sample with the Keras train_on_batch() could look like this:

    # before training init writer (for tensorboard log) / model
    writer = tf.summary.FileWriter(...)
    model = ...
    
    # train model
    loss = model.train_on_batch(...)
    summary = tf.Summary(value=[tf.Summary.Value(tag="loss", 
                                                 simple_value=value), ])
    writer.add_summary(summary)
    

    Note: For this example in TensorBoard you have to choose Horizontal Axis "RELATIVE" as no step is passed to the summary.