使用 python 将tensorboard(使用pytorch)数据导出到csv

Adn*_*Ali 3 python tensorboard pytorch

我有 Tensorboard 数据并希望它下载数据背后的所有 csv 文件,但我无法从官方文档中找到任何内容。从StackOverflow中,我只发现了这个 7 年前的问题,而且它与我使用 PyTorch 时的 TensorFlow 相关。

我们可以手动执行此操作,正如我们在屏幕截图中看到的,手动有一个选项。我想知道我们是否可以通过代码来做到这一点,或者这是不可能的?因为我有很多数据要处理。

在此输入图像描述

Adn*_*Ali 6

借助此脚本,下面是最短的工作代码,它可以获取所有数据,然后dataframe您可以进一步玩。

import traceback
import pandas as pd
from tensorboard.backend.event_processing.event_accumulator import EventAccumulator

# Extraction function
def tflog2pandas(path):
    runlog_data = pd.DataFrame({"metric": [], "value": [], "step": []})
    try:
        event_acc = EventAccumulator(path)
        event_acc.Reload()
        tags = event_acc.Tags()["scalars"]
        for tag in tags:
            event_list = event_acc.Scalars(tag)
            values = list(map(lambda x: x.value, event_list))
            step = list(map(lambda x: x.step, event_list))
            r = {"metric": [tag] * len(step), "value": values, "step": step}
            r = pd.DataFrame(r)
            runlog_data = pd.concat([runlog_data, r])
    # Dirty catch of DataLossError
    except Exception:
        print("Event file possibly corrupt: {}".format(path))
        traceback.print_exc()
    return runlog_data
path="Run1" #folderpath
df=tflog2pandas(path)
#df=df[(df.metric != 'params/lr')&(df.metric != 'params/mm')&(df.metric != 'train/loss')] #delete the mentioned rows
df.to_csv("output.csv")
Run Code Online (Sandbox Code Playgroud)

  • 请注意,“EventAccumulator”默认限制内存中存储的事件数量。因此,您的“event_list”可能不包含文件中的每个事件。将 `size_guidance={"scalars": 0}` 参数传递给构造函数以解除标量数据的限制。 (2认同)