「论文工具」莫烦Pytorch(一)

资源来源于:莫烦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.]]

2.3 什么是激励函数 (Activation Function)

2.4 激励函数 (Activation)

建造第一个神经网络

3.1 关系拟合 (回归)

3.2 区分类型 (分类)

3.3 快速搭建法

3.4 保存提取

3.5 批训练

3.6 加速神经网络训练 (Speed Up Training)

3.7 Optimizer 优化器

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章