Deep Learning

Chapter 7
Sequence Models and Temporal Learning

Post Graduate Program in AI and ML
Deep Learning   •   Post Graduate

Sequence Models and Temporal Learning

Introduction

Many important data sources are ordered rather than independent. Words occur in sentences, acoustic frames occur in speech, sensor readings arrive over time, and financial observations form temporal series. In such settings, the meaning or value of an observation depends partly on its position and on earlier or later observations. Sequence models are designed to learn these dependencies.

A sequence can be represented as x1:T = (x1, x2, ..., xT), where T is the sequence length and xt is the observation at time t. A model may produce an output at every step, such as a part-of-speech tag; one output for the entire sequence, such as a sentiment label; or a new sequence, such as a translation.

This chapter develops recurrent neural networks (RNNs), long short-term memory networks (LSTMs), gated recurrent units (GRUs), temporal convolutional networks (TCNs), and sequence-to-sequence architectures. It also examines training, evaluation, deployment, limitations, and responsible use in language, speech, forecasting, and time-series analytics.

Learning Objectives

By the end of this chapter, you should be able to:

  1. Explain why temporal dependence requires specialized neural architectures.
  2. Derive and interpret the basic recurrent update and its gradient behavior.
  3. Compare vanilla RNNs, LSTMs, GRUs, and temporal convolutions.
  4. Design sequence-to-sequence systems using encoders, decoders, and attention.
  5. Prepare sequential data and select suitable evaluation metrics.
  6. Apply sequence models to language, speech, forecasting, and multivariate time series.
  7. Diagnose leakage, instability, distribution shift, bias, and computational constraints.

1. Temporal Data and the Sequence-Learning Problem

In independent and identically distributed learning, examples are commonly treated as exchangeable. Temporal observations violate this assumption. A temperature reading at 10:00 may depend on the reading at 09:00, and the interpretation of a pronoun may depend on words several positions earlier.

A forecasting task often uses a historical window to predict a future value:

yt+1:t+H = f(xt-L+1:t, ct) + ε

Here, L is the look-back length, H is the forecast horizon, ct denotes optional known covariates such as calendar features, and ε is prediction error. The equation is a general functional description, not a claim that the relationship is linear. For probabilistic forecasting, the model should estimate a conditional distribution p(yt+1:t+H | xt-L+1:t, ct), rather than only a point estimate.

Important sequence-learning settings include:

  • Many-to-one: a sequence produces one output, for example sentiment classification.
  • Many-to-many aligned: one output corresponds to each input step, for example frame-level speech labels.
  • Many-to-many unaligned: input and output lengths differ, as in translation.
  • One-to-many: one context generates a sequence, as in caption generation.

Sequence models must address variable length, missing observations, irregular sampling, long-range dependence, delayed effects, and changing data distributions.

2. The Vanilla Recurrent Neural Network

An RNN maintains a hidden state that summarizes information from the past. A common formulation is:

ht = φ(Wx xt + Wh ht-1 + bh)
ot = g(Wo ht + bo)

The input xt ∈ ℝdx, hidden state ht ∈ ℝdh, and output ot are vectors. Wx, Wh, and Wo are learned weight matrices; bh and bo are biases; φ is usually tanh or a related activation; and g depends on the task. For a K-class classifier, g is commonly softmax:

p(yt = k | ht) = exp(zt,k) / ∑j=1K exp(zt,j)

where zt = Wo ht + bo. The probabilities sum to one and are appropriate for mutually exclusive classes.

The same parameters are reused at every time step. This parameter sharing allows a model to process sequences of different lengths and gives the architecture a notion of temporal stationarity: similar transition rules are applied repeatedly. It does not, however, guarantee that the data-generating process is stationary.

Training Through Time

For a sequence with target outputs, a typical negative log-likelihood loss is:

L = -∑t=1T mt log p(yt | ht)

where mt is a mask equal to 1 for valid targets and 0 for padding or missing labels. Dividing by ∑t mt gives a mean loss rather than a sum and makes comparisons less dependent on sequence length.

Backpropagation through time (BPTT) unfolds the recurrence across time and applies the chain rule. If a loss at a later time depends on an earlier hidden state, repeated multiplication by recurrent Jacobians occurs. In simplified scalar form, gradient magnitude may behave approximately like:

|∂L/∂hk| ≈ |∂L/∂hT| ∏t=k+1T |∂ht/∂ht-1|

This expression is a conceptual simplification; in a real network the terms are matrices and their norms or singular values matter. If the effective factors are consistently below one, gradients vanish; if above one, they can explode. Vanishing gradients make distant dependencies difficult to learn, while exploding gradients produce unstable optimization.

Gradient clipping limits an update when its norm is too large. For a gradient vector g, norm clipping uses:

gclipped = g × min(1, τ / ||g||)

where τ is the chosen threshold. Clipping does not solve vanishing gradients and changes the optimization direction only when the norm exceeds the threshold.

3. LSTM Networks

Long short-term memory networks introduce an explicit cell state and gates. One widely used formulation is:

it = σ(Wi xt + Ui ht-1 + bi)
ft = σ(Wf xt + Uf ht-1 + bf)
ot = σ(Wo xt + Uo ht-1 + bo)
ĝt = tanh(Wg xt + Ug ht-1 + bg)
ct = ft ⊙ ct-1 + it ⊙ ĝt
ht = ot ⊙ tanh(ct)

Here, it, ft, and ot are input, forget, and output gates; ct is the cell state; ĝt is a candidate update; σ is the sigmoid function; and ⊙ denotes element-wise multiplication. All gate vectors have the hidden-state dimension. The gates take values between 0 and 1, allowing the network to regulate information flow.

The additive cell-state update is central. When a forget gate is near 1 and the input contribution is small, information can pass across many steps with less multiplicative distortion than in a vanilla RNN. This improves, but does not eliminate, long-range learning problems.

A forget-gate bias is often initialized positively so that the initial behavior favors retaining information. This is a practical initialization choice, not a mathematical requirement. LSTMs use more parameters than vanilla RNNs and may be slower, especially for long sequences.

4. GRU Networks

A gated recurrent unit combines gating functions with a simpler state structure. One common convention is:

zt = σ(Wz xt + Uz ht-1 + bz)
rt = σ(Wr xt + Ur ht-1 + br)
ñt = tanh(Wn xt + Un(rt ⊙ ht-1) + bn)
ht = (1 - zt) ⊙ ht-1 + zt ⊙ ñt

The update gate zt controls how much of the candidate replaces the previous state, while the reset gate rt controls how much previous information contributes to the candidate. Some software libraries use the complementary convention, so the exact interpretation of the update gate must be checked in documentation.

GRUs usually have fewer parameters than LSTMs because they do not maintain a separate cell state and use fewer gates. They can therefore train faster, although neither architecture is universally superior. Choice should be based on validation performance, latency, memory, sequence length, and robustness.

PropertyVanilla RNNLSTMGRU
State mechanismHidden stateHidden and cell statesHidden state
GatesNoneInput, forget, outputUpdate, reset
Long dependenciesDifficultUsually improvedUsually improved
Parameter costLowestHighest of the threeIntermediate
Typical useSimple or short sequencesComplex dependenciesEfficient gated modeling

5. Temporal Convolutional Networks

Recurrent processing is sequential: the state at time t depends on the computation at t−1. Temporal convolutional networks provide an alternative based on one-dimensional convolutions. A causal convolution ensures that output at time t does not use future input:

yt = ∑k=0K-1 wk xt-dk

Here, K is kernel size, d is dilation, and wk are kernel weights. Causality requires padding or indexing that excludes indices greater than t. With dilation, the receptive field expands without proportionally increasing the kernel size.

For a stack of L layers with kernel size K and dilations d1, ..., dL, the receptive field, assuming stride 1 and suitable causal padding, is:

R = 1 + (K - 1) ∑ℓ=1L d_ℓ

If dilations double as 1, 2, 4, ..., 2L-1, then:

R = 1 + (K - 1)(2L - 1)

These formulas assume no pooling or striding and a standard dilated stack. Residual connections, normalization, and nonlinearities are commonly added. TCNs permit parallel computation across time during training and can offer stable gradients, but their effective memory is bounded by the receptive field unless the architecture is expanded.

A forecasting model must not use ordinary same-padding that leaks future values. For a non-causal classification task, future context may be legitimate; for real-time forecasting, it is not.

6. Sequence-to-Sequence Learning and Attention

A sequence-to-sequence (seq2seq) model maps an input sequence to an output sequence of potentially different length. An encoder converts the input into representations, and a decoder generates output tokens or values.

In an encoder-only recurrent version:

htenc = F(xt, ht-1enc)

A basic decoder initializes its state from the final encoder state and predicts autoregressively:

p(yt | y<t, x1:T) = G(yt-1, ht-1dec, c)

where c is a context vector. Compressing a long input into one vector can create a bottleneck. Attention addresses this by allowing the decoder to use all encoder states:

et,s = score(ht-1dec, hsenc)
αt,s = exp(et,s) / ∑j=1T exp(et,j)
ct = ∑s=1T αt,s hsenc

The attention weights αt,s form a distribution over input positions for decoder step t. The score may be a dot product, additive network, or another learned compatibility function. Attention improves access to relevant positions, but attention weights should not automatically be interpreted as a complete causal explanation.

Teacher Forcing and Exposure Bias

During training, teacher forcing supplies the true previous target yt-1 to the decoder. During inference, the model must use its own previous prediction. This mismatch is called exposure bias. Scheduled sampling and sequence-level objectives are possible remedies, but they introduce their own optimization and statistical complications. In all cases, inference-time decoding must be evaluated under the actual deployment procedure.

For categorical outputs, decoding may use greedy selection, beam search, or sampling. Beam search retains a fixed number of high-scoring partial sequences. It can improve sequence likelihood but may reduce diversity and does not guarantee the best task-specific result.

7. Data Preparation and Training Practice

The quality of sequence learning is often determined before model selection.

Temporal Splitting and Leakage

Randomly splitting overlapping windows can place nearly identical future information in both training and test sets. A sound default is chronological splitting: earlier observations for training, a later validation period, and the latest period for testing. If multiple entities are present, entity-based separation may also be necessary.

Scaling parameters must be fitted only on training data. For a feature with training mean μ and standard deviation σ > 0, standardization is:

x' = (x - μ) / σ

The same μ and σ are then used for validation, test, and production data. Target transformations must be inverted carefully before reporting metrics.

Padding, Masking, and Missingness

Batches often contain sequences of different lengths. Padding makes tensor shapes uniform; masking prevents padded positions from contributing to loss or attention. Missingness should not silently be treated as zero. Depending on the process, useful features include an imputed value, a missingness indicator, and elapsed time since the last observation.

Optimization Choices

Useful practices include truncated BPTT for very long streams, gradient clipping for unstable recurrent training, dropout applied consistently with the chosen recurrent implementation, learning-rate schedules, early stopping based on validation data, and checkpointing. Stateful training requires explicit control of when hidden states are reset; otherwise information may unintentionally cross independent sequences.

8. Applications and Evaluation

Language

RNNs and gated networks can perform language modeling, tagging, classification, and generation. A language model estimates:

p(w1:T) = ∏t=1T p(wt | w<t)

where wt is the token at position t. Cross-entropy is standard for training. Perplexity is:

PPL = exp(−(1/N) ∑t=1N log p(wt | w<t))

where N is the number of evaluated tokens and natural logarithms are assumed. Lower perplexity indicates better predictive likelihood on the same tokenization and evaluation set, but it does not guarantee factuality, fairness, or usefulness.

Speech

Speech recognition converts acoustic frames into text. Sequences may be long, noisy, and sampled at a higher rate than linguistic units. Bidirectional encoders can use past and future acoustic context, but they are unsuitable for strict real-time settings unless latency is acceptable. Word error rate is commonly calculated as:

WER = (S + D + I) / N

where S is substitutions, D deletions, I insertions, and N the number of reference words. WER can exceed 1 and depends on normalization and alignment conventions.

Forecasting and Time-Series Analytics

Models may predict a single future value, multiple horizons, quantiles, or a full predictive distribution. Mean absolute error is:

MAE = (1/n) ∑i=1n |yi − ŷi|

and root mean squared error is:

RMSE = √[(1/n) ∑i=1n (yi − ŷi)2]

MAE is in the target's units and is less sensitive to large errors than RMSE. RMSE emphasizes large errors. Neither metric alone measures calibration. For probabilistic forecasts, interval coverage and proper scoring rules such as negative log-likelihood or continuous ranked probability score are more informative.

Always compare with appropriate baselines: last-value persistence, seasonal persistence, moving averages, classical exponential smoothing, or autoregressive models. A deep model that does not outperform a transparent baseline may not justify its cost or complexity.

9. Illustrative Case: Predictive Maintenance for Industrial Pumps

Background. A manufacturer operates pumps with vibration, pressure, temperature, and electrical-current sensors. Failures are expensive, but failure labels are rare and maintenance records are irregular.

Problem. The organization wants a seven-day warning of likely failure while avoiding excessive false alarms. Sensor streams arrive every minute, but some devices have outages.

Analysis. Engineers first define the prediction time precisely. A window is labeled positive if a confirmed failure occurs within the next seven days, while a buffer period prevents measurements taken after maintenance scheduling from leaking into training. Data are split by machine and by time so that the same machine's future behavior does not appear in training.

Approach. Missingness indicators and elapsed-time features are added. Robust scaling is fitted on the training period. A persistence-informed baseline and a gradient-boosted model on engineered statistics are compared with an LSTM and a causal TCN. The TCN uses a receptive field covering fourteen days, ensuring that the seven-day target can use sufficient history without future leakage. Class-weighted loss is considered because failures are rare. Threshold selection is performed on the validation set using maintenance capacity as a constraint.

Outcome. In an illustrative deployment analysis, the LSTM achieves slightly better recall than the baseline but requires greater inference latency. The TCN provides comparable recall with predictable latency and parallel training. The final decision should not be based only on an aggregate score: performance is examined by machine type, sensor availability, operating regime, and time since installation.

Learning points. The most important decisions concern label definition, leakage prevention, operational thresholds, and monitoring. A model is useful only if alerts are actionable, maintenance teams trust the evidence, and performance remains stable after equipment or operating conditions change.

10. Limitations, Governance, and Contemporary Directions

Sequence models can memorize sensitive text, infer personal attributes, reproduce historical discrimination, and expose information through generated outputs. Speech systems may perform differently across accents, languages, ages, and recording conditions. Forecasting systems can amplify inequity when resource allocation follows biased predictions.

Governance requires data minimization, access control, retention limits, documented provenance, consent where appropriate, and evaluation across relevant subgroups. High-impact decisions should include human oversight, appeal mechanisms, audit logs, and a clear distinction between prediction and causal explanation. In regulated environments, records should preserve training data versions, feature definitions, model versions, thresholds, and monitoring results.

Contemporary sequence modeling increasingly includes Transformer architectures, which use self-attention rather than recurrence as the central mechanism. They offer strong parallelism during training and flexible long-range interactions, but their attention cost can grow substantially with sequence length. State-space models, linear-attention methods, efficient long-context architectures, neural ordinary differential equations for irregular time, and hybrid systems combining convolution, recurrence, and attention are active research areas. These developments do not remove the need for temporal validation, leakage control, interpretable operational requirements, and careful uncertainty analysis.

Best-Practice Checklist

  1. Define the prediction time and information availability before constructing labels.
  2. Use chronological or group-aware splits appropriate to deployment.
  3. Fit preprocessing only on training data.
  4. Mask padding and represent missingness explicitly.
  5. Establish simple, domain-relevant baselines.
  6. Select architectures according to dependency length, latency, memory, and data volume.
  7. Use gradient clipping and monitoring when recurrent optimization is unstable.
  8. Evaluate multiple horizons, subgroups, regimes, and failure costs.
  9. Calibrate probabilities when decisions depend on risk thresholds.
  10. Monitor drift, data quality, latency, and post-deployment outcomes.

Chapter Summary

Sequence models learn from ordered observations whose interpretation depends on temporal context. Vanilla RNNs reuse a hidden-state transition but may suffer from vanishing and exploding gradients. LSTMs address information preservation through gated cell states, while GRUs provide a simpler gated alternative. Temporal convolutional networks use causal and dilated convolutions to obtain parallel training and controlled receptive fields.

Sequence-to-sequence systems map inputs to outputs of different lengths; attention allows each decoder step to access relevant encoder states. Successful applications depend not only on architecture but also on chronological splitting, leakage prevention, scaling, padding, missing-data treatment, optimization, and suitable baselines. Language, speech, forecasting, and predictive maintenance require task-specific metrics and deployment-aware evaluation. Finally, temporal models must be governed for privacy, fairness, reliability, uncertainty, and distribution shift.

Key Terms

Sequence: An ordered collection of observations.

Temporal dependence: Statistical or functional dependence between observations at different times.

Hidden state: A learned vector summarizing information used by a recurrent model.

Recurrent neural network: A neural architecture that updates a state sequentially.

Backpropagation through time: Gradient computation obtained by unfolding a recurrent model across time.

Vanishing gradient: A gradient that becomes very small through repeated transformations.

Exploding gradient: A gradient whose magnitude becomes excessively large.

Gradient clipping: Limiting gradient magnitude to improve optimization stability.

LSTM: A gated recurrent architecture with hidden and cell states.

GRU: A gated recurrent architecture with update and reset gates.

Causal convolution: A convolution that excludes future inputs from a current output.

Dilation: Spacing between kernel elements in a convolution.

Receptive field: The input region that can influence a model output.

Sequence-to-sequence model: A model mapping one sequence to another, possibly of different length.

Attention: A mechanism that computes a weighted combination of representations.

Teacher forcing: Training a decoder with the true previous target as input.

Exposure bias: The difference between training-time and inference-time decoder inputs.

Data leakage: Use of information unavailable at prediction time.

Concept drift: Change in the data-generating process after deployment.

Calibration: Agreement between predicted probabilities and observed frequencies.

Review Questions

Short Answer Questions

  1. Why can a random split be invalid for forecasting data?
  2. What roles do the forget and input gates play in an LSTM?
  3. Define a causal convolution.
  4. What is exposure bias in sequence-to-sequence learning?
  5. Why should missingness often be represented explicitly?

Long Answer Questions

  1. Derive the vanilla RNN update and explain how repeated Jacobian multiplication produces vanishing or exploding gradients.
  2. Compare LSTM and GRU architectures in terms of state structure, gates, parameter cost, and practical selection.
  3. Explain how a dilated TCN obtains a large receptive field and state the assumptions behind the receptive-field formula.
  4. Describe an encoder–decoder model with attention, including attention scores, normalized weights, and context vectors.
  5. Design a rigorous evaluation protocol for a multivariate demand-forecasting system.

Analytical / Application Questions

  1. A real-time forecasting model uses sensor windows with ordinary same-padding. Analyze the possible leakage and propose a causal redesign.
  2. A speech recognizer has low average WER but substantially higher WER for one accent group. Propose an investigation and mitigation plan.
  3. An industrial alert model has high recall but too many alarms for the maintenance team. Explain how threshold selection, calibration, costs, and monitoring should be used to redesign deployment.

Multiple Choice Questions with Answers

Question 1

Which mechanism most directly helps an LSTM preserve information over long intervals?

A. An additive cell-state update regulated by gates

B. Randomly resetting the hidden state at every step

C. Using a larger output vocabulary

D. Removing nonlinear activation functions

Correct Answer: A. An additive cell-state update regulated by gates

Question 2

In a causal temporal convolution, the output at time t may use:

A. Only inputs strictly after t

B. Inputs at t and earlier, subject to the receptive field

C. All inputs in the complete sequence

D. Only the input at t

Correct Answer: B. Inputs at t and earlier, subject to the receptive field

Question 3

What is the principal purpose of gradient clipping?

A. To eliminate all generalization error

B. To prevent excessively large optimization updates

C. To increase the sequence length automatically

D. To convert an RNN into a TCN

Correct Answer: B. To prevent excessively large optimization updates

Question 4

For a language model evaluated with natural logarithms, perplexity is the exponential of:

A. Mean negative log-likelihood

B. Mean absolute error

C. Classification accuracy

D. The vocabulary size

Correct Answer: A. Mean negative log-likelihood

Question 5

Which split is generally safest for evaluating a forecasting model?

A. A random split of overlapping windows

B. A chronological split that respects deployment time

C. A split based only on target magnitude

D. A split made after inspecting the test outcomes

Correct Answer: B. A chronological split that respects deployment time

Question 6

In attention, what do the normalized weights α_{t,s} represent?

A. The learning rate at decoder step t

B. The relative contribution assigned to encoder position s for decoder step t

C. The number of layers in the encoder

D. The probability that the sequence is padded

Correct Answer: B. The relative contribution assigned to encoder position s for decoder step t

Question 7

A model has errors 2, 2, and 5. What is its MAE?

A. 2

B. 3

C. 3.67

D. 9

Correct Answer: C. 3.67

Question 8

Which statement correctly distinguishes MAE from RMSE?

A. MAE is always larger than RMSE

B. RMSE places greater emphasis on large errors

C. RMSE has no target units

D. MAE can only be used for classification

Correct Answer: B. RMSE places greater emphasis on large errors

Question 9

What is exposure bias?

A. A mismatch between training-time true previous targets and inference-time predicted inputs

B. A mismatch between sensor units and labels

C. A failure caused only by exploding gradients

D. The use of causal padding in a TCN

Correct Answer: A. A mismatch between training-time true previous targets and inference-time predicted inputs

Question 10

For a GRU using the displayed convention, the update gate primarily controls:

A. The vocabulary size

B. How much the candidate replaces the previous hidden state

C. Whether future inputs are visible

D. The number of attention heads

Correct Answer: B. How much the candidate replaces the previous hidden state

Deep Learning
Chapter 7 of 15