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.

56 CHAPTER 3 It starts with a tensor

5 7 4

1 3 2

7 3 8

OFfSET = 1

SHAPE = (3, 3)

ROWS

(first INDEX)

STRIDE = (3, 1)

COLS

(second INDEX)

+1 NEXT COL (STRIDE[1]=1)

6 5 7 4 1 3 2 7 3 8

+3 NEXT ROW (STRIDE[0]=3)

Figure 3.5 Relationship between a tensor’s offset, size, and stride. Here the tensor is a view

of a larger storage, like one that might have been allocated when creating a larger tensor.

indicating how many elements across each dimension the tensor represents. The storage

offset is the index in the storage corresponding to the first element in the tensor.

The stride is the number of elements in the storage that need to be skipped over to

obtain the next element along each dimension.

3.8.1 Views of another tensor’s storage

We can get the second point in the tensor by providing the corresponding index:

# In[21]:

points = torch.tensor([[4.0, 1.0], [5.0, 3.0], [2.0, 1.0]])

second_point = points[1]

second_point.storage_offset()

# Out[21]:

2

# In[22]:

second_point.size()

# Out[22]:

torch.Size([2])

The resulting tensor has offset 2 in the storage (since we need to skip the first point,

which has two items), and the size is an instance of the Size class containing one

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

Saved successfully!

Ooh no, something went wrong!