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

peiying410632
from peiying410632 More from this publisher
22.02.2024 Views

And then, we update our model configuration code to include the creation of thecorresponding function for the validation step.Define - Model Configuration V21 %%writefile model_configuration/v2.py23 device = 'cuda' if torch.cuda.is_available() else 'cpu'45 # Sets learning rate - this is "eta" ~ the "n"-like Greek letter6 lr = 0.178 torch.manual_seed(42)9 # Now we can create a model and send it at once to the device10 model = nn.Sequential(nn.Linear(1, 1)).to(device)1112 # Defines an SGD optimizer to update the parameters13 optimizer = optim.SGD(model.parameters(), lr=lr)1415 # Defines an MSE loss function16 loss_fn = nn.MSELoss(reduction='mean')1718 # Creates the train_step function for our model, loss function19 # and optimizer20 train_step_fn = make_train_step_fn(model, loss_fn, optimizer)2122 # Creates the val_step function for our model and loss function23 val_step_fn = make_val_step_fn(model, loss_fn) 11 Creating a function that performs a validation stepRun - Model Configuration V2%run -i model_configuration/v2.pyFinally, we need to change the training loop to include the evaluation of our model.The first step is to include another inner loop to handle the mini-batches that comefrom the validation loader, sending them to the same device as our model. Then,inside that inner loop, we use the validation step function to compute the loss. "Wait, this looks oddly familiar too…" Evaluation | 149

And indeed, it is—it is structurally the same as our mini-batch function (HelperFunction #2). So let’s use it once again!There is just one small, yet important detail to consider: Remember no_grad()?We used it in Chapter 1 to avoid messing with PyTorch’s dynamic computationgraph during the (manual) update of the parameters. And it is making a comebacknow—we need to use it to wrap our new validation’s inner loop:torch.no_grad(): Even though it won’t make a difference in oursimple model, it is a good practice to wrap the validation innerloop with this context manager [59] to disable any gradientcomputation that you may inadvertently trigger—gradientsbelong in training, not in validation steps.Now, our training loop should look like this:Define - Model Training V41 %%writefile model_training/v4.py23 # Defines number of epochs4 n_epochs = 20056 losses = []7 val_losses = [] 389 for epoch in range(n_epochs):10 # inner loop11 loss = mini_batch(device, train_loader, train_step_fn)12 losses.append(loss)1314 # VALIDATION - no gradients in validation!15 with torch.no_grad(): 116 val_loss = mini_batch(device, val_loader, val_step_fn)217 val_losses.append(val_loss) 31 Using no_grad() as context manager to prevent gradient computation2 Performing a validation step3 Keeping track of validation loss150 | Chapter 2: Rethinking the Training Loop

And indeed, it is—it is structurally the same as our mini-batch function (Helper

Function #2). So let’s use it once again!

There is just one small, yet important detail to consider: Remember no_grad()?

We used it in Chapter 1 to avoid messing with PyTorch’s dynamic computation

graph during the (manual) update of the parameters. And it is making a comeback

now—we need to use it to wrap our new validation’s inner loop:

torch.no_grad(): Even though it won’t make a difference in our

simple model, it is a good practice to wrap the validation inner

loop with this context manager [59] to disable any gradient

computation that you may inadvertently trigger—gradients

belong in training, not in validation steps.

Now, our training loop should look like this:

Define - Model Training V4

1 %%writefile model_training/v4.py

2

3 # Defines number of epochs

4 n_epochs = 200

5

6 losses = []

7 val_losses = [] 3

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(): 1

16 val_loss = mini_batch(device, val_loader, val_step_fn)2

17 val_losses.append(val_loss) 3

1 Using no_grad() as context manager to prevent gradient computation

2 Performing a validation step

3 Keeping track of validation loss

150 | Chapter 2: Rethinking the Training Loop

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

Saved successfully!

Ooh no, something went wrong!