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

Notebook Cell 1.8 - PyTorch’s loss in action: no more manual loss computation!1 # Sets learning rate - this is "eta" ~ the "n"-like2 # Greek letter3 lr = 0.145 # Step 0 - Initializes parameters "b" and "w" randomly6 torch.manual_seed(42)7 b = torch.randn(1, requires_grad=True, \8 dtype=torch.float, device=device)9 w = torch.randn(1, requires_grad=True, \10 dtype=torch.float, device=device)1112 # Defines an SGD optimizer to update the parameters13 optimizer = optim.SGD([b, w], lr=lr)1415 # Defines an MSE loss function16 loss_fn = nn.MSELoss(reduction='mean') 11718 # Defines number of epochs19 n_epochs = 10002021 for epoch in range(n_epochs):22 # Step 1 - Computes model's predicted output - forward pass23 yhat = b + w * x_train_tensor2425 # Step 2 - Computes the loss26 # No more manual loss!27 # error = (yhat - y_train_tensor)28 # loss = (error ** 2).mean()29 loss = loss_fn(yhat, y_train_tensor) 23031 # Step 3 - Computes gradients for both "b" and "w" parameters32 loss.backward()3334 # Step 4 - Updates parameters using gradients and35 # the learning rate36 optimizer.step()37 optimizer.zero_grad()3839 print(b, w)Loss | 101

1 Defining a loss function2 New "Step 2 - Computing Loss" using loss_fn()Outputtensor([1.0235], device='cuda:0', requires_grad=True)tensor([1.9690], device='cuda:0', requires_grad=True)Let’s take a look at the loss value at the end of training…lossOutputtensor(0.0080, device='cuda:0', grad_fn=)What if we wanted to have it as a Numpy array? I guess we could just use numpy()again, right? (And cpu() as well, since our loss is in the cuda device.)loss.cpu().numpy()OutputRuntimeErrorTraceback (most recent call last) in ----> 1 loss.cpu().numpy()RuntimeError: Can't call numpy() on Variable that requiresgrad. Use var.detach().numpy() instead.What happened here? Unlike our data tensors, the loss tensor is actually computinggradients; to use numpy(), we need to detach() the tensor from the computationgraph first:loss.detach().cpu().numpy()102 | Chapter 1: A Simple Regression Problem

1 Defining a loss function

2 New "Step 2 - Computing Loss" using loss_fn()

Output

tensor([1.0235], device='cuda:0', requires_grad=True)

tensor([1.9690], device='cuda:0', requires_grad=True)

Let’s take a look at the loss value at the end of training…

loss

Output

tensor(0.0080, device='cuda:0', grad_fn=<MeanBackward0>)

What if we wanted to have it as a Numpy array? I guess we could just use numpy()

again, right? (And cpu() as well, since our loss is in the cuda device.)

loss.cpu().numpy()

Output

RuntimeError

Traceback (most recent call last)

<ipython-input-43-58c76a7bac74> in <module>

----> 1 loss.cpu().numpy()

RuntimeError: Can't call numpy() on Variable that requires

grad. Use var.detach().numpy() instead.

What happened here? Unlike our data tensors, the loss tensor is actually computing

gradients; to use numpy(), we need to detach() the tensor from the computation

graph first:

loss.detach().cpu().numpy()

102 | Chapter 1: A Simple Regression Problem

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

Saved successfully!

Ooh no, something went wrong!