๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #207

Why RNN

The Recurrent Neural Network (RNN) was designed to satisfy exactly the three requirements laid out in Sequential Data: handle variable-length sequences, share parameters across every position, and maintain a memory of earlier elements while processing later ones.

The Core Idea

Instead of processing an entire sequence at once, an RNN processes it one element at a time, maintaining a running "hidden state" that acts as a compressed summary of everything seen so far. At each step, the network combines the new input with this running summary to produce an updated summary โ€” carrying information forward through the sequence.

How This Solves Each Requirement

RequirementHow RNNs Satisfy It
Variable-length sequencesThe same step-by-step process simply repeats for however many elements the sequence has โ€” no fixed input size required
Shared parameters across positionsThe exact same weight matrices are reused at every single time step โ€” the network learns one set of "how to update the summary" rules, applied uniformly regardless of position
Memory of earlier elementsThe hidden state, carried from step to step, is precisely this memory

Diagram โ€” The High-Level Idea

RNN cell hidden state loops back x_t h_t used for output y_t, and passed to the next step

The same cell processes each element of the sequence in turn, its hidden state carrying forward a summary of everything seen so far.

Code โ€” The Conceptual Loop

import torch
import torch.nn as nn

rnn_cell = nn.RNNCell(input_size=10, hidden_size=20)

sequence = torch.randn(5, 1, 10)   # 5 time steps, batch size 1, 10 features each
h = torch.zeros(1, 20)               # initial hidden state, usually all zeros

for t in range(5):
    x_t = sequence[t]
    h = rnn_cell(x_t, h)   # the SAME cell (same weights) is reused at every time step
    print(f"step {t}: hidden state shape {h.shape}")

Common Mistakes

  • Assuming an RNN has a separate set of weights for each time step โ€” it uses exactly one shared set of weights, reused identically at every step; this is precisely what lets it generalize to sequences of any length.
  • Confusing an RNN's "hidden state" with a hidden layer in a standard feedforward network โ€” an RNN's hidden state specifically carries information across time steps, not just across layers within one forward pass, as detailed in the next note.

Interview Relevance

Q: "What's the key architectural idea that lets an RNN handle sequences of any length with a fixed number of parameters?" Weight sharing across time steps โ€” the exact same weight matrices are applied at every position in the sequence, rather than learning distinct weights per position. This means the number of parameters depends only on the hidden state size and input feature size, never on how long the sequence happens to be.

Practice Question

Why does processing a sequence element-by-element with a carried hidden state solve the "variable length" problem that a fixed-size MLP input cannot?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Why RNN โ€“ FAQs

Quick answers about learning Why RNN in Deep Learning.

This free note from CodingNow 2.0 explains Why RNN in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Why RNN, is 100% free with no signup required.
With focused practice, most students grasp Why RNN in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now