Summary

Logistic Regression is an algorithm to predict probabilities for binary classification problems, using a sigmoid function to squash predictions between 0 and 1.

Sigmoid

The logistic (sigmoid) function maps any real score to a probability in :

The linear score is the log-odds (logit) of the positive class: .

Cross-Entropy Loss

We fit by maximum likelihood, equivalently by minimizing the binary cross-entropy (log loss):

This loss is convex in , so gradient descent reaches the global optimum.

Gradient

The sigmoid derivative makes the gradient remarkably clean. For a single example:

The residual scales the update, exactly as in linear regression. Optimize with Gradient Descent.

Decision Boundary

Predicting class 1 when is equivalent to , which is a linear hyperplane in feature space. Logistic regression is a linear classifier; curved boundaries require adding nonlinear features or a kernel.

Multiclass Softmax

For classes, replace the sigmoid with softmax over per-class logits :

The gradient keeps the same form: .

torch

import torch
import torch.nn as nn
 
model = nn.Linear(n_features, n_classes)   # produces logits
loss_fn = nn.CrossEntropyLoss()            # softmax + NLL, numerically stable
opt = torch.optim.SGD(model.parameters(), lr=0.1)
 
for xb, yb in loader:                      # yb: integer class labels
    opt.zero_grad()
    logits = model(xb)
    loss = loss_fn(logits, yb)
    loss.backward()
    opt.step()
 
# binary case: probability via sigmoid on a single logit
prob = torch.sigmoid(model(x)[:, 1] - model(x)[:, 0])

Pitfalls

  • Do not apply softmax before CrossEntropyLoss; it expects raw logits and applies log-softmax internally for stability.
  • Perfectly separable data drives weights to infinity; use L2 regularization to keep them finite.
  • Predicted probabilities are not automatically calibrated across all settings; check with a reliability curve if you rely on them.
  • Standardize features so gradient descent converges quickly.