Network Settings

Network Inspector

Hover over the unrolled 3D network to inspect states and trace data flow. Click a node to focus camera.

Rotate L-Drag
Pan R-Drag
Zoom Scroll
Focus Click Node

Understanding Recurrent Neural Networks

By Updated

Unlike traditional feedforward networks which process data in single, independent snapshots, Recurrent Neural Networks (RNNs) are designed specifically for sequential data. They maintain a "memory" of previous inputs by passing information across time steps.

1. The Temporal Dimension (Unrolling)

In the 3D visualization above, the network is "unrolled" across time along the Z-axis. While it looks like a massive network, it is actually the exact same set of weights being applied repeatedly at each time step $t$. The $Z$-axis visually represents the passage of time.

2. Deep RNNs (Multiple Layers)

Just like standard neural networks benefit from depth, RNNs can be stacked into Deep RNNs. By setting the "Hidden Layers" dropdown to 2 or 3, you create a hierarchy. The first hidden layer extracts basic temporal features from the raw input sequence, while higher layers piece together those basic features to understand more complex, long-term abstractions. In deep RNNs, the output of Hidden Layer 1 at time $t$ serves as the input to Hidden Layer 2 at time $t$.

3. RNN Architectures

By varying which inputs and outputs we pay attention to across time steps, we can solve drastically different problems. You can switch between these architectures using the settings above:

4. Sequence Length, Padding & Truncation

Neural networks generally require inputs of a fixed size. If you set the Sequence Length to 6, but type a sentence with only 4 words, the network must pad the remaining sequence. We do this by passing a blank [PAD] token (visually represented as faded inputs) to fill the remaining time steps. Conversely, if your sentence is too long, the excess words are truncated and ignored.

5. The Hidden State Math

The core feature of an RNN is its Hidden State ($h_t$). At any given time step $t$, a hidden layer $l$ receives two distinct inputs:

$$\mathbf{h}^{(l)}_t = \tanh\left(\mathbf{W}_{forward}^{(l)} \mathbf{input}_t + \mathbf{b}^{(l)} + \mathbf{W}_{recurrent}^{(l)} \mathbf{h}^{(l)}_{t-1}\right)$$

Read that equation as a sentence: the new memory is a squashed sum of what just arrived and what I already remembered. The $\tanh$ keeps the hidden state bounded between $-1$ and $1$, which prevents the values from growing without limit as they are fed back in step after step.

6. Counting the Weights

An RNN cell has three sets of learnable parameters, and every one of them is shared across all time steps. That sharing is what makes a recurrent network able to handle sequences of any length with a fixed number of weights.

$$ \text{params} = h \times d + h \times h + h = h(d + h + 1) $$

With an input size $d = 50$ and a hidden size $h = 100$, that is $100 \times (50 + 100 + 1) = 15{,}100$ weights — whether the sequence is 6 steps long or 600. The recurrent matrix dominates as the hidden size grows, since it scales with $h^2$ while the input matrix scales only with $h \times d$.

This is the direct analogue of parameter sharing in a convolutional network. There, one filter is reused at every position; here, one weight matrix is reused at every time step. Both assume that what is worth computing at one place in the data is worth computing everywhere in it.

7. Backpropagation Through Time

Training works by unrolling the network exactly as the visualisation above shows it, treating the result as a very deep feedforward network, and running ordinary backpropagation. The gradient for the shared weights is the sum of their contributions at every time step — which is why one weight matrix can be trained by many steps at once.

The complication is depth. A 100-step sequence unrolls into a 100-layer network, and the gradient must travel back through all of it. Because each step multiplies by the same recurrent matrix, that repeated multiplication is where recurrent networks get their notorious training problems.

Two practical techniques address the cost rather than the mathematics. Truncated backpropagation through time unrolls only the most recent $k$ steps, capping memory use at the price of never learning dependencies longer than $k$. Gradient clipping rescales any gradient whose norm exceeds a threshold, which is a crude fix and an extremely effective one.

8. Vanishing and Exploding Gradients

Propagating a gradient back through $n$ time steps involves multiplying by the recurrent weight matrix roughly $n$ times. The outcome depends on the magnitude of that matrix, and neither case is benign:

Exploding gradients are loud and easy to fix — clip them. Vanishing gradients are the harder problem, because nothing crashes. Training appears to proceed, the loss falls a little, and the model simply never learns anything that depends on information from more than ten or twenty steps ago.

The classic demonstration is a sentence whose subject and verb are far apart: "The keys that I left on the table in the hallway upstairs are missing." Choosing "are" over "is" requires remembering that the subject was plural, eleven words earlier. A plain RNN reliably fails at this, and the failure is a property of the architecture rather than of insufficient training.

9. LSTM and GRU: Adding a Gate

Long Short-Term Memory cells solve the vanishing-gradient problem by adding a separate cell state that information can travel along almost unchanged, plus three learned gates that decide what happens to it:

The important structural difference is that the cell state is updated by addition rather than by repeated matrix multiplication. Addition does not shrink a gradient, so a signal can survive hundreds of steps. This is the same trick as a residual connection in a deep convolutional network, arrived at independently and years earlier.

A GRU merges the forget and input gates into a single update gate and drops the separate cell state, giving roughly three-quarters of the parameters and comparable accuracy on most tasks. The practical advice is unglamorous: try a GRU first because it trains faster, and switch to an LSTM if the task genuinely needs longer memory.

10. Bidirectional and Stacked Variants

A plain RNN reads left to right, so its state at step $t$ knows nothing about what comes later. For many tasks that is an artificial handicap — when classifying the sentiment of a complete review, the end of the sentence is available and useful.

A bidirectional RNN runs two independent recurrences, one forward and one backward, and concatenates their hidden states. Every position then has context from both directions, at twice the parameters and twice the compute. It is the right default for classification and tagging, and it is impossible for genuine real-time prediction, since the backward pass needs the whole sequence up front.

Stacking, which you can switch on with the "Hidden Layers" control above, is the other axis. Two or three layers usually help; beyond that, returns diminish quickly and training difficulty rises. Depth in a recurrent network is far less productive than depth in a convolutional one, because the unrolled network is already extremely deep in the time direction.

11. What Replaced Them, and What Did Not

Since 2017, Transformers have displaced RNNs across most of natural language processing, for one decisive reason: an RNN must process step $t$ before step $t+1$, so training cannot be parallelised along the sequence. A Transformer's attention mechanism looks at all positions simultaneously, which maps directly onto GPU hardware and made training on internet-scale corpora feasible.

Attention also connects any two positions in one step rather than through a chain of $n$ intermediate states, which removes the long-range dependency problem outright instead of mitigating it.

Recurrent models remain preferable in specific circumstances, and they are worth knowing rather than being treated as history:

12. Recap

Predict, then reveal

About to run: switch Architecture Type to One-to-One (e.g., Standard NN). Before it does — what happens to the readout?

Committing to an answer first is the point — the reveal runs the experiment on the visualisation above and reads the real value back, so nothing here is scripted.

Recall check

0 of 3

Say the answer out loud before you reveal it — recalling it is what makes it stick, and rereading it is not.

  1. What does this module say about “Network Inspector”?

  2. What does this module say about “The Temporal Dimension (Unrolling)”?

  3. What does this module say about “Deep RNNs (Multiple Layers)”?

Cheat sheet

Interactive 3D RNN Visualizer

Unlike traditional feedforward networks which process data in single, independent snapshots, Recurrent Neural Networks (RNNs) are designed specifically for sequential data. They maintain a "memory" of previous inputs by passing information across time steps.

NLP · vizlearn.in/natural_language_processing/rnn.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.