Understanding RNNs: Why Not compare it with FNN to Understand the Math Behind it Better? – DAY 58

A feedforward network applies a fixed sequence of transformations to its input. An RNN repeatedly applies a shared transition to a hidden state plus the next input. That state creates an additional path for gradients.

In this article we try to show an example of FNN and for RNN TO understand the math behind it better by comparing to each other:

 

Neural Networks Example

Example Setup

  • Input for FNN: x=0.5
  • Target Output for FNN: y=0.8
  • RNNs are tailored for sequential data because they are designed to remember and utilize information from previous inputs in a sequence, allowing them to capture temporal relationships and context effectively. This characteristic differentiates RNNs from other neural network types that are not inherently sequence-aware., Input for RNN (Sequence): X=[0.5,0.7]
  • Target Output for RNN (Sequence): Y=[0.8,0.9]
  • Learning Rate: η=0.1

1. Feedforward Neural Network (FNN)

Structure

  • Input Layer: 1 neuron
  • Hidden Layer: 1 neuron
  • Output Layer: 1 neuron

Weights and Biases

  • Initial Weights:
    • Wih=0.4 (Input to Hidden weight)
    • Who=0.6 (Hidden to Output weight)
  • Biases:
    • bh=0.1 (Hidden layer bias)
    • bo=0.2 (Output layer bias)

Step-by-Step Calculation for FNN

Step 1: Forward Pass

  1. Hidden Layer Output:

    h=ReLU(Wih⋅x+bh)=ReLU(0.4⋅0.5+0.1)=ReLU(0.2+0.1)=ReLU(0.3)=0.3

  2. Output:

    ypred=Who⋅h+bo=0.6⋅0.3+0.2=0.18+0.2=0.38

Step 2: Loss Calculation

Using Mean Squared Error (MSE):

L=12(ypred−y)2=12(0.38−0.8)2=12(−0.42)2=12⋅0.1764=0.0882

Step 3: Backward Pass

  1. Gradient of Loss with respect to Output:

    ∂L∂ypred=ypred−y=0.38−0.8=−0.42

  2. Gradient of Output with respect to Hidden Layer:

    ∂ypred∂h=Who

  3. Gradient of Hidden Layer Output with respect to Weights:

    ∂L∂Who=∂L∂ypred⋅∂ypred∂Who=−0.42⋅0.3=−0.126

    ∂L∂Wih=∂L∂ypred⋅∂ypred∂h⋅∂h∂Wih=−0.42⋅Who⋅∂h∂Wih

    Assuming ∂h∂Wih=0.5:

    ∂L∂Wih=−0.42⋅0.6⋅0.5=−0.126

Step 4: Weight Update

  1. Update Output Weight:

    Who=Who−η⋅∂L∂Who=0.6−0.1⋅(−0.126)=0.6+0.0126=0.6126

  2. Update Input Weight:

    Wih=Wih−η⋅∂L∂Wih=0.4−0.1⋅(−0.126)=0.4+0.0126=0.4126

2. Recurrent Neural Network (RNN)

Structure

  • Input Layer: 1 neuron
  • Hidden Layer: 1 neuron
  • Output Layer: 1 neuron

Weights and Biases

  • Initial Weights:
    • Wxh=0.5 (Input to Hidden weight)
    • Whh=0.3 (Hidden to Hidden weight)
    • Why=0.7 (Hidden to Output weight)
  • Biases:
    • bh=0.1 (Hidden layer bias)
    • bo=0.2 (Output layer bias)

NOW Lets check Step-by-Step example Calculation for RNN

Step 1: Forward Pass

Assuming initial hidden state h0=0. This is where the memory concept starts; the hidden state retains information from previous time steps.

  1. For t=1 (Input x1=0.5):
    • Hidden State:

      h1=tanh(Wxh⋅x1+Whh⋅h0+bh)=tanh(0.5⋅0.5+0.3⋅0+0.1)=tanh(0.25+0.1)=tanh(0.35)≈0.337

      Here, h1 is influenced by the previous hidden state h0 (which is 0). This demonstrates how the RNN maintains memory; the hidden state captures the relevant information to influence future computations.

    • Output:

      y1,pred=Why⋅h1+bo=0.7⋅0.337+0.2≈0.2359+0.2=0.4359

  2. For t=2 (Input x2=0.7):
    • Hidden State:

      h2=tanh(Wxh⋅x2+Whh⋅h1+bh)=tanh(0.5⋅0.7+0.3⋅0.337+0.1)

      =tanh(0.35+0.1011+0.1)=tanh(0.5511)≈0.500

      In this step, h2 is influenced by both the current input x2 and the previous hidden state h1. This reflects the memory of the previous input and its influence on the current state.

    • Output:

      y2,pred=Why⋅h2+bo=0.7⋅0.500+0.2=0.35+0.2=0.55

Step 2: Loss Calculation

Using Mean Squared Error (MSE) for the sequence:

  1. For t=1:

    L1=12(y1,pred−0.8)2=12(0.4359−0.8)2≈0.0613

  2. For t=2:

    L2=12(y2,pred−0.9)2=12(0.55−0.9)2≈0.0600

Total Loss:

Ltotal=L1+L2≈0.0613+0.0600=0.1213

Step 3: Backward Pass (BPTT)

This is where backpropagation through time takes place. The gradients are computed considering how each hidden state affects the output across the entire sequence.

  1. Gradient of Loss w.r.t Output:
    • For t=1:

      ∂L1∂y1,pred=y1,pred−0.8=0.4359−0.8=−0.3641

    • For t=2:

      ∂L2∂y2,pred=y2,pred−0.9=0.55−0.9=−0.35

  2. Gradient of Output w.r.t Hidden:

    ∂y1,pred∂h1=Why=0.7

    ∂y2,pred∂h2=Why=0.7

  3. Gradient of Hidden States:
    • For t=1:

      ∂L1∂h1=∂L1∂y1,pred⋅∂y1,pred∂h1=−0.3641⋅0.7=−0.2549

    • For t=2:

      ∂L2∂h2=∂L2∂y2,pred⋅∂y2,pred∂h2=−0.35⋅0.7=−0.245

    • Memory Influence: The hidden state h2 depends on h1 and the current input x2. Thus, the gradients also account for the memory stored in previous hidden states.
  4. Gradient for Weights:
    • For Why:

      ∂L1∂Why=∂L1∂y1,pred⋅h1=−0.3641⋅0.337=−0.122

    • For Whh:

      ∂L2∂Whh=∂L2∂y2,pred⋅h1⋅Whh=−0.35⋅0.500⋅0.3=−0.0525

Step 4: Weight Update

  1. Update Weights:
    • For Why:

      Why=Why−η⋅∂L1∂Why=0.7−0.1⋅(−0.122)=0.7+0.0122=0.7122

    • For Whh:

      Whh=Whh−η⋅∂L2∂Whh=0.3−0.1⋅(−0.0525)=0.3+0.00525=0.30525

Summary Table

Feedforward Neural Network (FNN)

StepCalculation
Forward Passh=0.3, ypred≈0.38
LossL≈0.0882
Gradient (Output)∂L∂ypred≈−0.42
Weight UpdateWho=0.6126, Wih=0.4126

Recurrent Neural Network (RNN)

StepCalculationExplanation
Forward Passh1≈0.337, y1,pred≈0.4359Hidden state h1 remembers h0 (initial state).
 h2≈0.500, y2,pred≈0.55Hidden state h2 remembers h1, capturing memory.
LossLtotal≈0.1213Total loss calculated across the sequence.
Gradient (Output)For t=1:−0.3641, t=2:−0.35Gradients computed for each time step output.
Weight UpdateWhy=0.7122, Whh=0.30525Weights updated based on contributions from all previous states.

Key Takeaways

  • FNN: Each input is treated independently, and the backpropagation process is straightforward.
  • RNN: The model retains memory of previous states through the hidden state, making the calculations for gradients more complex, especially during backpropagation through time (BPTT). Each hidden state influences subsequent outputs and reflects the model’s ability to remember past inputs.

Another concept to understand here is, memorization in RNNs happens through the hidden states. Each hidden state (h_t) carries information from previous inputs:

  • At time step t=1, the hidden state h1 is influenced by the initial hidden state h_0 (which is 0).
  • At time step t=2, the hidden state h2 is influenced by both the current input x2 and the previous hidden state h1.

Our brief example  here was to explain the math behind Feedforward Neural Networks (FNNs) and Recurrent Neural Networks (RNNs) in a simple way, by highlighting their mathematical differences you can understand each model better.

Illustration from the neural network comparison

Don’t forget to check our apps! Visit here.

Correct the derivative through the recurrent link

Differentiate the second output with respect to its hidden state, the hidden state with respect to its pre-activation, and the pre-activation with respect to the recurrent weight. The initial state is zero, so the first hidden state has no dependence on that recurrent weight in this two-step example.

∂L2∂Whh=(y^2−y2)Why(1−h22)h1

Using the rounded original values, the four factors are −0.35, 0.7, 0.75, and 0.337. They give about −0.0619. The rate 0.1 therefore changes the recurrent weight from 0.3 to about 0.3062. For the readout weight, the two direct contributions are about −0.1227 and −0.175, for a total of about −0.2977.

Correct the derivative through the recurrent linkFirst hiddenCarry historySecond hiddenApply shared linkSecond lossDifferentiate back
At later times the same recurrent weight may influence the loss through several earlier states.

Check the output and state gradients

A feedforward model can accept a fixed lookback window by flattening it. An RNN shares the transition across positions and can process varying lengths. Compare the same observation windows and use masks when batches contain padding.

A numerical gradient check is handy for these small examples. Evaluate the same summed loss at slightly larger and smaller weights, divide their difference by twice the perturbation, and compare with the analytic derivative.

References

Practice and FAQs

Why is the recurrent derivative proportional to the previous hidden state?

The recurrent weight multiplies that state in the current pre-activation. The local derivative of the pre-activation with respect to the weight is the previous state.

What happens when tanh is saturated?

Its derivative is small, so the gradient passed back through that activation is small.

Does a feedforward model have to ignore history?

No. It can take a fixed-length history as input. An RNN incorporates history through a repeatedly updated state.

You Can Also Learn Deep Learning in 80 Days Inside the iOS App Continue this roadmap in a focused mobile study experience with offline access, flashcards, notes, and progress tracking.
Open on the App Store