首页 > 其他分享 >TensorBoard标量图中的平滑曲线是如何做的平滑?—— tensorflow TensorBoard标量图中“平滑”参数背后的数学原理是什么?—— 指数移动平均(EMA)

TensorBoard标量图中的平滑曲线是如何做的平滑?—— tensorflow TensorBoard标量图中“平滑”参数背后的数学原理是什么?—— 指数移动平均(EMA)

时间:2024-02-15 10:22:34浏览次数:25  
标签:debias last weight 平滑 TensorBoard 标量 EMA data smoothed

TensorFlow的tensorboard的平滑曲线的实现代码:

使用“指数移动平均”技术实现。


地址:
https://github.com/tensorflow/tensorboard/blob/34877f15153e1a2087316b9952c931807a122aa7/tensorboard/components/vz_line_chart2/line-chart.ts#L699


  private resmoothDataset(dataset: Plottable.Dataset) {
    let data = dataset.data();
    const smoothingWeight = this.smoothingWeight;
    // 1st-order IIR low-pass filter to attenuate the higher-
    // frequency components of the time-series.
    let last = data.length > 0 ? 0 : NaN;
    let numAccum = 0;
    const yValues = data.map((d, i) => this.yValueAccessor(d, i, dataset));
    // See #786.
    const isConstant = yValues.every((v) => v == yValues[0]);
    data.forEach((d, i) => {
      const nextVal = yValues[i];
      if (isConstant || !Number.isFinite(nextVal)) {
        d.smoothed = nextVal;
      } else {
        last = last * smoothingWeight + (1 - smoothingWeight) * nextVal;
        numAccum++;
        // The uncorrected moving average is biased towards the initial value.
        // For example, if initialized with `0`, with smoothingWeight `s`, where
        // every data point is `c`, after `t` steps the moving average is
        // ```
        //   EMA = 0*s^(t) + c*(1 - s)*s^(t-1) + c*(1 - s)*s^(t-2) + ...
        //       = c*(1 - s^t)
        // ```
        // If initialized with `0`, dividing by (1 - s^t) is enough to debias
        // the moving average. We count the number of finite data points and
        // divide appropriately before storing the data.
        let debiasWeight = 1;
        if (smoothingWeight !== 1) {
          debiasWeight = 1 - Math.pow(smoothingWeight, numAccum);
        }
        d.smoothed = last / debiasWeight;
      }
    });


等价的python代码:

def smooth(scalars: list[float], weight: float) -> list[float]:
    """
    EMA implementation according to
    https://github.com/tensorflow/tensorboard/blob/34877f15153e1a2087316b9952c931807a122aa7/tensorboard/components/vz_line_chart2/line-chart.ts#L699
    """
    last = 0
    smoothed = []
    num_acc = 0
    for next_val in scalars:
        last = last * weight + (1 - weight) * next_val
        num_acc += 1
        # de-bias
        debias_weight = 1
        if weight != 1:
            debias_weight = 1 - math.pow(weight, num_acc)
        smoothed_val = last / debias_weight
        smoothed.append(smoothed_val)
    return smoothed

指数移动平均是比较常见的平滑技术,但是和常见的计算公式不同,上面的计算中出现了一个 debias_weight 变量,最终的EMA值也是需要除以这个 debias_weight 获得的。


由于EMA的计算是对历史数据的指数加权,因此如果不除以 debias_weight 最终获得的数值是对历史数据的加权不等于1的,而这个 debias_weight 就是对历史数据的加权的权值。

EMA的等价的计算公式:

EMA = 0*s^(t) + c*(1 - s)*s^(t-1) + c*(1 - s)*s^(t-2) + ...
       = c*(1 - s^t)

加权权重的计算,并除以不为1的加权权重值,获得最终的平滑值:

debias_weight = 1 - math.pow(weight, num_acc)
smoothed_val = last / debias_weight


参考:

tensorflow TensorBoard标量图中“平滑”参数背后的数学原理是什么?



标签:debias,last,weight,平滑,TensorBoard,标量,EMA,data,smoothed
From: https://www.cnblogs.com/devilmaycry812839668/p/18015991

相关文章

  • Nginx--平滑升级
    在不中断服务的情况下,新的请求也不会丢失,使用新的nginx可执行程序替换旧的 1 查看老版本的编译选项[root@localhost~]#nginx-Vnginxversion:nginx/1.16.0builtbygcc4.8.520150623(RedHat4.8.5-44)(GCC)builtwithOpenSSL1.0.2k-fips26Jan2017TLSSNI......
  • Python时间序列分析苹果股票数据:分解、平稳性检验、滤波器、滑动窗口平滑、移动平均、
    全文链接:https://tecdat.cn/?p=33550原文出处:拓端数据部落公众号什么是时间序列?时间序列是一系列按时间顺序排列的观测数据。数据序列可以是等间隔的,具有特定频率,也可以是不规则间隔的,比如电话通话记录。在进行投资和交易研究时,对于时间序列数据及其操作要有专业的理解。本文......
  • 面试官:什么是JIT、逃逸分析、锁消除、栈上分配和标量替换?
    JIT、逃逸分析、锁消除、栈上分配和标量替换等都属于JVM的优化手段,JVM优化手段是指在运行Java程序时,通过对字节码的编译和执行过程进行优化,以提升程序的性能和效率。JVM优化手段主要有以下几个:JIT(Just-In-Time,即时编译):是一种在程序运行时将部分热点代码编译成机器代码的......
  • 如何用 Python 编写一个简单的技术指标量化策略
    技术指标是通过对历史价格、成交量等数据进行计算,来预测未来市场走势的工具。Python作为一种流行的编程语言,提供了许多强大的库,如Pandas和NumPy,可用于处理金融数据并实现量化策略。下面我们将详细介绍如何用Python编写一个简单的技术指标量化策略。步骤一:导入所需库在开始之前,我们......
  • 数学基础(一)-标量、向量、矩阵、张量以及各范数的含义
    1.标量、向量、矩阵、张量:①标量指有大小没有方向的数。②向量指既有大小也有方向的一组数。③矩阵指二维的一组数,一行是一个对象,一列是一个对象的一个特征【一行一对象,一列一特征】。④张量指一个数组分布在多维网格坐标中。  2.向量的范数:①向量的......
  • Java-虚拟机-逃逸分析/栈上分配/标量替换
    假设有下面一个类,本文会一直使用这个类演示publicclassUser{ publicintid; publicStringname;}逃逸分析:逃逸指的是在方法中创建的对象,逃到方法外,那么逃逸分析,指的就是分析一个在方法内创建的对象,有没有可能从它所在的方法中逃出去,例如下面的代码,就会逃到方法外publicsta......
  • [NLP复习笔记] N-gram 及基本平滑方法
    1.N-gram模型1.1N-gram模型介绍\(\text{N-gram}\)是一种基于统计语言模型的算法,用于预测文本中的单词,其中\(\text{N}\)一般指的是序列中的单词数量。其基本思想是将文本内容进行大小为\(\text{N}\)的滑动窗口操作来计算概率。例如:当\(\text{N}=1\)时,模型被称为"u......
  • 记录一次,nginx平滑升级,不需要停止nginx服务,不影响业务访问
    #下载新版本nginxwgethttp://nginx.org/download/nginx-1.24.0.tar.gz#解压并安装tarxfnginx-1.24.0.tar.gzcdnginx-1.24.0./configure--prefix=/usr/local/nginx--user=nginx--group=nginx\--with-http_ssl_module\--with-http_gzip_static_module\--with-poll_mo......
  • PPT-平滑及3D功能
    一、平滑功能如果想要实现两个图形的转场效果;可以利用动画效果来实现; 上面可以基本实现动画移动的效果,但是存在以下问题:1、想要在动画后图形不一样?;可以切换---平滑效果来实现 简单了解平滑功能后,做一个带有3D图形的平滑效果......
  • css 实现一个选项卡按钮边框弯曲平滑
    <Viewclass="tabs"style="display:flex;justify-content:space-between"><Viewclass="tabs-item"style="width:50%"><Buttonclass="tabs-btn":class="0?&#......