Interactive figures are drawn by lr_demos.js; the static figures come from
logistic.py (the .svg files) and figs/*.tex (the network diagrams). Build with
marp --html logistic_regression.md
--html is required: the demos are inline <div>s plus a <script>.
Bullets written with "*" unfold one at a time, and on demo slides each
reveal also advances the animation, so the clicker drives everything.
Mouse interaction (dragging, sliders, buttons) is extra and only affects
the window you use it in.
The three demos replace the animations from the "Animations of Logistic
Regression with Python" article, which is now a 404.
The previous (pandoc) version of this deck is logistic_regression_old.md.
This is the picture I drew on the board in the recorded lecture. Each click moves w and b;
the sliders are for questions. The red triangles are the 1s and the blue
circles the 0s; they are the training data for the next slide.
Without b the line has to go through the origin, so the sigmoid would
always be centred at x = 0. b is a weight attached to a constant input of
1, not a hyperparameter.
A negative w flips the curve: the slider goes there if anyone asks.
The orange dot on the right IS the orange curve on the left.
The thin lines run from each point to the model's prediction: their length
is |y - p|. The cross-entropy charges -log(1 - length) for each one, so a
long line costs far more than a short one. The gray
curve is where we started: shifted to the left, so it says 1 for
most of the points.
"Cost" is the mean cross-entropy, which we derive in a few slides. We could
use MSE here instead; the question of which loss is right is the second
half of today.
Note the long curved valley: w and b trade off against each other because
the x values are not centred. 120 iterations, eta = 0.8. Click the
contour plot to start somewhere else.
Step 1 runs 60 iterations of batch gradient descent from w = 0, b = 0,
where the surface is flat at 0.5. Step 2 rotates to straight overhead.
It gets 27 of the 32 training points right.
The thin vertical lines are each point's error, |y - p|, as on the
previous slide.
The plot under the bullets is the cost (mean cross-entropy) over (w1, w2),
with b held at its trained value (-0.73), so we can draw it: the real
surface has three parameters. The descent path (whose b is changing too)
ends at the minimum, cost 0.368, not 0.
# Bayesian Learning
* In Bayesian learning, we use data to update a probability
distribution over our model parameters $\theta$ given our training data $\mathcal{D} = (\mathbf{x}_1, \mathbf{x}_2, ... \mathbf{x}_n)$:
* $P(\theta \mid \mathcal{D}) = \frac{ P( \mathcal{D} \mid \theta) P(\theta)}
{P(\mathcal{D})}$
* The goal here is *not* to find the single best fit (or **point
estimate**) for our parameters. Instead, we maintain a probability
distribution over our parameters.
* This is related to the idea of ensemble learning.
* Here is a recent survey if you are interested: [Hands-on Bayesian Neural Networks - a Tutorial for Deep Learning Users](https://arxiv.org/abs/2007.06823)
Answers: 0.9 x 0.1 x 0.9 = 0.081.
The maximum is at theta = 2/3, the fraction of heads: (2/3)(1/3)(2/3) =
4/27, about 0.148. (In the recorded lecture I said "four ninths"; it is 4/27.)
Added brackets: without them the minus sign (and the sum) applies only to
the first term.
Derivation, per point, with p = sigma(z): d/dz [-y log p - (1 - y) log(1 - p)]
= -y (1 - p) + (1 - y) p = p - y. Then dz/dw_j = x_j.
Compare the last slide of the gradient descent lecture.
The old version of this slide said the loss is "guaranteed to have a single
global minima." Convexity rules out local minima, but on separable data
the minimum does not exist: Exercise 3 of the lab.
The animations link on the old slide is a 404; the demos earlier in the
deck replace it.
Teaser only; more later in the semester.
Scikit-learn minimizes (1/2)||w||^2 + C * sum of the per-point losses, so C
is roughly 1/(2 lambda). The bias b is not penalized.
Lab Exercise 3 shows the separable case: as C grows, ||w|| keeps growing.
The example is titanic.py (standardized features, no penalty): about 84%
correct on the test set; Sex (1 = male) -1.21 and Pclass -0.95 are the
biggest weights.