
EE 541 - Unit 5
Fall 2026
Data: 17,898 human-labeled pulsar candidates from a radio-telescope survey
Measurement \(x\): the mean of the candidate’s pulse profile
Decision: pulsar or noise, from \(x\) alone
Overlap: the same \(x\) occurs in both classes
Data: HTRU2, R. J. Lyon et al., MNRAS 2016; UCI Machine Learning Repository (CC BY 4.0)

Rule: decide pulsar if \(x < T\), noise otherwise
Two errors:
Example, \(T = 80\), counted over the 17,898 candidates:
Raising \(T\): fewer misses, more false alarms

Decision rule \(\delta(\mathbf{x})\): an action for each \(\mathbf{x}\)
Loss \(c(y, a)\): cost of action \(a\) when the class is \(y\) \[\mathbf{C} = \begin{bmatrix} c_{00} & c_{01} \\ c_{10} & c_{11} \end{bmatrix} = \begin{bmatrix} 0 & 1 \\ 10 & 0 \end{bmatrix}\]
Risk: \[R(\delta) = \mathbb{E}[c(Y, \delta(X))] = c_{01}\,P(\text{false alarm}) + c_{10}\,P(\text{miss})\]
Example, counted over the candidates:
Bayes rule \(\delta^*\): the rule with the smallest risk

Risk by iterated expectations: \[R(\delta) = \mathbb{E}_X\Big[\, \mathbb{E}[c(Y, \delta(X)) \mid X] \,\Big]\]
Bayes rule: \[\delta^*(\mathbf{x}) = \arg\min_a \sum_k c(k, a)\, P(Y=k \mid \mathbf{x})\]
Binary, with \(\pi(\mathbf{x}) = P(Y=1 \mid \mathbf{x})\):
\(\pi(\mathbf{x}) = \mathbb{E}[Y \mid X = \mathbf{x}]\): for \(Y \in \{0,1\}\), the posterior is the MMSE predictor
Example: a Gaussian fitted to each class, \(\mathcal{N}(116.6, 17.5^2)\) for noise and \(\mathcal{N}(56.7, 30.0^2)\) for pulsars, with \(P(Y{=}1) = 0.092\)

0-1 loss: every error costs 1, so the risk is the probability of error
Expected loss of deciding class \(a\): \(1 - P(Y=a \mid \mathbf{x})\)
MAP rule: the class with the largest posterior \[\delta_{\text{MAP}}(\mathbf{x}) = \arg\max_k P(Y=k \mid \mathbf{x})\]
Bayes error: the error rate of the MAP rule \[\mathbb{E}\Big[1 - \max_k P(Y=k \mid X)\Big]\]
Example: decide pulsar when \(x < 73.8\), where the fitted \(\pi(x) = 1/2\)

Decide pulsar when its expected loss is smaller: \[c_{01}(1-\pi) < c_{10}\,\pi \quad\Longleftrightarrow\quad \pi > \frac{c_{01}}{c_{01} + c_{10}}\]
Threshold on the posterior: \[\tau^* = \frac{c_{01}}{c_{01} + c_{10}}\]
Example, \(c_{01} = 1\), \(c_{10} = 10\): \(\tau^* = 1/11 = 0.091\)
| Rule | Pulsar if | Misses | False alarms |
|---|---|---|---|
| MAP, \(\tau = 0.5\) | \(x < 73.8\) | 30% of pulsars | 0.6% of noise |
| Costs, \(\tau^* = 0.091\) | \(x < 90.1\) | 16% of pulsars | 7.2% of noise |

Odds of an event: \(\text{odds}(A) = \dfrac{P(A)}{P(A^c)} = \dfrac{P(A)}{1 - P(A)}\)
Likelihood ratio: \[\Lambda(\mathbf{x}) = \frac{p(\mathbf{x} \mid Y=1)}{p(\mathbf{x} \mid Y=0)}\]
Bayes theorem, odds form (\(p(\mathbf{x})\) cancels): \[\underbrace{\frac{P(Y=1 \mid \mathbf{x})}{P(Y=0 \mid \mathbf{x})}}_{\text{posterior odds}} = \Lambda(\mathbf{x}) \cdot \underbrace{\frac{P(Y=1)}{P(Y=0)}}_{\text{prior odds}}\]
Log form: \(\log\) posterior odds \(= \log \Lambda(\mathbf{x}) + \log\) prior odds
Example at \(x = 80\), fitted densities:

Decide pulsar when posterior odds \(> c_{01}/c_{10}\)
Substituting posterior odds \(= \Lambda(\mathbf{x}) \times\) prior odds: \[\Lambda(\mathbf{x}) \;>\; \eta = \frac{c_{01}}{c_{10}} \cdot \frac{P(Y=0)}{P(Y=1)}\]
Example: \(\eta = \dfrac{1}{10} \cdot \dfrac{16{,}259}{1{,}639} = 0.99\)
| \(\eta\) from | \(\eta\) | Pulsar if |
|---|---|---|
| Neither (equal costs, equal priors) | 1 | \(x < 90.0\) |
| Prior only | 9.9 | \(x < 73.8\) |
| Costs and prior | 0.99 | \(x < 90.1\) |

Reject option: a third action, defer the decision to a human reviewer, at cost \(c_r\)
Expected loss of each action:
Bayes rule, the smallest of the three:
Two thresholds only if \(c_r < \dfrac{c_{01}\,c_{10}}{c_{01} + c_{10}}\), otherwise a single threshold, \(\tau^*\)
Example, \(c_{01} = 1\), \(c_{10} = 10\), \(c_r = 0.3 < 10/11 = 0.91\), two thresholds:

Gaussian fits to the pulsar data (spreads 17.5 and 30.0): log-odds \(= \log \Lambda(x) +\) log prior odds \[\log \frac{\pi(x)}{1 - \pi(x)} = 0.0011\,x^2 - 0.32\,x + 17.6\]
General: classes \(\mathcal{N}(\boldsymbol{\mu}_k, \boldsymbol{\Sigma}_k)\)
Shared \(\boldsymbol{\Sigma}\): the quadratic terms cancel \[\log \frac{P(Y=1 \mid \mathbf{x})}{P(Y=0 \mid \mathbf{x})} = \mathbf{w}^T\mathbf{x} + b, \qquad \mathbf{w} = \boldsymbol{\Sigma}^{-1}(\boldsymbol{\mu}_1 - \boldsymbol{\mu}_0)\] \[\pi(\mathbf{x}) = \frac{1}{1 + e^{-(\mathbf{w}^T\mathbf{x} + b)}}\]
Different variances, one feature: a quadratic term remains \[\log \frac{\pi(x)}{1 - \pi(x)} = a\,x^2 + w\,x + b, \qquad a = \frac{1}{2\sigma_0^2} - \frac{1}{2\sigma_1^2}\]
Data: labeled pairs \((\mathbf{x}_i, y_i)\), not densities and not \(\pi(\mathbf{x})\)
Estimating \(\pi(\mathbf{x})\) from labeled pairs:

Least squares on 0/1 labels estimates \(\mathbb{E}[Y \mid X = x] = P(Y = 1 \mid x)\)
Linear model \(\hat\pi(x) = w\,x + b\):
Example: least-squares line through the 17,898 pulsar labels

Odds \(\dfrac{\pi}{1 - \pi}\): from 0 to \(\infty\)
Log-odds \(\log \dfrac{\pi}{1 - \pi}\): any real number
Example: observed pulsar fraction per bin of \(x\) (figure)

Linear score \(z = \mathbf{w}^T\mathbf{x} + b\), which can be any real number
Model: the score is the log-odds \[\log \frac{\pi(\mathbf{x})}{1 - \pi(\mathbf{x})} = \mathbf{w}^T\mathbf{x} + b\]
Inverting: odds \(= e^{z}\), so \(\pi = e^{z}/(1 + e^{z})\) \[\boxed{\pi(\mathbf{x}) = \sigma(\mathbf{w}^T\mathbf{x} + b), \qquad \sigma(z) = \frac{1}{1 + e^{-z}}}\]
Sigmoid:
Not least squares on log-odds: a label’s log-odds is \(\log(1/0)\) or \(\log(0/1)\)
Example, fitted to the pulsar data: \(w = -0.111\), \(b = 7.97\)

Log-odds \(= \log \Lambda(\mathbf{x}) + \log\) prior odds
\(\log \Lambda\) linear in \(\mathbf{x}\):
Word counts, as in spam filtering: independent Poisson counts per word give \(\log \Lambda\) linear in the count vector

Two alternatives to the sigmoid:
Fit: nearly the same
Log-odds only:
Reason for the sigmoid: this reading, not its fit

Estimating \(\pi(\mathbf{x})\), with \(d\) input features:
Parameters, Gaussian classes with a shared covariance:
| \(d\) | Class densities | Logistic regression |
|---|---|---|
| 1 (profile mean) | 4 | 2 |
| 8 (all eight pulsar statistics) | 53 | 9 |
| 784 (\(28 \times 28\) image) | 309,289 | 785 |
Assumptions:
Limitations:
Log-odds \(= \sum_j w_j x_j + b\)
Example, pulsar fit \(w = -0.111\):
Decision boundary \(\pi(\mathbf{x}) = \tau\): \(\;\mathbf{w}^T\mathbf{x} + b = \log \frac{\tau}{1 - \tau}\)

Hard threshold on the score: decide 1 if \(z > 0\)
Clamp \(0.5 + z/4\), cut to \([0, 1]\)
Sigmoid \(\sigma(z)\): rises from 0 to 1 around \(z = 0\)
Fitting by gradient needs that slope: with the threshold or the clamp, most changes in \(\mathbf{w}\) leave the output unchanged

Chain rule on \(\sigma(z) = (1 + e^{-z})^{-1}\): \[\sigma'(z) = \frac{e^{-z}}{(1 + e^{-z})^2} = \underbrace{\frac{1}{1 + e^{-z}}}_{\sigma(z)} \cdot \underbrace{\frac{e^{-z}}{1 + e^{-z}}}_{1 - \sigma(z)}\]
\[\boxed{\sigma'(z) = \sigma(z)\,(1 - \sigma(z))}\]
Example: the slope of \(\pi(x)\) is \(w\,\pi(1 - \pi)\), largest in size at \(x = 72.1\), where it is \(-0.111 \times 1/4 = -0.028\)

Each label is a Bernoulli draw given its input: \[P(y_i \mid \mathbf{x}_i) = \pi_i^{\,y_i}(1 - \pi_i)^{1 - y_i}, \qquad \pi_i = \sigma(\mathbf{w}^T\mathbf{x}_i + b)\]
Independent labels: the likelihood is a product \[L(\mathbf{w}, b) = \prod_{i=1}^n \pi_i^{\,y_i}(1 - \pi_i)^{1 - y_i}\]
Negative log: the product becomes a sum \[\mathcal{L}(\mathbf{w}, b) = -\sum_{i=1}^n \big[\,y_i \log \pi_i + (1 - y_i) \log(1 - \pi_i)\,\big]\]
In floating point, the product underflows to 0 and the sum does not

Example: four labeled points \((x_i, y_i)\)
At \(w = 2\), \(b = -2\):
| \(x_i\) | \(y_i\) | \(\pi_i\) | loss |
|---|---|---|---|
| 0 | 0 | 0.12 | 0.13 |
| 1 | 1 | 0.50 | 0.69 |
| 1.5 | 0 | 0.73 | 1.31 |
| 2.5 | 1 | 0.95 | 0.05 |
\(\mathcal{L} = 2.18\)
Pulsar fit: the worst-predicted 1% of candidates carry 37% of \(\mathcal{L}\)

One label: \(z = \mathbf{w}^T\mathbf{x} + b\), \(\pi = \sigma(z)\) \[\ell = -\big[\,y \log \pi + (1 - y) \log(1 - \pi)\,\big]\]
Chain rule: \[\frac{\partial \ell}{\partial \pi} = -\frac{y}{\pi} + \frac{1 - y}{1 - \pi} = \frac{\pi - y}{\pi(1 - \pi)}, \qquad \frac{d\pi}{dz} = \pi(1 - \pi)\] \[\frac{\partial \ell}{\partial z} = \pi - y\]
Weights and bias: \[\frac{\partial \ell}{\partial \mathbf{w}} = (\pi - y)\,\mathbf{x}, \qquad \frac{\partial \ell}{\partial b} = \pi - y\]
Squared error on \(\pi\) keeps the factor \(\pi(1 - \pi)\), which is near 0 when the prediction is confidently wrong (figure)

Stack the data: \(\mathbf{X} \in \mathbb{R}^{n \times d}\) with rows \(\mathbf{x}_i^T\), \(\boldsymbol{\pi} = \sigma(\mathbf{X}\mathbf{w} + b\mathbf{1})\)
Sum over labels: \[\nabla_{\mathbf{w}}\mathcal{L} = \mathbf{X}^T(\boldsymbol{\pi} - \mathbf{y}), \qquad \frac{\partial \mathcal{L}}{\partial b} = \mathbf{1}^T(\boldsymbol{\pi} - \mathbf{y})\]
Least squares, same form: \[\nabla_{\mathbf{w}} \tfrac{1}{2}\|\mathbf{y} - \mathbf{X}\mathbf{w}\|^2 = \mathbf{X}^T(\mathbf{X}\mathbf{w} - \mathbf{y})\]
Example, four points at \(w = 2\), \(b = -2\) (figure):

Stationary point: \[\sum_{i=1}^n \big(\sigma(\mathbf{w}^T\mathbf{x}_i + b) - y_i\big)\,\mathbf{x}_i = \mathbf{0}, \qquad \sum_{i=1}^n \big(\sigma(\mathbf{w}^T\mathbf{x}_i + b) - y_i\big) = 0\]
The \(b\) equation: \(\sum_i \pi_i = \sum_i y_i\)
Example, four points: \(w = 1.60\), \(b = -2.00\)

Zero start: \(\mathbf{w} = \mathbf{0}\), \(b = 0\)
Class-fraction start: \(\mathbf{w} = \mathbf{0}\), \(b = \log \dfrac{\bar y}{1 - \bar y}\)
One minimum: the loss is convex, so every start ends at the same \((\mathbf{w}, b)\)
Random starts: needed for networks, where units that start equal stay equal

One iteration:
Example, four points, zero start, \(\eta = 0.5\):
Step size: \(\eta < 2/\lambda_{\max}\) of the Hessian

Hessian over \(\boldsymbol{\theta} = (\mathbf{w}, b)\), with \(\tilde{\mathbf{X}} = [\mathbf{X} \;\; \mathbf{1}]\): \[\mathbf{H} = \tilde{\mathbf{X}}^T \mathbf{S}\, \tilde{\mathbf{X}}, \qquad \mathbf{S} = \operatorname{diag}\big(\pi_i(1 - \pi_i)\big)\]
Positive semidefinite: for any \(\mathbf{v}\) \[\mathbf{v}^T\mathbf{H}\mathbf{v} = \sum_i \pi_i(1 - \pi_i)\,(\tilde{\mathbf{x}}_i^T\mathbf{v})^2 \ge 0\]
Example at the zero start: \(\mathbf{H} = \begin{bmatrix} 2.375 & 1.25 \\ 1.25 & 1 \end{bmatrix}\), eigenvalues 3.11 and 0.26
A minimum exists only if no hyperplane separates the classes

Newton: \(\boldsymbol{\theta} \leftarrow \boldsymbol{\theta} - \mathbf{H}^{-1}\mathbf{g}\), with \(\mathbf{g} = \tilde{\mathbf{X}}^T(\boldsymbol{\pi} - \mathbf{y})\), \(\mathbf{H} = \tilde{\mathbf{X}}^T\mathbf{S}\tilde{\mathbf{X}}\)
Rearranged: \[\tilde{\mathbf{X}}^T\mathbf{S}\tilde{\mathbf{X}}\,\boldsymbol{\theta}_{\text{new}} = \tilde{\mathbf{X}}^T\mathbf{S}\,\mathbf{r}, \qquad \mathbf{r} = \tilde{\mathbf{X}}\boldsymbol{\theta} + \mathbf{S}^{-1}(\mathbf{y} - \boldsymbol{\pi})\]
Cost per step:
Example, pulsar data, zero start, steps to \(\hat w\) within a relative \(10^{-6}\):

Reference: the fit to all 17,898 pulsar candidates, \(\hat w = -0.111\)
Subsamples: 500 random subsets at each \(n\), each fit by Newton
| \(n\) | median \(\hat w\) | middle 50% of \(\hat w\) | no finite \(\hat w\) |
|---|---|---|---|
| 50 | \(-0.117\) | \(-0.170\) to \(-0.090\) | 163 of 500 |
| 200 | \(-0.115\) | \(-0.138\) to \(-0.099\) | 1 of 500 |
| 1000 | \(-0.111\) | \(-0.120\) to \(-0.105\) | 0 |
Subsets of one data set stand in for new samples from the same survey

Data: 5,574 human-labeled English text messages
Legitimate:
Spam:
Decision: deliver the message, or send it to the spam folder
Costs: a lost legitimate message is worse than a delivered spam
Model: logistic regression on the words of each message
Data: T. A. Almeida, J. M. Gómez Hidalgo, A. Yamakami, SMS Spam Collection, UCI Machine Learning Repository (2011), CC BY 4.0.
Vocabulary: every word that appears in at least 5 of the 4,000 training messages
Features: \(x_j\) = number of times word \(j\) appears in the message
Example: “Free Msg: get Gnarls Barkleys”Crazy” ringtone TOTALLY FREE just reply GO to this message right now!”
{'crazy': 1, 'free': 2, 'get': 1, 'go': 1, 'just': 1, 'message': 1, 'msg': 1, 'now': 1, 'reply': 1, 'right': 1, 'ringtone': 1, 'this': 1, 'to': 1}
The loop from the four-point example, with the gradient averaged over the \(n = 4{,}000\) messages:
def sigmoid(z):
return 1 / (1 + np.exp(-z))
n = len(y_train)
w = np.zeros(len(vocab))
b = np.log(y_train.mean() / (1 - y_train.mean())) # class-fraction start
history = []
for step in range(1, 3001):
p = sigmoid(X_train @ w + b)
w -= 0.5 * X_train.T @ (p - y_train) / n
b -= 0.5 * (p - y_train).mean()
if step % 25 == 0:
p_test = sigmoid(X_test @ w + b)
history.append((step, ((p > 0.5) == y_train).mean(),
((p_test > 0.5) == y_test).mean(), np.linalg.norm(w)))Newton: a \(1{,}472 \times 1{,}472\) Hessian to form and solve at every step

Odds factor \(e^{w_j}\): each occurrence of word \(j\) multiplies the odds of spam by it
Largest factors:
Smallest factors:
Bias \(b = -4.55\): odds of spam 0.011 for a message with no vocabulary words

Score: \(z = b + \sum_j w_j x_j\), one term per word occurrence
Three test messages:
| Message | \(z\) | \(\pi\) |
|---|---|---|
| “For your chance to WIN a FREE Bluetooth Headset then simply reply back with”ADP”” | 2.02 | 0.88 |
| “Now am free call me pa.” | \(-1.31\) | 0.21 |
| “Latest News! Police station toilet stolen, cops have nothing to go on!” (spam) | \(-3.19\) | 0.04 |

Threshold from the costs, \(c_{01} = 10\), \(c_{10} = 1\): \[\tau^* = \frac{10}{10 + 1} = 0.91\]
Test messages (1,574, of which 200 spam):
| Threshold | Legitimate sent to spam | Spam delivered | Cost |
|---|---|---|---|
| \(\tau = 0.5\) | 5 | 22 | 72 |
| \(\tau^* = 0.91\) | 0 | 44 | 44 |
Lowest test cost: 37, at \(\tau = 0.69\), not at \(\tau^*\)

Spam test set: 1,574 messages, 200 spam (12.7%)
Accuracy, fraction correct:
Confusion matrix, \(\tau = 1/2\):
| sent to spam | delivered | |
|---|---|---|
| spam | TP = 178 | FN = 22 |
| legitimate | FP = 5 | TN = 1,369 |
Four counts:
At \(\tau^* = 0.91\): TP 156, FN 44, FP 0, TN 1,374
Accuracy and cost disagree whenever the two errors cost different amounts
Two rates, one per true class: \[\text{TPR} = \frac{\text{TP}}{\text{TP} + \text{FN}}, \qquad \text{FPR} = \frac{\text{FP}}{\text{FP} + \text{TN}}\]
At \(\tau = 1/2\): TPR \(= 178/200 = 0.89\), FPR \(= 5/1{,}374 = 0.0036\)
ROC curve: (FPR, TPR) as \(\tau\) runs from 1 to 0
Area under the curve (AUC): 0.979

Precision: fraction of messages sent to spam that are spam \[\text{Precision} = \frac{\text{TP}}{\text{TP} + \text{FP}}\]
Same TPR and FPR, rarer spam, by Bayes theorem: \[\text{Precision} = \frac{\text{TPR}\cdot P(Y{=}1)}{\text{TPR}\cdot P(Y{=}1) + \text{FPR}\cdot P(Y{=}0)}\]
| Spam fraction | Precision |
|---|---|
| 12.7% (this test set) | 0.97 |
| 1% | 0.71 |
| 0.1% | 0.20 |

Calibrated: among messages given \(\pi \approx q\), a fraction \(q\) is spam
Test messages, grouped by \(\pi\):
| \(\pi\) range | messages | mean \(\pi\) | fraction spam |
|---|---|---|---|
| 0 to 0.1 | 1,349 | 0.009 | 0.008 |
| 0.1 to 0.5 | 42 | 0.23 | 0.26 |
| 0.5 to 0.9 | 27 | 0.72 | 0.81 |
| 0.9 to 1 | 156 | 0.99 | 1.00 |
Separate from ranking: rescaling \(\pi\) without reordering changes calibration and leaves the ROC curve unchanged

Log-loss: the negative log-likelihood per message
Spam model, by gradient step:
| step | training log-loss | test log-loss | \(\|\mathbf{w}\|\) |
|---|---|---|---|
| 300 | 0.081 | 0.094 | 5.0 |
| 3,000 | 0.027 | 0.065 | 11.6 |
| 20,000 | 0.008 | 0.066 | 22.0 |

Posterior: a vector \(\boldsymbol{\pi}(\mathbf{x}) = [\pi_1(\mathbf{x}), \ldots, \pi_K(\mathbf{x})]^T\), with \(\pi_k(\mathbf{x}) = P(Y = k \mid \mathbf{x})\)
Cost matrix \(\mathbf{C} \in \mathbb{R}^{K \times K}\): \(c_{ka}\) = cost of deciding \(a\) when the class is \(k\)
Expected loss of deciding \(a\): \[R(a \mid \mathbf{x}) = \sum_{k=1}^K c_{ka}\,\pi_k(\mathbf{x}), \qquad \text{all } K \text{ at once: } \mathbf{C}^T\boldsymbol{\pi}(\mathbf{x})\]
Bayes rule: \(\delta^*(\mathbf{x}) = \arg\min_a R(a \mid \mathbf{x})\)
Example, \(K = 3\), class 3 costly to miss: \[\mathbf{C} = \begin{bmatrix} 0 & 1 & 1 \\ 1 & 0 & 1 \\ 5 & 5 & 0 \end{bmatrix}\]

One-vs-rest: \(K\) binary logistic models, class \(k\) against the other \(K - 1\)
Example data: 342 penguins, three species
One-vs-rest on these birds:
Argmax of the \(K\) outputs still gives a label
Expected losses \(\mathbf{C}^T\boldsymbol{\pi}\) need a probability vector
Data: Palmer Penguins, A. M. Horst, A. P. Hill, K. B. Gorman, 2020 (CC0); collected by K. Gorman and Palmer Station LTER

Softmax of scores \(\mathbf{z} \in \mathbb{R}^K\): \[\operatorname{softmax}(\mathbf{z})_k = \frac{e^{z_k}}{\sum_{j=1}^K e^{z_j}}\]
Example, \(\mathbf{z} = [2, 1, -1]\):
Scores tripled, \(\mathbf{z} = [6, 3, -3]\): \([0.953, 0.047, 0.0001]\)

Log of each numerator of Bayes theorem, classes \(\mathcal{N}(\boldsymbol{\mu}_k, \boldsymbol{\Sigma})\): \[\log P(Y{=}k)\,p(\mathbf{x} \mid Y{=}k) = z_k(\mathbf{x}) \underbrace{-\,\tfrac{1}{2}\mathbf{x}^T\boldsymbol{\Sigma}^{-1}\mathbf{x} + \text{const}}_{\text{same for every } k}\] \[z_k(\mathbf{x}) = \mathbf{w}_k^T\mathbf{x} + b_k\] \[\mathbf{w}_k = \boldsymbol{\Sigma}^{-1}\boldsymbol{\mu}_k, \qquad b_k = -\tfrac{1}{2}\boldsymbol{\mu}_k^T\boldsymbol{\Sigma}^{-1}\boldsymbol{\mu}_k + \log P(Y{=}k)\]
Normalizing: the shared term cancels \[\pi_k(\mathbf{x}) = \frac{e^{z_k(\mathbf{x})}}{\sum_{j=1}^K e^{z_j(\mathbf{x})}} = \operatorname{softmax}(\mathbf{z}(\mathbf{x}))_k\]
Example: one Gaussian per species with a shared covariance, fit to the 240 training penguins

Model: one weight vector and one bias per class \[\mathbf{z} = \mathbf{W}\mathbf{x} + \mathbf{b}, \qquad \boldsymbol{\pi}(\mathbf{x}) = \operatorname{softmax}(\mathbf{z})\]
Pairwise log-odds, linear: \[\log \frac{\pi_k(\mathbf{x})}{\pi_j(\mathbf{x})} = (\mathbf{w}_k - \mathbf{w}_j)^T\mathbf{x} + (b_k - b_j)\]
Assumption: every pairwise log-odds is linear in \(\mathbf{x}\); no model of \(p(\mathbf{x} \mid Y = k)\)
Example, flipper 205 mm: Adelie below a bill length of 44.1 mm, Gentoo to 47.6 mm, Chinstrap above

Shift by \(c\): the factor \(e^{c}\) cancels \[\frac{e^{z_k + c}}{\sum_j e^{z_j + c}} = \frac{e^{z_k}}{\sum_j e^{z_j}}\]
Reference class \(z_1 = 0\): \((K - 1)(d + 1)\) parameters
\(K = 2\): \(\pi_2 = e^{z_2} / (1 + e^{z_2}) = \sigma(z_2)\), logistic regression
Without a reference class: adding one \(\mathbf{v}\) to every \(\mathbf{w}_k\) leaves \(\boldsymbol{\pi}\) unchanged
One label \(y \in \{1, \ldots, K\}\), one-hot \(\mathbf{y}\): the categorical cross-entropy, the negative log-likelihood of a discrete label \[\ell = -\sum_{k=1}^{K} y_k \log \pi_k = -\log \pi_y\]
Softmax substituted: \[\ell = -z_y + \log \sum_{k=1}^{K} e^{z_k}\]
Data set: \(\mathcal{L}(\mathbf{W}, \mathbf{b}) = \sum_{i=1}^{n} \ell_i\)
\(K = 2\), \(z_1 = 0\), labels \(y \in \{0, 1\}\): \(\ell = \log(1 + e^{z}) - y\,z\), the binary cross-entropy
Example: a test penguin, Chinstrap, bill 50.8 mm, flipper 210 mm
| Adelie | Chinstrap | Gentoo | |
|---|---|---|---|
| \(z_k\) | \(-3.28\) | 1.35 | 1.93 |
| \(\pi_k\) | 0.003 | 0.357 | 0.640 |

Overflow: \(e^{z}\) exceeds the largest float for
Shift by \(m = \max_k z_k\), which changes nothing: \[\operatorname{softmax}(\mathbf{z}) = \operatorname{softmax}(\mathbf{z} - m)\] \[\log \sum_k e^{z_k} = m + \log \sum_k e^{z_k - m}\]
Loss from the scores: \(\ell = -z_y + m + \log \sum_k e^{z_k - m}\)
\(K = 2\): \(\ell = \log(1 + e^{z}) - y\,z\) is np.logaddexp(0, z) - y * z
[nan nan nan]
[0.66524096 0.24472847 0.09003057]
1000.4076059644444
Differentiating \(\ell = -z_y + \log \sum_k e^{z_k}\): \[\frac{\partial \ell}{\partial z_j} = -y_j + \frac{e^{z_j}}{\sum_k e^{z_k}} = \pi_j - y_j, \qquad \nabla_{\mathbf{z}}\,\ell = \boldsymbol{\pi} - \mathbf{y}\]
Weights, \(\mathbf{z} = \mathbf{W}\mathbf{x} + \mathbf{b}\): \[\nabla_{\mathbf{W}}\,\ell = (\boldsymbol{\pi} - \mathbf{y})\,\mathbf{x}^T \in \mathbb{R}^{K \times d}, \qquad \nabla_{\mathbf{b}}\,\ell = \boldsymbol{\pi} - \mathbf{y}\]
Data set, one row per sample, \(\boldsymbol{\Pi}, \mathbf{Y} \in \mathbb{R}^{n \times K}\): \[\nabla_{\mathbf{W}}\,\mathcal{L} = (\boldsymbol{\Pi} - \mathbf{Y})^T \mathbf{X}\]
Example, the test Chinstrap, \(\mathbf{y} = [0, 1, 0]\), standardized \(\mathbf{x} = [1.26, 0.59]\):
\[\nabla_{\mathbf{W}}\,\ell = \begin{bmatrix} 0.004 & 0.002 \\ -0.81 & -0.38 \\ 0.81 & 0.38 \end{bmatrix}\]
Descent step on this bird:
Standardized features, averaged gradient:
Penguins, 240 training and 102 test birds:
Test confusion matrix, rows the true species:
| Adelie | Chinstrap | Gentoo | |
|---|---|---|---|
| Adelie | 43 | 0 | 0 |
| Chinstrap | 1 | 22 | 2 |
| Gentoo | 0 | 0 | 34 |

Separable: a hyperplane (in one dimension, a threshold) with every training point on the side of its class
Example: 50 pulsar candidates, 6 of them pulsars
Sharper threshold, lower loss: multiplying \(\mathbf{w}\) and \(b\) by \(c > 1\) keeps the boundary and steepens \(\pi(x)\)
Spam training messages: close to separable, \(\|\mathbf{w}\| = 22.0\) at step 20,000 and still growing

Penalized loss, bias not penalized: \[R_\lambda(\mathbf{w}, b) = \frac{1}{n}\mathcal{L}(\mathbf{w}, b) + \frac{\lambda}{2}\|\mathbf{w}\|^2\]
Unique: Hessian \(\frac{1}{n}\tilde{\mathbf{X}}^T\mathbf{S}\tilde{\mathbf{X}} + \lambda\,\operatorname{diag}(1, \ldots, 1, 0)\) is positive definite
Finite: \(R_\lambda \to \infty\) as \(\|\mathbf{w}\| \to \infty\), also for separable data
Gradient step: \[\mathbf{w} \leftarrow (1 - \eta\lambda)\,\mathbf{w} - \frac{\eta}{n}\mathbf{X}^T(\boldsymbol{\pi} - \mathbf{y})\]
Example, the separable subset, \(\lambda = 0.01\): \(w = -2.60\) at step 1,000 and at step 100,000

MAP estimate: \[\hat{\mathbf{w}}_{\text{MAP}} = \arg\min_{\mathbf{w}, b}\; \mathcal{L}(\mathbf{w}, b) - \log p(\mathbf{w})\]
Gaussian prior, independent \(w_j \sim \mathcal{N}(0, \tau^2)\): \[-\log p(\mathbf{w}) = \frac{\|\mathbf{w}\|^2}{2\tau^2} + \text{const}\]
Laplace prior, independent \(p(w_j) = \frac{1}{2s}e^{-|w_j|/s}\): \[-\log p(\mathbf{w}) = \frac{\|\mathbf{w}\|_1}{s} + \text{const}\]
Narrower prior: larger penalty

One weight, loss \(\tfrac{1}{2}(w - a)^2\) around the unpenalized value \(a\):
| Penalty | \(\hat w\) | \(\hat w = 0\) when |
|---|---|---|
| \(\tfrac{\lambda}{2}w^2\) | \(\dfrac{a}{1 + \lambda}\) | \(a = 0\) |
| \(\lambda \lvert w \rvert\) | \(\operatorname{sign}(a)\max(\lvert a \rvert - \lambda,\, 0)\) | \(\lvert a \rvert \le \lambda\) |
Penalty slope at \(w = 0\):
Many weights: \(\hat w_j = 0\) whenever \(\lvert \partial(\text{loss}) / \partial w_j \rvert \le \lambda\) at the solution

Proximal gradient: a gradient step, then the soft threshold on every weight
Spam model, 5,000 steps:
| \(\lambda\) | words kept | test accuracy | test log-loss |
|---|---|---|---|
| 0 | 1,471 | 98.3% | 0.065 |
| \(3 \times 10^{-4}\) | 194 | 97.9% | 0.076 |
| \(10^{-3}\) | 85 | 97.1% | 0.097 |
| \(3 \times 10^{-3}\) | 38 | 96.0% | 0.132 |
Subgradient steps, \(\lambda\operatorname{sign}(\mathbf{w})\) added to the gradient: no weight exactly 0 after 5,000 steps

5-fold cross-validation on the 4,000 training messages:
Spam model, L2: lowest average log-loss 0.058, at \(\lambda = 10^{-4}\)

Validation split: 800 of the 4,000 training messages; gradient descent on the other 3,200
Stopping rule: the step with the lowest validation loss
Relation to L2: from \(\mathbf{w} = \mathbf{0}\), \(\|\mathbf{w}\|\) grows with the number of steps

XOR: class 1 where \(x_1\) and \(x_2\) have opposite signs
Logistic fit on 400 points:
Every model in this lecture has this limit

Added feature \(x_1 x_2\): logistic regression on \((x_1, x_2, x_1 x_2)\)
Choosing the feature: \(x_1 x_2\) comes from knowing the pattern
Number of features, all monomials of degree 1 to \(k\) in \(d\) inputs: \(\binom{d + k}{k} - 1\)
| \(d\) | \(k = 2\) | \(k = 3\) |
|---|---|---|
| 2 | 5 | 9 |
| 30 | 495 | 5,455 |
| 784 (\(28 \times 28\) image) | 308,504 | 80,931,144 |

Neuron: a weighted sum plus a bias, then a nonlinearity \[z = \mathbf{w}^T\mathbf{x} + b, \qquad a = \sigma(z)\]
Fitting the neuron, as in this lecture:
Label at the output: \(y\) enters the gradient through \(\pi - y\)

Hidden layer: \(m\) logistic units on the input \[\mathbf{h} = \sigma(\mathbf{W}_1\mathbf{x} + \mathbf{b}_1), \qquad \mathbf{W}_1 \in \mathbb{R}^{m \times d}\]
Output unit: logistic regression on \(\mathbf{h}\) \[\pi = \sigma(\mathbf{w}_2^T\mathbf{h} + b_2)\]
XOR with \(m = 2\), weights set by hand (figure):

Output weights \(\mathbf{w}_2\): logistic regression on \(\mathbf{h}\)
Hidden weights \(\mathbf{W}_1\): no label at \(\mathbf{h}\), so no \(\pi - y\) there
Cost of the hidden layer:
