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.

Define - Model Training V5

1 %%writefile model_training/v5.py

2

3 # Defines number of epochs

4 n_epochs = 200

5

6 losses = []

7 val_losses = []

8

9 for epoch in range(n_epochs):

10 # inner loop

11 loss = mini_batch(device, train_loader, train_step_fn)

12 losses.append(loss)

13

14 # VALIDATION - no gradients in validation!

15 with torch.no_grad():

16 val_loss = mini_batch(device, val_loader, val_step_fn)

17 val_losses.append(val_loss)

18

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

20 writer.add_scalars(main_tag='loss', 1

21 tag_scalar_dict={

22 'training': loss,

23 'validation': val_loss},

24 global_step=epoch)

25

26 # Closes the writer

27 writer.close()

1 Sending losses to TensorBoard

Run - Model Training V5

%run -i model_training/v5.py

TensorBoard | 161

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

Saved successfully!

Ooh no, something went wrong!