Gating, Time Scales & Chrono Initialization
Why gates are learned time constants, and how to initialize them.
How long should a recurrent network remember? A leaky unit answers with one fixed time constant; a gate answers anew at every step. Tallec and Ollivier showed that this is no design accident: asking a network to cope with time that is stretched or compressed by unknown amounts forces gates on it. The same argument tells you how to initialize them, and why the usual forget bias of 1 is far too short for long dependencies.
What you’ll learn
- Derive the memory time scale of a leaky unit, about steps, from its continuous-time origin, and the time scale of a forget gate with bias .
- Derive gating from invariance to time warping: rescaling time yields leaky RNNs, and general warpings yield input-dependent gates.
- Derive chrono initialization, and , describe the time scales it produces, and compare it with uniform gate initialization.
- Explain the copy and adding benchmarks and what large ablations and initialization studies show about which gating choices matter.
Leaky units and time constants
Start from a continuous-time neuron, , which relaxes toward its drive with time constant . One Euler step of size gives a leaky unit, with . Mozer (1992) used units with different time constants to capture structure at several time scales at once, and leaky echo state networks use the same update.
Switch the input off. Each step keeps a fraction of the state, so after steps:
The memory decays exponentially with a time constant of steps, and a half-life of about . A gate that keeps a fraction per step is the same thing with :
At initialization a gate's weighted inputs are small and . Because , the bias alone fixes the starting time scale, and each extra unit of bias multiplies it by about :
A forget gate holds steady at . Using , how many steps does the cell's memory last?
Show the answer
20 steps
steps; the exact time constant steps is almost the same. After 20 steps of the content is left; after 100 steps less than 1%.
A fixed leak sets one time scale for the whole sequence. Hand-designed alternatives spread several fixed scales across units, as in the Clockwork RNN, which updates groups of units at different clock periods (Koutník, Greff, Gomez & Schmidhuber, 2014). A gate does something different: is computed from the current input and state, so the leak rate itself changes from step to step. The next step shows why that is exactly what time warping demands.
Gates from time warping
Tallec & Ollivier (2018) start from an observation: an ordinary RNN can be made to fail at an easy task just by inserting a fixed number of blank characters between the input symbols. Its class of functions is not invariant to a change of time unit. They ask which model classes are, and derive the answer from the continuous-time model behind the RNN.
The ordinary RNN is the Euler discretization, with step 1, of
Feed it rescaled data . A solution for the new data is ; the chain rule multiplies the right-hand side by , and the Euler step now gives a leaky RNN. With learnable, the leaky class is invariant to rescaling, and the ordinary class is not:
A general time warping is any increasing differentiable , with data . Now the chain rule brings in , which changes over time and is unknown in advance, so it must be estimated from the inputs seen so far by a learnable function :
Discretize again. With a sigmoid for , the result is a gated RNN whose input gate is and whose forget gate is . Give each unit its own gate and you have, up to details, the cell update of an LSTM with coupled gates, or a GRU:
Their experiments test this directly. The base task is trivial, outputting the previous character of a random sequence, and the only difficulty is warping: every character repeated a fixed number of times (uniform warping) or a random number of times (variable warping), or padded with blanks, up to 100-fold. Ordinary RNNs degrade as the warping grows; leaky RNNs handle uniform warping; gated RNNs also handle variable warping.
Why does a single learned leak suffice for uniformly warped data but not for variably warped data?
- Uniform warping makes sequences shorter, so less memory is needed
- Leaky units cannot be trained by gradient descent on variable-length data
- Variable warping changes the vocabulary, which only gates can handle
- Under uniform warping is a constant that one learned can match; under variable warping changes with the data, so the leak must be computed from the inputs at every step, which is a gate
Show the answer
D. Under uniform warping is a constant that one learned can match; under variable warping changes with the data, so the leak must be computed from the inputs at every step, which is a gate
The derivation turns into the leak. A constant means a constant leak; a data-dependent means a data-dependent leak, , which is precisely a gate.
Chrono initialization
The derivation identifies the gate with the local speed of time: a unit with gate remembers for about steps. If a task's dependencies span to steps, the gates should start in . Tallec & Ollivier turn this into an initialization for the LSTM, where the forget gate plays and the input gate plays .
A sigmoid gate with bias and small other inputs outputs exactly , and one with bias outputs :
To give unit a time scale , set and . Drawing uniformly from gives the chrono initialization:
Each unit's starting time scale is then uniform on . With , about 90% of the units start with memories longer than 100 steps. The usual constant gives every unit about 3.7 steps.
import torch, torch.nn as nn
def chrono_init_(lstm: nn.LSTM, t_max: float):
"""Chrono initialization (Tallec & Ollivier, 2018) of a unidirectional nn.LSTM."""
n = lstm.hidden_size
for layer in range(lstm.num_layers):
b_ih = getattr(lstm, f"bias_ih_l{layer}") # blocks: input, forget, cell, output
b_hh = getattr(lstm, f"bias_hh_l{layer}")
with torch.no_grad():
b_f = torch.log(torch.empty(n).uniform_(1.0, t_max - 1.0))
b_ih[n:2 * n] = b_f # forget gate starts near 1 - 1/T
b_ih[:n] = -b_f # input gate starts near 1/T
b_hh[:2 * n] = 0.0 # PyTorch adds both bias vectorsPyTorch keeps two bias vectors per layer and adds them, so the rule is written into one and the other is zeroed for these two gates. is a guess at the longest dependency; the authors used on the copy task below.
On the synthetic benchmarks, single-layer LSTMs with 128 units and chrono initialization heavily outperformed the same LSTMs with a forget bias of 1: on the copy task at and , where chrono's loss did not plateau at the best memoryless value, on variable-length copying at (both did well at ), and on the adding problem at and . On character-level Text8 and word-level Penn Treebank, chrono initialization matched or slightly improved the standard initialization.
Uniform gate initialization (Gu, Gulcehre, Paine, Hoffman & Pascanu, 2020) draws the gate activation uniformly instead, with . A unit's time scale then exceeds with probability about : many short memories and a long tail, where chrono puts most units at long time scales and depends on the guess . The same paper adds a refine gate that pushes the effective forget gate closer to 0 or 1 than alone, without saturating the sigmoid, and reports gains on memory tasks, language modeling and reinforcement learning.
What matters, and how to test it
Two synthetic tasks going back to the original LSTM paper (Hochreiter & Schmidhuber, 1997), in the form standardized by Arjovsky, Shah & Bengio (2016), isolate long-range memory:
- Copy. Ten symbols drawn from 8 letters, then blank steps (the last a signal symbol), then the network must reproduce the ten symbols. A model with no memory can only guess among 8 letters at the ten output positions, a cross-entropy of per step averaged over the sequence.
- Adding. Two sequences of length : numbers drawn from , and markers flagging one position in each half. The target is the sum of the two flagged numbers.
Think first A model that ignores the markers in the adding problem predicts the constant 1. What mean squared error does it get, and why is that the baseline to beat?
The target is a sum of two independent variables, with mean 1 and variance . Predicting the mean gives exactly that error, and a model that cannot tell which numbers are flagged can do essentially no better. Learning the task means driving the error well below , which requires carrying a flagged number across up to steps.
What, then, matters in a gated cell? Large ablations agree on the forget gate. Greff et al. (2017) found it, with the output nonlinearity, the most critical component of the LSTM; Jozefowicz, Zaremba & Sutskever (2015) found that its bias alone closed the gap between the LSTM and the GRU; and the initialization studies above show that the distribution of its starting time scales decides whether long dependencies are learnable at all. State space models confront the same question in another form: their decay rates and step sizes are initialized to cover a range of time scales, which the State Space Models lesson Structured State Spaces: HiPPO & S4 takes up.
Key takeaways
- A leaky unit remembers for about steps; a gate at for about , and at initialization gives exactly .
- Requiring a model class to handle rescaled time yields leaky RNNs; requiring it to handle general, data-dependent warpings yields a learned gate and the update (Tallec & Ollivier, 2018).
- Chrono initialization sets , , so starting time scales are uniform on ; it beats a forget bias of 1 by wide margins on copy and adding tasks. Uniform gate initialization spreads time scales with a tail instead.
- The forget gate and the distribution of its time scales are what matter most in gated cells, as ablations and initialization studies agree; state space models face the same choice when initializing their decay rates.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
A leaky unit updates . For about how many steps does it remember a value once the input stops?
- About 50 steps, the time constant with
- About 2 steps
- About 98 steps
- Forever, because is close to 1
Show the answer
A. About 50 steps, the time constant with
, a time constant of steps (exactly ).
Every character of a training text is repeated exactly 4 times (a uniform time warping). Which model class can represent the warped task with the same weights it would use on the original, by adjusting a single parameter?
- An ordinary RNN, since it shares weights across time
- No recurrent model: the sequences are four times longer
- A leaky RNN with a learnable leak: rescaling time multiplies the continuous-time dynamics by a constant, which the leak absorbs
- Only a transformer, since attention ignores time
Show the answer
C. A leaky RNN with a learnable leak: rescaling time multiplies the continuous-time dynamics by a constant, which the leak absorbs
With , the continuous dynamics are multiplied by , and the Euler step gives : a leaky RNN with . Variable warpings need a data-dependent leak, a gate.
Under chrono initialization, one LSTM unit drew (so ). With its other gate inputs near zero, what are its gates and its starting memory?
- , : a memory of 2 steps
- , : a memory of about 100 steps, with little of each input written
- , : it overwrites its cell at every step
- , : it never changes
Show the answer
B. , : a memory of about 100 steps, with little of each input written
and . The time scale is steps, and the input gate writes only a small fraction per step, as a discretization of a slow continuous system would.
Why can chrono initialization succeed on the copy task with where a constant forget bias of 1 fails?
- Chrono uses a larger learning rate
- Chrono removes the need for backpropagation through time
- A bias of 1 makes the gradient explode on long sequences
- A bias of 1 gives every unit a memory of about 3.7 steps, so the gradient along the cells dies long before it crosses 2,000 steps; chrono starts units with memories spread up to , so a signal can cross the gap from the first update
Show the answer
D. A bias of 1 gives every unit a memory of about 3.7 steps, so the gradient along the cells dies long before it crosses 2,000 steps; chrono starts units with memories spread up to , so a signal can cross the gap from the first update
With per step, is astronomically small. Chrono gives many units with in the thousands, so the cell path carries both the stored symbols and their gradients across the delay.
Further reading
- C. Tallec & Y. Ollivier, "Can recurrent neural networks warp time?," ICLR (2018).
- A. Gu, C. Gulcehre, T. Paine, M. Hoffman & R. Pascanu, "Improving the gating mechanism of recurrent neural networks," ICML (2020).
- M. C. Mozer, "Induction of multiscale temporal structure," NeurIPS 4 (1992).
- J. Koutník, K. Greff, F. Gomez & J. Schmidhuber, "A Clockwork RNN," ICML (2014).
- M. Arjovsky, A. Shah & Y. Bengio, "Unitary evolution recurrent neural networks," ICML (2016).