【问题标题】:Get CSV from Tensorflow summaries从 Tensorflow 摘要中获取 CSV
【发布时间】:2017-11-05 06:21:12
【问题描述】:

我有一些非常大的 tensorflow 摘要。如果这些是使用 tensorboard 绘制的,我可以从中下载 CSV 文件。

但是,使用 tensorboard 绘制这些图需要很长时间。我在the docs 中发现有一种方法可以直接在 Python 中读取摘要。这个方法是summary_iterator,可以这样使用:

import tensorflow as tf

for e in tf.train.summary_iterator(path to events file):
    print(e)

我可以使用这种方法直接创建 CSV 文件吗?如果是这样,我该怎么做?这样可以节省很多时间。

【问题讨论】:

    标签: python csv tensorflow


    【解决方案1】:

    一种可能的方法是这样的:

    from tensorboard.backend.event_processing import event_accumulator      
    import numpy as np
    import pandas as pd
    import sys
    
    def create_csv(inpath, outpath):
        sg = {event_accumulator.COMPRESSED_HISTOGRAMS: 1,
              event_accumulator.IMAGES: 1,
              event_accumulator.AUDIO: 1,
              event_accumulator.SCALARS: 0,
              event_accumulator.HISTOGRAMS: 1}
        ea = event_accumulator.EventAccumulator(inpath, size_guidance=sg)
        ea.Reload()
        scalar_tags = ea.Tags()['scalars']
        df = pd.DataFrame(columns=scalar_tags)
        for tag in scalar_tags:
            events = ea.Scalars(tag)
            scalars = np.array(map(lambda x: x.value, events))
            df.loc[:, tag] = scalars
        df.to_csv(outpath)
    
    if __name__ == '__main__':
        args = sys.argv
        inpath = args[1]
        outpath = args[2]
        create_csv(inpath, outpath)
    

    请注意,此代码会将整个事件文件加载到内存中,因此最好在集群上运行。有关EventAccumulatorsg 参数的信息,请参阅this SO question

    另一个改进可能是不仅存储每个标量的value,还存储step

    注意代码 sn-p 已针对 TF 的最新版本进行了更新。 对于 TF ,请改用以下导入:

    from tensorflow.tensorboard.backend.event_processing import event_accumulator as eva
    

    【讨论】:

    • 好主意!顺便说一句,从 TF 1.1 开始,包是 tensorboard.backend.event_processing import event_accumulator 。我冒昧地更新了您的代码
    猜你喜欢
    • 2021-12-16
    • 2018-08-02
    • 2013-12-23
    • 2021-03-04
    • 2020-09-16
    • 2014-09-08
    • 2018-02-26
    • 2018-07-26
    • 1970-01-01
    相关资源
    最近更新 更多