AliY
AliY

Reputation: 567

Tensorboard smoothing

I downloaded the CSV files from tesnorboard in order to plot the losses myself as I want them Smoothed.

This is currently my code:

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()

I was reading how to smooth the graph and I found out another member here wrote the code on how tensorboard actually smooths graphs, but I really don't know how to implement it in my code.

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

Thank you.

Upvotes: 4

Views: 4156

Answers (1)

n1colas.m
n1colas.m

Reputation: 3989

If you are working with pandas library you can use the function ewm (Pandas EWM) and ajust the alpha factor to get a good approximation of the smooth function from tensorboard.

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

CSV file 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()

enter image description here

Upvotes: 13

Related Questions