CS5720 - Week 6
Slide 118 of 120

Building Your First RNN

Step-by-Step Guide

Let's build a simple RNN from scratch! We'll create a character-level language model that learns to generate text.
  • Step 1
    Prepare Your Data
    Load text, create vocabulary, encode sequences
  • Step 2
    Define RNN Architecture
    Choose layers, hidden size, activation functions
  • Step 3
    Implement Forward Pass
    Process sequences through the network
  • Step 4
    Train Your RNN
    Loss function, optimizer, training loop
  • Step 5
    Generate Text
    Use trained model to create new sequences

Quick Start Code

Here's a minimal RNN implementation to get you started. Click for the full code!
import torch import torch.nn as nn class SimpleRNN(nn.Module): def __init__(self, vocab_size, hidden_size): super().__init__() self.embedding = nn.Embedding(vocab_size, hidden_size) self.rnn = nn.RNN(hidden_size, hidden_size, batch_first=True) self.fc = nn.Linear(hidden_size, vocab_size) def forward(self, x, hidden=None): embedded = self.embedding(x) output, hidden = self.rnn(embedded, hidden) output = self.fc(output) return output, hidden # Click for complete implementation...
Pro Tip:
Start simple! Get a basic RNN working before adding complexity like LSTM/GRU cells or attention mechanisms.

RNN Architecture Overview

📝
Input Layer
Text sequences
🔤
Embedding
Word vectors
🔄
RNN Cell
Hidden states
📊
Output Layer
Predictions
Click on components to learn about each part of the RNN architecture
Prepared by Dr. Gorkem Kar