22.02.2024 Views

Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

In code, the implementation of the alpha version of EWMA looks like this:

def EWMA(past_value, current_value, alpha):

return (1- alpha) * past_value + alpha * current_value

For computing it over a series of values, given a period, we can define a function

like this:

def calc_ewma(values, period):

alpha = 2 / (period + 1)

result = []

for v in values:

try:

prev_value = result[-1]

except IndexError:

prev_value = 0

new_value = EWMA(prev_value, v, alpha)

result.append(new_value)

return np.array(result)

In the try..except block, you can see that, if there is no previous value for the

EWMA (as in the very first step), it assumes a previous value of zero.

The way the EWMA is constructed has its issues—since it does not need to keep

track of all the values inside its period, in its first steps, the "average" will be way

off (or biased). For an alpha=0.1 (corresponding to the 19-periods average), the

very first "average" will be exactly the first value divided by ten.

To address this issue, we can compute the bias-corrected EWMA:

Equation 6.8 - Bias-corrected EWMA

The beta in the formula above is the same as before: 1 - alpha. In code, we can

implement the correction factor like this:

Learning Rates | 459

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!