
Deep Learning · Session 3
Department of Computer Engineering, Chiang Mai University
Reference: Bishop & Bishop (2024), Chapter 5 — Single-layer Networks: Classification
Lecture 2 (regression):
Today (classification):
Same recipe, different noise model: choose a distribution for the target → the negative log-likelihood is your loss
Targets are now discrete classes. For \(K\) classes use 1-of-K (one-hot): class 2 of 3 → \(\vt=(0,1,0)\).
A linear model splits input space with a decision boundary:
\[ a(\vx) = \vw^{\mathsf T}\vx + b \]
But a raw score \(a(\vx)\) is not a probability — we will fix that.
Labels are \(t\in\{0,1\}\). We could fit them with the regression line from Lecture 2.
Add a cluster of points that are clearly class 1 but far from the boundary. Will the least-squares boundary stay put (those points are already correct) or swing toward them? Watch the next slide.


Add a few far-away points (right) and the least-squares boundary (magenta) swings away and misclassifies — while logistic regression (green) stays sensible.
Least squares penalizes being “too correct” — wrong noise model for class labels
Source: Bishop & Bishop (2024), Deep Learning, Fig 5.4
We need outputs in \([0,1]\). Pass the score through the logistic sigmoid:
\[ \sigma(a) = \frac{1}{1+e^{-a}}, \qquad p(\mathcal{C}_1\given\vx) = y(\vx) = \sigma\!\big(\vw^{\mathsf T}\boldsymbol{\phi}(\vx)\big) \]

Each label is Bernoulli: \(p(t\given\vx)=y^{\,t}(1-y)^{1-t}\), with \(t\in\{0,1\}\).
Negative log-likelihood over the dataset → the cross-entropy error:
\[ E(\vw) = -\sum_{n=1}^{N}\Big\{ t_n\ln y_n + (1-t_n)\ln(1-y_n) \Big\} \]
Cross-entropy = MLE under a Bernoulli target — the mirror image of “squared error = MLE under Gaussian” (Lecture 2)
A point gets score \(a=\vw^{\mathsf T}\boldsymbol\phi=1.0\), so the model leans class 1.
If this point’s true label is actually \(t=0\) (the model is confidently wrong), is its cross-entropy loss just a little bigger than the \(t=1\) case — or a lot?
\(y=\sigma(1)=\dfrac{1}{1+e^{-1}}=0.731\), and \(\text{loss}=-[\,t\ln y+(1-t)\ln(1-y)\,]\):
Confident and wrong costs ~4× more — cross-entropy punishes overconfidence, exactly why it beats squared error for labels.
A point has score \(a=2.0\).
Unlike regression, there is no closed-form solution — minimize by gradient descent. The gradient is strikingly simple:
\[ \nabla_{\vw} E = \sum_{n=1}^{N} \big(y_n - t_n\big)\,\boldsymbol{\phi}(\vx_n) \]
Exactly the same form as linear regression — (prediction − target) × input. The sigmoid’s nonlinearity cancels neatly against the cross-entropy.
Bishop §5.4.3 — the sigmoid + cross-entropy pairing is a canonical link
import numpy as np, matplotlib.pyplot as plt
rng = np.random.default_rng(0)
X = np.vstack([rng.normal([-1, -1], 0.8, (50, 2)),
rng.normal([1.3, 1.3], 0.8, (50, 2))])
y = np.r_[np.zeros(50), np.ones(50)]
Xb = np.c_[np.ones(len(X)), X]; w = np.zeros(3)
for _ in range(800): # gradient descent
p = 1/(1 + np.exp(-Xb @ w))
w -= 0.1 * Xb.T @ (p - y) / len(y) # ∇E = Σ (y−t) x
xx, yy = np.meshgrid(np.linspace(-4, 4, 200), np.linspace(-4, 4, 200))
P = 1/(1 + np.exp(-np.c_[np.ones(xx.size), xx.ravel(), yy.ravel()] @ w))
plt.figure(figsize=(4.6, 2.7))
plt.contourf(xx, yy, P.reshape(xx.shape), levels=20, cmap="RdBu", alpha=.6)
plt.contour(xx, yy, P.reshape(xx.shape), levels=[0.5], colors="k")
plt.scatter(X[:, 0], X[:, 1], c=y, cmap="RdBu", edgecolor="white", s=22, linewidths=0.6)
plt.xticks([]); plt.yticks([]); plt.tight_layout(); plt.show()
For \(K\) classes, compute a score per class \(a_k=\vw_k^{\mathsf T}\boldsymbol{\phi}(\vx)\) and normalize with softmax:
\[ y_k = p(\mathcal{C}_k\given\vx) = \frac{e^{a_k}}{\sum_{j=1}^{K} e^{a_j}} \quad\Big(y_k>0,\ \textstyle\sum_k y_k = 1\Big) \]
With one-hot targets \(t_{nk}\), the loss is categorical cross-entropy:
\[ E(\vw) = -\sum_{n=1}^{N}\sum_{k=1}^{K} t_{nk}\,\ln y_{nk} \]
\(K=2\) softmax = the sigmoid. Softmax + cross-entropy is the standard output layer of almost every classifier you will build.
Three class scores (logits) \(\mathbf a=(2,\,1,\,0)\).
Class 1 has the largest logit, by a gap of just 1. Guess its probability: more or less than \(\tfrac{2}{3}\)?
\(e^{2},\,e^{1},\,e^{0}=7.39,\ 2.72,\ 1.00\); sum \(=11.11\). \[\mathbf y=\Big(\tfrac{7.39}{11.11},\ \tfrac{2.72}{11.11},\ \tfrac{1.00}{11.11}\Big)=(0.665,\ 0.245,\ 0.090)\] If the true class is the 2nd: cross-entropy \(=-\ln 0.245 = \mathbf{1.41}\).
A 1-unit logit gap gives only \(0.665\) — softmax is “soft”, it keeps mass on the runners-up.
Logits \(\mathbf a=(1,\,3,\,0)\).
Logistic/softmax gives us \(p(\mathcal C_k\given\vx)\) — that was inference. But we must still act: treat the patient or not?
To minimize the misclassification rate, assign \(\vx\) to the class with the largest posterior:
\[\hat k = \arg\max_k\; p(\mathcal C_k \given \vx)\]
Pillar: this is the Bayes-optimal classifier — no rule beats picking the most probable class, if costs are symmetric. The decision step is trivial once inference gives us the probabilities.
Bishop & Bishop (2024), §5.2 (and §4.2 for regression) — Decision Theory
A loss matrix \(L_{kj}\) = cost of predicting \(j\) when truth is \(k\). Minimize expected loss → assign \(\vx\) to
\[\arg\min_j \sum_k L_{kj}\,p(\mathcal C_k\given\vx)\]
Melanoma (L0): missing a cancer (FN) is far worse than a false alarm (FP). Say \(L_{\text{FN}}=100,\ L_{\text{FP}}=1\).
The default threshold is \(0.5\). With FN \(100\times\) worse than FP, does the “predict cancer” threshold move up or down — and roughly how far?
Predict cancer when its expected loss is smaller:
\[1\cdot p(\text{benign}\given\vx) < 100\cdot p(\text{cancer}\given\vx)\]
\[\Rightarrow\ p(\text{cancer}\given\vx) > \tfrac{1}{101}\approx 0.01\]
The threshold drops from 0.5 → 0.01: flag cancer even at 1% probability.
When the model is unsure, it can abstain instead of guessing:
Trades coverage for accuracy on what it does answer.
This is exactly selective prediction (L15). It only works if the probabilities are calibrated — another reason inference quality matters.
input space \(\vx\) — not separable

feature space \(\boldsymbol{\phi}(\vx)\) — linearly separable

A fixed basis can make classes separable — but the next leap is to learn the features. That is the deep network.
Source: Bishop & Bishop (2024), Deep Learning, Fig 5.15 — two Gaussian basis functions map the data to a separable space
Something to think about: show that for \(K=2\) the softmax reduces exactly to the logistic sigmoid. (Hint: divide numerator and denominator by \(e^{a_1}\).)
Next session: Statistical Learning & Generalization — why and when models generalize.
Reading: Bishop & Bishop (2024) §4.3 (bias–variance); optional Shalev-Shwartz & Ben-David, Understanding Machine Learning (ERM, capacity, VC dimension).
Figures from Bishop & Bishop, Deep Learning: Foundations and Concepts (2024), Chapter 5 — https://www.bishopbook.com/
Deep Learning — Lecture 3