This book takes you from an empty folder to a working GPT that chats inside your browser. It is written in simple language, with diagrams and complete code at every step.

Chapters

  1. What a GPT really does – A GPT only guesses the next character; repeating that guess produces whole passages of text.
  2. The plan and the tools – The step-by-step pipeline, the tools (Python, PyTorch, plain JavaScript, HTML/CSS) and the project folder.
  3. Step 1: Collect and clean the books – Three public-domain classics, with the Project Gutenberg licence text and non-ASCII characters removed.
  4. Step 2: Turn text into numbers – Mapping each of the 81 characters to a number and holding back 10% of the text for testing.
  5. Step 3: Build the model – Embeddings, self-attention and feed-forward layers of a small decoder-only transformer, and its settings.
  6. Step 4: Train it – The guess-check-adjust training loop for 2,500 steps, and how to read the loss numbers.
  7. Step 5: Make it write – Sampling text from the trained model and how temperature changes its output.
  8. Step 6: Move the model into the browser – Exporting the weights, rewriting the model in JavaScript and verifying it matches the PyTorch version.
  9. Step 7: Build the chat page – The chat interface with a typing effect, stop button and suggestion buttons.
  10. Step 8: Run and test on your computer – Running a local web server, a test checklist and troubleshooting tips.
  11. Where to go next – Experiments to improve the model, from longer training to word-piece tokens.
  12. Run my project yourself – Chat with the included model in a minute, or train your own copy in about 15 minutes.

Who it is for

Developers and students who know a little Python and want to understand how large language models work under the hood. No GPU, API key or paid service is needed.

Source code

The complete project is open source: github.com/sourabhsomani/tiny-gpt