as_fraction
def as_fraction(
x, name:str, layer:str | None=None, allow_zero:bool=True
)->float:Read a compression ratio as a fraction in [0, 1]; a value in (1, 100] is read as a percent for one release
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 |
Read a compression ratio as a fraction in [0, 1]; a value in (1, 100] is read as a percent for one release
layer= only changes the message, so a bad value inside a per-layer dict names its layer:
sparsity as a fractionpruning_ratio as a fractionTests live in nbs/tests/test_ratio.ipynb.