我猜测它是一种移动平均,但有效范围在0和1之间。
原始答案
它被称为指数移动平均值,以下是代码解释如何创建它。
假设所有实数标量值都在名为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现在包括去偏差因子。
这里是实际的源代码片段,它执行指数平滑,并在注释中解释了一些额外的去偏差操作,以补偿零初始值的选择:
last = last * smoothingWeight + (1 - smoothingWeight) * nextVal
在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