# pyTorch quick start

Posted by slashpine on Thu, 13 Feb 2020 08:02:29 +0100

Preface:
The epidemic is too serious to start school. It may only start in March or even April. This winter vacation is really long. Use this time to learn something at home. Self discipline can let me make full use of my time; self discipline can let me supplement my shortcomings; self discipline can let me study the theoretical basis in depth; self discipline can let me further improve the code ability.

This year, I hope that I can be more self disciplined and step by step close to qualified Algorithm Engineers.

In recent days, I'm very interested in the book "hands on deep learning pyTorch edition", but I'm not familiar with pyTorch. So let's quickly learn. As the introduction to pyTorch, I attach the official link of pyTorch https://pyTorch.org/tutorials/starter/blitz/sensor_tutorial.html#sphx-glr-begin-blitz-sensor-tutorial-py

## 1. pyTorch

pyTorch is similar to numpy, while Tensors in pyTorch are similar to darray in numpy.

## 2. Basic operation

(1) Declares an uninitialized matrix, but does not contain a known value 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) Constructing a random initialization matrix

```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) Construct a matrix whose elements are all zero and whose type is 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) Construct tensors directly from data

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

(5) Create tensors based on existing tensors (unless the user provides a new value, these methods will reuse the properties of existing tensors, 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! Same as Xsize above
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) Vector addition, subtraction, multiplication and division

```y = torch.rand(5, 3)
#Law 1
print(x + y)

#Law two

#Law three
result = torch.empty(5, 3)
print(result)

#Law four
print(y)
```

The result is the same after operation

```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) Index that can be similar to NumPy

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

(8) Resize
To adjust the tensor size, you can use torch.view:

```x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8)  # -1 means to infer from other dimensions
print(x.size(), y.size(), z.size())
```
```torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
```

(9) If there is only one element's tensor, use. item() to get the value

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

(10) The transformation of Torch tensor and NumPy array

```# torch to numpy
a = torch.ones(5)
print(a)
b = a.numpy()
print(b)

tensor([1., 1., 1., 1., 1.])
[1. 1. 1. 1. 1.]
```
```# numpy to torch
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)