Recurrent Neural Networks
Recurrent Neural Networks (RNNs) are a type of neural network designed for processing sequences by leveraging hidden states to capture temporal information. They are particularly well-suited for tasks like language modeling, where the goal is to predict the next token based on the historical sequence of previous tokens.
Basics of RNNs
-
Latent Variable Models: RNNs utilize latent variable models to approximate the probability of a token given all previous tokens . This is represented mathematically as:
where denotes the hidden state at time .
-
Hidden State Calculation: The hidden state is updated at each timestep using the current input and the previous hidden state via a function , as shown:
This function, often nonlinear, allows the RNN to compactly represent the history of observed data up to the current timestep.
-
Difference from Hidden Layers: Hidden states in RNNs should not be confused with hidden layers in other types of neural networks. Hidden states serve as inputs to each step of the RNN, reflecting the sequence's memory up to that point.
Neural Networks without Hidden States
For a simpler neural network model like the Multi-Layer Perceptron (MLP) with a single hidden layer, the computation does not involve any temporal dynamics:
where is an activation function, and , are the weight and bias parameters respectively.
Recurrent Neural Networks with Hidden States
In contrast to the non-recurrent model, RNNs maintain a hidden state across timesteps, updating it recurrently using both the current input and the previous hidden state:
This recurrent update mechanism allows RNNs to remember information across many timesteps, making them ideal for tasks like time series forecasting and language modeling.
RNN-Based Character-Level Language Models
An RNN can be used to model language at the character level, where the network predicts the next character based on the past sequence of characters. This approach involves:
- Shifting the sequence to align inputs and labels for training (e.g., input: "machine", label: "achine").
- Using softmax and cross-entropy loss to train the model on predicting the next character in the sequence.
Example:
RNN in Python
Below is a basic example using PyTorch:
import torch
import torch.nn as nn
class SimpleRNN(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(SimpleRNN, self).__init__()
self.hidden_size = hidden_size
self.rnn = nn.RNN(input_size, hidden_size, batch_first=True)
self.fc = nn.Linear(hidden_size, output_size)
def forward(self, x):
out, _ = self.rnn(x)
out = self.fc(out[:, -1, :])
return out
# Example usage
rnn = SimpleRNN(input_size=10, hidden_size=20, output_size=1)
input = torch.randn(5, 10, 10) # (batch_size, sequence_length, input_size)
output = rnn(input)
print(output)
This Python code defines a simple RNN module using PyTorch's nn.RNN layer. It processes input sequences and returns output using a fully connected layer after the last sequence element has been processed.