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

Logistic Regression

A model for functions from ℝ^n to [0, 1] that predicts σ(w·x) with the sigmoid σ and learns the weights w by minimizing the binary cross-entropy loss, e.g. with the update w ← w + η·(y − σ(w·x))·x per example.

Know these firstLinear ClassifierSigmoid

Formula#

Gradient of the loss for one example (x,y)(x, y) with z=w⋅xz = w \cdot x:

∇wL(y,σ(w⋅x))=(σ(w⋅x)−y)⋅x.\nabla_w L\big(y, \sigma(w \cdot x)\big) = \big(\sigma(w \cdot x) - y\big) \cdot x.
ProofDerivation

Use σ′(z)=σ(z) σ(−z)\sigma'(z) = \sigma(z)\,\sigma(-z) and 1−σ(z)=σ(−z)1 - \sigma(z) = \sigma(-z) (Sigmoid > Formula). Then

∂L∂z=−[yσ′(z)σ(z)−(1−y)σ′(z)1−σ(z)]=−[y σ(−z)−(1−y) σ(z)]=−[y−yσ(z)−σ(z)+yσ(z)]=σ(z)−y,\frac{\partial L}{\partial z} = -\Big[y \frac{\sigma'(z)}{\sigma(z)} - (1 - y)\frac{\sigma'(z)}{1 - \sigma(z)}\Big] = -\big[y\,\sigma(-z) - (1 - y)\,\sigma(z)\big] = -\big[y - y\sigma(z) - \sigma(z) + y\sigma(z)\big] = \sigma(z) - y,

and by the chain rule ∇wL=∂L∂z⋅∇wz=(σ(z)−y) x\nabla_w L = \frac{\partial L}{\partial z} \cdot \nabla_w z = (\sigma(z) - y)\,x.

Stochastic gradient descent with batch size 1 and learning rate η\eta: for each training example in turn,

w←w+η⋅(yi−σ(w⋅xi))⋅xi.w \leftarrow w + \eta \cdot \big(y_i - \sigma(w \cdot x_i)\big) \cdot x_i.

One pass over all examples is an epoch. The factor yi−σ(w⋅xi)y_i - \sigma(w \cdot x_i) is the error between label and predicted probability.

Bias. A bias is included by a constant component 11 in every xx (then ww contains the bias weight), see Linear Classifier > Formula.

Properties#

  • It is a Linear Classifier: predicting class 1 iff σ(w⋅x)>1/2\sigma(w \cdot x) > 1/2 is the same as w⋅x>0w \cdot x > 0, because σ\sigma is strictly increasing with σ(0)=1/2\sigma(0) = 1/2. Unlike the Perceptron, it also outputs a probability, and the update is proportional to the error instead of only happening on mistakes.
  • Loss behaviour: for y=1y = 1 the loss is −ln⁡y′-\ln y', which is 00 at y′=1y' = 1 and grows to ∞\infty as y′→0y' \to 0; symmetrically for y=0y = 0. Confidently wrong predictions are punished heavily.
  • Multi-class generalization: replace σ\sigma by Softmax over one score per class. For two classes with scores (x,0)(x, 0), softmax gives (σ(x),σ(−x))(\sigma(x), \sigma(-x)), i.e. exactly logistic regression.

Example#

def epoch(X: torch.Tensor, y: torch.Tensor, w: torch.Tensor) -> torch.Tensor:
    for i in range(X.shape[0]):
        factor = 0.1 * (y[i] - torch.sigmoid(torch.dot(X[i], w)))
        w = w + factor * X[i]
    return w

(Exam WS 23/24, Task 5.2: one epoch, batch size 1, learning rate 0.1, bias already in X.)

Sources#

  • Not covered by a WS 25/26 lecture. Source: the DBIS/IR exams.
  • Exam WS 23/24 (2nd date), Task 5.1 (not published) (model and loss)
  • Exam WS 23/24, Task 5.2 (not published) (one training epoch)

Search

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