22.02.2024 Views

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Model Training — Top Model

1 sbs_top = StepByStep(top_model, multi_loss_fn, optimizer_top)

2 sbs_top.set_loaders(train_preproc_loader, val_preproc_loader)

3 sbs_top.train(10)

See? That was blazing fast!

Now we can attach the trained model to the top of the full (frozen) model:

Replacing the Top Layer

1 sbs_alex.model.classifier[6] = top_model

2 print(sbs_alex.model.classifier)

Output

Sequential(

(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): Sequential(

(0): Linear(in_features=4096, out_features=3, bias=True)

)

)

The sixth element of the classifier part corresponds to our small trained model.

Let’s see how it performs on the validation set.

We’re using the full model again, so we should use the original

dataset instead of the pre-processed one.

StepByStep.loader_apply(val_loader, sbs_alex.correct)

520 | Chapter 7: Transfer Learning

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

Saved successfully!

Ooh no, something went wrong!