Before a classifier goes on a GPU for a day, run three checks that take a second and catch most setup bugs: the output shape, the loss at initialization, and whether the model can memorize one batch. Write the function that runs them and reports what passed.
preflight(model, x, y, num_classes, steps=100, lr=0.01, init_tol=0.3, overfit_target=0.05) -> dict
x is one input batch, y its integer labels of shape (B,), and model(x) should return logits of shape (B, num_classes). Return a dict with these keys:
"shape_ok": the model output has shape exactly (B, num_classes)."init_loss": the mean cross-entropy of the untouched model on the batch, rounded to 3 decimals."expected_init_loss": the loss a classifier that spreads probability evenly over the classes would get, , rounded to 3 decimals."init_loss_ok": init_tol, using the unrounded loss."trainable_params": the number of parameter elements with requires_grad=True."overfit_ok": train a copy of the model on this one batch with torch.optim.Adam(lr=lr) for steps steps of cross-entropy; true when the loss of the trained copy on the batch is below overfit_target.shape_ok is false, set "init_loss" to None and both "init_loss_ok" and "overfit_ok" to false, and do not train."overfit_ok" is false and nothing is trained.