πŸŽ‰ 75% of content is free forever β€” Unlock Premium from $10/mo β†’
CW
Search courses…
πŸ’Ό Servicesℹ️ Aboutβœ‰οΈ ContactView Pricing Plansfrom $10

Recurrent Neural Networks for NLP

Neural NLPRNNs🟒 Free Lesson

Advertisement

Recurrent Neural Networks (RNNs)

RNNs are neural networks designed for sequential data. Unlike feedforward networks, RNNs maintain a hidden state that captures information from previous time steps, making them naturally suited for NLP tasks like language modeling, text generation, and machine translation.

RNN Hidden State

ht=tanh⁑(Whhhtβˆ’1+Wxhxt+b)h_t = \tanh(W_{hh} h_{t-1} + W_{xh} x_t + b)

RNN Output

yt=Whyht+cy_t = W_{hy} h_t + c

Vanishing and Exploding Gradients

The fundamental challenge with vanilla RNNs is that gradients can vanish or explode during backpropagation through time (BPTT).

Gradient Through Time

βˆ‚LTβˆ‚ht=βˆ‚LTβˆ‚hT∏k=t+1Tβˆ‚hkβˆ‚hkβˆ’1\frac{\partial L_T}{\partial h_t} = \frac{\partial L_T}{\partial h_T} \prod_{k=t+1}^{T} \frac{\partial h_k}{\partial h_{k-1}}
ProblemCauseEffectSolution
Vanishing gradientSmall eigenvalues of W_hhCannot learn long-range dependenciesLSTMs, GRUs
Exploding gradientLarge eigenvalues of W_hhUnstable trainingGradient clipping

Basic RNN Implementation

import torch
import torch.nn as nn

class SimpleRNN(nn.Module):
    def __init__(self, vocab_size, embed_dim, hidden_dim, output_dim):
        super().__init__()
        self.embedding = nn.Embedding(vocab_size, embed_dim)
        self.rnn = nn.RNN(embed_dim, hidden_dim, batch_first=True)
        self.fc = nn.Linear(hidden_dim, output_dim)

    def forward(self, text, hidden=None):
        embedded = self.embedding(text)  # (batch, seq_len, embed_dim)
        output, hidden = self.rnn(embedded, hidden)
        prediction = self.fc(output)
        return prediction, hidden

# Initialize model
model = SimpleRNN(vocab_size=10000, embed_dim=128,
                  hidden_dim=256, output_dim=10000)

# Forward pass
batch = torch.randint(0, 10000, (32, 50))  # (batch, seq_len)
output, hidden = model(batch)
print(output.shape)  # (32, 50, 10000)
print(hidden.shape)  # (1, 32, 256)

RNN for Text Classification

class RNNClassifier(nn.Module):
    def __init__(self, vocab_size, embed_dim, hidden_dim, num_classes):
        super().__init__()
        self.embedding = nn.Embedding(vocab_size, embed_dim)
        self.rnn = nn.RNN(embed_dim, hidden_dim, batch_first=True)
        self.fc = nn.Linear(hidden_dim, num_classes)

    def forward(self, text):
        embedded = self.embedding(text)
        output, hidden = self.rnn(embedded)
        # Use last hidden state for classification
        hidden = hidden.squeeze(0)  # (batch, hidden_dim)
        return self.fc(hidden)

RNN for Language Modeling

class RNNLM(nn.Module):
    def __init__(self, vocab_size, embed_dim, hidden_dim):
        super().__init__()
        self.embedding = nn.Embedding(vocab_size, embed_dim)
        self.rnn = nn.RNN(embed_dim, hidden_dim, batch_first=True)
        self.fc = nn.Linear(hidden_dim, vocab_size)

    def forward(self, text, hidden=None):
        embedded = self.embedding(text)
        output, hidden = self.rnn(embedded, hidden)
        prediction = self.fc(output)
        return prediction, hidden

    def generate(self, start_token, max_len=50):
        self.eval()
        tokens = [start_token]
        hidden = None
        with torch.no_grad():
            for _ in range(max_len):
                x = torch.tensor([[tokens[-1]]])
                output, hidden = self(x, hidden)
                prob = torch.softmax(output[:, -1], dim=-1)
                next_token = torch.argmax(prob, dim=-1).item()
                tokens.append(next_token)
        return tokens

RNN Variants

VariantDescriptionAdvantage
Vanilla RNNBasic recurrent structureSimple, fast
LSTMLong Short-Term MemoryCaptures long dependencies
GRUGated Recurrent UnitSimpler than LSTM
BidirectionalForward + backward passFull context
Deep RNNMultiple stacked layersMore capacity
⭐

Premium Content

Recurrent Neural Networks for NLP

Unlock this lesson and 900+ advanced tutorials with a Premium plan.

🎯End-to-end Projects
πŸ’ΌInterview Prep
πŸ“œCertificates
🀝Community Access

Already a member? Log in

Need Expert NLP Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement