Simple Algorithm
Deep Learning · Emergence · 26 min

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 1/α1/\alpha steps, from its continuous-time origin, and the time scale 1+ebf1+e^{b_f} of a forget gate with bias bfb_f.
  • Derive gating from invariance to time warping: rescaling time yields leaky RNNs, and general warpings yield input-dependent gates.
  • Derive chrono initialization, bf∼log⁡U([1,Tmax⁡−1])b_f\sim\log\mathcal U([1,T_{\max}-1]) and bi=−bfb_i=-b_f, 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, τ h˙=−h+ϕ(input)\tau\,\dot h=-h+\phi(\text{input}), which relaxes toward its drive with time constant τ\tau. One Euler step of size Δt\Delta t gives a leaky unit, ht+1=(1−α) ht+α ϕ(⋅)h_{t+1}=(1-\alpha)\,h_t+\alpha\,\phi(\cdot) with α=Δt/τ\alpha=\Delta t/\tau. 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.

DerivationHow long a leaky unit remembers
  1. ht+k=(1−α)k ht=ekln⁡(1−α) ht≈e−αk ht(α≪1)h_{t+k}=(1-\alpha)^k\,h_t=e^{k\ln(1-\alpha)}\,h_t\approx e^{-\alpha k}\,h_t\qquad(\alpha\ll1)

    Switch the input off. Each step keeps a fraction 1−α1-\alpha of the state, so after kk steps:

  2. τf=−1ln⁡f≈11−f\tau_f=-\frac1{\ln f}\approx\frac1{1-f}

    The memory decays exponentially with a time constant of −1/ln⁡(1−α)≈1/α-1/\ln(1-\alpha)\approx1/\alpha steps, and a half-life of about ln⁡2/α\ln2/\alpha. A gate that keeps a fraction ff per step is the same thing with α=1−f\alpha=1-f:

  3. 11−σ(bf)=1+e bf:bf=0↦2,bf=1↦3.7,bf=3↦21,bf=5↦149 steps\frac1{1-\sigma(b_f)}=1+e^{\,b_f}:\qquad b_f=0\mapsto2,\quad b_f=1\mapsto3.7,\quad b_f=3\mapsto21,\quad b_f=5\mapsto149\ \text{steps}

    At initialization a gate's weighted inputs are small and f≈σ(bf)f\approx\sigma(b_f). Because 1−σ(b)=σ(−b)=1/(1+eb)1-\sigma(b)=\sigma(-b)=1/(1+e^{b}), the bias alone fixes the starting time scale, and each extra unit of bias multiplies it by about ee:

Predict before you look

A forget gate holds steady at f=0.95f=0.95. Using τ≈1/(1−f)\tau\approx1/(1-f), how many steps does the cell's memory last?

Show the answer

20 steps

1/(1−0.95)=201/(1-0.95)=20 steps; the exact time constant −1/ln⁡0.95≈19.5-1/\ln0.95\approx19.5 steps is almost the same. After 20 steps 0.9520≈0.36≈1/e0.95^{20}\approx0.36\approx1/e 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: ftf_t 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.

DerivationFrom time warping to gates (Tallec & Ollivier, 2018)
  1. dh(t)dt=tanh⁡(Wxx(t)+Whh(t)+b)−h(t)\frac{d\mathbf h(t)}{dt}=\tanh\big(W_x\mathbf x(t)+W_h\mathbf h(t)+\mathbf b\big)-\mathbf h(t)

    The ordinary RNN ht+1=tanh⁡(Wxxt+Whht+b)\mathbf h_{t+1}=\tanh(W_x\mathbf x_t+W_h\mathbf h_t+\mathbf b) is the Euler discretization, with step 1, of

  2. dHdt=αtanh⁡(WxX+WhH+b)−αH  ⟹  ht+1=αtanh⁡(Wxxt+Whht+b)+(1−α) ht\frac{d\mathbf H}{dt}=\alpha\tanh(W_x\mathbf X+W_h\mathbf H+\mathbf b)-\alpha\mathbf H\ \ \Longrightarrow\ \ \mathbf h_{t+1}=\alpha\tanh(W_x\mathbf x_t+W_h\mathbf h_t+\mathbf b)+(1-\alpha)\,\mathbf h_t

    Feed it rescaled data x(αt)\mathbf x(\alpha t). A solution for the new data is H(t)=h(αt)\mathbf H(t)=\mathbf h(\alpha t); the chain rule multiplies the right-hand side by α\alpha, and the Euler step now gives a leaky RNN. With α\alpha learnable, the leaky class is invariant to rescaling, and the ordinary class is not:

  3. dhdt=c′(t)tanh⁡(⋯ )−c′(t) h(t)⟶dhdt=g(t)tanh⁡(⋯ )−g(t) h(t)\frac{d\mathbf h}{dt}=c'(t)\tanh(\cdots)-c'(t)\,\mathbf h(t)\qquad\longrightarrow\qquad\frac{d\mathbf h}{dt}=g(t)\tanh(\cdots)-g(t)\,\mathbf h(t)

    A general time warping is any increasing differentiable cc, with data x(c(t))\mathbf x(c(t)). Now the chain rule brings in c′(t)c'(t), which changes over time and is unknown in advance, so it must be estimated from the inputs seen so far by a learnable function g(t)>0g(t)>0:

  4. ht+1=gt⊙tanh⁡(Wxxt+Whht+b)+(1−gt)⊙ht,gt=σ(Wgxxt+Wghht+bg)\mathbf h_{t+1}=\mathbf g_t\odot\tanh(W_x\mathbf x_t+W_h\mathbf h_t+\mathbf b)+(1-\mathbf g_t)\odot\mathbf h_t,\qquad\mathbf g_t=\sigma(W_{gx}\mathbf x_t+W_{gh}\mathbf h_t+\mathbf b_g)

    Discretize again. With a sigmoid for gg, the result is a gated RNN whose input gate is gtg_t and whose forget gate is 1−gt1-g_t. 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.

Check your understanding

Why does a single learned leak α\alpha suffice for uniformly warped data but not for variably warped data?

  1. Uniform warping makes sequences shorter, so less memory is needed
  2. Leaky units cannot be trained by gradient descent on variable-length data
  3. Variable warping changes the vocabulary, which only gates can handle
  4. Under uniform warping c′(t)c'(t) is a constant that one learned α\alpha can match; under variable warping c′(t)c'(t) 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 c′(t)c'(t) is a constant that one learned α\alpha can match; under variable warping c′(t)c'(t) changes with the data, so the leak must be computed from the inputs at every step, which is a gate

The derivation turns c′(t)c'(t) into the leak. A constant c′c' means a constant leak; a data-dependent c′c' means a data-dependent leak, gt=σ(⋅)g_t=\sigma(\cdot), which is precisely a gate.

Chrono initialization

The derivation identifies the gate with the local speed of time: a unit with gate gg remembers for about 1/g1/g steps. If a task's dependencies span Tmin⁡T_{\min} to Tmax⁡T_{\max} steps, the gates should start in [1/Tmax⁡,1/Tmin⁡][1/T_{\max},1/T_{\min}]. Tallec & Ollivier turn this into an initialization for the LSTM, where the forget gate plays 1−g1-g and the input gate plays gg.

DerivationDeriving the chrono rule
  1. σ(−log⁡(T−1))=11+(T−1)=1T,σ(log⁡(T−1))=1−1T\sigma\big(-\log(T-1)\big)=\frac1{1+(T-1)}=\frac1T,\qquad\sigma\big(\log(T-1)\big)=1-\frac1T

    A sigmoid gate with bias −log⁡(T−1)-\log(T-1) and small other inputs outputs exactly 1/T1/T, and one with bias +log⁡(T−1)+\log(T-1) outputs 1−1/T1-1/T:

  2. bf∼log⁡U([1, Tmax⁡−1]),bi=−bfb_f\sim\log\mathcal U\big([1,\,T_{\max}-1]\big),\qquad b_i=-b_f

    To give unit jj a time scale TjT_j, set f≈1−1/Tjf\approx1-1/T_j and i≈1/Tji\approx1/T_j. Drawing Tj−1T_j-1 uniformly from [1,Tmax⁡−1][1,T_{\max}-1] gives the chrono initialization:

  3. 1+e bf∼U([2, Tmax⁡])1+e^{\,b_f}\sim\mathcal U\big([2,\,T_{\max}]\big)

    Each unit's starting time scale 1+ebf=Tj1+e^{b_f}=T_j is then uniform on [2,Tmax⁡][2,T_{\max}]. With Tmax⁡=1,000T_{\max}=1{,}000, about 90% of the units start with memories longer than 100 steps. The usual constant bf=1b_f=1 gives every unit about 3.7 steps.

python
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 vectors

PyTorch 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. Tmax⁡T_{\max} is a guess at the longest dependency; the authors used Tmax⁡=3T/2T_{\max}=3T/2 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 T=500T=500 and T=2,000T=2{,}000, where chrono's loss did not plateau at the best memoryless value, on variable-length copying at T=1,000T=1{,}000 (both did well at T=500T=500), and on the adding problem at T=200T=200 and T=750T=750. 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, bf=σ−1(u)b_f=\sigma^{-1}(u) with u∼U[ϵ,1−ϵ]u\sim\mathcal U[\epsilon,1-\epsilon]. A unit's time scale 1/(1−u)1/(1-u) then exceeds ss with probability about 1/s1/s: many short memories and a long tail, where chrono puts most units at long time scales and depends on the guess Tmax⁡T_{\max}. The same paper adds a refine gate rtr_t that pushes the effective forget gate gt=rt(1−(1−ft)2)+(1−rt)ft2g_t=r_t\big(1-(1-f_t)^2\big)+(1-r_t)f_t^2 closer to 0 or 1 than ftf_t 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 TT 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 10ln⁡8/(T+20)10\ln8/(T+20) per step averaged over the sequence.
  • Adding. Two sequences of length TT: numbers drawn from U[0,1]\mathcal U[0,1], 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 U[0,1]\mathcal U[0,1] variables, with mean 1 and variance 2×112=16≈0.1672\times\tfrac1{12}=\tfrac16\approx0.167. 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 0.1670.167, which requires carrying a flagged number across up to TT 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 ht+1=(1−α)ht+αϕ(⋅)h_{t+1}=(1-\alpha)h_t+\alpha\phi(\cdot) remembers for about 1/α1/\alpha steps; a gate at ff for about 1/(1−f)1/(1-f), and at initialization f≈σ(bf)f\approx\sigma(b_f) gives exactly 1+ebf1+e^{b_f}.
  • Requiring a model class to handle rescaled time yields leaky RNNs; requiring it to handle general, data-dependent warpings c(t)c(t) yields a learned gate gt≈c′(t)g_t\approx c'(t) and the update ht+1=gt⊙tanh⁡(⋅)+(1−gt)⊙ht\mathbf h_{t+1}=\mathbf g_t\odot\tanh(\cdot)+(1-\mathbf g_t)\odot\mathbf h_t (Tallec & Ollivier, 2018).
  • Chrono initialization sets bf∼log⁡U([1,Tmax⁡−1])b_f\sim\log\mathcal U([1,T_{\max}-1]), bi=−bfb_i=-b_f, so starting time scales are uniform on [2,Tmax⁡][2,T_{\max}]; it beats a forget bias of 1 by wide margins on copy and adding tasks. Uniform gate initialization spreads time scales with a 1/s1/s 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.

Question 1

A leaky unit updates ht+1=0.98 ht+0.02 ϕ(⋅)h_{t+1}=0.98\,h_t+0.02\,\phi(\cdot). For about how many steps does it remember a value once the input stops?

  1. About 50 steps, the time constant 1/α1/\alpha with α=0.02\alpha=0.02
  2. About 2 steps
  3. About 98 steps
  4. Forever, because 0.980.98 is close to 1
Show the answer

A. About 50 steps, the time constant 1/α1/\alpha with α=0.02\alpha=0.02

ht+k=0.98kht≈e−0.02khth_{t+k}=0.98^kh_t\approx e^{-0.02k}h_t, a time constant of 1/0.02=501/0.02=50 steps (exactly −1/ln⁡0.98≈49.5-1/\ln0.98\approx49.5).

Question 2

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?

  1. An ordinary RNN, since it shares weights across time
  2. No recurrent model: the sequences are four times longer
  3. A leaky RNN with a learnable leak: rescaling time multiplies the continuous-time dynamics by a constant, which the leak absorbs
  4. 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 H(t)=h(t/4)\mathbf H(t)=\mathbf h(t/4), the continuous dynamics are multiplied by 1/41/4, and the Euler step gives ht+1=14tanh⁡(⋅)+34ht\mathbf h_{t+1}=\tfrac14\tanh(\cdot)+\tfrac34\mathbf h_t: a leaky RNN with α=1/4\alpha=1/4. Variable warpings need a data-dependent leak, a gate.

Question 3

Under chrono initialization, one LSTM unit drew bf=log⁡99b_f=\log99 (so bi=−log⁡99b_i=-\log99). With its other gate inputs near zero, what are its gates and its starting memory?

  1. f=0.5f=0.5, i=0.5i=0.5: a memory of 2 steps
  2. f=0.99f=0.99, i=0.01i=0.01: a memory of about 100 steps, with little of each input written
  3. f=0.01f=0.01, i=0.99i=0.99: it overwrites its cell at every step
  4. f=1f=1, i=0i=0: it never changes
Show the answer

B. f=0.99f=0.99, i=0.01i=0.01: a memory of about 100 steps, with little of each input written

σ(log⁡99)=99/100\sigma(\log99)=99/100 and σ(−log⁡99)=1/100\sigma(-\log99)=1/100. The time scale is 1+ebf=1001+e^{b_f}=100 steps, and the input gate writes only a small fraction per step, as a discretization of a slow continuous system would.

Question 4

Why can chrono initialization succeed on the copy task with T=2,000T=2{,}000 where a constant forget bias of 1 fails?

  1. Chrono uses a larger learning rate
  2. Chrono removes the need for backpropagation through time
  3. A bias of 1 makes the gradient explode on long sequences
  4. 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 Tmax⁡T_{\max}, 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 Tmax⁡T_{\max}, so a signal can cross the gap from the first update

With f≈0.73f\approx0.73 per step, 0.7320000.73^{2000} is astronomically small. Chrono gives many units f≈1−1/Tf\approx1-1/T with TT 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).