Logistic Regression

Logistic Regression

Logistic regression is a probabilistic discriminative classifier.

For binary classification, it directly models the posterior probability that the label is :

Since the two class probabilities must sum to one,

The predicted class is the one with the largest posterior probability:

Sigmoid Function

Logistic regression applies the sigmoid function to an affine transformation of the input:

where

The output of the sigmoid satisfies

which allows it to be interpreted as a probability.

The weight vector is

while is the bias or intercept.

During training, the parameters are iteratively adjusted to improve the model’s predictions.

The Affine Transformation

The quantity

can be expanded as

Strictly speaking, is an affine transformation, because it consists of the linear map followed by the translation .

In machine learning, it is nevertheless common to informally call this transformation “linear.”

The set

is a hyperplane in and constitutes the decision boundary of binary logistic regression when the classification threshold is .

Learning Logistic Regression

The parameters of logistic regression are estimated by maximizing the likelihood of the observed training labels.

Given

the likelihood is

assuming the training examples are conditionally independent given the model parameters.

Maximum-likelihood estimation chooses

Direct analytical maximization is generally unavailable, so numerical optimization algorithms are used.

Log-Likelihood

Instead of maximizing the likelihood directly, it is more convenient to maximize its logarithm:

Because the logarithm converts products into sums,

Since is strictly increasing,

Using the log-likelihood provides two important practical advantages.

First, it improves numerical stability. Multiplying many probabilities smaller than can produce extremely small numbers and cause floating-point underflow. Summing their logarithms avoids this problem.

Second, it simplifies optimization because derivatives of sums are much easier to compute than derivatives of large products.

Absorbing the Bias into the Weight Vector

The affine transformation

can be rewritten as a single dot product by appending a constant feature equal to to every input vector.

Define

Then

Thus, the bias can be treated as an additional model weight. This changes only the notation, not the model itself.

From this point onward, can be understood as the augmented input vector and as the corresponding bias-absorbed weight vector.

Binary Logistic Regression

After absorbing the bias into ,

and

For a binary label , the probability of the observed label can be written compactly as

Taking logarithms gives

Therefore, the total log-likelihood is

where

The negative log-likelihood is the binary cross-entropy loss, up to whether a sum or mean over examples is used.

Therefore,

Binary Cross-Entropy Loss

A common training objective is the mean binary cross-entropy:

where

The objective of training is to find parameters that minimize this loss.

Gradient Descent

Gradient descent is an iterative optimization algorithm used to minimize a differentiable function.

Given a loss , gradient descent moves the parameters in the direction opposite to the gradient because the gradient identifies the direction of steepest local increase.

Gradient of a Multivariate Function

For

the gradient is

Each component

measures the rate at which the loss changes as varies while the other parameters are held fixed.

The gradient points in the direction of steepest local ascent. Consequently,

is the direction of steepest local descent under the Euclidean norm.

Gradient-Descent Update

For each parameter,

or, in vector form,

The positive scalar is called the learning rate and controls the size of each optimization step.

Gradient Descent as a Local Optimization Problem

The gradient-descent update can be derived from a local approximation of the loss.

At iteration , approximate around using its first-order Taylor expansion:

Because this approximation is reliable only locally, introduce a quadratic penalty that discourages moving too far away from :

The quadratic term controls the size of the step. A smaller corresponds to a stronger penalty and therefore produces a smaller update.

To solve this optimization problem, differentiate the objective with respect to :

Therefore,

and hence

This is exactly the standard gradient-descent update.

Learning Rate

The learning rate strongly affects optimization.

If is too small, the parameter updates are very small and convergence can be extremely slow.

If is too large, the algorithm can overshoot the minimum, oscillate, or even diverge.

An appropriate learning rate therefore balances optimization speed and stability.

Convexity

A function is convex if, for any , , and ,

For a differentiable convex function, every local minimum is also a global minimum.

Under suitable conditions on the learning rate and the objective, gradient-based optimization can converge to a global minimizer of a convex loss.

For non-convex objectives, optimization may instead reach local minima, saddle points, or other stationary regions.

The unregularized binary logistic-regression cross-entropy loss is convex with respect to its parameters.

Simplifying the Logistic-Regression Loss

For logistic regression,

Also,

Starting from

substitute the logistic probabilities:

Using logarithm properties,

while

Therefore,

Equivalently,

Derivative of the Logistic-Regression Loss

Consider

For parameter ,

Moreover,

Since

we obtain

Equivalently,

Final Gradient and Update

Collecting all partial derivatives gives

If the examples are arranged row-wise in a design matrix

with

then the gradient is

The gradient-descent update is therefore

or explicitly,

Decision Boundary

The decision boundary is the set of points at which the two classes are equally probable:

Equivalently,

For logistic regression, the log-odds are

Therefore, the decision boundary satisfies

Hence,

is a hyperplane.

If the bias is written explicitly rather than absorbed into , the boundary is

Although the predicted probability is obtained through the nonlinear sigmoid function, the decision boundary is linear in the input features. Logistic regression is therefore a linear classifier.

Multiclass Logistic Regression

Logistic regression can be generalized from two classes to classes using multinomial logistic regression, also called softmax regression.

A common parameterization assigns one weight vector to each class and defines

Because adding the same vector contribution to every class score leaves the probabilities unchanged, one class can alternatively be chosen as a reference class. Setting its score to zero gives

and

The predicted class is

Evaluating a Classifier: Accuracy

Given true labels and predicted labels , accuracy is the proportion of correctly classified examples:

where is the indicator function:

Accuracy satisfies

It is often expressed as a percentage.

Training accuracy can be useful for diagnosing optimization and overfitting, but estimates of generalization performance should be computed on validation or test data rather than inferred from training accuracy.

Accuracy can also be misleading on highly imbalanced datasets. For example, if of the examples belong to one class, a classifier that always predicts that class already achieves accuracy despite learning no useful distinction between the classes.

Optimization Algorithms

Batch Gradient Descent

In batch gradient descent, the gradient used for every parameter update is computed from the entire training dataset.

For a dataset containing examples,

This provides an exact gradient of the empirical loss but can be computationally expensive when is very large.

Mini-Batch Stochastic Gradient Descent

In mini-batch stochastic gradient descent, each update uses only a subset of examples, where typically

For a mini-batch ,

and the update is

This gradient is an estimate of the full-dataset gradient.

Mini-batch training is particularly convenient because it provides a compromise between computational efficiency and gradient stability.

Stochastic Gradient Descent

In the strict sense, stochastic gradient descent (SGD) uses a single randomly selected training example for each update:

In modern machine-learning terminology, however, “SGD” is also commonly used more broadly to include mini-batch stochastic gradient methods.

SGD with Momentum

Momentum incorporates information from previous gradients rather than relying only on the current gradient.

One common formulation is

followed by

where

The parameter

determines how strongly past gradients are retained. A common value is

Momentum tends to smooth the optimization trajectory, reduce oscillations in directions with rapidly changing gradients, and accelerate movement in directions where gradients are consistently aligned.

Different libraries may use slightly different but equivalent momentum conventions, for example omitting the factor and compensating through the effective learning rate.

Adam Optimizer

Adam, or Adaptive Moment Estimation, combines two central ideas:

Momentum-like averaging uses an exponentially weighted average of past gradients to smooth the update direction.

Adaptive scaling uses an exponentially weighted average of squared gradients to assign different effective step sizes to different parameters.

Let

be the gradient at iteration .

First-Moment Estimate

Adam maintains an exponential moving average of gradients:

This quantity estimates the first moment of the gradient.

Second-Moment Estimate

Adam also maintains an exponential moving average of the element-wise squared gradients:

where denotes element-wise squaring.

This quantity estimates the uncentered second moment of the gradient.

Bias Correction

Because and are initialized to zero, the moving averages are biased toward zero during the first optimization steps.

Adam therefore computes

and

Adam Update

The parameters are updated element-wise according to

The small constant prevents numerical instability and division by zero.

Typical default values are

Adam: Advantages and Disadvantages

Advantages

Adam often performs well with relatively little hyperparameter tuning. It can handle noisy and sparse gradients effectively and frequently reaches useful solutions quickly in practical machine-learning problems.

Disadvantages

Adam requires additional memory because it stores both and for every parameter.

Depending on the model and task, solutions obtained using Adam may also generalize differently from those obtained with SGD or SGD with momentum. Consequently, Adam is not universally superior, and optimizer choice remains problem-dependent.

References

  1. J. Duchi, E. Hazan, and Y. Singer. Adaptive Subgradient Methods for Online Learning and Stochastic Optimization. Journal of Machine Learning Research, 12:2121–2159, 2011.

  2. D. P. Kingma and J. Ba. Adam: A Method for Stochastic Optimization. arXiv:1412.6980, 2014.

  3. M. D. Zeiler. ADADELTA: An Adaptive Learning Rate Method. arXiv:1212.5701, 2012.