7 routes42 sections324 units

Unit 3.7 · about 3 h

191 / 324

Objective checks, tiny-set overfit and shuffled-label controls

A failing run is usually a broken objective before it is a broken optimizer. Shape, mask and reduction bugs change the loss silently, and the two controls separate them from optimization failures in minutes.

  • implement
  • measure

You can now

  • Derive per-position cross-entropy with class weights, ignore_index masks and mean versus sum reduction.
  • Run single-batch overfit and shuffled-label controls and state what each outcome proves about capacity, optimization and data.

Covers

  • Objective target shapes, masks, weights and reductions
  • Tiny-set overfit and shuffled-label controls

Study, in this order

4 steps

  1. Loss construction from likelihood
    Understanding Deep Learning - fitting, gradients and regularization

    Chapter 5: Maximum likelihood (5.1), Recipe for constructing loss functions (5.2) and multiclass classification (5.5)

  2. Target shapes, masks, weights and reductions
    PyTorch CrossEntropyLoss reference

    Shape, Parameters (weight, ignore_index, reduction, label_smoothing) and both Examples

  3. Random-label fitting evidence
    Understanding Deep Learning - fitting, gradients and regularization

    Section 8.4 with 15 percent randomized labels, and Chapter 20, Section 20.2.1: fitting random pixels and labels as capacity evidence

  4. Overfit, underfit and selection
    D2L - optimization and regularization slice

    Underfitting or Overfitting and Model Selection: capacity versus error and cross-validation discipline

Focus on

  • Mean reduction divides by the number of non-ignored positions, or by the sum of their weights when weight is set, not by the batch size. Averaging per-batch means with different valid counts weights tokens unequally; accumulate sums and counts instead.
  • ignore_index removes padding from both loss and gradient but keeps the positions in the tensor. Weight rescales classes for imbalance; neither fixes mislabeled data.
  • Single-batch overfit that fails proves an optimization or capacity bug, not a data bug: the model cannot fit even a handful of examples.
  • Shuffle the training labels only. On held-out data the shuffled run must score at chance; anything above chance means leakage between splits or an evaluation bug. On training data a large model can still memorize shuffled labels, more slowly than true ones, so a true-label run that trains no faster than the shuffled one is not using the signal.

Reproduce closed-book

Closed-book, compute cross-entropy for logits [[2, 1, 0], [1, 2, 0]] with targets [0, -100] under ignore_index -100 and mean reduction: 0.408. Then write the two controls with their pass and fail readings.

Traps

  • Dividing a masked loss by the batch size or sequence length. PyTorch divides by the valid count (the sum of valid weights); a hand-written mask that sums and divides by the full size shrinks the loss and its gradient as padding grows.
  • Expecting a shuffled-label run to fail to fit the training set. Overparameterized nets memorize random labels (UDL 20.2.1); the readings that matter are chance-level held-out accuracy and slower fitting than with true labels.
  • Comparing cross-entropy values across different reductions or target sets as quality scores. Mean versus sum differ by the denominator, and different targets differ in what they average over.

Practice

PyTorch CrossEntropyLoss doc examples, extended

Examples with class indices and class probabilities; extend the first with ignore_index and weight and check the denominator by hand

D2L Generalization: Exercises

Exercises: the zero-training-error question and the cross-validation questions

Self-test

  1. Logits [[2, 1, 0], [1, 2, 0]], targets [0, -100], ignore_index -100, mean reduction. What is the loss?

  2. Single-batch overfit stalls at high loss. What is ruled out, and what remains?

  3. Shuffled-label training reaches near-zero loss but true-label training does not generalize. What does the control prove?

  4. A shuffled-label run scores 30 percent held-out accuracy on a balanced 10-class task. What does that mean?

  5. Class weight 5.0 on the rare class under mean reduction. How does one rare example count?

Completion criterion

Stop when

Your hand-computed masked loss matches the framework to three decimals, and both controls give the predicted reading on a broken and a healthy run.

Mark passed, then open 3.8 Only mark this when you can do it without notes.

Leads to

Comes after

Resources for this unit

  • Understanding Deep Learning - fitting, gradients and regularization
    Simon J. D. Princeprimarytheory
    Use Chapters 6-9 and targeted parts of Chapter 20 for optimization, initialization, measurement, regularization and why training works. Re-derive the update rules you keep in the trainer.
  • D2L - optimization and regularization slice
    D2L authorsrequiredimplementation
    Use MLP implementation, dropout, deep generalization, numerical stability, optimization introduction, SGD, minibatch SGD, momentum, AdaGrad, RMSProp, Adam and learning-rate scheduler. Implement momentum, AdamW, initialization, dropout, one normalization, clipping and one schedule.
  • PyTorch CrossEntropyLoss reference
    PyTorchreferencereference
    Read Shape, Parameters (weight, ignore_index, reduction, label_smoothing) and both Examples; extend the class-index example with ignore_index and a class weight.

Also in this section

Quick find

Search Michi

Type to search every route, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels