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.

Output

tensor(3.1416)

tensor([1, 2, 3])

tensor([[1., 1., 1.],

[1., 1., 1.]])

tensor([[[-1.0658, -0.5675, -1.2903, -0.1136],

[ 1.0344, 2.1910, 0.7926, -0.7065],

[ 0.4552, -0.6728, 1.8786, -0.3248]],

[[-0.7738, 1.3831, 1.4861, -0.7254],

[ 0.1989, -1.0139, 1.5881, -1.2295],

[-0.5338, -0.5548, 1.5385, -1.2971]]])

You can get the shape of a tensor using its size() method or its shape attribute.

print(tensor.size(), tensor.shape)

Output

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

All tensors have shapes, but scalars have "empty" shapes, since they are

dimensionless (or zero dimensions, if you prefer):

print(scalar.size(), scalar.shape)

Output

torch.Size([]) torch.Size([])

You can also reshape a tensor using its view() (preferred) or reshape() methods.

PyTorch | 73

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

Saved successfully!

Ooh no, something went wrong!