tensorboard 界面smooth参数实现

tensorboard中有一个调节曲线平滑的参数smooth

tensorboard中的smooth调节参数


增大这个参数的设置,可以使得原本波动起伏很大曲线变得平滑。

例如:

smooth = 0 时:

smooth = 0

smooth = 0.5 时:

smooth = 0.5

smooth = 0.9 时:

smooth = 0.9

那么这个神奇的参数smooth到底是如何运作的呢?

聚合

官方给出的定义是这样的:
官方定义链接

     /**
     * Whether smoothing is enabled or not. If true, smoothed lines will be
     * plotted in the chart while the unsmoothed lines will be ghosted in
     * the background.
     *
     * The smoothing algorithm is a simple moving average, which, given a
     * point p and a window w, replaces p with a simple average of the
     * points in the [p - floor(w/2), p + floor(w/2)] range.  If there
     * aren't enough points to cover the entire window to the left, the
     * window is reduced to fit exactly the amount of elements available.
     * This means that the smoothed line will be less in and gradually
     * become more smooth until the desired window is reached. However when
     * there aren't enough points on the right, the line stops being
     * rendered at all.
     */
     /*
     * Weight (between 0.0 and 1.0) of the smoothing. This weight controls
     * the window size, and a weight of 1.0 means using 50% of the entire
     * dataset as the window, while a weight of 0.0 means using a window of
     * 0 (and thus replacing each point with themselves).
     *
     * The growth between 0.0 and 1.0 is not linear though. Because
     * changing the window from 0% to 30% of the dataset smooths the line a
     * lot more than changing the window from 70% to 100%, an exponential
     * function is used instead: http://i.imgur.com/bDrhEZU.png. This
     * function increases the size of the window slowly at the beginning
     * and gradually speeds up the growth, but 0.0 still means a window of
     * 0 and 1.0 still means a window of the dataset's length.
     */

以点p为中心,w为窗口长度,取窗口的所有点的均值。而smooth就是在控制整个窗口w的大小。

例如:如果smooth设置为1,那么原图中前50%的数据求平均绘制一个点,后50%的数据求平均绘制一个点。
不管原图多么起伏波动,最终一定是一条直线。

通过这种简单平均的方式,最终绘制的点数事实上少了很多。

以上只是第一步操作,简化数据点数目。

平滑滤波

真正意义上的数据平滑使用的是一个类似于IIR滤波器的结构来实现的,滤除高频保留低频,即让数据变化更加平缓。

官方代码连接694-708行

  private resmoothDataset(dataset: Plottable.Dataset) {
    let data = dataset.data();
    const smoothingWeight = this.smoothingWeight;
    let last = data.length > 0 ? data[0].scalar : NaN;
    data.forEach((d) => {
      if (!_.isFinite(last)) {
        d.smoothed = d.scalar;
      } else {
        // 1st-order IIR low-pass filter to attenuate the higher-
        // frequency components of the time-series.
        d.smoothed = last * smoothingWeight + (1 - smoothingWeight) * d.scalar;
      }
      last = d.smoothed;
    });
  }

最关键的一句就在

d.smoothed = last * smoothingWeight + (1 - smoothingWeight) * d.scalar

用一个流程图来表示一下,对于IIR滤波器的解释属于通信方面的内容,在这里也给出一个流程图。可以发现二者有着极大的共同之处。
代码流程图解释:

代码流程图

IIR滤波器:

IIR滤波器结构图

其实就是一个简单的滤波器,滤除高频部分。在数据列中高频部分就是变化快的数据,限制数据不要变化的过快。如果学过通信原理再理解这个IIR滤波器或是这种平滑操作会简单很多。

Excel实现smooth

最后,小小的实践一下。使用excel的公式编辑器来实现smooth的数据平滑,非常实用!!

    =F2*$I$2 + (1-$I$2) * E3

其中
F2是指上一个时间戳平滑后的数据(第一个平滑数不需要计算,直接复制过来)

$I$2是EXCEL中对I2格子的绝对引用,为smooth的参数值。

E3为当前时间戳没有平滑的原始数据值。

excel公式编辑器实现数据平滑