Same loop as linear regression, two changes:
- Sigmoid squashes the linear output to
(0, 1)— a probability. - Binary cross-entropy loss replaces MSE; it punishes confident wrong answers heavily.
We also introduce two PyTorch building blocks you'll use for every real model:
nn.Linear(in, out)— a learnable affine layer. Equivalent tox @ W.T + b.torch.optim.SGD— wraps the "subtract learning rate times gradient" step.
It's the same math, but combining them avoids a numerical-stability problem (the log of a near-zero sigmoid output blows up). Use the combined loss in real code.
python 04_logistic_regression/main.py