Skip to content

AditSKumar/transformer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bigram GPT model

A minimal implementation of a Generative Pretrained Transformer (GPT) built from scratch in PyTorch, based on the seminal paper "Attention is All You Need" and inspired by Andrej Karpathy's educational lecture series.

This project trains a character-level Transformer model on a tiny version of Shakespeare's text to predict and generate new text in a GPT-style fashion.


Features

  • Character-level language modeling
  • Custom multi-head self-attention from scratch
  • Transformer blocks with residual connections & layer normalization
  • Text generation with sampling
  • Based on core concepts from the GPT family of models

Getting Started

1. Clone the repository

git clone https://github.com/yourusername/transformer.git
cd gpt-from-scratch

2. Download the dataset

wget https://raw.githubusercontent.com/karpathy/char-rnn/master/data/tinyshakespeare/input.txt

3. Install requirements

pip install torch

4. Train the model

python main.py

How It Works

Tokenization & Encoding

  • Reads the dataset (input.txt) and builds a character-level vocabulary.
  • Each character is assigned a unique integer ID.
  • The entire text is encoded as a tensor of integers.

Model Architecture

  • Token Embeddings: Maps input character IDs to dense vectors.
  • Positional Embeddings: Injects token position information.
  • Transformer Blocks: Multiple stacked blocks with:
    • Multi-Head Self-Attention
    • Feedforward layers
    • Residual connections & LayerNorm
  • Language Modeling Head: Outputs logits over vocabulary to predict the next character.

Training

  • Trains on batches of sequences using cross-entropy loss.
  • Periodically evaluates on a validation set.
  • After training, the model can generate Shakespeare-like text.

Acknowledgments

This project is heavily inspired by:


License

MIT License

About

Implementing a Generatively Pretrained Transformer (GPT) based on the 'Attention is All You Need' paper and insights from Andrej Karpathy's lecture

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages