366
FODVV
&11 QQ0RGXOH
GHI
BBLQLWBB VHOI
VXSHU
&11VHOI BBLQLWBB
VHOIFRQY QQ&RQYG
VHOIFRQY QQ&RQYG
VHOIGHQVH QQ/LQHDU
VHOIGHQVH QQ/LQHDU QXPBFODVVHV
GHI
IRUZDUG VHOI[
[ )UHOX VHOIFRQY [
[ )UHOX VHOIFRQY [
[ )PD[BSRROG [
[ )GURSRXW [
[ [YLHZ
[ )UHOX VHOIGHQVH [
[ )GURSRXW [
[ VHOIGHQVH [
UHWXUQ
)ORJBVRIWPD[ [GLP
Figure B-5. Defining the model
appendix B intro to pytorch
367
With that out of the way, you can define both the training and testing functions (see
Figure
B-7
and Figure
B-8
for
the training function, and Figure
B-9
and Figure
B-10
for
the testing function).
Figure B-6. The code in Figure
B-5
in a Jupyter cell
appendix B intro to pytorch
368
GHI
WUDLQ PRGHOGHYLFHWUDLQBORDGHUFULWHULRQRSWLPL]HUHSRFK
VDYHBGLU PRGHOFNSW
WRWDOBVWHS OHQ WUDLQBORDGHU
IRU
L LPDJHVODEHOV
LQ HQXPHUDWH
WUDLQBORDGHU
LPDJHV LPDJHVWR GHYLFH
ODEHOV ODEHOVWR GHYLFH
)RUZDUGSDVV
RXWSXWV PRGHO LPDJHV
ORVV FULWHULRQ RXWSXWVODEHOV
%DFNZDUGDQGRSWLPL]H
RSWLPL]HU]HURBJUDG
ORVVEDFNZDUG
RSWLPL]HUVWHS
LI
L
SULQW
(SRFK>^`^`@6WHS>^`^`@/RVV
^I` IRUPDW HSRFKQXPBHSRFKVLWRWDOBVWHSORVVLWHP
WRUFKVDYH PRGHOVWDWHBGLFW S\WRUFKBPQLVWBFQQFNSW
Figure B-7. The training algorithm. The for loop takes each pair of image and
labels and passes them into the GPU as a tensor. They then go into the model, and
the gradients are calculated. The information about the epoch and loss are then
output
appendix B intro to pytorch
369
The training function takes in the following parameters:
•
model: An instance of a model class. In this case, it’s an instance of
the CNN class defined above.
•
device: This basically tells PyTorch what device (if the GPU is an
option, which GPU to run on, and if not, the CPU is the device) to run
on. In this case, you define the device right after the imports.
•
train_loader: The loader for the training data set. In this case, you
use a data_loader because that’s how the MNIST data is formatted
when importing from torchvision. This data loader contains the
training samples for the MNIST data set.
•
criterion: The loss function to use. Define this before calling the train
function.
•
optimizer: The optimization function to use. Define this before
calling the train function.
•
epoch: What epoch is running. In this case, you call the training
function in a for loop while passing in the iteration as the epoch.
The testing function is shown in Figure
B-9
and Figure
B-10
.
Figure B-8. The code in Figure
B-7
in a Jupyter cell
appendix B intro to pytorch