20.03.2021 Views

Deep-Learning-with-PyTorch

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

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

52 CHAPTER 3 It starts with a tensor

Under the hood, to checks whether the conversion is necessary and, if so, does it. The

dtype-named casting methods like float are shorthands for to, but the to method

can take additional arguments that we’ll discuss in section 3.9.

When mixing input types in operations, the inputs are converted to the larger type

automatically. Thus, if we want 32-bit computation, we need to make sure all our

inputs are (at most) 32-bit:

# In[51]:

points_64 = torch.rand(5, dtype=torch.double)

points_short = points_64.to(torch.short)

points_64 * points_short # works from PyTorch 1.3 onwards

# Out[51]:

tensor([0., 0., 0., 0., 0.], dtype=torch.float64)

3.6 The tensor API

At this point, we know what PyTorch tensors are and how they work under the hood.

Before we wrap up, it is worth taking a look at the tensor operations that PyTorch

offers. It would be of little use to list them all here. Instead, we’re going to get a general

feel for the API and establish a few directions on where to find things in the

online documentation at http://pytorch.org/docs.

First, the vast majority of operations on and between tensors are available in the

torch module and can also be called as methods of a tensor object. For instance, the

transpose function we encountered earlier can be used from the torch module

# In[71]:

a = torch.ones(3, 2)

a_t = torch.transpose(a, 0, 1)

a.shape, a_t.shape

# Out[71]:

(torch.Size([3, 2]), torch.Size([2, 3]))

or as a method of the a tensor:

# In[72]:

a = torch.ones(3, 2)

a_t = a.transpose(0, 1)

a.shape, a_t.shape

# Out[72]:

(torch.Size([3, 2]), torch.Size([2, 3]))

rand initializes the tensor elements to

random numbers between 0 and 1.

There is no difference between the two forms; they can be used interchangeably.

We mentioned the online docs earlier (http://pytorch.org/docs). They are

exhaustive and well organized, with the tensor operations divided into groups:

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

Saved successfully!

Ooh no, something went wrong!