TensorBoard distributions and histograms with Keras and fit_generator

I use Keras to train CNN using the fit_generator function.

It seems like a known issue is that TensorBoard does not show histograms and distributions in this setting.

Has anyone figured out how to make it work?

+17
python tensorflow keras tensorboard
source share
2 answers

There is no easy way to connect it with a single line of code, you have to write your resumes manually.

The good news is that it is not complicated, and you can use the TensorBoard callback code in Keras as a link: https://github.com/fchollet/keras/blob/master/keras/callbacks.py#L537

Essentially write a function like write_summaries(model) and call it whenever you want to write your resume (like right after your fit_generator() )

Inside your write_summaries(model) uses the tf.summary function, histogram_summary function, and other resulting functions to log the data you want to see on tensorboard.

If you don’t know exactly how to do this, check out the official tutorial: https://www.tensorflow.org/get_started/summaries_and_tensorboard and this great MNIST example with summaries: https://github.com/tensorflow/tensorflow/blob/master /tensorflow/examples/tutorials/mnist/mnist_with_summaries.py

+10
source share

I believe the explanation of bartgras is superseded by later versions of Keras (I am using Keras 2.2.2). To get the histograms in the Tensorboard, all I did was the following (where bg is the data processing class that the generator provides for gb.training_batch() ; gb.validation_batch() however is NOT a generator):

 NAME = "Foo_{}".format(datetime.now().isoformat(timespec='seconds')).replace(':', '-') tensorboard = keras.callbacks.TensorBoard( log_dir="logs/{}".format(NAME), histogram_freq=1, write_images=True) callbacks = [ tensorboard ] history = model.fit_generator( bg.training_batch(), validation_data=bg.validation_batch(), epochs=EPOCHS, steps_per_epoch=bg.steps_per_epoch, validation_steps=bg.validation_steps, verbose=1, shuffle=False, callbacks=callbacks) 
+3
source share

All Articles