22.02.2024 Views

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Step 3

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

b_grad = 2 * error.mean()

w_grad = 2 * (x_train * error).mean()

print(b_grad, w_grad)

Output

-3.044811379650508 -1.8337537171510832

Step 4 - Update the Parameters

In the final step, we use the gradients to update the parameters. Since we are

trying to minimize our losses, we reverse the sign of the gradient for the update.

There is still another (hyper-)parameter to consider: the learning rate, denoted by

the Greek letter eta (that looks like the letter n), which is the multiplicative factor

that we need to apply to the gradient for the parameter update.

"How do you choose a learning rate?"

That is a topic on its own and beyond the scope of this section as

well. We’ll get back to it later on.

In our example, let’s start with a value of 0.1 for the learning rate (which is a

relatively high value, as far as learning rates are concerned!).

Step 4

# Sets learning rate - this is "eta" ~ the "n"-like Greek letter

lr = 0.1

print(b, w)

# Step 4 - Updates parameters using gradients and

# the learning rate

b = b - lr * b_grad

w = w - lr * w_grad

print(b, w)

66 | Chapter 1: A Simple Regression Problem

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

Saved successfully!

Ooh no, something went wrong!