Tensorboard平滑化

4

为了自己画出平滑的损失曲线,我从Tensorboard下载了CSV文件。

目前我的代码如下:

import pandas as pd

import numpy as np

import seaborn as sns

import matplotlib.pyplot as plt

df = pd.read_csv('C:\\Users\\ali97\\Desktop\\Project\\Database\\Comparing Outlier Fractions\\10 Percent (MAE)\\MSE Validation.csv',usecols=['Step','Value'],low_memory=True)

df2 = pd.read_csv('C:\\Users\\ali97\\Desktop\\Project\\Database\\Comparing Outlier Fractions\\15 Percent (MAE)\\MSE Validation.csv',usecols=['Step','Value'],low_memory=True)

df3 = pd.read_csv('C:\\Users\\ali97\\Desktop\\Project\\Database\\Comparing Outlier Fractions\\20 Percent (MAE)\\MSE Validation.csv',usecols=['Step','Value'],low_memory=True)




plt.plot(df['Step'],df['Value'] , 'r',label='10% Outlier Frac.' )
plt.plot(df2['Step'],df2['Value'] , 'g',label='15% Outlier Frac.' )
plt.plot(df3['Step'],df3['Value'] , 'b',label='20% Outlier Frac.' )

plt.xlabel('Epochs')
plt.ylabel('Validation score')
plt.show()

我在阅读有关如何平滑图表的内容时,发现这里的另一位成员编写了代码来展示tensorboard如何平滑图表,但我真的不知道如何在我的代码中实现它。

def smooth(scalars: List[float], weight: float) -> List[float]:  # Weight between 0 and 1
    last = scalars[0]  # First value in the plot (first timestep)
    smoothed = list()
    for point in scalars:
        smoothed_val = last * weight + (1 - weight) * point  # Calculate smoothed value
        smoothed.append(smoothed_val)                        # Save it
        last = smoothed_val                                  # Anchor the last smoothed value



    return smoothed

谢谢。

1个回答

13

如果你正在使用 pandas 库,你可以使用函数 ewm (Pandas EWM) 并调整 alpha 因子来获得与 TensorBoard 中平滑函数的良好近似。

df.ewm(alpha=(1 - ts_factor)).mean()

CSV文件mse_data.csv

           step      value
0      0.000000   9.716303
1      0.200401   9.753981
2      0.400802   9.724551
3      0.601202   7.926591
4      0.801603  10.181700
..          ...        ...
495   99.198400   0.298243
496   99.398800   0.314511
497   99.599200  -1.119387
498   99.799600  -0.374202
499  100.000000   1.150465
import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv("mse_data.csv")
print(df)

TSBOARD_SMOOTHING = [0.5, 0.85, 0.99]

smooth = []
for ts_factor in TSBOARD_SMOOTHING:
    smooth.append(df.ewm(alpha=(1 - ts_factor)).mean())

for ptx in range(3):
    plt.subplot(1,3,ptx+1)
    plt.plot(df["value"], alpha=0.4)
    plt.plot(smooth[ptx]["value"])
    plt.title("Tensorboard Smoothing = {}".format(TSBOARD_SMOOTHING[ptx]))
    plt.grid(alpha=0.3)

plt.show()

输入图像描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接