Reputation: 183
How do I add text summary to Tensorboard when using Keras?
I have setup a Tensorboard callback, but I am lost on how to add a text summary.
For example, I would like to add a text summary of different parameters used in the run into the Tensorboard so that for documentation and not lost when I revisit the run log.
One option seems to be to include all the parameter details to the logfile dir name, but that looks exhausting.
How can I better solve this problem?
Upvotes: 3
Views: 3079
Reputation: 11
This works on tf 2.1 2.2 and 2.3
class TensorBoardExtended(TensorBoard):
"""
Extended Tensorboard log that allows to add text
By default logs:
- host
- gpus available
Parameters
-------------
text_dict_to_log : dict
Dictionary with key, value string that will be logged with Tensorboard
kwargs : dict
All the other parameters that are fed to Tensorboard
"""
def __init__(self, text_dict_to_log=None, **kwargs):
super().__init__(**kwargs)
self.text_dict_to_log = text_dict_to_log
def on_train_begin(self, logs=None):
# pylint: disable= E1101
super().on_train_begin(logs=logs)
try:
writer = self._get_writer('train')
except AttributeError: # this is due to differences between tf21, 22 and 23
writer = self._train_writer
with writer.as_default():
for key, value in self.text_dict_to_log.items():
tf.summary.text(key, tf.convert_to_tensor(value), step=0)
Upvotes: 0
Reputation: 183
I have not looked if they removed or moved the default writer. But you can create your own writer and it will show up as a text summary in the Tensorboard. Since TF2.0 defaults to Eager mode, no need for session.
dir_name = os.path.join("your_log_dir", "Text Summary")
writer = tf.summary.create_file_writer(logdir=dir_name)
with writer.as_default():
tf.summary.text(name="Run_Settings", data= self.settings_str, step=0)
Upvotes: 1
Reputation: 183
For anyone else looking for this, I ended up writing a custom callback extending Keras Tensorboard. I will probably log more than summary later and we can extend this to log more stuff on different events. I simplified from another question - to add plot
from keras.callbacks import TensorBoard
import tensorflow as tf
class LoggingTensorBoard(TensorBoard):
def __init__(self, log_dir, settings_str_to_log, **kwargs):
super(LoggingTensorBoard, self).__init__(log_dir, **kwargs)
self.settings_str = settings_str_to_log
def on_train_begin(self, logs=None):
TensorBoard.on_train_begin(self, logs=logs)
tensor = tf.convert_to_tensor(self.settings_str)
summary = tf.summary.text ("Run Settings", tensor)
with tf.Session() as sess:
s = sess.run(summary)
self.writer.add_summary(s)
Create an instance of this Tensorboard and add to model.fit callback and your settings_str_to_log will show up on the Text Tab in Tensorboard.
Upvotes: 5