【问题标题】:How do you read Tensorboard files programmatically?你如何以编程方式读取 Tensorboard 文件?
【发布时间】:2017-04-25 18:35:17
【问题描述】:

如何在不启动 GUI tensorboard --logdir=... 的情况下编写 Python 脚本来读取 Tensorboard 日志文件、提取损失和准确率等数值数据?

【问题讨论】:

    标签: python machine-learning tensorflow tensorboard


    【解决方案1】:

    您可以使用 TensorBoard 的 Python 类或脚本来提取数据:

    How can I export data from TensorBoard?

    如果您想导出数据以在其他地方进行可视化(例如 iPython Notebook),这也是可能的。您可以直接依赖 TensorBoard 用于加载数据的底层类:python/summary/event_accumulator.py(用于从单次运行中加载数据)或python/summary/event_multiplexer.py(用于从多次运行中加载数据,并使其保持井井有条)。这些类加载事件文件组,丢弃因 TensorFlow 崩溃而“孤立”的数据,并按标签组织数据。

    作为另一种选择,有一个脚本 (tensorboard/scripts/serialize_tensorboard.py) 可以像 TensorBoard 一样加载 logdir,但将所有数据以 json 格式写入磁盘,而不是启动服务器。该脚本设置为制作“假 TensorBoard 后端”以进行测试,所以它的边缘有点粗糙。

    使用EventAccumulator

    # In [1]: from tensorflow.python.summary import event_accumulator  # deprecated
    In [1]: from tensorboard.backend.event_processing import event_accumulator
    
    In [2]: ea = event_accumulator.EventAccumulator('events.out.tfevents.x.ip-x-x-x-x',
       ...:  size_guidance={ # see below regarding this argument
       ...:      event_accumulator.COMPRESSED_HISTOGRAMS: 500,
       ...:      event_accumulator.IMAGES: 4,
       ...:      event_accumulator.AUDIO: 4,
       ...:      event_accumulator.SCALARS: 0,
       ...:      event_accumulator.HISTOGRAMS: 1,
       ...:  })
    
    In [3]: ea.Reload() # loads events from file
    Out[3]: <tensorflow.python.summary.event_accumulator.EventAccumulator at 0x7fdbe5ff59e8>
    
    In [4]: ea.Tags()
    Out[4]: 
    {'audio': [],
     'compressedHistograms': [],
     'graph': True,
     'histograms': [],
     'images': [],
     'run_metadata': [],
     'scalars': ['Loss', 'Epsilon', 'Learning_rate']}
    
    In [5]: ea.Scalars('Loss')
    Out[5]: 
    [ScalarEvent(wall_time=1481232633.080754, step=1, value=1.6365480422973633),
     ScalarEvent(wall_time=1481232633.2001867, step=2, value=1.2162202596664429),
     ScalarEvent(wall_time=1481232633.3877788, step=3, value=1.4660096168518066),
     ScalarEvent(wall_time=1481232633.5749283, step=4, value=1.2405034303665161),
     ScalarEvent(wall_time=1481232633.7419815, step=5, value=0.897326648235321),
     ...]
    

    size_guidance:

    size_guidance: Information on how much data the EventAccumulator should
      store in memory. The DEFAULT_SIZE_GUIDANCE tries not to store too much
      so as to avoid OOMing the client. The size_guidance should be a map
      from a `tagType` string to an integer representing the number of
      items to keep per tag for items of that `tagType`. If the size is 0,
      all events are stored.
    

    【讨论】:

    • 您能否提供一个工作示例,说明在这些脚本中究竟要使用什么?
    • 从 Tensorflow 1.1.0 版本开始,event_accumulator 已移至 tensorflow/tensorflow/tensorboard/backend/event_processing。要使代码在 1.1.0 版中工作,导入语句应为 from tensorflow.tensorboard.backend.event_processing import event_accumulator more info here
    • 从 1.3 开始,它已从 TensorFlow 存储库移至专用的 TensorBoard 存储库。新家:github.com/tensorflow/tensorboard。可以作为独立包进行 pip 安装,导入语句现在为:from tensorboard.backend.event_processing import event_accumulator
    • 似乎 API 已更改,并且自 TensorFlow 1.1+ 以来无法使用。
    • 仅供参考:我创建了一个工具,可以聚合多个 tensorboard 摘要并将结果保存到新的 tensorboard 摘要或.csv 文件中。看看这里:github.com/Spenhouet/tensorboard-aggregator
    【解决方案2】:

    要完成 user1501961 的回答,您可以使用 pandas pd.DataFrame(ea.Scalars('Loss)).to_csv('Loss.csv') 轻松将标量列表导出到 csv 文件

    【讨论】:

      猜你喜欢
      • 2021-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-24
      • 2020-01-12
      • 1970-01-01
      • 1970-01-01
      • 2011-08-22
      相关资源
      最近更新 更多