WRITE · TRACE · UNDERSTAND

Project: train logistic regression and restore the best validation checkpoint

Require nonempty identity-disjoint rectangular finite feature partitions with binary integer y, positive finite learning_rate, integer steps>=1 and finite l2>=0.

Read lesson: Project: train logistic regression and restore the best validation checkpoint →

Your experiment

Require nonempty identity-disjoint rectangular finite feature partitions with binary integer y, positive finite learning_rate, integer steps>=1 and finite l2>=0. Standardize from train population means/stds only; constant scales become one. Start weights/bias at zero. Each full-batch gradient step uses mean sigmoid error, L2 on weights only, then measures unregularized validation log loss. Keep the earliest strict best checkpoint by copying weights. Restore it for final test evaluation. Return weights,bias,means,scales,best_step,validation_loss,test_loss,predictions and validation history. Use stable logaddexp for sigmoid and loss.

Python runs in your browser. Your draft stays on this device.

What to look for

Review gradient normalization, regularization, fitted transformations and checkpoint ownership. Explain validation selection bias and preserve an independent final test population. Replay follows the main orchestration function; helper computations execute and are checked separately.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.

About this algorithm

How Project: train logistic regression and restore the best validation checkpoint works

The idea

Logistic regression scores a row with a weighted sum of its features and passes the score through a sigmoid to get a probability. Training lowers the log loss by gradient descent, and the gradient has a simple form: the mean over rows of probability minus label, times the features.

train_logistic adds what a real training run needs. Features are standardised with means and scales from train only. After every full-batch step it measures the log loss on validation and keeps a copy of the weights from the best step so far. Training loss keeps falling, but validation loss can turn upward once the model overfits, so the best checkpoint is restored before test is touched. The sigmoid and the loss use np.logaddexp, so large scores do not overflow.

How it works

  1. Validate the partitions and hyperparameters, then standardise all three sets with the train means and population standard deviations.
  2. Start with zero weights and bias, and compute probabilities as exp(-logaddexp(0, -logits)).
  3. Move the weights by learning_rate times the mean error gradient plus l2 * w, and the bias by the mean error alone.
  4. After each step, compute the unregularised validation log loss and append it to history.
  5. If that loss is strictly lower than the best so far, save the step number, w.copy() and b.
  6. After the last step, restore the saved checkpoint and use it for test_loss and the 0 or 1 predictions.

What it costs

Per step
O(n·d)
Total
O(steps · n · d)
Memory
O(n·d), one checkpoint

Every step is one pass over all n training rows and d features, plus a pass over the validation rows to score the checkpoint.

Where it is used

  • Keras provides EarlyStopping(restore_best_weights=True), which reloads the weights from the epoch with the best monitored value.
  • Credit scoring models are commonly logistic regressions, because each weight can be read as a change in log odds.
  • scikit-learn's LogisticRegression applies an L2 penalty by default, with its strength set by the C parameter.

Watch out for

  • Saving a reference to w instead of a copy, so a later in-place update such as w -= step overwrites the checkpoint.
  • Computing the sigmoid as 1 / (1 + exp(-z)), which overflows for large negative scores and can put log(0) into the loss.
  • Comparing losses with <=, which keeps the latest tie instead of the earliest best step that the contract asks for.