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.

# THIRD

# We can create regular tensors and send them to

# the device (as we did with our data)

torch.manual_seed(42)

b = torch.randn(1, dtype=torch.float).to(device)

w = torch.randn(1, dtype=torch.float).to(device)

# and THEN set them as requiring gradients...

b.requires_grad_()

w.requires_grad_()

print(b, w)

Output

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

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

This approach worked fine; we managed to end up with gradient-requiring GPU

tensors for our parameters b and w. It seems a lot of work, though… Can we do

better still?

Yes, we can do better: We can assign tensors to a device at the moment of their

creation.

Notebook Cell 1.4 - Actually creating variables for the coefficients

# FINAL

# We can specify the device at the moment of creation

# RECOMMENDED!

# Step 0 - Initializes parameters "b" and "w" randomly

torch.manual_seed(42)

b = torch.randn(1, requires_grad=True, \

dtype=torch.float, device=device)

w = torch.randn(1, requires_grad=True, \

dtype=torch.float, device=device)

print(b, w)

84 | Chapter 1: A Simple Regression Problem

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

Saved successfully!

Ooh no, something went wrong!