# Week 1 classroom experiment

Use **Python 3.12 and PyTorch on CPU**, managed by uv. PyTorch carries the same array
operations into later neural-network lectures. We write the derivatives and
updates explicitly here; automatic differentiation comes later. NumPy is a good
alternative for array calculations, but learning two interfaces is unnecessary
for this first exercise. No GPU, downloaded dataset, or C++ compiler is needed.

The exercise uses the same synthetic sensor data, seed, split,
observed-minus-predicted residuals, mean squared loss, equations, and parameter
order `[weight, bias]` as the
[Week 1 chapter](../../chapters/01-linear-regression/concepts.qmd). Only the 40
training examples participate in fitting.

## Get the repository

```sh
git clone https://github.com/HealthML/deep-learning-book.git
cd deep-learning-book
```

For an existing checkout, commit or save local work before updating with
`git pull --ff-only`.

## Install and check

The first lecture assumes no earlier installation. Install
[uv](https://docs.astral.sh/uv/getting-started/installation/), open a new terminal,
and run from the repository root:

```sh
uv --version
uv sync --locked
```

The same commands work on Linux, macOS, and Windows PowerShell. uv downloads
Python 3.12 if needed, creates `.venv`, and installs the book and development
packages from `uv.lock`. Project settings select CPU PyTorch wheels on Linux and
Windows and the standard package source on macOS. The exercise keeps all tensors
on CPU. No environment activation or separate package installation is needed.
After pulling changes, use `uv sync --locked` again to update your environment.

```sh
uv run --locked python -m compileall -q exercises/week01/starter.py
uv run --locked python exercises/week01/starter.py
```

Python runs the script directly. `compileall` checks its syntax and creates
bytecode; it is the compilation check, not a model-training step. The script
prints its PyTorch version, CPU device, float64 precision, and training-set size.
An all-zero prediction gives training MSE approximately **3.7590**. If installation
is still running after four minutes, pair with a working machine for the first
experiment and finish independent setup after class.

## Try, discuss, then fit

Open [worksheet.qmd](worksheet.qmd) and `starter.py`. Edit the constants at the
top of the script or use the commands below. A window appears with `--plot`;
use `--save fit.png` on machines without a graphical display.

```sh
uv run --locked python exercises/week01/starter.py --w 1.2 --plot
uv run --locked python exercises/week01/starter.py --model affine --w 1.2 --b 0.3 --plot
uv run --locked python exercises/week01/starter.py --model affine --method partials --w 0.5 --b 0
uv run --locked python exercises/week01/starter.py --model affine --method coordinate --steps 5
uv run --locked python exercises/week01/starter.py --model affine --method sgd --lr 0.025 --steps 40
uv run --locked python exercises/week01/starter.py --model affine --method batch --lr 0.1 --steps 40
uv run --locked python exercises/week01/starter.py --model affine --method lstsq
```

Pause for the worksheet's predictions before revealing each fitting mode.
One SGD update processes one example; one batch update processes all 40. Thus
40 SGD updates and one batch update process the same number of examples, although
their trajectories and compute overhead differ. The sampled SGD loss can change
because the next update uses a different example; use the full training MSE for a
consistent progress measure.

[instructor-solutions.md](instructor-solutions.md) contains expected numerical
checks and discussion notes. The direct solve returns approximately
`w=1.655874, b=0.459148`, training MSE `0.142308`.
