§10.2.3 Recurrent networks as directed graphical models
So far our loss has been the cross-entropy between target and output . 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 , or, if outputs feed back as inputs, .
Take the purest case: an RNN modeling a sequence of scalars with no inputs , where the input at step is simply the output at . 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)
| chain rule (eq 3.6): the joint over the whole sequence is a product of one-step conditional predictions | τ factors | |
| each step conditions on the ENTIRE past — no Markov cutoff | distribution | |
| the negative log-likelihood is again a sum of per-step terms | scalar |
Drawn as a graphical model over the values alone (marginalizing out the hidden units), this is the complete graph (fig 10.7): every past links directly to each future — an ever-growing fan-in. But re-introduce the hidden state as a node (fig 10.8) and something remarkable appears: decouples the past from the future, every stage becomes identical, and the parameters can be shared. Toggle the two views, then grow and the vocabulary to watch the tabular cost explode while the RNN’s stays flat:
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 -ary variables as a lookup table and you need entries — hopeless for long sequences. The RNN obtains the same full connectivity (any past value can influence any future one, via ) with parameters in , by reusing the same function and parameters at every step. The graphical-model view rnn as a directed graphical model The view of an RNN over outputs y as a graphical model whose hidden state h^(t) gives an efficient O(1)-parameter, stationary parametrization of dependencies that a tabular model would need O(k^τ) parameters to represent. defined in ch. 10 — open in glossary 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 given step is stationary stationary transition The assumption that the conditional distribution over variables at one time step given the previous ones is the same at every step — what lets an RNN share one transition function across all of time. defined in ch. 10 — open in glossary : the relationship between consecutive steps does not depend on . (One could feed as an extra input to let the model discover time-dependence while still sharing — better than a separate distribution per , but then the network must extrapolate to new values of .)
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 . Three options:
| How it works | Applies to | |
|---|---|---|
| End-of-sequence symbol | Add a special ⟨EOS⟩ token; stop when it is sampled. | symbol-vocabulary outputs only |
| Bernoulli halt output | An extra sigmoid output each step decides continue vs stop. | any RNN |
| Predict τ directly | An extra output predicts the integer length τ; feed τ−t as an extra input each step. | any RNN |
§10.2.4 Modeling sequences conditioned on context
An RNN need not model only — it can model . As with feedforward nets (§6.2.1), any model of becomes a model of by making the parameters a function of the input .
A single fixed vector as context. When is one fixed-size vector (say, an image), it can enter the -generating RNN as (1) an extra input at every step, (2) the initial state , or (3) both. The most common choice (fig 10.9) introduces a new weight matrix ; the same product is added to the hidden units at every step — effectively an input-dependent bias. This is exactly the shape of an image-captioning conditional rnn (context) An RNN whose generation is conditioned on an extra input: a fixed vector x can enter as an extra per-step input (weight matrix R), as the initial state h^(0), or both (e.g. image captioning), or the condition can itself be a sequence. defined in ch. 10 — open in glossary model: one image in, a sentence out.
A sequence as context. When the input is itself a sequence , the RNN of eq 10.8 corresponds to a conditional distribution that makes a conditional independence assumption:
The conditional-independence assumption (eq 10.35)
| the joint over outputs factorizes into independent per-step predictions — the y values are conditionally independent GIVEN the x sequence | τ factors | |
| each y⁽ᵗ⁾ depends on the inputs up to t, but NOT directly on the other y values | distribution |
To remove that assumption — to let the values depend on each other — add connections from the output at step to the hidden unit at step (fig 10.10, another use of a matrix ). Now the model can represent an arbitrary distribution over the sequence:
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)?