22.02.2024 Views

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

And then we modify the train() method to include a call to the protected method

defined above. It should come after the validation inner loop.

StepByStep Method

def train(self, n_epochs, seed=42):

# To ensure reproducibility of the training process

self.set_seed(seed)

for epoch in range(n_epochs):

# Keeps track of the number of epochs

# by updating the corresponding attribute

self.total_epochs += 1

# inner loop

# Performs training using mini-batches

loss = self._mini_batch(validation=False)

self.losses.append(loss)

# VALIDATION

# no gradients in validation!

with torch.no_grad():

# Performs evaluation using mini-batches

val_loss = self._mini_batch(validation=True)

self.val_losses.append(val_loss)

self._epoch_schedulers(val_loss)

1

# If a SummaryWriter has been set...

if self.writer:

scalars = {'training': loss}

if val_loss is not None:

scalars.update({'validation': val_loss})

# Records both losses for each epoch under tag "loss"

self.writer.add_scalars(main_tag='loss',

tag_scalar_dict=scalars,

global_step=epoch)

if self.writer:

# Closes the writer

self.writer.close()

setattr(StepByStep, 'train', train)

1 Calls the learning rate scheduler at the end of every epoch

484 | Chapter 6: Rock, Paper, Scissors

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

Saved successfully!

Ooh no, something went wrong!