Exponential Moving Average with backward

BaruchYoussin: <p>I am interested in implementing Exponential Moving Average that would allow running <code>backward()</code> on it, in such way that it could be applied to tensors with substantial graphs creating them.</p>
<p>The straightforward implementations create an expanding graph that includes all graphs that create the past versions of the averaged tensor, and running <code>backward()</code> quickly runs out of memory.</p>
<p>I understand that this has already been implemented in batch normalization where the means and variances of the inputs to the layer are kept as exponential moving averages.  However, it is written in C++ and I could not dig out the implementation in the code.</p>
<p>I think the idea is to keep all the gradients of the moving average tensor w.r. to all the leaves together with the moving average (rather than with the leaf tensors); this would allow easy calculation of the gradients together with the moving average update.</p>
<p>However, the way of implementing it in python that I see, is tricky and inefficient, by subclassing <code>Tensor</code> to keep all the leaf gradients with it, and overriding <code>Tensor.backward()</code>.</p>
<p>Any better idea?</p>

googlebot: <p>Batch normalization uses buffers for running_mean, running_var, i.e. they’re updated without autograd.</p>
<p>If you’re talking about trainable “momentum”, with one step per batch, I think you’d have to approach this as hyperparameter optimization / meta-learning task.</p>

BaruchYoussin: <p><a class="mention" href="/u/googlebot">@googlebot</a> Thanks for your response.</p>
<p>I do not quite understand you here:</p>
<blockquote>
<p>Batch normalization uses buffers for running_mean, running_var, i.e. they’re updated without autograd.</p>
</blockquote>
<p>running_mean and running_var are used in calculating the layer output, and for this reason <code>backward()</code> needs their gradients.</p>
<p>As for my purpose, I want to use Exponential Moving Averages in my loss function similarly to the way they are used to calculate the layer outputs in Batch normalization.</p>

BaruchYoussin: <p>I looked up the original Batch Normalization paper, and its authors mention moving averages in passing, for the purpose of tracking the accuracy of the model.  They suggest using only the batch mean and variance for producing the layer output.</p>
<p>However, the <a href="https://pytorch.org/docs/stable/generated/torch.nn.BatchNorm1d.html#torch.nn.BatchNorm1d" rel="nofollow noopener">torch implemention</a> indicates that the mean and the variance are estimated using moving averages.</p>
<p>Are you saying that the BN code does not keep the gradients of the moving average buffers, and uses only the current batch to calculate the derivatives of the mean and average?</p>
<p>Did I get this correctly?</p>
<p>This would add random noise to the output of <code>backward()</code>.  Interesting.  This requires batches to be large enough for the noise not to be too large.</p>

googlebot: <p>These are calibration values, estimating long-term moments of layer inputs. If you make them trainable, it is no longer a normalization layer.</p>
<p>And to train a weighted averaging coefficient you need a sequence of at least two values in one batch.</p>
<aside class="quote no-group" data-username="BaruchYoussin" data-post="3" data-topic="93219">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/b/c2a13f/48.png" class="avatar"> BaruchYoussin:</div>
<blockquote>
<p>running_mean and running_var are used in calculating the layer output, and for this reason <code>backward()</code> needs their gradients.</p>
</blockquote>
</aside>
<p>this statement is wrong, that’s not how reverse mode autodiff works.</p>