PyTorch learning: 1. What is PyTorch?

1. What is PyTorch

He is a scientific computing package based on Python. There are two types of target users

  • In order to use GPU instead of numpy
  • A deep learning research platform: providing maximum flexibility and speed

Start

Tensors

Tensors are similar to numpy ndarrays, the difference is that tensors can use GPU to speed up calculations

from __future__ import print_function
import torch

Construct an uninitialized 5*3 matrix:

x = torch.Tensor(5, 3)
print(x)
输出:
tensor([[ 0.0000e+00,  0.0000e+00,  1.3004e-42],
        [ 0.0000e+00,  7.0065e-45,  0.0000e+00],
        [-3.8593e+35,  7.8753e-43,  0.0000e+00],
        [ 0.0000e+00,  1.8368e-40,  0.0000e+00],
        [-3.8197e+35,  7.8753e-43,  0.0000e+00]])
Construct a zero matrix, using the long type
x = torch.zeros(5, 3, dtype=torch.long)
print(x)
输出:
tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]])

Construct a tensor directly from the data:

x = torch.tensor([5.5, 3])
print(x)
输出:

tensor([5.5000, 3.0000])

Or build a tensor from an existing tensor. These methods will reuse the attributes of the input tensor, for example, dtype, unless the user provides a new value

x = x.new_ones(5, 3, dtype=torch.double)      # new_* methods take in sizes
print(x)

x = torch.randn_like(x, dtype=torch.float)    # 覆盖类型!
print(x)                                      # result 的size相同

输出:

tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]], dtype=torch.float64)
tensor([[ 1.1701, -0.8342, -0.6769],
        [-1.3060,  0.3636,  0.6758],
        [ 1.9133,  0.3494,  1.1412],
        [ 0.9735, -0.9492, -0.3082],
        [ 0.9469, -0.6815, -1.3808]])

Get the size of the tensor

print(x.size())

输出:

torch.Size([5, 3])

note

torch.SizeIt is actually a tuple, so it supports all operations on tuples.

operating

There are multiple grammatical forms of operations on tensors. Let's take addition as an example to explain.

Grammar 1

y = torch.rand(5, 3)
print(x + y)

输出:

tensor([[ 1.7199, -0.1819, -0.1543],
        [-0.5413,  1.1591,  1.4098],
        [ 2.0421,  0.5578,  2.0645],
        [ 1.7301, -0.3236,  0.4616],
        [ 1.2805, -0.4026, -0.6916]])

Grammar Two

print(torch.add(x, y))

输出:

tensor([[ 1.7199, -0.1819, -0.1543],
        [-0.5413,  1.1591,  1.4098],
        [ 2.0421,  0.5578,  2.0645],
        [ 1.7301, -0.3236,  0.4616],
        [ 1.2805, -0.4026, -0.6916]])

Syntax 3:

Give an output tensor as a parameter

result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)

输出:

tensor([[ 1.7199, -0.1819, -0.1543],
        [-0.5413,  1.1591,  1.4098],
        [ 2.0421,  0.5578,  2.0645],
        [ 1.7301, -0.3236,  0.4616],
        [ 1.2805, -0.4026, -0.6916]])

 

Syntax 4:

In-place operation (in-place)

# 把x加到y上
y.add_(x)
print(y)

输出:

tensor([[ 1.7199, -0.1819, -0.1543],
        [-0.5413,  1.1591,  1.4098],
        [ 2.0421,  0.5578,  2.0645],
        [ 1.7301, -0.3236,  0.4616],
        [ 1.2805, -0.4026, -0.6916]])

note

Any operation that changes a tensor in-place has a _suffix. For example x.copy_(y), the  x.t_()operation will change x.

You can use all numpyindex operations.

You can use various fancy indexing functions similar to standard NumPy

print(x[:, 1])

输出:

tensor([-0.8342,  0.3636,  0.3494, -0.9492, -0.6815])

Resize: If you want to resize/reshape the tensor, you can use torch.view:

x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8)  # -1的意思是没有指定维度
print(x.size(), y.size(), z.size())

输出:

torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])

If you have a single element tensor, use .item()the value as a Python number

x = torch.randn(1)
print(x)
print(x.item())

输出:

tensor([0.3441])
0.34412217140197754

Read later

There are more than one hundred tensor operations described here , including transpose, indexing, mathematical operations, linear algebra, random numbers, etc.

numpy bridge

Converting a torch tensor to a numpy array or vice versa is easy.

Torch tensors and numpy arrays will share potential memory, and changing one will also change the other.

Convert Torch tensor to numpy array

a = torch.ones(5)
print(a)

输出:

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

b = a.numpy()
print(b)
print(type(b))

输出:

[ 1.  1.  1.  1.  1.]
<class 'numpy.ndarray'>

Through the following operations, we see how the value of the numpy array changes.

a.add_(1)
print(a)
print(b)

输出:

tensor([2., 2., 2., 2., 2.])
[ 2.  2.  2.  2.  2.]

Convert numpy array to torch tensor

See how changing the numpy array automatically changes the torch tensor.

import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print(a)
print(b)

输出:

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

All tensors on the CPU, except for character tensors, support conversion between numpy.

CUDA tensor

You can use the .to method to move the tensor to any device.

# let us run this cell only if CUDA is available
# We will use ``torch.device`` objects to move tensors in and out of GPU
if torch.cuda.is_available():
    device = torch.device("cuda")          # a CUDA device object
    y = torch.ones_like(x, device=device)  # directly create a tensor on GPU
    x = x.to(device)                       # or just use strings ``.to("cuda")``
    z = x + y
    print(z)
    print(z.to("cpu", torch.double))       # ``.to`` can also change dtype together!

输出:

tensor([1.3441], device='cuda:0')
tensor([1.3441], dtype=torch.float64)

 

Guess you like

Origin blog.csdn.net/qq_40716944/article/details/107789158