Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Preface

From Neuron to GPT

A tutorial that starts from a single neuron and builds a GPT by hand, step by step, using nothing but NumPy. It is available in Chinese and English.

It uses no deep learning framework such as PyTorch or TensorFlow. Automatic differentiation, network layers, optimizers, attention, the Transformer, training, inference, and fine-tuning are all implemented from scratch, and each is explained step by step in a Jupyter Notebook.

Features

Table of contents

The tutorial has two parts with 34 chapters in total, plus one framework summary. Chapter numbers in the two parts are independent: Part 1 uses 01–16 and Part 2 uses 01–18.

Part 1: Neural network fundamentals

Directory: artificial-neural-network, under tutorial/zh (Chinese) and tutorial/en (English).

DirectoryChaptersContents
01-multilayer-perceptron01–10Linear regression, loss function, gradient descent, automatic differentiation, learning rate, iteration, batch, epoch, hidden layer, activation function
02-recurrent-neural-network11–16Encoding, embedding, cross-entropy loss, recurrent neural network, Adam optimizer, learning rate scheduler
03-core-frameworkSummaryConsolidates the previous 16 chapters into a complete core deep learning framework (src/core)

Part 2: Large language model fundamentals

Directory: large-language-model, under tutorial/zh (Chinese) and tutorial/en (English).

DirectoryChaptersContents
01-tokenizer01–02Token embedding, position embedding
02-attention03–06Self-attention, causal attention, weighted attention, multi-head attention
03-pre-training07–10Residual connection, normalization, dropout, model persistence
04-inference11–14Sampling strategies (temperature, top-k, top-p), stopping criteria, KV cache, batch inference
05-fine-tuning15–18SFT data preparation and training, DPO data preparation and training

Quick start

Requirements

Installation

git clone https://github.com/gptmini/from-neuron-to-gpt.git
cd from-neuron-to-gpt

python -m venv .venv
# Windows: .venv\Scripts\activate
source .venv/bin/activate

pip install -r requirements.txt
pip install jupyterlab

Data

The tutorial uses tinyshakespeare, a collection of Shakespeare’s plays, as training data. It is stored at data/tinyshakespeare.txt.

Running the tutorial

Run jupyter lab from the project root, then open the notebooks under tutorial/zh (Chinese) or tutorial/en (English) in numerical order.

Running the full pipeline from the command line

If you only want to train the models and don’t want to read the chapters, run the three scripts in order from the project root:

python -m src.gpt.gpt   # Pre-training: download the data, train the GPT, generate sample text
python -m src.sft.sft   # Supervised fine-tuning: prepare SFT samples, train, generate sample text
python -m src.dpo.dpo   # Direct preference optimization: prepare DPO samples, train, generate sample text

The three stages depend on each other, so run them in order:

StageDepends onOutput (all in data/)
Pre-trainingtinyshakespeare.txttinyshakespeare-gpt.npz
SFTPre-trained modelsft-samples.pkl, tinyshakespeare-sft.npz
DPOPre-trained model, SFT modeldpo-samples.pkl, tinyshakespeare-dpo.npz

03-pre-training/10 produces the pre-trained model, which 04-inference and 05-fine-tuning then use in turn.

Model size

The default GPT configuration is as follows. It corresponds to the Part 2 chapter “Model Persistence” (03-pre-training/10) and to src/gpt/gpt.py:

ParameterValue
Context size256
Embedding size384
Attention heads6
Transformer blocks6
Batch size16
Epochs10
Vocabulary (character-level)65
Parametersabout 10.8M

The chapters before 03-pre-training/10 use smaller configurations so that they run quickly. The “Model Persistence” chapter switches to the configuration above, in order to train a model that is usable for inference and fine-tuning. The model is trained entirely on the CPU and is small. The goal is to see the principle behind every step clearly, not to maximize generation quality.

Environment and running time

All the code was developed on a Mac mini M4 with 16 GB of memory and runs entirely on the CPU.

Running time depends on the CPU, the number of cores, and the BLAS library that NumPy is linked against, and it can differ greatly between machines.

Project structure

.
├── data/                       # Training data (tinyshakespeare) plus the model parameters and samples produced by training
├── src/
│   ├── core/                   # Core framework: tensors, layers, activation functions, loss functions, optimizers,
│   │                           #         learning rate schedulers, datasets, trainers
│   ├── gpt/                    # GPT model, dataset, loss function, trainer; gpt.py is the pre-training script
│   ├── sft/                    # Supervised fine-tuning; sft.py is the fine-tuning script
│   └── dpo/                    # Direct preference optimization; dpo.py is the optimization script
├── tutorial/
│   ├── en/                     # English tutorial (Jupyter Notebooks)
│   │   ├── artificial-neural-network/
│   │   ├── large-language-model/
│   │   └── myst.yml            # 教程站点(MyST / Jupyter Book)配置
│   └── zh/                     # Chinese tutorial, same structure as en
├── requirements.txt
└── LICENSE

Acknowledgments and references

The training data, tinyshakespeare, comes from Andrej Karpathy’s char-rnn project.

Main references: