Foundations to Frontier
Tests
E. Deep Learning › E11 Recurrent and Sequence Models

Peephole LSTM

A peephole LSTM (Gers and Schmidhuber, 2000) lets each gate look directly at the cell state through diagonal 'peephole' weights: the input and forget gates see c(t−1), and the output gate sees c_t. Without peepholes, gates see the cell only through h = o ⊙ tanh(c), which is blank whenever the output gate is closed. Peepholes were introduced for tasks that need precise timing and counting. Large comparisons later found they rarely matter. This page covers the peephole equations, cost and evidence. In the exercises you implement a peephole step, then show on a spike-timing task that peepholes learn faster: after 100 steps exact timing is 1.0 on every seed, against 0.84, 0.71 and 0.19 without. By 200 steps the gap has mostly closed.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Letting the valves see the memory

In a standard LSTM the valves (gates) decide what to do from the input and the visible output, not from the memory itself. If the output valve is shut, the visible output is blank, and the valves cannot tell what is stored. A peephole is a small window from the memory to each valve. With it, a valve can open exactly when the stored count reaches a threshold, like a timer going off. The demo shows how gates control what survives in memory.

Sequence-model lab
steps 0 … 30
After 30 steps the written value keeps 3.1e-5 in a plain RNN (blue), 0.215 in an LSTM cell (green) and 0.042 in a GRU (yellow). Gates near "keep" carry memory far.
Why engineers care

Precise timing matters in rhythm, speech prosody and control loops. Peepholes are a cheap option to try when a recurrent model must act at exact times.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A peephole connects…
  2. 2. Without peepholes, a closed output gate hides…
  3. 3. Peepholes were introduced for…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Equations: with diagonal peephole vectors p_i, p_f, p_o,
    • i = σ(zWᵢ + bᵢ + p_i ⊙ cₜ₋₁);
    • f = σ(zW_f + b_f + p_f ⊙ cₜ₋₁);
    • cₜ = f ⊙ cₜ₋₁ + i ⊙ g;
    • o = σ(zWₒ + bₒ + p_o ⊙ cₜ). The output gate sees the new cell.
  • Cost: 3n extra parameters. For d = 10 and n = 32 that is 96 on top of 5504 (1.7%); for d = 100 and n = 512, 0.12%.
  • Timer example:
    • a cell adding 0.5 per step crosses 3.0 after 7 steps;
    • an output gate with bias −4 and peephole 1 is σ(−4) = 0.018 at c = 0 and σ(2) = 0.88 at c = 6;
    • it opens when the count is high.
  • Without peepholes: when o ≈ 0, h = o·tanh(c) ≈ 0. Gates then receive no information about c, so a closed output gate cannot reopen based on the stored count alone.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Extra parameters for n = 32:
  2. 2. Which cell does the output-gate peephole see?
  3. 3. Steps for 0.5 per step to exceed 3.0:

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Peepholes in code

Exercise 1.

  • With peephole 1 and bias −4, the output gate stayed shut (below 0.05) for a cell at 0 and opened (above 0.85) for a cell at 6.
  • The parameter count was 5600.

Practice.

  • PyTorch’s nn.LSTM has no peepholes. Write a custom cell and lose cuDNN speed.
  • TensorFlow 1’s LSTMCell(use_peepholes=True) had them, and so do some speech toolkits.
  • Initialise peephole weights near zero so the model starts as a standard LSTM.
  • Given the evidence below, try peepholes only for timing-sensitive tasks, and measure.
class PeepholeLSTMCell(nn.Module):
    def __init__(self, d, n):
        super().__init__(); self.lin = nn.Linear(d + n, 4 * n); self.p = nn.Parameter(torch.zeros(3, n))
    def forward(self, x, h, c):
        a = self.lin(torch.cat([x, h], -1)); ai, af, ag, ao = a.chunk(4, -1)
        i, f = torch.sigmoid(ai + self.p[0] * c), torch.sigmoid(af + self.p[1] * c)
        c = f * c + i * torch.tanh(ag); o = torch.sigmoid(ao + self.p[2] * c)
        return o * torch.tanh(c), c
🐍 Exercise 1: a peephole step
Implement a peephole LSTM step and its parameter count, and show the output gate opening on the cell's value.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Does PyTorch's nn.LSTM support peepholes?
  2. 2. Peephole weights should start…
  3. 3. A custom cell loses…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

What peepholes add.

  • Gates of a standard LSTM are functions of xₜ and hₜ₋₁ = oₜ₋₁ ⊙ tanh(cₜ₋₁).
  • When oₜ₋₁ is small, hₜ₋₁ carries almost no information about the cell. The gates are then blind to the stored value and must decide from the input alone.
  • Peepholes add p ⊙ c to the gate inputs, making each gate a direct function of its own cell, cheaply (diagonal, 3n weights).

When it matters.

  • Gers and Schmidhuber showed peephole LSTMs learning to generate and measure precisely timed spikes, where standard LSTMs struggled.
  • In practice a standard LSTM can often keep its output gate slightly open and count through h, which is enough on many tasks.
  • In Greff et al.’s large comparison (speech, handwriting, music), removing peepholes did not significantly hurt, and they suggested dropping them for simplicity.
📝 Quick check (Level 3)
0/3 answered
  1. 1. Standard gates see the cell only through…
  2. 2. Peephole weights are…
  3. 3. Greff et al. found removing peepholes…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Do peepholes help timing?

History.

  • F. Gers and J. Schmidhuber introduced peephole connections (“Recurrent nets that time and count”, IJCNN 2000).
  • F. Gers, N. Schraudolph and J. Schmidhuber developed precise-timing tasks (JMLR 2002).
  • A. Graves’s speech and handwriting LSTMs used peepholes (2013).
  • K. Greff and colleagues found them unnecessary on standard benchmarks (2017).

Exercise 2.

  • Task: an 8-unit LSTM sees one pulse at t = 0 whose height encodes a delay d between 4 and 16. It must spike exactly at step d.
  • Training: batches of 32 with Adam; the standard and peephole versions are trained identically.
  • Measurement: exact-timing accuracy (the output’s peak at step d) on 500 test sequences after 100 and 200 steps, 3 seeds.
model after 100 steps after 200 steps
standard LSTM 0.838, 0.712, 0.188 0.768, 1.000, 0.936
peephole LSTM 1.000, 1.000, 1.000 1.000, 1.000, 0.936
  • Peepholes speed up learning to time. After 100 steps every peephole run was exact, against a mean of 0.58 without.
  • The advantage is mostly about speed here. By 200 steps the standard LSTM caught up on two seeds. In a longer 400-step run, both reached 1.0 on all three seeds.
  • The result is fragile. With longer delays (8–32) and 200 steps, both versions were erratic across seeds, and no consistent ordering appeared. Treat peepholes as a cheap option to test on timing tasks, not a default.
🐍 Exercise 2 (research): do peepholes help timing?
Train standard and peephole LSTMs to spike after an input-coded delay, and compare exact-timing accuracy during training.

Peepholes in AI, robotics and aerospace

  • Rhythm and timing: music generation and speech prosody models have used peepholes for timing cues.
  • Event timing in control: recurrent controllers that must trigger actions after measured intervals may benefit.
  • Default choice: for most sensor and language tasks, a standard LSTM or GRU is simpler and as accurate.

References

  1. F. A. Gers, J. Schmidhuber (2000). “Recurrent nets that time and count.” Proc. IJCNN.
  2. F. A. Gers, N. N. Schraudolph, J. Schmidhuber (2002). “Learning precise timing with LSTM recurrent networks.” JMLR 3.
  3. K. Greff, R. K. Srivastava, J. Koutník, B. R. Steunebrink, J. Schmidhuber (2017). “LSTM: a search space odyssey.” IEEE TNNLS 28(10).