Skip to content

Repository files navigation

RNN_LSTM-from-Scratch

Overview

This repository implements two fundamental sequence models from scratch using NumPy: Recurrent Neural Networks (RNNs) and Long Short-Term Memory Networks (LSTMs). These models are trained for character-level language modeling, offering insight into how sequential neural networks function internally—without deep learning libraries.


What is an RNN?

A Recurrent Neural Network (RNN) processes sequential data by maintaining a hidden state vector that passes information from one timestep to the next. This mechanism enables the model to learn patterns across time or sequence.

Mathematical Formulation:

alt text

RNNs are trained using Backpropagation Through Time (BPTT), but they are limited by vanishing/exploding gradients, making it difficult to learn long-term dependencies.


What is an LSTM?

Long Short-Term Memory (LSTM) networks address the limitations of vanilla RNNs by introducing gates that control the flow of information and help preserve gradients.

Main Equations:

alt text

Where the gates ((i, f, o)) regulate input, memory retention, and output—enabling the network to model longer-term dependencies.


Repository Structure

Model Implementations

  • rnn.py: Contains a class for the vanilla character-level RNN, with manual forward and backward (BPTT) passes.
  • lstm_scratch.py: Implements an LSTM from scratch, including cell/gate logic and memory updates.

Training Scripts

  • training2.py: Trains the RNN model, fetches data, preprocesses it, starts training, and prints sampled text after epochs.
  • training_loop.py: Hosts an analogous training loop for the LSTM model.

About

Character-level Recurrent Neural Network (RNN) and Long Short-Term Memory (LSTM) implementations built from scratch in NumPy.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages