RNN Forward Pass

Four-panel comic: 'Encode' shows a robot stamping a one-hot card for the letter 'e'; 'Blend' shows a robot chef mixing 'new input' and 'old memory' in a tanh blender to produce 'updated memory'; 'Vote' shows a softmax election where robots shout scores 7.4, 2.7, 1.1 yielding 66%, 24%, 10%; 'Penalty' shows cross-entropy court where a robot judge contrasts a tiny 0.1 fine for 'confident and right' vs a huge 4.6 fine for 'confident and wrong'
What You’ll Learn

You’ll learn how an RNN turns inputs into one-hot vectors, blends each input with the previous hidden state, and produces a prediction. You’ll also see how softmax turns scores into probabilities and how cross-entropy loss grades those predictions.

In the Introduction, we learned what an RNN does: it processes sequences while maintaining memory. Now let’s see how it actually computes predictions, step by step.

Watch the Forward Pass First

Before we break anything down, watch it work. Step through each word and see how the hidden state (memory) updates and predictions shift:

🧠
RNN Step-by-Step
Watch how memory builds as each word is read
β†’ β†’ β†’
Memory before
Empty β€” nothing read yet
memory pattern
reads "I"
↓
mix old memory
+ new word,
then compress
↓
Memory after
"I"
updated pattern
πŸ’¬ First word: "I". The memory activates for the first time, but with just one word the predictions are spread thin.
🎯 Next word prediction:
"love"
30%
"my"
26%
"dog"
24%
Step 1 of 4

What to look for: Step through the words one at a time. Watch the hidden state β€œfingerprint” (the colored dots) change after each word as the network blends new input with old memory. Notice how the prediction bars reshuffle at every step: after β€œI,” many next-words are plausible; after β€œI love my,” the distribution narrows sharply toward pets and family.

Now let’s break down what happens at each stage.

Step 1: Encoding the Input

Computers don’t understand words β€” they understand numbers. So we first convert each character or word into a vector.

One-Hot Encoding

The simplest approach: a vector of all 0s except for a single 1 at the position of our character.

If our vocabulary is ['a', 'b', 'c', 'd']:

  • β€˜a’ β†’ [1, 0, 0, 0]
  • β€˜b’ β†’ [0, 1, 0, 0]
  • β€˜c’ β†’ [0, 0, 1, 0]
  • β€˜d’ β†’ [0, 0, 0, 1]

The vector has exactly one β€œhot” (1) element β€” hence the name one-hot encoding.

Step 2: Updating the Hidden State

This is the core of an RNN. The network combines two things:

  • What it just saw β€” the current input
  • What it remembers β€” the previous hidden state

Think of it like a blender: you pour in the new ingredient (current input) and the old smoothie (previous memory), blend them together, and get a new smoothie (updated memory). The blending recipe is what the network learns during training.

Let’s trace through a concrete example. Imagine processing β€œto be” and we just reached the letter 'e':

IngredientWhat it contributesIntuition
Current input”What signal does 'e' carry?”The new ingredient
Previous memory”What did we learn from 'to b'?”The old smoothie
BiasLearned offset, adds flexibilityA pinch of salt
Squashing (tanh)Keeps values in [-1, 1] so they don’t explodeThe blender’s speed limit

Why squash the result? Without it, each step multiplies values by weight matrices β€” they’d grow or shrink without bound. The squashing function is a β€œpressure valve”: no matter what’s fed in, the output stays between βˆ’1 and 1.

Fill in the Blanks
The hidden state is computed by combining the input with the hidden state, then applying a non-linear activation.
Fill in every blank, then check your answer.

Step 3: Computing Output Probabilities

Now we turn the hidden state into a prediction: β€œWhat’s the most likely next character?”

Raw Scores (Logits)

First, we compute one raw score per vocabulary item. These raw scores are called logits β€” they can be any real number, positive or negative. Higher score = network thinks that character is more likely. But they’re not yet probabilities β€” they don’t sum to 1.

Softmax: From Scores to Probabilities

We need to convert logits into proper probabilities. You might think: β€œjust divide each score by the total.” But that fails with negative scores. Softmax solves this β€” think of it as an election where confident voters shout louder:

  1. Every score gets exponentiated β€” this makes everything positive, and amplifies differences
  2. Then we divide by the total so everything sums to 1

Concrete example: logits are [2.0, 1.0, 0.1]:

  • Exponentiate: [e^2.0, e^1.0, e^0.1] = [7.4, 2.7, 1.1]
  • Normalize: [7.4/11.2, 2.7/11.2, 1.1/11.2] = [0.66, 0.24, 0.10]

The gap between 2.0 and 1.0 in logit space becomes a 2.7x gap in probability space β€” softmax amplifies differences.

Quick Check
If the logits before softmax are [2.0, 1.0, 0.1], which statement is true about the softmax output?
Your turn
Choose an answer to check your understanding.

Step 4: Measuring the Error (Loss)

How do we know if our prediction was good? We need a loss function β€” a single number that says β€œhow wrong was the model?”

We use cross-entropy loss β€” it takes the probability the model assigned to the correct answer and applies negative log. Think of it as a confidence penalty:

  • Predicted correctly with probability 0.9 β†’ loss = 0.10 β€” low, well done
  • Predicted correctly with probability 0.01 β†’ loss = 4.6 β€” huge penalty!

The key property: being confidently wrong is punished far more than being slightly uncertain. This is exactly what we want β€” the model should never be confident and wrong.

Quick Check
In the forward pass, why do we store all the intermediate values (xs, hs, ys, ps)?
Your turn
Choose an answer to check your understanding.

Explore the Computation Graph

Ready to go deeper? This interactive graph lets you inspect every single node β€” one-hot vectors, weight matrix multiplications, hidden states, logits, and softmax outputs β€” as the RNN processes a sentence character by character:

RNN Computation Graph

Forward
vectors matrices ops activations
100%
p (softmax)y (logits)h (hidden)x (input)β†’ forward
V=0 H=4 L=β€”

What to look for:

  • x (red): One-hot encoded inputs, sparse vectors with a single 1
  • h (green): Hidden states, updated at every timestep
  • y and p (blue): Output logits (yy) and final softmax probability distributions (pp)
  • W (yellow) and b (indigo): Shared weight matrices and bias vectors reused across all timesteps

Quick Summary

StepWhat happensIntuition
1. EncodeConvert input to one-hot vectorGive the character an address in vocabulary space
2. UpdateBlend input + memory through squashingNew ingredient + old smoothie = updated smoothie
3. OutputCompute raw scores (logits)β€œHow likely is each character?β€œ
4. SoftmaxConvert scores to probabilitiesElection: confident voters shout louder
5. LossMeasure prediction errorConfidence penalty: being wrong and sure is expensive
Learning Checklist
  • I can explain what a one-hot vector is and why its size equals the vocabulary size
  • I understand how the hidden state combines new input with previous memory
  • I know why tanh is used as the activation function (keeps values bounded)
  • I can trace through the softmax computation and explain why it uses exponentials
  • I understand cross-entropy loss and why confident wrong predictions are heavily penalized

Next Steps

You now know exactly how an RNN makes predictions. But how does it learn β€” how do the weights get tuned? Next up: Backward Pass & BPTT β†’, where gradients flow backwards through the sequence to update every parameter.