資源來源於:莫煩Pytorch
實驗的環境:
- macOs Catalina 10.15
- Python 3.6.9
PyTorch 簡介
1.1 科普: 人工神經網絡 VS 生物神經網絡
1.2 什麼是神經網絡 (Neural Network)
1.3 神經網絡 梯度下降
1.4 科普: 神經網絡的黑盒不黑
1.5 Why Pytorch?
PyTorch 是 Torch 在 Python 上的衍生. 因爲 PyTorch 是一個使用 Torch 語言的神經網絡庫, Torch 很好用, 但是 Lua 又不是特別流行, 所以開發團隊將 Lua 的 Torch 移植到了更流行的語言 Python 上.
1.6 Pytorch 安裝
PyTorch 神經網絡基礎
2.1 Torch 或 Numpy
Torch 自稱爲神經網絡界的 Numpy, 因爲他能將 torch 產生的 tensor 放在 GPU 中加速運算 (前提是你有合適的 GPU), 就像 Numpy 會把 array 放在 CPU 中加速運算. 所以神經網絡的話, 當然是用 Torch 的 tensor 形式數據最好咯. 就像 Tensorflow 當中的 tensor 一樣.
torch 做的和 numpy 能很好的兼容.
- 轉換 numpy array 和 torch tensor
np_data = np.arange(6).reshape((2, 3))
torch_data = torch.from_numpy(np_data)
tensor2array = torch_data.numpy()
print(
'\nnumpy array:', np_data,
'\ntorch tensor:', torch_data,
'\ntensor to array:', tensor2array,
)
numpy array: [[0 1 2]
[3 4 5]]
torch tensor: tensor([[0, 1, 2],
[3, 4, 5]])
tensor to array: [[0 1 2]
[3 4 5]]
- 數學運算
# abs 絕對值計算
data = [-1, -2, 1, 2]
tensor = torch.FloatTensor(data) # 轉換成32位浮點 tensor
print(
'\nabs',
'\nnumpy: ', np.abs(data), # [1 2 1 2]
'\ntorch: ', torch.abs(tensor) # [1 2 1 2]
)
# sin 三角函數 sin
print(
'\nsin',
'\nnumpy: ', np.sin(data), # [-0.84147098 -0.90929743 0.84147098 0.90929743]
'\ntorch: ', torch.sin(tensor) # [-0.8415 -0.9093 0.8415 0.9093]
)
# mean 均值
print(
'\nmean',
'\nnumpy: ', np.mean(data), # 0.0
'\ntorch: ', torch.mean(tensor) # 0.0
)
abs
numpy: [1 2 1 2]
torch: tensor([1., 2., 1., 2.])
sin
numpy: [-0.84147098 -0.90929743 0.84147098 0.90929743]
torch: tensor([-0.8415, -0.9093, 0.8415, 0.9093])
mean
numpy: 0.0
torch: tensor(0.)
- 矩陣運算
import torch
import numpy as np
# np_data = np.arange(6).reshape((2, 3))
# torch_data = torch.from_numpy(np_data)
# tensor2array = torch_data.numpy()
# print(
# '\nnumpy array:', np_data, # [[0 1 2], [3 4 5]]
# '\ntorch tensor:', torch_data, # 0 1 2 \n 3 4 5 [torch.LongTensor of size 2x3]
# '\ntensor to array:', tensor2array, # [[0 1 2], [3 4 5]]
# )
# # abs 絕對值計算
# data = [-1, -2, 1, 2]
# tensor = torch.FloatTensor(data) # 轉換成32位浮點 tensor
# print(
# '\nabs',
# '\nnumpy: ', np.abs(data), # [1 2 1 2]
# '\ntorch: ', torch.abs(tensor) # [1 2 1 2]
# )
#
# # sin 三角函數 sin
# print(
# '\nsin',
# '\nnumpy: ', np.sin(data), # [-0.84147098 -0.90929743 0.84147098 0.90929743]
# '\ntorch: ', torch.sin(tensor) # [-0.8415 -0.9093 0.8415 0.9093]
# )
#
# # mean 均值
# print(
# '\nmean',
# '\nnumpy: ', np.mean(data), # 0.0
# '\ntorch: ', torch.mean(tensor) # 0.0
# )
# matrix multiplication 矩陣點乘
data = [[1,2], [3,4]]
tensor = torch.FloatTensor(data) # 轉換成32位浮點 tensor
# correct method
print(
'\nmatrix multiplication (matmul)',
'\nnumpy: ', np.matmul(data, data), # [[7, 10], [15, 22]]
'\ntorch: ', torch.mm(tensor, tensor) # [[7, 10], [15, 22]]
)
data2 = [1, 2, 3, 4]
tensor2 = torch.FloatTensor(data2) # 轉換成32位浮點 tensor
print(
'\nmatrix multiplication (dot)',
'\nnumpy: ', np.dot(data, data), # [[7, 10], [15, 22]] 在numpy 中可行
#tensor.dot() 有了新的改變, 它只能針對於一維的數組
'\ntorch: ', torch.dot(tensor2, tensor2) # torch 會轉換成 [1,2,3,4].dot([1,2,3,4) = 30.0
)
numpy: [[ 7 10]
[15 22]]
torch: tensor([[ 7., 10.],
[15., 22.]])
[1, 2, 3, 4]
tensor([1., 2., 3., 4.])
matrix multiplication (dot)
numpy: [[ 7 10]
[15 22]]
torch: tensor(30.)
2.2 變量 (Variable)
import torch
from torch.autograd import Variable # torch 中 Variable 模塊
# 先生雞蛋
tensor = torch.FloatTensor([[1,2],[3,4]])
# 把雞蛋放到籃子裏, requires_grad是參不參與誤差反向傳播, 要不要計算梯度
variable = Variable(tensor, requires_grad=True)
print(tensor)
print(variable)
t_out = torch.mean(tensor*tensor) # x^2
v_out = torch.mean(variable*variable) # x^2
print(t_out)
print(v_out) # 7.5
v_out.backward() # 模擬 v_out 的誤差反向傳遞
# 下面兩步看不懂沒關係, 只要知道 Variable 是計算圖的一部分, 可以用來傳遞誤差就好.
# v_out = 1/4 * sum(variable*variable) 這是計算圖中的 v_out 計算步驟
# 針對於 v_out 的梯度就是, d(v_out)/d(variable) = 1/4*2*variable = variable/2
print(variable.grad) # 初始 Variable 的梯度
print(variable) # Variable 形式
print(variable.data) # tensor 形式
print(variable.data.numpy()) # numpy 形式
tensor([[1., 2.],
[3., 4.]])
tensor([[1., 2.],
[3., 4.]], requires_grad=True)
tensor(7.5000)
tensor(7.5000, grad_fn=<MeanBackward0>)
tensor([[0.5000, 1.0000],
[1.5000, 2.0000]])
tensor([[1., 2.],
[3., 4.]], requires_grad=True)
tensor([[1., 2.],
[3., 4.]])
[[1. 2.]
[3. 4.]]