Set up your lab
Python, uv, PyTorch, free GPUs, Hugging Face, API keys and cost hygiene
Every practice item from 1.1 Vectors, dot products and norms onward assumes you can open a notebook, run NumPy and PyTorch, and — from Phase 4 — use a GPU. Setting this up once, properly, removes the single biggest source of friction in self-study. You will also create the accounts you need later (Hugging Face for models and datasets, an API key for a frontier model) and learn the cost hygiene that keeps experimentation cheap when you reach 8.13 Project: fine-tune a small LLM with LoRA and ship a chat app, 9.11 Project: a cited RAG assistant over your own documents, with evals and 10.12 Project: build a research agent with tools, an MCP server and an eval harness.
You'll be able to
- Install Python 3.12+ and manage project environments with uv
- Run PyTorch on a GPU — locally if you have one, otherwise on Colab or Kaggle — and verify it
- Create and secure Hugging Face and model-API accounts, storing keys in environment variables with spending limits
- Use a consistent project template with Git from day one
Key ideas
- Minimal stack: Python 3.12+, uv for environments, VS Code + Jupyter, Git/GitHub, and a reachable GPU (local or Colab/Kaggle).
- One environment per project with a lockfile; install the GPU build of PyTorch using the official selector.
- Explore in notebooks, keep anything you run twice in .py modules, and restart the kernel before trusting a result.
Show 3 more key ideas
- You do not need to buy a GPU: Colab and Kaggle cover every exercise through Phase 7 and small fine-tunes in Phase 8; VRAM is the number that matters locally.
- Keys live in environment variables or a git-ignored .env file; set a monthly spending limit before the first API call.
- Use coding assistants for scaffolding and debugging, but write the practice solutions yourself first.
Suggested path through this lesson
The minimal stack
You need five things: Python 3.12 or newer; uv, a fast package and environment manager that replaces pip, venv and most of conda; VS Code with the Python and Jupyter extensions (or JupyterLab if you prefer); Git with a GitHub account; and a GPU you can reach — either in your machine or in the cloud for free. Everything else (PyTorch, NumPy, pandas, scikit-learn, Hugging Face libraries) is installed per project.
Why uv instead of conda? It is dramatically faster, it pins exact versions in a lockfile so your project runs the same next month, and it installs Python itself. Conda still shines for some scientific packages, but for this path uv is enough and simpler.
Environments in sixty seconds
# install uv (macOS/Linux)
curl -LsSf https://astral.sh/uv/install.sh | sh
# install uv (Windows PowerShell)
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
# create a project with its own environment
uv init learnai-lab
cd learnai-lab
uv add numpy pandas matplotlib scikit-learn jupyter ipykernel
uv add torch torchvision # CPU build; see the GPU note below
uv run python -c "import torch; print(torch.__version__)"
For a GPU build of PyTorch, use the selector on the PyTorch "Get Started" page: it gives you the exact install command for your operating system and CUDA version. On a Mac with Apple silicon the default build already supports the GPU through the mps device.
Intuition. One environment per project, never a global install. When something breaks you delete a folder, not your computer.
Notebooks versus scripts
Notebooks are for exploration: plotting data, trying an idea, reading a model's outputs. Scripts and modules are for anything you will run twice: training loops, data pipelines, evaluations. A healthy habit from Phase 3 on is to prototype in a notebook, then move working code into a .py file you import. Keep notebooks short and restart the kernel before you trust a result — hidden state is the classic notebook bug.
GPUs: what you actually need
Deep learning from 4.1 From perceptron to MLP; universal approximation onward is far more pleasant on a GPU, but you do not need to buy one. Google Colab gives free access to a modest GPU with session limits; Kaggle Notebooks offers a weekly quota of GPU hours (about 30 at the time of writing) and persistent datasets. Both are enough for every exercise through Phase 7 and for small fine-tunes in Phase 8. If you do have a local card, the number that matters is VRAM:
| VRAM | comfortable for |
|---|---|
| 4–6 GB | everything in Phases 3–6, small CNNs, character-level language models |
| 8–12 GB | GPT-style models of tens of millions of parameters, QLoRA fine-tunes of 1–3B models |
| 16–24 GB | QLoRA fine-tunes of 7–8B models, Stable Diffusion inference, most of Phase 8 |
| 40 GB and up | full fine-tunes of small models; otherwise rent by the hour |
Cloud GPU rentals by the hour (several providers exist; prices change often) are the right choice for the occasional large run. The compute math behind these numbers is in 4.12 Compute, hardware and scale basics and 7.10 Efficient inference: KV cache, GQA, FlashAttention, speculative decoding, quantization.
Accounts, keys and cost hygiene
Create a Hugging Face account and generate an access token; you will use the Hub for models, datasets and hosting demos (2.5 Tooling: Git, GPUs, experiments and the Hugging Face Hub, 5.10 Project: train and deploy an image classifier). Create an API account with Anthropic and/or OpenAI for Phases 8–10. Then protect yourself:
- Store keys in environment variables or a local
.envfile that is listed in.gitignore. Never paste a key into a notebook cell you might commit. - Set a monthly spending limit in the provider's console before your first call. Learning-scale usage — short prompts, small models, caching repeated prompts — typically costs a few dollars a month; a runaway loop can cost far more in an hour.
- Prefer the cheapest model that can do the job while you learn; switch to the frontier model only to compare.
# .env (never commit this file)
ANTHROPIC_API_KEY=sk-ant-...
HF_TOKEN=hf_...
A project template
learnai-lab/
pyproject.toml # dependencies, pinned by uv.lock
README.md # what this is, how to run it
data/ # raw and processed data (git-ignored if large)
notebooks/ # exploration
src/ # reusable code: models.py, data.py, train.py
experiments/ # configs and results, one folder per run
tests/ # small tests for data and model code
Initialize Git on day one: git init, commit the template, push to GitHub. Commit at the end of every session with a message that says what changed. Your future self, and every reviewer of your portfolio (11.3 Portfolio project ladder), will thank you.
Working with AI assistants while you learn
Coding assistants (Claude Code, Cursor, Copilot and others) are standard professional tools and you should use them — for scaffolding, reading error messages, explaining library APIs and generating test data. The one rule for this path: the practice items are yours. Write the NumPy backprop, the attention head and the RAG retriever by hand first; ask the assistant to review your version afterwards. The struggle is where the learning is, and by 10.8 Coding agents and computer use you will understand these tools well enough to know exactly when to lean on them.
Verify your setup
Run this in a notebook and in a Colab or Kaggle notebook with a GPU runtime selected:
import sys, torch, numpy as np
print(sys.version)
print("torch", torch.__version__, "cuda:", torch.cuda.is_available(),
"mps:", getattr(torch.backends, "mps", None) and torch.backends.mps.is_available())
device = "cuda" if torch.cuda.is_available() else ("mps" if torch.backends.mps.is_available() else "cpu")
x = torch.randn(4096, 4096, device=device)
print((x @ x).sum().item(), "on", device)
Expected: a Python 3.12+ version string, a PyTorch version, cuda: True on a GPU runtime (or mps: True on Apple silicon), and a matrix product that finishes instantly. If anything fails, the fix is almost always "wrong environment active" or "wrong PyTorch build for your GPU".
Where this shows up later
- Python idioms, NumPy and pandas — 2.1 Python for ML, fast, 2.2 NumPy and vectorized thinking, 2.3 pandas, data wrangling and plotting; PyTorch in depth — 2.4 PyTorch I: tensors, autograd and the training loop, 4.9 PyTorch II: real training pipelines.
- Git, experiment tracking and the Hugging Face Hub — 2.5 Tooling: Git, GPUs, experiments and the Hugging Face Hub.
- Free GPUs carry you through the fine-tuning project — 8.13 Project: fine-tune a small LLM with LoRA and ship a chat app; API keys and budgets matter for the RAG and agent projects — 9.11 Project: a cited RAG assistant over your own documents, with evals, 10.12 Project: build a research agent with tools, an MCP server and an eval harness.
Common pitfalls (4)
- Installing packages globally and ending up with conflicting versions across projects.
- Committing an API key to GitHub — bots find and abuse leaked keys within minutes.
- Trusting a notebook result produced with stale hidden state; restart and run all before you believe it.
- Buying a GPU before you know what you need; the free tiers are enough for most of this path.
Recap: all 6 key points
- Minimal stack: Python 3.12+, uv for environments, VS Code + Jupyter, Git/GitHub, and a reachable GPU (local or Colab/Kaggle).
- One environment per project with a lockfile; install the GPU build of PyTorch using the official selector.
- Explore in notebooks, keep anything you run twice in .py modules, and restart the kernel before trusting a result.
- You do not need to buy a GPU: Colab and Kaggle cover every exercise through Phase 7 and small fine-tunes in Phase 8; VRAM is the number that matters locally.
- Keys live in environment variables or a git-ignored .env file; set a monthly spending limit before the first API call.
- Use coding assistants for scaffolding and debugging, but write the practice solutions yourself first.
Watch
Only the first two hours now: environment setup, Colab, tensors and device handling. The rest pairs with Phases 2 and 4.
Read
Exercises
- Install and verify locally
Install uv, create the
learnai-labproject from the notes, add the packages, and run the verification snippet. Expected: Python 3.12+, a PyTorch version, and the device line printed without errors (CPU is fine locally). - Verify a free GPU
Open Colab (or Kaggle), select a GPU runtime, and run the verification snippet. Time the 4096×4096 matrix product on CPU and on GPU with
time.perf_counter. Expected:cuda: Trueand a GPU speed-up of at least 10× on the product. - Accounts, keys and a budget
Create a Hugging Face account and token, and one model-API account. Set a monthly spending limit. Store both keys in a git-ignored
.envfile and load them withpython-dotenv. Make one API call that asks the model to quiz you on lesson 0.2. Expected: a response printed in your notebook and no key visible in your Git history (git log -pshows none).
Quiz
Flashcards
These cards are now in your spaced-repetition queue. Review all due cards →