Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub

peiying410632
from peiying410632 More from this publisher
22.02.2024 Views

OutputDownloading: "https://download.pytorch.org/models/alexnet-owt-4df8aa71.pth" to ./pretrained/alexnet-owt-4df8aa71.pthFrom now on, it works as if we had saved a model to disk. To load the model’s statedictionary, we can use its load_state_dict() method:Loading Model1 alex.load_state_dict(state_dict)Output<All keys matched successfully>There we go! We have a fully trained AlexNet to play with! Now what?Model FreezingIn most cases, you don’t want to continue training the whole model. I mean, intheory, you could pick it up where it was left off by the original authors and resumetraining using your own dataset. That’s a lot of work, and you’d need a lot of data tomake any kind of meaningful progress. There must be a better way! Of course,there is: We can freeze the model.Freezing the model means it won’t learn anymore; that is, itsparameters / weights will not be updated anymore.What best characterizes a tensor representing a learnable parameter? It requiresgradients. So, if we’d like to make them stop learning anything, we need to changeexactly that:Helper Function #6 — Model freezing1 def freeze_model(model):2 for parameter in model.parameters():3 parameter.requires_grad = FalseTransfer Learning in Practice | 509

freeze_model(alex)The function above loops over all parameters of a given model and freezes them."If the model is frozen, how I am supposed to train it for my ownpurpose?"Excellent question! We have to unfreeze a small part of the model or, better yet,replace a small part of the model. We’ll be replacing the…Top of the ModelThe "top" of the model is loosely defined as the last layer(s) of the model, usuallybelonging to its classifier part. The featurizer part is usually left untouched sincewe’re trying to leverage the model’s ability to generate features for us. Let’sinspect AlexNet’s classifier once again:print(alex.classifier)OutputSequential((0): Dropout(p=0.5, inplace=False)(1): Linear(in_features=9216, out_features=4096, bias=True)(2): ReLU(inplace=True)(3): Dropout(p=0.5, inplace=False)(4): Linear(in_features=4096, out_features=4096, bias=True)(5): ReLU(inplace=True)(6): Linear(in_features=4096, out_features=1000, bias=True))It has two hidden layers and one output layer. The output layer produces 1,000logits, one for each class in the ILSVRC challenge. But, unless you are playing withthe dataset used for the challenge, you’d have your own classes to compute logitsfor.In our Rock Paper Scissors dataset, we have three classes. So, we need to replace theoutput layer accordingly:510 | Chapter 7: Transfer Learning

Output

Downloading: "https://download.pytorch.org/models/alexnet-owt-

4df8aa71.pth" to ./pretrained/alexnet-owt-4df8aa71.pth

From now on, it works as if we had saved a model to disk. To load the model’s state

dictionary, we can use its load_state_dict() method:

Loading Model

1 alex.load_state_dict(state_dict)

Output

<All keys matched successfully>

There we go! We have a fully trained AlexNet to play with! Now what?

Model Freezing

In most cases, you don’t want to continue training the whole model. I mean, in

theory, you could pick it up where it was left off by the original authors and resume

training using your own dataset. That’s a lot of work, and you’d need a lot of data to

make any kind of meaningful progress. There must be a better way! Of course,

there is: We can freeze the model.

Freezing the model means it won’t learn anymore; that is, its

parameters / weights will not be updated anymore.

What best characterizes a tensor representing a learnable parameter? It requires

gradients. So, if we’d like to make them stop learning anything, we need to change

exactly that:

Helper Function #6 — Model freezing

1 def freeze_model(model):

2 for parameter in model.parameters():

3 parameter.requires_grad = False

Transfer Learning in Practice | 509

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!