Ratios

How fasterai reads every compression ratio

Overview

Every compression ratio in fasterai is a fraction in [0, 1]: sparsity=0.5 means 50% of the weights, pruning_ratio=0.3 means 30% of the filters. Sparsifier, SparsifyCallback, Pruner, and PruneCallback all read their ratio arguments through as_fraction.

You pass fasterai reads What happens
0.4 0.4 40%
0 0.0 leave this one alone (per-layer dict value)
1 1.0 100%, not 1%
40 0.4 FutureWarning: percents are read as x/100 for one release
150, -1 ValueError
'0.4', True TypeError

Usage

from fasterai.core.ratio import as_fraction

as_fraction(0.4, 'sparsity')                       # 0.4
as_fraction(40, 'sparsity')                        # 0.4 + FutureWarning
as_fraction(0, 'pruning_ratio', layer='fc')        # 0.0 — leave 'fc' alone
as_fraction(0, 'pruning_ratio', allow_zero=False)  # ValueError: a ratio of 0 removes nothing

layer= only changes the message, so a bad value inside a per-layer dict names its layer:

as_fraction(150, 'pruning_ratio', layer='layer1.0.conv1')
# ValueError: pruning_ratio for layer 'layer1.0.conv1' must be a fraction in [0, 1] (0.4 = 40%), got 150.0

See Also

Tests live in nbs/tests/test_ratio.ipynb.