pyTorch Quick Start

Introduction:
the epidemic is too serious to postpone school, probably in March or even April before school, this winter really long, use this time to learn a lot at home. Self-discipline, allow me to make full use of time; self-discipline, allow me to add his own shortcomings; self-discipline, let me solid theoretical foundation depth learning; self-discipline will allow me to further enhance the ability of the code.

This year, they hope to be able to some self-discipline, a step closer algorithm qualified engineer.

Official Links These days, my "hands-on science learning pyTorch depth version of" very interested in this book, but are not familiar with pyTorch, so quick to learn about, as the entry of pyTorch, attach pytorch of https://pytorch.org/ tutorials / beginner / blitz / tensor_tutorial.html # sphx-glr-beginner-blitz-tensor-tutorial-py

1. pyTorch brief

pyTorch similar numpy, and the pyTorch of NumPy ndarray Tensors and similar.

2. Basic Operations

(1) to declare an uninitialized matrix, but does not contain a known value determined before use.

x = torch.empty(5, 3)
print(x)
tensor([[2.3576e-36, 0.0000e+00, 0.0000e+00],
        [0.0000e+00, 0.0000e+00, 0.0000e+00],
        [0.0000e+00, 0.0000e+00, 2.8026e-45],
        [0.0000e+00, 1.1210e-44, 0.0000e+00],
        [1.4013e-45, 0.0000e+00, 0.0000e+00]])

(2) a random initialization matrix structure

x = torch.rand(5, 3)
print(x)
tensor([[0.2031, 0.8195, 0.2181],
        [0.4732, 0.4602, 0.8097],
        [0.6037, 0.4483, 0.2570],
        [0.1677, 0.1944, 0.7259],
        [0.3056, 0.8363, 0.1282]])

(3) a structure element of all zeros, the matrix type long

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]])

(4) directly to the data structure tensors

x = torch.tensor([5.5, 3])
print(x)
tensor([5.5000, 3.0000])

(5) to create a tensor based on existing tensor (unless the user provides the new value, otherwise these methods will reuse the existing tensor properties, such as dtype)

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

x = torch.randn_like(x, dtype=torch.float)    # override dtype!与上面的Xsize相同
print(x)  
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]], dtype=torch.float64)
        
tensor([[-0.0929,  0.9791,  0.1481],
        [-1.7100,  0.4316, -0.4209],
        [-0.6479,  1.5173, -0.3646],
        [-1.3288,  2.6447, -0.6539],
        [-0.0300,  1.8167,  0.8633]])
print(x.size())
torch.Size([5, 3])

(6) computing the vector arithmetic

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

#法二
print(torch.add(x, y))

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

#法四
y.add_(x)
print(y)

After the operation the same result

tensor([[ 0.4184,  0.6502,  0.3735],
        [ 2.0532,  0.1959,  0.6630],
        [ 1.6664,  0.4967,  0.7961],
        [ 0.8452,  1.1509,  0.6831],
        [ 1.0740,  0.8284, -0.2371]])
tensor([[ 0.4184,  0.6502,  0.3735],
        [ 2.0532,  0.1959,  0.6630],
        [ 1.6664,  0.4967,  0.7961],
        [ 0.8452,  1.1509,  0.6831],
        [ 1.0740,  0.8284, -0.2371]])
tensor([[ 0.4184,  0.6502,  0.3735],
        [ 2.0532,  0.1959,  0.6630],
        [ 1.6664,  0.4967,  0.7961],
        [ 0.8452,  1.1509,  0.6831],
        [ 1.0740,  0.8284, -0.2371]])
tensor([[ 0.4184,  0.6502,  0.3735],
        [ 2.0532,  0.1959,  0.6630],
        [ 1.6664,  0.4967,  0.7961],
        [ 0.8452,  1.1509,  0.6831],
        [ 1.0740,  0.8284, -0.2371]])

(7) may be similar to the index of NumPy

print(x[:, 1])
tensor([ 0.5996, -0.0382,  0.4017,  0.5467,  0.2213])

(8) adjust the size of the
adjustment tensor size, may be used 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])

(9) if only one element of the tensor, may be used .Item () Gets the value

x = torch.randn(1)
print(x)
print(x.item())
tensor([-0.4073])
-0.40732377767562866

(10) Conversion Torch tensor NumPy array

# torch转为numpy
a = torch.ones(5)
print(a)
b = a.numpy()
print(b)

tensor([1., 1., 1., 1., 1.])
[1. 1. 1. 1. 1.]
# numpy转为torch
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)
Published 77 original articles · won praise 9 · views 6732

Guess you like

Origin blog.csdn.net/u013075024/article/details/104293447