Logistic regression
Logistic regression models class probabilities using a linear score. Binary classification uses a sigmoid; multiclass classification uses softmax. In both cases, maximum likelihood leads to a cross-entropy objective.
Vectors are column vectors, denotes the natural logarithm, and losses are summed over observations unless stated otherwise.
1. The binary model
Given observations
define the score
Here, is the number of observations, the number of features, the weight vector, and the intercept. The score can take any real value; it is not yet a probability.
Odds, logit, and sigmoid
For , the odds and logit are
For example, gives odds of : the event is four times as likely as its complement. Conversely, .
| Probability | Odds | Logit |
|---|---|---|
The logit ranges over all of . Logistic regression assumes that it is linear in the features:
Solving for gives
The sigmoid is the inverse of the logit. Its useful identities are
is the observed label, the model score, and the predicted probability. Training fits the probabilities to the labels; it does not try to make .
Prediction and coefficient interpretation
At threshold ,
The indicator is when its condition holds and otherwise. Since , the decision boundary is . For instance, gives the line .
For a general threshold ,
The boundary is linear in the given features. With fixed parameters and , changing the threshold shifts it parallel to itself.
Increasing by one unit, holding other features fixed, increases the log-odds by . Hence
The odds are multiplied by ; the probability does not increase by . If and the original probability is , the new odds are and the new probability is .
2. Maximum likelihood and cross-entropy
The Bernoulli probability mass function is
This selects when and when . Assuming conditional independence of the labels given the inputs and parameters,
The data are fixed while varies. A higher likelihood means a higher joint probability assigned to the observed labels; it is not a probability distribution over .
For labels , predictions give likelihood , whereas give .
Taking logarithms converts the product into a sum. Since the logarithm is increasing, maximizing likelihood is equivalent to minimizing negative log-likelihood:
This is binary cross-entropy. For one observation,
If , predictions of , , and incur losses of approximately , , and . Assigning little probability to the observed class incurs a large loss.
Using and , the same loss becomes
3. Gradient descent
The dependence of the loss on the parameters is . By the chain rule,
Since ,
Each observation contributes its probability error multiplied by its input vector.
Stacking inputs as rows of gives
The sigmoid acts componentwise. The gradient and update are
Here, is the iteration index and is the learning rate. Probabilities are recomputed after each update.
One update by hand
For , , and zero initial parameters,
The initial score is , so . Then
With ,
The new score is , giving . The loss decreases from approximately to .
For the mean loss , divide the gradient by . Without regularization, the minimizers are unchanged, but a fixed learning rate produces a smaller update.
4. Convexity and regularization
The Hessian
Differentiating the th gradient component with respect to gives
Thus,
The transpose appears because the second derivatives form an outer product. For ,
In contrast, is a scalar. Summing the Hessians over observations,
For any ,
The Hessian is positive semidefinite, so is convex. The quadratic form above is the second derivative of at : it measures curvature along direction .
For the previous example at initialization,
What convexity guarantees
Every local minimum is a global minimum. In particular, if , the first-order inequality for a convex function gives
for every .
This rules out suboptimal local minima. It does not guarantee a unique finite minimizer or convergence with an arbitrary learning rate. Minimizing training loss also does not guarantee good performance on new data.
If has full column rank, then for every . Since at finite parameters, the Hessian is positive definite and the loss is strictly convex. A finite minimizer, if it exists, is unique.
Rank deficiency can leave flat directions. If every input equals , the score is . Replacing with leaves every prediction unchanged.
A finite minimizer need not exist
Consider and , with
Although has full column rank,
No finite parameter vector attains zero loss. On strictly separable data, scaling a separating vector can keep reducing the loss without reaching a finite optimum.
L2 regularization
Penalizing all parameters, including the intercept, gives
The Hessian is positive definite, and the objective tends to infinity as . Hence there is a unique finite minimizer. If the intercept is unpenalized, this conclusion requires additional conditions.
5. Multiclass logistic regression
Suppose there are mutually exclusive classes. Let be the observed class and use a one-hot vector
For example, class 2 among three classes is represented by .
Assign each class a parameter vector and stack them as rows:
Softmax
Softmax converts the class scores into probabilities:
All probabilities are positive and sum to one. For example,
The predicted class is , equivalently the class with the largest score.
Softmax couples the probabilities through a shared denominator. Increasing one score while holding the others fixed increases that class’s probability and decreases the others. This differs from fitting independent binary classifiers.
For two classes,
recovering the sigmoid model through the score difference.
Cross-entropy and gradient
The one-hot exponents select the observed-class probability:
The negative log-likelihood is
For one observation,
Differentiation gives
The same result follows from the softmax derivative
where if and otherwise:
Since ,
This is a matrix, matching . For a batch, stack probabilities and labels as rows of :
For the mean loss, the update becomes
6. A multiclass example
Consider three observations with two features:
| Sample | First feature | Second feature | Class |
|---|---|---|---|
| 1 | 1 | ||
| 2 | 2 | ||
| 3 | 3 |
Use the mean loss without regularization. The input and label matrices are
Let the current parameters be
Each row corresponds to a class. The columns contain the intercept and the two feature coefficients.
Scores and loss
Applying softmax to each row gives
The observed-class probabilities are , , and , so
Gradient and update
Therefore,
For example, the entry in the first row and second column is
This is the derivative with respect to the first feature coefficient of class 1. With ,
Recomputing the scores and probabilities gives
The new mean loss is
The overall loss decreases, although the first two observations receive slightly lower probabilities for their correct classes. A batch update need not improve every observation individually. The third observation remains misclassified, so a loss decrease need not immediately improve classification accuracy.
7. Multiclass convexity and regularization
The Hessian of a single-observation loss with respect to its scores is
For any , let . Then
The loss is convex in the scores. Since the scores depend linearly on , it is also convex in .
Softmax is invariant to a common shift:
Adding the same vector to every class parameter adds to all scores for observation , leaving its probabilities unchanged. Thus this unrestricted parameterization does not have a unique unregularized minimizer, even when has full column rank. A finite minimizer may also fail to exist under separation.
An L2 penalty on all entries of gives
is the sum of squared matrix entries. With and all entries penalized, the objective has a unique finite minimizer. Changing from summed to mean cross-entropy requires rescaling the regularization coefficient to preserve the same relative penalty.
8. Numerical evaluation
The identity
avoids directly exponentiating a large positive score in binary cross-entropy.
For softmax, subtract the largest score in each row, :
This leaves probabilities unchanged and makes every exponent nonpositive. Log probabilities can be evaluated directly as
The main matrix formulas, using mean losses without regularization, are:
| Quantity | Binary | Multiclass |
|---|---|---|
| Parameters | ||
| Scores | ||
| Probabilities | ||
| Gradient |
The transpose positions follow from the storage convention: contains observations as rows, and contains class parameters as rows.
Source: Youngjoon Hong, Mathematical Foundations of Deep Neural Networks, Week 3 Monday lecture. Numerical examples and notes on uniqueness, existence, and regularization supplement the lecture material.