【问题标题】:How do I add text summary to Tensorboard on Keras?如何在 Keras 上向 Tensorboard 添加文本摘要?
【发布时间】:2019-02-26 10:17:43
【问题描述】:

在使用 Keras 时如何将文本摘要添加到 Tensorboard?

我已经设置了 Tensorboard 回调,但我不知道如何添加文本摘要。

例如,我想将运行中使用的不同参数的文本摘要添加到 Tensorboard 中,以便在我重新访问运行日志时不会丢失。

一个选项似乎是将所有参数详细信息包含到日志文件目录名称中,但这看起来很累。

我怎样才能更好地解决这个问题?

【问题讨论】:

    标签: tensorflow keras tensorboard


    【解决方案1】:

    对于寻找这个的其他人,我最终编写了一个自定义回调扩展 Keras Tensorboard。稍后我可能会记录更多内容,我们可以扩展它以记录更多关于不同事件的内容。我从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)
    

    创建此 Tensorboard 的实例并添加到 model.fit 回调,您的 settings_str_to_log 将显示在 Tensorboard 的文本选项卡上。

    【讨论】:

    • 在 TF2 中,最后一行错误为 Exception has occurred: AttributeError 'LoggingTensorBoard' object has no attribute 'writer'。你碰巧知道如何在 TF2 中做到这一点吗?
    • 见下文。我无法在评论中格式化代码。
    【解决方案2】:

    我没有查看他们是否删除或移动了默认编写器。但是您可以创建自己的编写器,它将在 Tensorboard 中显示为文本摘要。由于 TF2.0 默认为 Eager 模式,因此无需 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)
    
    

    【讨论】:

    • 是的,这行得通。我现在发现 TF2 使用了两个可以劫持的编写器:_writers['train']_writer['validation']。使用_get_writer()_train_run_name 等。
    【解决方案3】:

    这适用于 tf 2.1 2.2 和 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)
    

    【讨论】:

      猜你喜欢
      • 2018-03-27
      • 2019-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-12
      • 1970-01-01
      • 1970-01-01
      • 2018-02-26
      相关资源
      最近更新 更多