48

I presume it is some kind of moving average, but the valid range is between 0 and 1.

Willian Mitsuda
  • 1,249
  • 1
  • 12
  • 14

3 Answers3

91

ORIGINAL ANSWER

It is called exponential moving average, below is a code explanation how it is created.

Assuming all the real scalar values are in a list called scalars the smoothing is applied as follows:

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

UPDATED ANSWER

As @SaPropper correctly pointed out, TensorBoard now includes the debiasing factor.

bluesummers
  • 11,365
  • 8
  • 72
  • 108
5

Here is the actual piece of source code that performs that exponential smoothing with some additional de-biasing explained in the comments to compensate for the choice of the zero initial value:

last = last * smoothingWeight + (1 - smoothingWeight) * nextVal

Source: https://github.com/tensorflow/tensorboard/blob/34877f15153e1a2087316b9952c931807a122aa7/tensorboard/components/vz_line_chart2/line-chart.ts#L714

Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
Ben Usman
  • 7,969
  • 6
  • 46
  • 66
3

The implementation of EMA smoothing used for TensorBoard can be found here.

The equivalent in Python is actually:

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
SaPropper
  • 463
  • 3
  • 11