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

• Step 1: compute model’s predictions• Step 2: compute the loss• Step 3: compute the gradients• Step 4: update the parametersThis sequence is repeated over and over until the number of epochs is reached.The corresponding code for this part also comes from Notebook Cell 1.10, lines 17-36."What happened to the random initialization step?"Since we are not manually creating parameters anymore, the initialization ishandled inside each layer during model creation.Define - Model Training V01 %%writefile model_training/v0.py23 # Defines number of epochs4 n_epochs = 100056 for epoch in range(n_epochs):7 # Sets model to TRAIN mode8 model.train()910 # Step 1 - Computes model's predicted output - forward pass11 yhat = model(x_train_tensor)1213 # Step 2 - Computes the loss14 loss = loss_fn(yhat, y_train_tensor)1516 # Step 3 - Computes gradients for both "b" and "w" parameters17 loss.backward()1819 # Step 4 - Updates parameters using gradients and20 # the learning rate21 optimizer.step()22 optimizer.zero_grad()Putting It All Together | 119

Run - Model Training V0%run -i model_training/v0.pyOne last check to make sure we have everything right:print(model.state_dict())OutputOrderedDict([('0.weight', tensor([[1.9690]], device='cuda:0')),('0.bias', tensor([1.0235], device='cuda:0'))])Now, take a close, hard look at the code inside the training loop.Ready? I have a question for you then…"Would this code change if we were using a different optimizer, orloss, or even model?"Before I give you the answer, let me address something else that may be on yourmind: "What is the point of all this?"Well, in the next chapter we’ll get fancier, using more of PyTorch’s classes (likeDataset and DataLoader) to further refine our data preparation step, and we’ll alsotry to reduce boilerplate code to a minimum. So, splitting our code into threelogical parts will allow us to better handle these improvements.And here is the answer: NO, the code inside the loop would not change.I guess you figured out which boilerplate I was referring to, right?120 | Chapter 1: A Simple Regression Problem

• Step 1: compute model’s predictions

• Step 2: compute the loss

• Step 3: compute the gradients

• Step 4: update the parameters

This sequence is repeated over and over until the number of epochs is reached.

The corresponding code for this part also comes from Notebook Cell 1.10, lines 17-

36.

"What happened to the random initialization step?"

Since we are not manually creating parameters anymore, the initialization is

handled inside each layer during model creation.

Define - Model Training V0

1 %%writefile model_training/v0.py

2

3 # Defines number of epochs

4 n_epochs = 1000

5

6 for epoch in range(n_epochs):

7 # Sets model to TRAIN mode

8 model.train()

9

10 # Step 1 - Computes model's predicted output - forward pass

11 yhat = model(x_train_tensor)

12

13 # Step 2 - Computes the loss

14 loss = loss_fn(yhat, y_train_tensor)

15

16 # Step 3 - Computes gradients for both "b" and "w" parameters

17 loss.backward()

18

19 # Step 4 - Updates parameters using gradients and

20 # the learning rate

21 optimizer.step()

22 optimizer.zero_grad()

Putting It All Together | 119

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

Saved successfully!

Ooh no, something went wrong!