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.

Tensors: Multidimensional arrays

45

# Out[11]:

tensor([[4., 1.],

[5., 3.],

[2., 1.]])

Here, we pass a list of lists to the constructor. We can ask the tensor about its shape:

# In[12]:

points.shape

# Out[12]:

torch.Size([3, 2])

This informs us about the size of the tensor along each dimension. We could also use

zeros or ones to initialize the tensor, providing the size as a tuple:

# In[13]:

points = torch.zeros(3, 2)

points

# Out[13]:

tensor([[0., 0.],

[0., 0.],

[0., 0.]])

Now we can access an individual element in the tensor using two indices:

# In[14]:

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

points

# Out[14]:

tensor([[4., 1.],

[5., 3.],

[2., 1.]])

# In[15]:

points[0, 1]

# Out[15]:

tensor(1.)

This returns the Y-coordinate of the zeroth point in our dataset. We can also access

the first element in the tensor as we did before to get the 2D coordinates of the first

point:

# In[16]:

points[0]

# Out[16]:

tensor([4., 1.])

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

Saved successfully!

Ooh no, something went wrong!