【问题标题】:Tensorboard histograms to matplotlib张量板直方图到 matplotlib
【发布时间】:2018-07-10 21:39:29
【问题描述】:

我想“转储”张量板直方图并通过 matplotlib 绘制它们。我会有更多吸引科学论文的情节。

我设法使用tf.train.summary_iterator 破解了摘要文件的方法,并转储了我想要转储的直方图(tensorflow.core.framework.summary_pb2.HistogramProto 对象)。 通过这样做并实现 java 脚本代码对数据 (https://github.com/tensorflow/tensorboard/blob/c2fe054231fe77f3a5b05dbc519f713d2e738d1c/tensorboard/plugins/histogram/tf_histogram_dashboard/histogramCore.ts#L104) 所做的事情,我设法得到了与张量板图相似的东西(相同的趋势),但不是完全相同的图。

我能解释一下吗?

谢谢

【问题讨论】:

    标签: tensorflow tensorboard


    【解决方案1】:

    为了使用 matplotlib 绘制张量板直方图,我正在执行以下操作:

    event_acc = EventAccumulator(path, size_guidance={
        'histograms': STEP_COUNT,
    })
    event_acc.Reload()
    tags = event_acc.Tags()
    result = {}
    for hist in tags['histograms']:
        histograms = event_acc.Histograms(hist)
        result[hist] = np.array([np.repeat(np.array(h.histogram_value.bucket_limit), np.array(h.histogram_value.bucket).astype(np.int)) for h in histograms])
    return result
    

    h.histogram_value.bucket_limit 给我这个值,h.histogram_value.bucket 给我这个值的计数。因此,当我相应地重复这些值时(np.repeat(...)),我得到了一个巨大的预期大小数组。现在可以使用默认的 matplotlib 逻辑绘制这个数组。

    【讨论】:

    • 这是最好的解决方案,同时 tf 支持在 csv 下载它。
    【解决方案2】:

    最好的解决方案是加载所有事件并重建所有直方图(作为@khuesmann 的答案),但不使用EventAccumulator,而是使用EventFileLoader。这将为您提供每壁时间和步长的直方图,作为 Tensorboard 绘图。它可以扩展为按时间步长和墙上时间返回动作列表。

    别忘了检查你将使用哪个标签。

    from tensorboard.backend.event_processing.event_file_loader import EventFileLoader
    # Just in case, PATH_OF_FILE is the path of the file, not the folder
    loader = EventFileLoader(PATH_Of_FILE)
    
    # Where to store values
    wtimes,steps,actions = [],[],[]
    for event in loader.Load():
        wtime   = event.wall_time
        step    = event.step
        if len(event.summary.value) > 0:
            summary = event.summary.value[0]
            if summary.tag == HISTOGRAM_TAG:
                wtimes += [wtime]*int(summary.histo.num)
                steps  += [step] *int(summary.histo.num)
    
                for num,val in zip(summary.histo.bucket,summary.histo.bucket_limit):
                    actions += [val] *int(num)
    

    请记住,tensorflow 近似于动作并将动作视为连续变量,因此即使您有离散动作(例如 0,1,3),您最终也会将动作视为 0.2,0.4,0.9,1.4 ...在这种情况下,值会做到这一点。

    【讨论】:

    • 我想,这些应该是答案...比第一个回复更好
    【解决方案3】:

    @khuesmann 提供的一个很好的解决方案,但这只允许您检索累积的直方图,而不是每一步的直方图 - 这是实际显示在 tensorboard 中的直方图。

    如果你想要分布,到目前为止,我的理解是 Tensorboard 通常会压缩直方图以减少用于存储数据的内存——想象一下存储超过 400 万步的 2D 直方图,内存可以快速增加.通过这样做可以访问这些压缩直方图:

    from tensorboard.backend.event_processing.event_accumulator import EventAccumulator
    
    n2n = EventAccumulator(PATH)
    n2n.Reload()
    
    # Check the tags under histograms and choose the one you want
    n2n.Tags()
    
    # This will give you the list used by tensorboard 
    # of the compress histograms by timestep and wall time
    n2n.CompressedHistograms(HISTOGRAM_TAG)
    

    唯一的问题是它将直方图压缩到五个百分位数(在Basic points 中,它们是 0、668、1587、3085、5000、6915、8413、9332、10000),对应于 (-Inf, -1.5, -1, -0.5, 0, 0.5, 1, 1.5, Inf) 的标准差。检查代码here

    我读的不多,但重建 tensorboard 显示的时间直方图并不难。如果我有办法做到这一点,我会在这里发布。

    【讨论】:

      【解决方案4】:

      最简单的方法是使用tbparse 解析事件并使用seaborn kde_ridgeplot 绘制直方图。

      This tutorial 用大约 30 行 Python 代码生成堆积分布图:

      • 张量板预览:

      • 由 tbparse 解析并由 seaborn 绘制:

      如果您在解析过程中遇到任何问题,可以打开issue。 (我是tbparse的作者)

      【讨论】:

        猜你喜欢
        • 2016-07-25
        • 1970-01-01
        • 2020-12-10
        • 2017-09-01
        • 2019-01-10
        • 2019-03-19
        • 2011-07-16
        • 2017-07-08
        • 1970-01-01
        相关资源
        最近更新 更多