0%

Tensorboard的smooth效果

在TensorFlow的可视化工具Tensorboard中,有一个相当好用的选项:设置曲线的smooth参数。我们可以通过增大这个参数的设置,使得原本波动起伏很大的曲线变得平滑,从而得到更加清晰的变化趋势。

虽然Tensorboard提供了数据下载的接口(csv、json格式),但是只针对于原始数据,因此在进行绘图时有必要实现跟Tensorboard一样的平滑设置。参照Tensorboard中使用的smooth函数,编写数据处理脚本如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import pandas as pd
import numpy as np
import os
import matplotlib.pyplot as plt

def smooth(csv_path, weight=0.85):
data = pd.read_csv(filepath_or_buffer=csv_path, header=0, names=['Step','Value'], dtype={'Step':np.int, 'Value':np.float})
scalar = data['Value'].values
last = scalar[0]
smoothed = []
for point in scalar:
smoothed_val = last * weight + (1 - weight) * point
smoothed.append(smoothed_val)
last = smoothed_val

save = pd.DataFrame({'Step':data['Step'].values, 'Value':smoothed})
save.to_csv('smooth_' + csv_path)


def smooth_and_plot(csv_path, weight=0.85):
data = pd.read_csv(filepath_or_buffer=csv_path, header=0, names=['Step','Value'], dtype={'Step':np.int, 'Value':np.float})
scalar = data['Value'].values
last = scalar[0]
print(type(scalar))
smoothed = []
for point in scalar:
smoothed_val = last * weight + (1 - weight) * point
smoothed.append(smoothed_val)
last = smoothed_val

# save = pd.DataFrame({'Step':data['Step'].values, 'Value':smoothed})
# save.to_csv('smooth_' + csv_path)

steps = data['Step'].values
steps = steps.tolist()
origin = scalar.tolist()

fig = plt.figure(1)
plt.plot(steps, origin, label='origin')
plt.plot(steps, smoothed, label='smoothed')
# plt.ylim(0, 220) # Tensorboard中会滤除过大的数据,可通过设置坐标最值来实现
plt.legend()
plt.show()

if __name__=='__main__':
# smooth('total_loss.csv')
smooth_and_plot('total_loss.csv')

可视化效果如下图:

扩展:

上述smooth函数旨在构建一个类似于IIR滤波器的结构以滤除高频部分保留低频部分,即让数据变化更加平缓。

Ref:

Tensorboard 下Smooth功能探究
https://dingguanglei.com/tensorboard-xia-smoothgong-neng-tan-jiu/

tensorboard 平滑损失曲线代码_人工智能_Charel_CHEN的博客-CSDN博客
https://blog.csdn.net/Charel_CHEN/article/details/80364841