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

48

我猜测它是一种移动平均,但有效范围在0和1之间。

3个回答

93

原始答案

它被称为指数移动平均值,以下是代码解释如何创建它。

假设所有实数标量值都在名为scalars的列表中,则平滑应用如下:

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

更新的答案

正如@SaPropper 正确指出 的那样,TensorBoard现在包括去偏差因子。


2
我希望你能在这两者之间进行选择:一个固定的移动平均线或是一个滑动的移动平均线! - Avedis
这真的很有帮助!谢谢。 - Abhik Banerjee
这不是TensorBoard实现的Python等效版本。 - SaPropper
更明确地说:TensorBoard实现目前也具有去偏差因子,就像SaPropper的回答中所述。 - Eric O. Lebigot

5

4

在TensorBoard中使用的EMA平滑的实现可以在这里找到。

而在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

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