Environment Setup
Before we start building, let's get your environment ready. We'll use standard Python with PyTorch — no notebooks required.
Why These Tools?
- Python 3.9+ — Readable syntax, rich ecosystem, industry standard (GPT, LLaMA, Claude all use Python)
- PyTorch — Pythonic design, dynamic computation, research favorite with great documentation
Clone the Repository
Get the complete source code:
bash
1git clone https://github.com/slahiri/small_calculator_model.git2cd small_calculator_modelWell-commented code: The source files in
src/ contain detailed comments explaining every component. This tutorial shows simplified snippets — refer to the GitHub repo for the full, annotated code.Install Dependencies
Create a virtual environment and install PyTorch:
bash
1python -m venv venv2source venv/bin/activate # On Windows: venv\Scripts\activate3pip install torchVerify Setup
Run this to verify PyTorch is working:
bash
1python -c "import torch; print(f'PyTorch {torch.__version__} ✓')"Project Structure
small_calculator_model/
├── config/
│ ├── config.json # Model hyperparameters
│ └── vocab.json # Vocabulary mapping
├── src/
│ ├── tokenizer.py # Text → token IDs
│ ├── model.py # Transformer architecture
│ ├── data.py # Training data generation
│ ├── train.py # Training loop
│ └── generate.py # Inference & evaluation
├── tests/ # 78 pytest tests
│ ├── test_tokenizer.py
│ ├── test_model.py
│ ├── test_data.py
│ ├── test_train.py
│ └── test_generate.py
└── app.py # Gradio web demoRun Tests
The project includes 78 tests. Run them to verify everything works:
bash
1pip install pytest2pytest tests/ -vNo GPU required. This model is tiny (~100K parameters) and trains in minutes on CPU.
Helpful?