Study Vault Search notes, aliases and full text
Graph
TopicDBISAI-written · not yet proofread

Linear Classifiers and Logistic Regression

Linear decision boundaries (hyperplanes, distance, separability), the perceptron, and logistic regression with the sigmoid, softmax and the cross-entropy loss trained by gradient steps.

Concepts#

  1. Linear Classifierhyperplane {w⋅x=b}\{w \cdot x = b\}, distance formula, linear separability, XOR start here
  2. Perceptronerror-driven updates w←w+yxw \leftarrow w + yx, b←b−yb \leftarrow b - y
  3. Sigmoidσ(z)=1/(1+e−z)\sigma(z) = 1/(1 + e^{-z}), derivative σ(z)σ(−z)\sigma(z)\sigma(-z)
  4. Softmaxvector to probability distribution; two-class case equals the sigmoid
  5. Logistic Regressionmodel σ(w⋅x)\sigma(w \cdot x), binary cross-entropy, update w←w+η(y−σ(w⋅x))xw \leftarrow w + \eta (y - \sigma(w \cdot x)) x

How it fits together#

  • Same decision rule, different learning: all models below classify by the sign of w⋅x−bw \cdot x - b (Linear Classifier).
ModelLearns w,bw, b byOutputUpdates
Perceptronmistakes onlyclasson misclassified points, fixed step
Logistic Regressiongradient of cross-entropyprobability σ(w⋅x)\sigma(w \cdot x)on every example, scaled by the error
Naive Bayes Classifiercounting, then wi=log⁡(pwiX/pwiY)w_i = \log(p_{w_i X}/p_{w_i Y})class (posterior ratio)none (closed form)
  • Chain for logistic regression: score z=w⋅xz = w \cdot x →\to Sigmoid gives a probability →\to cross-entropy compares it with the label →\to σ′=σ(z)σ(−z)\sigma' = \sigma(z)\sigma(-z) makes the gradient collapse to (σ(z)−y) x(\sigma(z) - y)\,x →\to gradient step.
  • Sigmoid vs. softmax: softmax([x,0])=(σ(x),σ(−x))\text{softmax}([x, 0]) = (\sigma(x), \sigma(-x)), so two-class softmax regression is logistic regression.
  • Limits: XOR-like data is not linearly separable, so no linear classifier gets all points right (WS 22/23). The perceptron does not converge there; logistic regression converges to a compromise.
  • Connection to retrieval: a score qTAq^T A in the vector space model (Word Embeddings and Language Models) is also a dot product; F1F_1, precision and recall come from Evaluation.

Key Formulas#

  • Hyperplane distance: dist⁡(x,H)=∣w⋅x−b∣/∥w∥\operatorname{dist}(x, H) = |w \cdot x - b| / \|w\| (Linear Classifier)
  • Sigmoid: σ(z)=1/(1+e−z)\sigma(z) = 1/(1 + e^{-z}), σ′=σ(z)σ(−z)\sigma' = \sigma(z)\sigma(-z), σ(−z)=1−σ(z)\sigma(-z) = 1 - \sigma(z) (Sigmoid)
  • Softmax: ezi/∑jezje^{z_i}/\sum_j e^{z_j} (Softmax)
  • Loss: L(y,y′)=−[yln⁡y′+(1−y)ln⁡(1−y′)]L(y, y') = -[y \ln y' + (1 - y)\ln(1 - y')]; SGD: w←w+η(y−σ(w⋅x)) xw \leftarrow w + \eta(y - \sigma(w \cdot x))\,x (Logistic Regression)

Exercises & Exam Questions#

SuccessSolution (not in the exam note; written by Claude, values checked by simulation)

4.1: after object 1 (boundary): w=−1w = -1, b=1b = 1; objects 2 to 4 are correct; object 5: w=4w = 4, b=0b = 0; objects 6 to 8 correct. End of round 1: w=4w = 4, b=0b = 0. 4.2: see the code in Perceptron > Example; the result is w∗=5w^* = 5, b∗=23b^* = 23. 4.3: induction over the update steps; if (wˉi,bˉi)=(−wi,−bi)(\bar w_i, \bar b_i) = (-w_i, -b_i) then the test (−y)(wˉix−bˉi)≤0(-y)(\bar w_i x - \bar b_i) \le 0 is the same as y(wix−bi)≤0y(w_i x - b_i) \le 0, so both runs update at the same steps, and wˉi+1=−wi−yx=−wi+1\bar w_{i+1} = -w_i - yx = -w_{i+1}, bˉi+1=−bi+y=−bi+1\bar b_{i+1} = -b_i + y = -b_{i+1}. The base case is 0=−00 = -0. For the swapped labels the simulation gives (−5,−23)(-5, -23).

SuccessSolution

The normal vector is w=(2,−2,1)w = (2, -2, 1) with ∣w∣=4+4+1=3|w| = \sqrt{4 + 4 + 1} = 3. dist(x,H)=∣w⋅x−b∣∣w∣\text{dist}(x, H) = \frac{|w \cdot x - b|}{|w|} with w⋅x=2−4+3=1w \cdot x = 2 - 4 + 3 = 1, so ∣1−7∣3=2\frac{|1 - 7|}{3} = 2.

SuccessSolution
  1. Chain rule: σ′(t)=−1(1+e−t)2⋅(−e−t)=e−t(1+e−t)2\sigma'(t) = \frac{-1}{(1 + e^{-t})^2} \cdot (-e^{-t}) = \frac{e^{-t}}{(1 + e^{-t})^2}.
  2. Equivalently σ′(t)=11+e−t⋅e−t1+e−t\sigma'(t) = \frac{1}{1 + e^{-t}} \cdot \frac{e^{-t}}{1 + e^{-t}}; the first factor is σ(t)\sigma(t).
  3. Multiplying the second factor by et/ete^t/e^t gives 1et+1=11+et=σ(−t)\frac{1}{e^t + 1} = \frac{1}{1 + e^{t}} = \sigma(-t).
  4. Hence σ′(t)=σ(t) σ(−t)\sigma'(t) = \sigma(t)\,\sigma(-t). ■\blacksquare
SuccessSolution

The origin is in HH iff w⋅0=bw \cdot \mathbf{0} = b, i.e. iff b=0b = 0. Distance: w⋅p=2w \cdot p = 2, ∣w∣=4⋅14=1|w| = \sqrt{4 \cdot \frac14} = 1, so dist(p,H)=∣2−0∣1=2\text{dist}(p, H) = \frac{|2 - 0|}{1} = 2.

SuccessSolution

(⇐\Leftarrow) P=R=1P = R = 1 gives F1=2/2=1F_1 = 2/2 = 1. (⇒\Rightarrow) F1=1F_1 = 1 means 1/P+1/R=21/P + 1/R = 2; since 0<P,R≤10 < P, R \le 1 we have 1/P≥11/P \ge 1 and 1/R≥11/R \ge 1, so both equal 11. XOR-like points: (1,1)(1,1) and (2,2)(2,2) in class 0, (2,1)(2,1) and (1,2)(1,2) in class 1. F1=1F_1 = 1 for both classes needs all points classified correctly, but class 0 sits on one diagonal and class 1 on the other, and no straight line separates them. (Proof by the midpoint argument in Linear Classifier > Properties.)

SuccessSolution

M(x,w)=σ(w∙x)M(x, w) = \sigma(w \bullet x) with σ(z)=11+e−z\sigma(z) = \frac{1}{1 + e^{-z}}, and the binary cross-entropy L(y,y′)=−[yln⁡y′+(1−y)ln⁡(1−y′)]L(y, y') = -[y \ln y' + (1 - y)\ln(1 - y')].

SuccessSolution
def epoch(X: torch.Tensor, y: torch.Tensor, w: torch.Tensor) -> torch.Tensor:
    for i in range(X.shape[0]):
        xi = X[i]    # the i-th input vector, a 1D tensor
        yi = y[i]    # the i-th label, a single float
        factor = 0.1 * (yi - torch.sigmoid(torch.dot(xi, w)))
        w = w + factor * xi
    return w

For cross-entropy loss, the gradient step for one example is w←w+η (yi−σ(w⋅xi)) xiw \leftarrow w + \eta\,(y_i - \sigma(w \cdot x_i))\,x_i with η=0.1\eta = 0.1; the bracket is the error between label and predicted probability.

SuccessSolution

softmax(x,0)=(exex+e0,e0ex+e0)=(exex+1,1ex+1)\text{softmax}(x, 0) = \left(\frac{e^x}{e^x + e^0}, \frac{e^0}{e^x + e^0}\right) = \left(\frac{e^x}{e^x + 1}, \frac{1}{e^x + 1}\right). Multiplying numerator and denominator of the first component by e−xe^{-x} gives 11+e−x=σ(x)\frac{1}{1 + e^{-x}} = \sigma(x); the second is 11+ex=σ(−x)\frac{1}{1 + e^{x}} = \sigma(-x). So softmax([x,0])=(σ(x),σ(−x))\text{softmax}([x, 0]) = (\sigma(x), \sigma(-x)), consistent with σ(x)+σ(−x)=1\sigma(x) + \sigma(-x) = 1.

Occurrence table#

SemesterTaskPointsSubtopics
WS 15/16 (not published)Task 420perceptron round, implementation, label-swap proof
WS 21/22 (not published)Task 5.2–5.315 of 25distance to a plane, derivative of the sigmoid
WS 22/23 (not published)Task 3.2–3.312 of 20origin in hyperplane, distance, F1F_1, XOR
WS 23/24 (not published)Task 5.2–5.313 of 20SGD epoch in PyTorch, softmax and sigmoid
WS 23/24 (2nd) (not published)Task 5.15 of 20model and cross-entropy loss

Lectures#

No WS 25/26 lecture covers this topic. Sigmoid, softmax and logistic regression appear in the two WS 23/24 exams, which already include the PyTorch parts that the WS 25/26 course also uses (DBIS_VL11 introduces PyTorch).

Search

Type In
↑ ↓ move ↵ open ⇥ next section Ctrl↵ results page Prefix # to search text only