learning parameters num_epochs learning_rate device torch device ('cpu 'use this to run on cpu device torch device ('cuda 'use this to run on gpu instance of the conv net cnn cnn (cnn todevice device #loss function and optimizer criterion nn crossentropyloss (optimizer torch optim adam(cnn parameters (lrlearning_rate the learning loop losses [for epoch in range ( num_epochs + )for (images labels in enumerate train_loader )images variable images float ()todevice device labels variable labels todevice device optimizer zero_grad (outputs cnnimages loss criterion (outputs labels loss backward (optimizer step (losses append (loss item ()ifepoch == or epoch = )print (epoch "epoch "training loss"loss item ()evaluate on the test set cnn eval (correct total for images labels in test_loader images variable images float ()todevice device outputs cnnimages _predicted torch maxoutputs data total +labels size ( correct +predicted cpu (=labels sum (print ("test accuracy of the model on the , training test images "( correct item (total ),"%"plot plt rc('text 'usetex =trueplt rc('font 'family ='serif ',size = plt tight_layout (plt plot(np array losses )[ lenlosses )]plt xlabel ( 'iteration }',fontsize = plt ylabel ( 'batch loss}',fontsize = plt subplots_adjust (top = plt show (