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.

Figure 1.4 - Tensors are just higher-dimensional matrices - make sure to check this version out :-)

You can create tensors in PyTorch pretty much the same way you create arrays in

Numpy. Using tensor() you can create either a scalar or a tensor.

PyTorch’s tensors have equivalent functions to its Numpy counterparts, like

ones(), zeros(), rand(), randn(), and many more. In the example below, we create

one of each: scalar, vector, matrix, and tensor—or, saying it differently, one scalar

and three tensors.

scalar = torch.tensor(3.14159)

vector = torch.tensor([1, 2, 3])

matrix = torch.ones((2, 3), dtype=torch.float)

tensor = torch.randn((2, 3, 4), dtype=torch.float)

print(scalar)

print(vector)

print(matrix)

print(tensor)

72 | Chapter 1: A Simple Regression Problem

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

Saved successfully!

Ooh no, something went wrong!