# Python:PyTorch 初识 (七十五)

## 神经网络

\begin{align} y &= f(w_1 x_1 + w_2 x_2 + b) \ y &= f\left(\sum_i w_i x_i \right) \end{align}

$$h = \begin{bmatrix} x_1 \, x_2 \cdots x_n \end{bmatrix} \cdot \begin{bmatrix} w_1 \ w_2 \ \vdots \ w_n \end{bmatrix}$$

### 堆叠起来！

$$\vec{h} = [h_1 \, h_2] = \begin{bmatrix} x_1 \, x_2 \cdots \, xn \end{bmatrix} \cdot \begin{bmatrix} w {11} & w{12} \ w {21} &w{22} \ \vdots &\vdots \ w {n1} &w_{n2} \end{bmatrix}$$

$$y = f_2 ! \left(\, f_1 ! \left(\vec{x} \, \mathbf{W_1}\right) \mathbf{W_2} \right)$$

## 张量

%matplotlib inline
%config InlineBackend.figure_format = 'retina'

import numpy as np
import torch

import helper

x = torch.rand(3, 2)
x
tensor([[ 0.2219,  0.6481],
[ 0.9546,  0.5206],
[ 0.2628,  0.6034]])
y = torch.ones(x.size())
y
tensor([[ 1.,  1.],
[ 1.,  1.],
[ 1.,  1.]])
z = x + y
z
tensor([[ 1.2219,  1.6481],
[ 1.9546,  1.5206],
[ 1.2628,  1.6034]])

z[0]
tensor([ 1.2219,  1.6481])
z[:, 1:]
tensor([[ 1.6481],
[ 1.5206],
[ 1.6034]])

# Return a new tensor z + 1
z.add(1)
tensor([[ 2.2219,  2.6481],
[ 2.9546,  2.5206],
[ 2.2628,  2.6034]])
# z tensor is unchanged
z
tensor([[ 1.2219,  1.6481],
[ 1.9546,  1.5206],
[ 1.2628,  1.6034]])
# Add 1 and update z tensor in-place
z.add_(1)
tensor([[ 2.2219,  2.6481],
[ 2.9546,  2.5206],
[ 2.2628,  2.6034]])
# z has been updated
z
tensor([[ 2.2219,  2.6481],
[ 2.9546,  2.5206],
[ 2.2628,  2.6034]])

### 改变形状

z.size()
torch.Size([3, 2])
z.resize_(2, 3)
tensor([[ 2.2219,  2.6481,  2.9546],
[ 2.5206,  2.2628,  2.6034]])
z
tensor([[ 2.2219,  2.6481,  2.9546],
[ 2.5206,  2.2628,  2.6034]])

## 在 Numpy 与 Torch 之间转换

a = np.random.rand(4,3)
a
array([[ 0.57399177,  0.8398885 ,  0.99316486],
[ 0.71728533,  0.116773  ,  0.77742645],
[ 0.16646228,  0.34508491,  0.27752307],
[ 0.16144883,  0.26954114,  0.74938328]])
b = torch.from_numpy(a)
b
tensor([[ 0.5740,  0.8399,  0.9932],
[ 0.7173,  0.1168,  0.7774],
[ 0.1665,  0.3451,  0.2775],
[ 0.1614,  0.2695,  0.7494]], dtype=torch.float64)
b.numpy()
array([[ 0.57399177,  0.8398885 ,  0.99316486],
[ 0.71728533,  0.116773  ,  0.77742645],
[ 0.16646228,  0.34508491,  0.27752307],
[ 0.16144883,  0.26954114,  0.74938328]])

# Multiply PyTorch Tensor by 2, in place
b.mul_(2)
tensor([[ 1.1480,  1.6798,  1.9863],
[ 1.4346,  0.2335,  1.5549],
[ 0.3329,  0.6902,  0.5550],
[ 0.3229,  0.5391,  1.4988]], dtype=torch.float64)
# Numpy array matches new values from Tensor
a
array([[ 1.14798353,  1.67977701,  1.98632973],
[ 1.43457067,  0.233546  ,  1.55485289],
[ 0.33292455,  0.69016982,  0.55504614],
[ 0.32289765,  0.53908228,  1.49876657]])