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.
Formula#
Gradient of the loss for one example with :
Stochastic gradient descent with batch size 1 and learning rate : for each training example in turn,
One pass over all examples is an epoch. The factor is the error between label and predicted probability.
Bias. A bias is included by a constant component in every (then contains the bias weight), see Linear Classifier > Formula.
Properties#
- It is a Linear Classifier: predicting class 1 iff is the same as , because is strictly increasing with . 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 the loss is , which is at and grows to as ; symmetrically for . Confidently wrong predictions are punished heavily.
- Multi-class generalization: replace by Softmax over one score per class. For two classes with scores , softmax gives , 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)