§10.2.3–10.2.4RNNs as Directed Graphical Models · Conditioning on Context

Part II DL pp. 386–393 · ~8 min read

  • rnn as a directed graphical model
  • stationary transition
  • conditional rnn (context)

§10.2.3 Recurrent networks as directed graphical models

So far our loss L(t)L^{(t)} has been the cross-entropy between target y(t)y^{(t)} and output o(t)o^{(t)}. As with a feedforward net, almost any loss works — but we usually interpret the output as a probability distribution and use its cross-entropy. (Mean squared error, for instance, is just the cross-entropy of a unit-Gaussian output.) Training with a predictive log-likelihood objective trains the RNN to estimate the conditional distribution of the next element given the past — either logp(y(t)x(1),,x(t))\log p(y^{(t)} \mid x^{(1)}, \dots, x^{(t)}), or, if outputs feed back as inputs, logp(y(t)x(1),,x(t),y(1),,y(t1))\log p(y^{(t)} \mid x^{(1)}, \dots, x^{(t)}, y^{(1)}, \dots, y^{(t-1)}).

Take the purest case: an RNN modeling a sequence of scalars yy with no inputs xx, where the input at step tt is simply the output at t1t-1. By the chain rule of probability, the joint factorizes as a product of one-step predictions — and every past value may condition the present:

The sequence joint, factorized (eqs 10.31–10.33)

P(y)=t=1τP ⁣(y(t)y(t1),y(t2),,y(1)),L=tlogP ⁣(y(t)y(t1),,y(1))P(\mathbf{y}) = \prod_{t=1}^{\tau} P\!\big(y^{(t)} \mid y^{(t-1)}, y^{(t-2)}, \dots, y^{(1)}\big), \qquad L = \sum_t -\log P\!\big(y^{(t)} \mid y^{(t-1)}, \dots, y^{(1)}\big)
t=1τ\prod_{t=1}^{\tau}chain rule (eq 3.6): the joint over the whole sequence is a product of one-step conditional predictionsτ factors
P(y(t)y(t1),,y(1))P(y^{(t)} \mid y^{(t-1)},\dots,y^{(1)})each step conditions on the ENTIRE past — no Markov cutoffdistribution
L=tlogP()L = \sum_t -\log P(\cdots)the negative log-likelihood is again a sum of per-step termsscalar

Drawn as a graphical model over the yy values alone (marginalizing out the hidden units), this is the complete graph (fig 10.7): every past y(i)y^{(i)} links directly to each future y(t)y^{(t)} — an ever-growing fan-in. But re-introduce the hidden state h(t)h^{(t)} as a node (fig 10.8) and something remarkable appears: hh decouples the past from the future, every stage becomes identical, and the parameters can be shared. Toggle the two views, then grow τ\tau and the vocabulary kk to watch the tabular cost explode while the RNN’s stays flat:

Figs 10.7–10.8 (interactive) — the sequence as a directed graphical model. Complete graph: every past y links to each future y. Hidden-state view: h decouples past and future, so one shared f gives full connectivity at O(1) parameters — versus the tabular O(kᵗ).
y1y2y3y4y5
Tabular joint P(y⁽¹⁾,…,y⁽τ⁾)
O(kᵗ) = 35 = 243 entries
grows exponentially with sequence length
RNN (shared f, θ)
O(1) in τ — same parameter count
one transition function reused at every step

Complete graph: every past yⁱ may directly influence a future yᵗ — an ever-growing fan-in and, tabulated, an O(kᵗ) blow-up.

Full connectivity, efficient parametrization

Represent an arbitrary joint over τ discrete kk-ary variables as a lookup table and you need O(kτ)O(k^\tau) entries — hopeless for long sequences. The RNN obtains the same full connectivity (any past value can influence any future one, via hh) with O(1)O(1) parameters in τ\tau, by reusing the same function ff and parameters θ\theta at every step. The graphical-model view makes the trade explicit — and its price: fewer parameters can make optimization harder, and some operations (like predicting a missing value in the middle of a sequence) stay difficult.

Why sharing is allowed — stationarity. Using the same parameters at every step is equivalent to assuming the conditional distribution at step t+1t+1 given step tt is stationary : the relationship between consecutive steps does not depend on tt. (One could feed tt as an extra input to let the model discover time-dependence while still sharing — better than a separate distribution per tt, but then the network must extrapolate to new values of tt.)

Sampling — how does generation stop? To sample from the model we draw from the conditional at each step, but we also need a mechanism to decide the sequence length τ\tau. Three options:

Determining sequence length when sampling — click a cell for detail.
How it worksApplies to
End-of-sequence symbolAdd a special ⟨EOS⟩ token; stop when it is sampled.symbol-vocabulary outputs only
Bernoulli halt outputAn extra sigmoid output each step decides continue vs stop.any RNN
Predict τ directlyAn extra output predicts the integer length τ; feed τ−t as an extra input each step.any RNN
Dotted-underlined cells have explanations — click one.

§10.2.4 Modeling sequences conditioned on context

An RNN need not model only P(y)P(\mathbf{y}) — it can model P(yx)P(\mathbf{y} \mid \mathbf{x}). As with feedforward nets (§6.2.1), any model of P(y;θ)P(\mathbf{y}; \theta) becomes a model of P(yω)P(\mathbf{y} \mid \omega) by making the parameters ω\omega a function of the input x\mathbf{x}.

A single fixed vector as context. When x\mathbf{x} is one fixed-size vector (say, an image), it can enter the yy-generating RNN as (1) an extra input at every step, (2) the initial state h(0)h^{(0)}, or (3) both. The most common choice (fig 10.9) introduces a new weight matrix RR; the same product xR\mathbf{x}R is added to the hidden units at every step — effectively an input-dependent bias. This is exactly the shape of an image-captioning model: one image in, a sentence out.

Fig 10.9 — a fixed vector x mapped to a distribution over sequences. The new matrix R injects x·R into every hidden state (an input-dependent bias). Used for image captioning: one image → a sequence of words.
xRh⁽t−1⁾o⁽t−1⁾y⁽t−1⁾Rh⁽t⁾o⁽t⁾y⁽t⁾Rh⁽t+1⁾o⁽t+1⁾y⁽t+1⁾WW

A sequence as context. When the input is itself a sequence x(t)x^{(t)}, the RNN of eq 10.8 corresponds to a conditional distribution that makes a conditional independence assumption:

The conditional-independence assumption (eq 10.35)

P(y(1),,y(τ)x(1),,x(τ))=tP(y(t)x(1),,x(t))P\big(y^{(1)}, \dots, y^{(\tau)} \mid x^{(1)}, \dots, x^{(\tau)}\big) = \prod_{t} P\big(y^{(t)} \mid x^{(1)}, \dots, x^{(t)}\big)
t\prod_tthe joint over outputs factorizes into independent per-step predictions — the y values are conditionally independent GIVEN the x sequenceτ factors
P(y(t)x(1),,x(t))P(y^{(t)} \mid x^{(1)},\dots,x^{(t)})each y⁽ᵗ⁾ depends on the inputs up to t, but NOT directly on the other y valuesdistribution

To remove that assumption — to let the yy values depend on each other — add connections from the output at step tt to the hidden unit at step t+1t+1 (fig 10.10, another use of a matrix RR). Now the model can represent an arbitrary distribution over the yy sequence:

Fig 10.10 — a conditional RNN with output→hidden feedback (R). Unlike fig 10.3, the previous output feeds the next hidden state, so the y values are no longer conditionally independent given x. One restriction remains: input and output sequences share the same length (lifted in §10.4).
x⁽t−1⁾Uh⁽t−1⁾Vo⁽t−1⁾y⁽t−1⁾x⁽t⁾Uh⁽t⁾Vo⁽t⁾y⁽t⁾x⁽t+1⁾Uh⁽t+1⁾Vo⁽t+1⁾y⁽t+1⁾WWRR

That output-to-hidden model still has one restriction — input and output sequences must be the same length. Lifting that gives the encoder–decoder architecture: bidirectional RNNs, deep & recursive nets, and sequence-to-sequence.

Check yourself — RNNs as graphical models and conditioning on context

1.Why is an RNN's parametrization of the joint distribution over a sequence so much more efficient than a tabular representation?

2.In the hidden-state graphical-model view (fig 10.8), what role does h⁽ᵗ⁾ play?

3.Parameter sharing across time relies on which assumption?

4.When sampling from an RNN, how can the model decide when to STOP generating (determine the sequence length τ)?

5.To condition an RNN on a single fixed vector x (e.g. image captioning, fig 10.9), the book adds a new weight matrix R. What does R do, and what is the vector-input RNN's conditional-independence assumption (fig 10.10)?

5 questions