22.02.2024 Views

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

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

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

Losses

def plot_losses(self):

fig = plt.figure(figsize=(10, 4))

plt.plot(self.losses, label='Training Loss', c='b')

if self.val_loader:

plt.plot(self.val_losses, label='Validation Loss', c='r')

plt.yscale('log')

plt.xlabel('Epochs')

plt.ylabel('Loss')

plt.legend()

plt.tight_layout()

return fig

setattr(StepByStep, 'plot_losses', plot_losses)

Finally, if both training loader and TensorBoard were already configured, we can

use the former to fetch a single mini-batch and the latter to build the model graph

in TensorBoard:

Model Graph

def add_graph(self):

if self.train_loader and self.writer:

# Fetches a single mini-batch so we can use add_graph

x_dummy, y_dummy = next(iter(self.train_loader))

self.writer.add_graph(self.model, x_dummy.to(self.device))

setattr(StepByStep, 'add_graph', add_graph)

The Full Code

If you’d like to check what the full code of the class looks like, you can see it here [67]

or in the Jupyter notebook of this chapter.

We are classy now, so let’s build a classy pipeline too!

Going Classy | 193

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

Saved successfully!

Ooh no, something went wrong!