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.

Apart from returning the loss value, the inner perform_train_step_fn() function

below is exactly the same as the code inside the loop in Model Training V0. The

code should look like this:

Helper Function #1

1 def make_train_step_fn(model, loss_fn, optimizer):

2 # Builds function that performs a step in the train loop

3 def perform_train_step_fn(x, y):

4 # Sets model to TRAIN mode

5 model.train()

6

7 # Step 1 - Computes model's predictions - forward pass

8 yhat = model(x)

9 # Step 2 - Computes the loss

10 loss = loss_fn(yhat, y)

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

12 loss.backward()

13 # Step 4 - Updates parameters using gradients and

14 # the learning rate

15 optimizer.step()

16 optimizer.zero_grad()

17

18 # Returns the loss

19 return loss.item()

20

21 # Returns the function that will be called inside the

22 # train loop

23 return perform_train_step_fn

Then we need to update our model configuration code (adding line 20 in the next

snippet) to call this higher-order function to build a train_step_fn() function. But

we need to run a data preparation script first.

Run - Data Preparation V0

%run -i data_preparation/v0.py

Rethinking the Training Loop | 131

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

Saved successfully!

Ooh no, something went wrong!