RNN Structure

(RNN की संरचना और गणितीय कार्यविधि):अब हम RNN की आंतरिक संरचना (Structure) को विस्तार से समझते हैं — ताकि यह स्पष्ट हो सके कि RNN किस तरह sequential data को process करता है और memory बनाए रखता है।


🔶 1. Basic Idea Behind RNN

RNN का मुख्य विचार यह है कि यह input sequence के हर step पर एक ही cell (या unit) को बार-बार उपयोग करता है, लेकिन हर बार अलग hidden state के साथ।

Structure (Unrolled):

x₁ ──► [RNN Cell] ──► h₁  
x₂ ──► [RNN Cell] ──► h₂
x₃ ──► [RNN Cell] ──► h₃
(shares weights)
  • xt​: Input at time step t
  • ht: Hidden state at time step t

RNN में hidden state ht, पहले state ht−1​ और वर्तमान input xtपर निर्भर करता है।


🧠 2. Key Components of RNN

ComponentDescription
Input xtSequence का current step
Hidden state htMemory representation
Weights WShared across time steps
Output ytFinal prediction (optional at each step)

🧮 3. Mathematical Equations

🔹 Hidden State Update:

  • 🔹 Output (Optional):

🔄 4. Weight Sharing

RNNs में हर time step पर same weights

​ use होते हैं। यह मॉडल को बहुत parameter efficient बनाता है।


🧱 5. RNN Cell Diagram

          x_t

[ Linear Layer ]

+ h_{t-1}

[ tanh Activation ]

h_t

(optional)
y_t

🔧 6. PyTorch Implementation (Simple RNN Layer)

import torch
import torch.nn as nn

rnn = nn.RNN(input_size=8, hidden_size=16, batch_first=True)

x = torch.randn(4, 10, 8) # batch of 4, sequence length 10, features=8
h0 = torch.zeros(1, 4, 16) # (num_layers, batch, hidden_size)

output, hn = rnn(x, h0)

print(output.shape) # → [4, 10, 16]
print(hn.shape) # → [1, 4, 16]

📊 7. Variants of RNNs (आगे के टॉपिक्स)

VariantSpecial Feature
Vanilla RNNSimple structure (as above)
LSTMLong memory, gating mechanism
GRUEfficient, fewer gates than LSTM

📝 Practice Questions:

  1. RNN में hidden state क्या दर्शाता है?
  2. RNN में weight sharing का क्या लाभ है?
  3. RNN Cell किस तरह input और पिछले state से output निकालता है?
  4. Mathematical formula for hth_tht​ क्या है?
  5. PyTorch में RNN के लिए input tensor का shape क्या होता है?

🎯 Summary

ConceptMeaning
RNN CellBasic unit that processes each time step
Hidden StateInformation summary till time t
Shared WeightsSame weights used for all time steps
ActivationUsually tanh or ReLU
OutputOptional at each step or only at the end