G12: Machine learning objectives, generalization, and decisions
On this page
- Prerequisites and observable goals
- The central question: what is learned, from which evidence?
- Derive a stable binary objective
- The derivative is a sensitivity, not a probability
- Curvature explains part of the optimization behavior
- Fitting, selection, and evaluation are separate stages
- From probabilities to actions
- Independent practice
- Smallest useful hints
- Complete solutions: G12
- Readiness and continuation
Prerequisites and observable goals#
foundation A01–A05 and T03; G04–G05; chain rule and gradients.
Derive logistic loss and its gradient, separate fitting from evaluation, and implement a stable learner whose success is measured on an appropriate held-out unit.
The central question: what is learned, from which evidence?#
A learning algorithm maps a training dataset to model parameters. A fitted model maps an input to a prediction. A decision policy maps that prediction to an action. These are three different functions with different failure modes. A low training loss says the first function found parameters that fit observed examples; it does not prove the prediction will transfer or that the action is justified.
Write a dataset , where and . The index identifies an example; the feature coordinate identifies a component. In the lab, rows are examples and weights are a length- vector. The empirical objective is an average of losses, possibly plus a regularizer:
The bias is deliberately not penalized. A different convention is possible but changes the derivatives and the fit. The target quantity for generalization is an expectation over the intended future population, not merely this finite average. Describe that population and the available evidence for sampling it. Stanford’s CS229 notes provide a primary course treatment of supervised objectives and classification. The numerical examples here are original. [G-ML]
Derive a stable binary objective#
Let the logit be and probability . The logit ranges over real numbers; the probability is in in exact arithmetic. These objects are not interchangeable. A Bernoulli likelihood for target is . Taking its negative logarithm gives
The last expression avoids calculating two logs of probabilities but can still overflow when is large. Use the algebraically equivalent form
Implement the last logarithm with log1p. For , the loss is approximately 1000, not NaN. A numerically saturated sigmoid of zero does not mean the mathematical likelihood model assigned an exact impossible event.
For , compute the sigmoid directly using ; for , use . Each branch avoids exponentiating a large positive value. This is a numerical implementation choice, not a new statistical model.
The derivative is a sensitivity, not a probability#
Differentiate the logit form:
By the chain rule, and . Therefore
The gradient has one component per parameter. It is not the vector of predictions and not an instruction to change the inputs. A gradient-descent update subtracts a learning-rate multiple of that gradient from the parameters, holding the training examples fixed.
Worked one-example step: . Then , , loss , weight gradient , and bias gradient . With learning rate , the simultaneous new parameters are . The new logit is , probability about , and loss about , smaller than . Compute both updates from the old state; updating one parameter before calculating the other can implement a different algorithm.
Curvature explains part of the optimization behavior#
With an augmented input and parameter vector , the data Hessian is
For any direction , its quadratic form is . Thus the data objective is convex in these linear-model parameters. Adding nonnegative L2 regularization preserves convexity. Positive definiteness and uniqueness require more conditions, especially because the bias is unpenalized.
On linearly separable observations, unregularized logistic loss can decrease as the weight norm grows without attaining a finite minimizer. A decreasing training curve is not evidence that parameters will converge to a bounded value. Regularization changes the objective and may control this growth; its strength must be selected without inspecting the final test outcomes.
Newton-type methods use curvature to choose a step by solving a linear system rather than literally forming an inverse. Damping or a line search can help control unsuitable steps. G05 explains conditioning; nearly dependent features can make curvature calculations difficult. The reference deliberately uses simple gradient descent so the state changes and derivatives remain inspectable, not because it is universally optimal.
Fitting, selection, and evaluation are separate stages#
Training data fits weights and preprocessing. Validation data chooses features, hyperparameters, thresholds, or stopping rules. Test data estimates the behavior of the frozen choice on the declared evaluation unit. If one user’s sessions appear in both training and test, the measurement may describe memorization of known users rather than performance on new users. Split by group when new groups are the intended target; split by time when forecasting the future. Neither split universally replaces the other.
Fit a scaler only on training rows; retain its parameters and apply them unchanged. Repeatedly comparing dozens of configurations on a validation set can overfit that set too. A final held-out set is not a renewable source of independent evidence after every design change. GP2 records a deliberately small predeclared candidate set and evaluates the test only after selection. Rerunning the same deterministic experiment reproduces the computation; it does not create another independent dataset.
Accuracy can obscure rare-class failures. Log loss measures probability assignments; Brier score measures squared probability error; precision and recall describe threshold decisions. Report counts and class balance. Calibration asks whether outcomes occur at frequencies consistent with predicted probabilities in a specified population and grouping procedure. Good discrimination does not automatically imply good calibration.
From probabilities to actions#
Suppose correct actions have cost zero, a false positive costs , and a false negative costs . If is the relevant posterior probability, the expected cost of a positive decision is ; the negative decision costs . Choose positive when
This derivation explains why is not a universal threshold. It depends on calibrated probabilities for the use population, suitable cost assumptions, and a legitimate decision policy. Review capacity, abstention, uncertainty, and stakeholder concerns can require a different design. Cost arithmetic does not settle the ethics in G11.
Machine learning is also not all of AI. Search uses explicit states and transitions, planning uses action consequences, and reinforcement learning uses rewards and state distributions affected by a policy. For a discounted finite Markov decision process, the Bellman optimality expression is with . For a self-looping state receiving reward 1 every step and , the fixed point is . This is a controlled model calculation, not permission to deploy a learned controller. Further reinforcement-learning study requires the modeling and evaluation assumptions that this supervised lab does not cover.
Independent practice#
F foundation C standard course work T transfer S optional stretch L runnable lab or project. Attempt the complete set before opening its solutions.
Reproduce the one-example logistic update at , rate , no penalty. Give old loss, both gradients, new logit, and why both updates use the old state.
For a regularized objective with and weights , compute the regularization loss and its gradient contribution. Explain why the reference adds no bias penalty.
A training loss decreases while validation loss increases over repeated epochs. Give two plausible mechanisms, one diagnostic experiment, and a selection policy that does not repeatedly inspect test outcomes.
With false-positive cost 9 and false-negative cost 1, derive the threshold and decide what the cost model recommends for . State two assumptions required for that recommendation.
A team reports 99% accuracy on data with 1% positives. Construct a trivial predictor with that accuracy, compute its recall, and propose two additional measurements. Then explain why adding more training iterations does not repair an invalid test split.
Implement sigmoid, logit_loss, loss_gradient, fit, and predict in starters/ml.py. Use average loss, unpenalized bias, finite-input checks, and strict labels. Compare derivatives with central differences and test logits . Train the supplied four-point example and distinguish fit from generalization.
Smallest useful hints#
For .02, differentiate . For .04, compare two expected costs before inserting the numbers. For .05, first ask what happens if every prediction is negative.
Complete solutions: G12#
Compare the first step where your work differs. A corrected answer is not yet an independent success.
G12.01Show solution
Substitute, differentiate, update, verify
The old logit is zero, probability , and loss . Gradients are and . Updating simultaneously gives , so and loss . The loss decreased for this instance and step. Using an updated weight when calculating the bias gradient would evaluate sensitivities at two different parameter states, not the stated gradient-descent update.
Back to G12.01G12.02Show solution
The convention determines the derivatives
The penalty is . Its weight gradient is . The objective explicitly excludes the bias, so its penalty derivative is zero. Penalizing the bias is another objective, not a hidden implementation optimization. Verify by differentiating the displayed formula rather than remembering an ambiguous “L2 gradient.”
Back to G12.02G12.03Show solution
A learning curve is a clue, not a diagnosis
Overfitting to training noise and mismatch between training/validation populations are plausible. A bug such as inconsistent preprocessing is another possibility. Inspect split units and frozen preprocessing, then compare a simple regularized baseline and curves under a controlled seed while keeping test outcomes hidden. Select a stopping rule on validation data or a predeclared schedule; evaluate the frozen result on the test. A rising validation loss does not by itself prove which mechanism caused it.
Back to G12.03G12.04Show solution
The expensive error raises the threshold
Choose positive when , so . At , positive expected cost is 1.8 and negative cost .8; choose negative within this model. The calculation assumes the probability is meaningful for this population and that these costs accurately represent the consequences with zero correct-decision costs. Ethical or operational constraints can invalidate automatic use even when the arithmetic is correct.
Back to G12.04G12.05Show solution
A majority classifier can hide complete failure on positives
Always predict zero: 99 of every 100 representative examples are correct, but TP=0 and FN includes all positives, so recall is zero. Report the confusion matrix and a probability-sensitive loss or a precision–recall analysis with explicit thresholds and counts. More optimization cannot turn leaked, duplicated, or improperly grouped test data into an independent evaluation; repair the sampling/splitting procedure and rerun the experiment transparently.
Back to G12.05G12.06Show solution
A small learner with visible assumptions
The code uses separate sigmoid branches, a stable logit loss, averaged gradients, and explicit regularization. Tests compare both weight and bias derivatives with numerical differences. The four-point fit is a functional test on known inputs, not a benchmark for a real application. The final parameter validation also exposes unstable steps instead of returning NaNs silently.
"""G12: from-scratch binary logistic regression; no external dependencies."""
import math
def validate_xy(X, y):
n, d = len(X), len(X[0]) if X else 0
if not 1 <= n <= 10000 or not 1 <= d <= 64 or len(y) != n:
raise ValueError("bounded nonempty samples/features and matching labels required")
if any(len(row) != d for row in X):
raise ValueError("ragged feature matrix")
if any(type(v) is not int or v not in (0,1) for v in y):
raise ValueError("strict 0/1 labels required")
if any(not math.isfinite(v) for row in X for v in row):
raise ValueError("finite features required")
return n, d
def sigmoid(z):
if not math.isfinite(z):
raise ValueError("finite logit required")
if z >= 0:
return 1/(1+math.exp(-z))
e = math.exp(z)
return e/(1+e)
def logit_loss(z, y):
if not math.isfinite(z) or type(y) is not int or y not in (0,1):
raise ValueError("finite logit and strict binary target required")
return max(z, 0.) - y*z + math.log1p(math.exp(-abs(z)))
def loss_gradient(w, b, X, y, lam=0.):
n, d = validate_xy(X, y)
if len(w) != d or not all(math.isfinite(v) for v in [*w,b,lam]) or lam < 0:
raise ValueError("valid parameters and nonnegative regularization required")
loss, grad, db = 0., [0.]*d, 0.
for row, target in zip(X,y):
z = math.fsum(a*v for a,v in zip(w,row)) + b
loss += logit_loss(z,target)/n
error = (sigmoid(z)-target)/n
db += error
for j in range(d):
grad[j] += error*row[j]
loss += .5*lam*math.fsum(v*v for v in w)
grad = [g+lam*v for g,v in zip(grad,w)]
if not all(math.isfinite(v) for v in [loss,*grad,db]):
raise ValueError("nonfinite loss/gradient: check scale")
return loss, grad, db
def fit(X, y, lam=.01, rate=.1, steps=400):
_, d = validate_xy(X,y)
if type(steps) is not int or not 1 <= steps <= 10000 or not math.isfinite(rate) or rate <= 0:
raise ValueError("bounded positive step count and rate required")
w,b = [0.]*d,0.
for _ in range(steps):
_,gw,gb = loss_gradient(w,b,X,y,lam)
w = [a-rate*g for a,g in zip(w,gw)]
b -= rate*gb
final,_,_ = loss_gradient(w,b,X,y,lam)
return {"weights":w, "bias":b, "objective":final}
def predict(model, X):
w,b = model["weights"],model["bias"]
if not w or not all(math.isfinite(v) for v in [*w,b]):
raise ValueError("invalid model")
if any(len(row) != len(w) or any(not math.isfinite(x) for x in row) for row in X):
raise ValueError("invalid feature shape or value")
return [sigmoid(math.fsum(a*x for a,x in zip(w,row))+b) for row in X]
if __name__ == "__main__":
X,y = [[-2.],[-1.],[1.],[2.]],[0,0,1,1]
model = fit(X,y)
print(model)
print(predict(model,X))Readiness and continuation#
Explain every dimension in the gradient and why a test sample must not fit the scaler. Then implement the same objective with a framework and compare outputs, gradients, and reduction conventions before trusting convenience APIs. Further study includes kernels, statistical learning theory, probabilistic modeling, calibrated uncertainty, and reinforcement learning. These require additional problem solving, not just a larger model.