20.03.2021 Views

Deep-Learning-with-PyTorch

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

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

296 CHAPTER 11 Training a classification model to detect suspected tumors

Init model

Initialized

with Random

weights

FuLly

Trained

Init data loaders

LOop over epochs

Training LOop

Load batch tuple

ClaSsify Batch

Calculate LoSs

Record metrics

Update weights

Validation LOop

Load

batch tuple

ClaSsify Batch

Calculate LoSs

Record metrics

Log Metrics

console

tensorboard

Figure 11.7 The training and validation script we will implement in this chapter, with

a focus on the nested loops over each epoch and batches in the epoch

The code is relatively compact (the doTraining function is only 12 statements; it’s longer

here due to line-length limitations).

Listing 11.10

training.py:137, LunaTrainingApp.main

def main(self):

# ... line 143

for epoch_ndx in range(1, self.cli_args.epochs + 1):

trnMetrics_t = self.doTraining(epoch_ndx, train_dl)

self.logMetrics(epoch_ndx, 'trn', trnMetrics_t)

# ... line 165

def doTraining(self, epoch_ndx, train_dl):

self.model.train()

trnMetrics_g = torch.zeros(

METRICS_SIZE,

len(train_dl.dataset),

device=self.device,

)

Initializes an empty

metrics array

batch_iter = enumerateWithEstimate(

train_dl,

"E{} Training".format(epoch_ndx),

start_ndx=train_dl.num_workers,

)

for batch_ndx, batch_tup in batch_iter:

self.optimizer.zero_grad()

Sets up our batch looping

with time estimate

Frees any leftover

gradient tensors

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

Saved successfully!

Ooh no, something went wrong!