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

Chapter 2.1Going ClassySpoilersIn this chapter, we will:• define a class to handle model training• implement the constructor method• understand the difference between public, protected, and private methods ofa class• integrate the code we’ve developed so far into our class• instantiate our class and use it to run a classy pipelineJupyter NotebookThe Jupyter notebook corresponding to Chapter 2.1 [61] is part of the official DeepLearning with PyTorch Step-by-Step repository on GitHub. You can also run itdirectly in Google Colab [62] .If you’re using a local installation, open your terminal or Anaconda prompt andnavigate to the PyTorchStepByStep folder you cloned from GitHub. Then, activatethe pytorchbook environment and run jupyter notebook:$ conda activate pytorchbook(pytorchbook)$ jupyter notebookIf you’re using Jupyter’s default settings, this link should open Chapter 2.1’snotebook. If not, just click on Chapter02.1.ipynb on your Jupyter’s home page.ImportsFor the sake of organization, all libraries needed throughout the code used in anygiven chapter are imported at its very beginning. For this chapter, we’ll need thefollowing imports:Spoilers | 175

import numpy as npimport datetimeimport torchimport torch.optim as optimimport torch.nn as nnimport torch.functional as Ffrom torch.utils.data import DataLoader, TensorDataset, random_splitfrom torch.utils.tensorboard import SummaryWriterimport matplotlib.pyplot as plt%matplotlib inlineplt.style.use('fivethirtyeight')Going ClassySo far, the %%writefile magic has helped us to organize the code into three distinctparts: data preparation, model configuration, and model training. At the end ofChapter 2, though, we bumped into some of its limitations, like being unable tochoose a different number of epochs without having to edit the model trainingcode.Clearly, this situation is not ideal. We need to do better. We need to go classy; thatis, we need to build a class to handle the model training part.I am assuming you have a working knowledge of object-orientedprogramming (OOP) in order to benefit the most from thischapter. If that’s not the case, and if you didn’t do it in Chapter 1,now is the time to follow tutorials like Real Python’s "Object-Oriented Programming (OOP) in Python 3" [63] and "SuperchargeYour Classes With Python super()." [64]The ClassLet’s start by defining our class with a rather unoriginal name: StepByStep. We’restarting it from scratch: Either we don’t specify a parent class, or we inherit it fromthe fundamental object class. I personally prefer the latter, so our class definitionlooks like this:176 | Chapter 2.1: Going Classy

Chapter 2.1

Going Classy

Spoilers

In this chapter, we will:

• define a class to handle model training

• implement the constructor method

• understand the difference between public, protected, and private methods of

a class

• integrate the code we’ve developed so far into our class

• instantiate our class and use it to run a classy pipeline

Jupyter Notebook

The Jupyter notebook corresponding to Chapter 2.1 [61] is part of the official Deep

Learning with PyTorch Step-by-Step repository on GitHub. You can also run it

directly in Google Colab [62] .

If you’re using a local installation, open your terminal or Anaconda prompt and

navigate to the PyTorchStepByStep folder you cloned from GitHub. Then, activate

the pytorchbook environment and run jupyter notebook:

$ conda activate pytorchbook

(pytorchbook)$ jupyter notebook

If you’re using Jupyter’s default settings, this link should open Chapter 2.1’s

notebook. If not, just click on Chapter02.1.ipynb on your Jupyter’s home page.

Imports

For the sake of organization, all libraries needed throughout the code used in any

given chapter are imported at its very beginning. For this chapter, we’ll need the

following imports:

Spoilers | 175

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

Saved successfully!

Ooh no, something went wrong!