--- title: Lottery Ticket Hypothesis keywords: fastai sidebar: home_sidebar summary: "How to find winning tickets with fastai" description: "How to find winning tickets with fastai" nb_path: "nbs/03_tutorial.lottery_ticket.ipynb" ---
{% raw %}
{% endraw %} {% raw %}
 
{% endraw %}

The Lottery Ticket Hypothesis

The Lottery Ticket Hypothesis is a really intriguing discovery made in 2019 by Frankle & Carbin. It states that:

A randomly-initialized, dense neural network contains a subnetwork that is initialised such that — when trained in isolation — it can match the test accuracy of the original network after training for at most the same number of iterations.

Meaning that, once we find that subnetwork. Every other parameter in the network becomes useless.

The way authors propose to find those subnetwork is as follows:1. Initialize the neural network2. Train it to convergence

  1. Prune the smallest magnitude weights by creating a mask $m$
  2. Reinitialize the weights to their original value; i.e at iteration $0$.
  3. Repeat from step 2 until reaching the desired level of sparsity.
{% raw %}
from fasterai.sparse.all import *
{% endraw %} {% raw %}
path = untar_data(URLs.PETS)
files = get_image_files(path/"images")

def label_func(f): return f[0].isupper()

device = 'cuda:0' if torch.cuda.is_available() else 'cpu'

dls = ImageDataLoaders.from_name_func(path, files, label_func, item_tfms=Resize(64), device=device)
{% endraw %}

What we are trying to prove is that: in a neural network A, there exists a subnetwork B able to get an accuracy $a_B > a_A$, in a training time $t_B < t_A$.

Let's get the baseline for network A:

{% raw %}
learn = Learner(dls, resnet18(num_classes=2), metrics=accuracy)
{% endraw %}

Let's save original weights

{% raw %}
initial_weights = deepcopy(learn.model.state_dict())
{% endraw %} {% raw %}
learn.fit(5, 1e-3)
epoch train_loss valid_loss accuracy time
0 0.590996 0.526236 0.740189 00:14
1 0.544030 0.507539 0.767930 00:14
2 0.518233 0.540433 0.707713 00:08
3 0.462022 0.488503 0.749662 00:08
4 0.430044 0.645429 0.702977 00:08
{% endraw %}

We now have our accuracy $a_A$ of $74\%$ and our training time $t_A$ of $5$ epochs

To find the lottery ticket, we will perform iterative pruning but, at each pruning step we will re-initialize the remaining weights to their original values (i.e. before training).

We will restart from the same initialization to be sure to not get lucky.

{% raw %}
learn = Learner(dls, resnet18(num_classes=2), metrics=accuracy)
learn.model.load_state_dict(initial_weights)
<All keys matched successfully>
{% endraw %}

We can pass the parameters lth=True to make the weights of the network reset to their original value after each pruning step, i.e. step 4) of the LTH. To empirically validate the LTH, we need to retrain the found "lottery ticket" after the pruning phase. Lottery tickets are usually found following an iterative pruning schedule. We set the start_epoch parameter to $5$ to begin the pruning process after $5$ epochs.

{% raw %}
sp_cb = SparsifyCallback(50, 'weight', 'local', large_final, iterative, start_epoch=5, lth=True)
{% endraw %}

As our iterative schedule makes $3$ pruning steps by default, it means that we have to train our network for start_epoch + $3*t_B$, so $20$ epochs in order to get our LTH. After each step, the remaining weights will be reinitialized to their original value

{% raw %}
learn.fit(20, 1e-3, cbs=sp_cb)
Pruning of weight until a sparsity of [50]%
Saving Weights at epoch 0
epoch train_loss valid_loss accuracy time
0 0.589156 0.558439 0.721245 00:08
1 0.557100 0.518048 0.735453 00:08
2 0.506434 0.541184 0.702300 00:08
3 0.483072 0.535563 0.720568 00:08
4 0.440117 0.504635 0.739513 00:08
5 0.549870 0.583103 0.664411 00:08
6 0.515395 0.598495 0.721922 00:08
7 0.484048 0.510811 0.731394 00:08
8 0.445385 0.775911 0.541949 00:08
9 0.396762 0.503371 0.779432 00:08
10 0.478721 0.438473 0.792287 00:08
11 0.445955 0.685162 0.554127 00:08
12 0.394884 0.392843 0.815968 00:08
13 0.355993 0.426261 0.807848 00:20
14 0.331038 0.351541 0.849120 00:08
15 0.424500 0.484486 0.756428 00:08
16 0.359705 0.489595 0.788227 00:08
17 0.323614 0.369151 0.835589 00:08
18 0.298134 0.435388 0.820704 00:08
19 0.266635 0.383132 0.835589 00:08
Sparsity at the end of epoch 0: [0.0]%
Sparsity at the end of epoch 1: [0.0]%
Sparsity at the end of epoch 2: [0.0]%
Sparsity at the end of epoch 3: [0.0]%
Sparsity at the end of epoch 4: [0.0]%
Resetting Weights to their epoch 0 values
Sparsity at the end of epoch 5: [16.67]%
Sparsity at the end of epoch 6: [16.67]%
Sparsity at the end of epoch 7: [16.67]%
Sparsity at the end of epoch 8: [16.67]%
Sparsity at the end of epoch 9: [16.67]%
Resetting Weights to their epoch 0 values
Sparsity at the end of epoch 10: [33.33]%
Sparsity at the end of epoch 11: [33.33]%
Sparsity at the end of epoch 12: [33.33]%
Sparsity at the end of epoch 13: [33.33]%
Sparsity at the end of epoch 14: [33.33]%
Resetting Weights to their epoch 0 values
Sparsity at the end of epoch 15: [50.0]%
Sparsity at the end of epoch 16: [50.0]%
Sparsity at the end of epoch 17: [50.0]%
Sparsity at the end of epoch 18: [50.0]%
Sparsity at the end of epoch 19: [50.0]%
Final Sparsity: [50.0]%
Sparsity in Conv2d 1: 50.00%
Sparsity in Conv2d 7: 50.00%
Sparsity in Conv2d 10: 50.00%
Sparsity in Conv2d 13: 50.00%
Sparsity in Conv2d 16: 50.00%
Sparsity in Conv2d 20: 50.00%
Sparsity in Conv2d 23: 50.00%
Sparsity in Conv2d 26: 50.00%
Sparsity in Conv2d 29: 50.00%
Sparsity in Conv2d 32: 50.00%
Sparsity in Conv2d 36: 50.00%
Sparsity in Conv2d 39: 50.00%
Sparsity in Conv2d 42: 50.00%
Sparsity in Conv2d 45: 50.00%
Sparsity in Conv2d 48: 50.00%
Sparsity in Conv2d 52: 50.00%
Sparsity in Conv2d 55: 50.00%
Sparsity in Conv2d 58: 50.00%
Sparsity in Conv2d 61: 50.00%
Sparsity in Conv2d 64: 50.00%
{% endraw %}

We indeed have a network B, whose accuracy $a_B > a_A$ in the same training time.

Lottery Ticket Hypothesis with Rewinding

In some case, LTH fails for deeper networks, author then propose a solution, which is to rewind the weights to a more advanced iteration instead of the initialization value.

{% raw %}
learn = Learner(dls, resnet18(num_classes=2), metrics=accuracy)
learn.model.load_state_dict(initial_weights)
<All keys matched successfully>
{% endraw %}

This can be done in fasterai by passing the rewind_epoch parameter, that will save the weights at that epoch, then resetting the weights accordingly.

{% raw %}
sp_cb = SparsifyCallback(50, 'weight', 'local', large_final, iterative, start_epoch=5, lth=True, rewind_epoch=1)
{% endraw %} {% raw %}
learn.fit(20, 1e-3, cbs=sp_cb)
Pruning of weight until a sparsity of [50]%
epoch train_loss valid_loss accuracy time
0 0.594084 0.877393 0.686062 00:08
1 0.569780 0.603819 0.711096 00:08
2 0.520782 0.645143 0.725981 00:08
3 0.495962 0.506920 0.738160 00:08
4 0.463314 0.502395 0.751691 00:08
5 0.524804 0.626302 0.701624 00:08
6 0.481978 0.535437 0.717185 00:08
7 0.438287 0.439397 0.797700 00:08
8 0.400104 0.696266 0.588633 00:08
9 0.366289 0.511664 0.760487 00:08
10 0.431968 0.649742 0.627876 00:08
11 0.395432 0.413708 0.819350 00:13
12 0.365231 0.397297 0.827470 00:14
13 0.323907 0.381637 0.820027 00:08
14 0.296680 0.397857 0.843031 00:08
15 0.366682 0.415927 0.801759 00:08
16 0.324701 0.388377 0.843031 00:08
17 0.294933 0.643028 0.776049 00:08
18 0.254401 0.443790 0.809878 00:08
19 0.229753 0.553675 0.804465 00:08
Sparsity at the end of epoch 0: [0.0]%
Saving Weights at epoch 1
Sparsity at the end of epoch 1: [0.0]%
Sparsity at the end of epoch 2: [0.0]%
Sparsity at the end of epoch 3: [0.0]%
Sparsity at the end of epoch 4: [0.0]%
Resetting Weights to their epoch 1 values
Sparsity at the end of epoch 5: [16.67]%
Sparsity at the end of epoch 6: [16.67]%
Sparsity at the end of epoch 7: [16.67]%
Sparsity at the end of epoch 8: [16.67]%
Sparsity at the end of epoch 9: [16.67]%
Resetting Weights to their epoch 1 values
Sparsity at the end of epoch 10: [33.33]%
Sparsity at the end of epoch 11: [33.33]%
Sparsity at the end of epoch 12: [33.33]%
Sparsity at the end of epoch 13: [33.33]%
Sparsity at the end of epoch 14: [33.33]%
Resetting Weights to their epoch 1 values
Sparsity at the end of epoch 15: [50.0]%
Sparsity at the end of epoch 16: [50.0]%
Sparsity at the end of epoch 17: [50.0]%
Sparsity at the end of epoch 18: [50.0]%
Sparsity at the end of epoch 19: [50.0]%
Final Sparsity: [50.0]%
Sparsity in Conv2d 1: 50.00%
Sparsity in Conv2d 7: 50.00%
Sparsity in Conv2d 10: 50.00%
Sparsity in Conv2d 13: 50.00%
Sparsity in Conv2d 16: 50.00%
Sparsity in Conv2d 20: 50.00%
Sparsity in Conv2d 23: 50.00%
Sparsity in Conv2d 26: 50.00%
Sparsity in Conv2d 29: 50.00%
Sparsity in Conv2d 32: 50.00%
Sparsity in Conv2d 36: 50.00%
Sparsity in Conv2d 39: 50.00%
Sparsity in Conv2d 42: 50.00%
Sparsity in Conv2d 45: 50.00%
Sparsity in Conv2d 48: 50.00%
Sparsity in Conv2d 52: 50.00%
Sparsity in Conv2d 55: 50.00%
Sparsity in Conv2d 58: 50.00%
Sparsity in Conv2d 61: 50.00%
Sparsity in Conv2d 64: 50.00%
{% endraw %}

Super-Masks

Researchers from Uber AI investigated the LTH and found the existence of what they call "Super-Masks", i.e. masks that, applied on a untrained neural network, allows to reach better-than-random results.

{% raw %}
learn = Learner(dls, resnet18(num_classes=2), metrics=accuracy)
learn.model.load_state_dict(initial_weights)
<All keys matched successfully>
{% endraw %}

To find supermasks, authors perform the LTH method then apply the mask on the original, untrained network. In fasterai, you can pass the parameter reset_end=True, which will reset the weights to their original value at the end of the training, but keeping the pruned weights (i.e. the mask) unchanged.

{% raw %}
sp_cb = SparsifyCallback(50, 'weight', 'local', large_final, iterative, start_epoch=5, lth=True, reset_end=True)
{% endraw %} {% raw %}
learn.fit(20, 1e-3, cbs=sp_cb)
Pruning of weight until a sparsity of [50]%
Saving Weights at epoch 0
epoch train_loss valid_loss accuracy time
0 0.600230 0.748891 0.686062 00:08
1 0.556918 0.889012 0.487145 00:08
2 0.521960 0.499532 0.768606 00:08
3 0.465840 0.665433 0.731394 00:08
4 0.446823 0.480587 0.769283 00:08
5 0.549696 0.968958 0.690798 00:08
6 0.521101 0.534350 0.744926 00:08
7 0.464701 0.443272 0.789581 00:08
8 0.422845 0.566891 0.702977 00:12
9 0.388480 0.400607 0.825440 00:16
10 0.480315 0.566955 0.691475 00:08
11 0.421218 0.471286 0.763193 00:08
12 0.386341 0.598505 0.697564 00:08
13 0.344785 0.420422 0.804465 00:08
14 0.313814 0.423030 0.803112 00:08
15 0.377422 0.462871 0.789581 00:08
16 0.343494 0.553291 0.810555 00:08
17 0.304831 0.451385 0.823410 00:08
18 0.274829 0.328464 0.864682 00:08
19 0.251222 0.347079 0.853857 00:08
Sparsity at the end of epoch 0: [0.0]%
Sparsity at the end of epoch 1: [0.0]%
Sparsity at the end of epoch 2: [0.0]%
Sparsity at the end of epoch 3: [0.0]%
Sparsity at the end of epoch 4: [0.0]%
Resetting Weights to their epoch 0 values
Sparsity at the end of epoch 5: [16.67]%
Sparsity at the end of epoch 6: [16.67]%
Sparsity at the end of epoch 7: [16.67]%
Sparsity at the end of epoch 8: [16.67]%
Sparsity at the end of epoch 9: [16.67]%
Resetting Weights to their epoch 0 values
Sparsity at the end of epoch 10: [33.33]%
Sparsity at the end of epoch 11: [33.33]%
Sparsity at the end of epoch 12: [33.33]%
Sparsity at the end of epoch 13: [33.33]%
Sparsity at the end of epoch 14: [33.33]%
Resetting Weights to their epoch 0 values
Sparsity at the end of epoch 15: [50.0]%
Sparsity at the end of epoch 16: [50.0]%
Sparsity at the end of epoch 17: [50.0]%
Sparsity at the end of epoch 18: [50.0]%
Sparsity at the end of epoch 19: [50.0]%
Final Sparsity: [50.0]%
Sparsity in Conv2d 1: 50.00%
Sparsity in Conv2d 7: 50.00%
Sparsity in Conv2d 10: 50.00%
Sparsity in Conv2d 13: 50.00%
Sparsity in Conv2d 16: 50.00%
Sparsity in Conv2d 20: 50.00%
Sparsity in Conv2d 23: 50.00%
Sparsity in Conv2d 26: 50.00%
Sparsity in Conv2d 29: 50.00%
Sparsity in Conv2d 32: 50.00%
Sparsity in Conv2d 36: 50.00%
Sparsity in Conv2d 39: 50.00%
Sparsity in Conv2d 42: 50.00%
Sparsity in Conv2d 45: 50.00%
Sparsity in Conv2d 48: 50.00%
Sparsity in Conv2d 52: 50.00%
Sparsity in Conv2d 55: 50.00%
Sparsity in Conv2d 58: 50.00%
Sparsity in Conv2d 61: 50.00%
Sparsity in Conv2d 64: 50.00%
{% endraw %} {% raw %}
learn.validate()
(#2) [1.0171763896942139,0.6853856444358826]
{% endraw %}