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

41 optimizer.zero_grad() 34243 print(b, w)1 Defining an optimizer2 New "Step 4 - Updating Parameters" using the optimizer3 New "gradient zeroing" using the optimizerLet’s inspect our two parameters just to make sure everything is still working fine:Outputtensor([1.0235], device='cuda:0', requires_grad=True)tensor([1.9690], device='cuda:0', requires_grad=True)Cool! We’ve optimized the optimization process :-) What’s left?LossWe now tackle the loss computation. As expected, PyTorch has us covered onceagain. There are many loss functions to choose from, depending on the task athand. Since ours is a regression, we are using the mean squared error (MSE) as loss,and thus we need PyTorch’s nn.MSELoss():# Defines an MSE loss functionloss_fn = nn.MSELoss(reduction='mean')loss_fnOutputMSELoss()Notice that nn.MSELoss() is NOT the loss function itself: We do not passpredictions and labels to it! Instead, as you can see, it returns another function,which we called loss_fn: That is the actual loss function. So, we can pass aprediction and a label to it and get the corresponding loss value:Loss | 99

# This is a random example to illustrate the loss functionpredictions = torch.tensor(0.5, 1.0)labels = torch.tensor(2.0, 1.3)loss_fn(predictions, labels)Outputtensor(1.1700)Moreover, you can also specify a reduction method to be applied;that is, how do you want to aggregate the errors for individualpoints? You can average them (reduction=“mean”) or simply sumthem up (reduction=“sum”). In our example, we use the typicalmean reduction to compute MSE. If we had used sum as reduction,we would actually be computing SSE (sum of squared errors).Technically speaking, nn.MSELoss() is a higher-order function.If you’re not familiar with the concept, I will explain it briefly inChapter 2.We then use the created loss function in the code below, at line 29, to compute theloss, given our predictions and our labels:100 | Chapter 1: A Simple Regression Problem

# This is a random example to illustrate the loss function

predictions = torch.tensor(0.5, 1.0)

labels = torch.tensor(2.0, 1.3)

loss_fn(predictions, labels)

Output

tensor(1.1700)

Moreover, you can also specify a reduction method to be applied;

that is, how do you want to aggregate the errors for individual

points? You can average them (reduction=“mean”) or simply sum

them up (reduction=“sum”). In our example, we use the typical

mean reduction to compute MSE. If we had used sum as reduction,

we would actually be computing SSE (sum of squared errors).

Technically speaking, nn.MSELoss() is a higher-order function.

If you’re not familiar with the concept, I will explain it briefly in

Chapter 2.

We then use the created loss function in the code below, at line 29, to compute the

loss, given our predictions and our labels:

100 | Chapter 1: A Simple Regression Problem

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

Saved successfully!

Ooh no, something went wrong!