liveBook · Manning

Class balancing

As explained in Deep Learning with PyTorch, Second Edition

A training-data preparation technique that equalizes class frequencies so the model does not learn a degenerate majority-class shortcut.

Class balancing reshapes training data so positive and negative samples appear at roughly equal rates. Without it, a class that is 500 times larger can fill about 14 of every 15 batches with negative examples, repeatedly pushing the model toward predicting only the majority class. section 14.4.1

A balanced training stream works by:

  • Reusing scarce positive samples when the dataset contains only a small fixed number of them. section 14.4.1

A balanced training option

A command-line --balanced flag enables ratio-based sampling. The training loader passes ratio_int=int(cli_args.balanced) to LunaDataset; with the flag enabled, the dataset reports a fixed length and alternates positive and negative selection according to the ratio.

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Explains imbalance consequences and implements ratio-based indexing, list shuffling, dataset length, and a command-line switch.
Examples
Uses LUNA candidate lists, repeated positive samples, alternating labels, and a --balanced training argument.
What it assumes
Assumes labeled positive and negative samples, batches, and basic dataset indexing; motivates the need for balancing through observed predictions.
What you can do after
Operate/configure a dataset loader that presents balanced positive and negative samples during training.
How it connects
Connects class imbalance with training batches, binary classification, dataset indexing, and model discrimination.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.