PyTorch是一个比较新的模块,比Tensorflow能更好地诠释神经网络的性能。
不同: Tensorflow是静态过程,PyTorch是动态过程
import torch
import numpy as np
np_data = np.arange(6).reshape((2, 3)) # 生成一个numpy数组
torch_data = torch.from_numpy(np_data) # numpy转换成torch
tensor2array = torch_data.numpy() # torch转换称numpy
print(
'\nnumpy', np_data,
'\notrch', torch_data,
'\ntensor2array', tensor2array
)
numpy [[0 1 2]
[3 4 5]]
otrch tensor([[0, 1, 2],
[3, 4, 5]], dtype=torch.int32)
tensor2array [[0 1 2]
[3 4 5]]
# ads绝对值
data = [-1, -2, 1, 2]
tensor = torch.FloatTensor(data) # 转换成23bitflotpoints
# http://pytorch.org/docs/torch.html#math-operations
# 包括tensor的一些运算
print(
'\nabs',
'\nnumpy: ', np.abs(data), # numpy形式
'\ntorch: ', torch.abs(tensor) #torch形式
)
# 其他计算形式类似
print(
'\nsin',
'\nnumpy: ', np.sin(data), # numpy形式
'\ntorch: ', torch.sin(tensor) #torch形式
)
# 矩阵形式运算
data = [[1, 2], [3, 4]]
tensor = torch.FloatTensor(data)
data = np.array(data)
print(
'\nnumpy:', np.matmul(data, data), # 或np.dot(data)
'\ntorch:', torch.mm(tensor, tensor) # 不能使用torch.dot(data)
)
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])
numpy: [[ 7 10]
[15 22]]
torch: tensor([[ 7., 10.],
[15., 22.]])
import torch
from torch.autograd import Variable
tensor = torch.FloatTensor([[1, 2], [3, 4]]) # 不能反向传播
variable = Variable(tensor, requires_grad=True) # requires_grad=True可以反向传播
print(tensor)
print(variable) # Variable containing
t_out = torch.mean(tensor*tensor)
v_out = torch.mean(variable*variable)
print('t_out =', t_out)
print('v_out =', v_out)
v_out.backward()
# v_out = 1/4*sum(variable*variable)
# d(v_out)/d(var) = 1/4*2*variable = variable/2
print(variable.grad) # 输出variable的梯度
print(variable)
print(variable.data) # variable转换成tensor
print(variable.data.numpy()) # variable转换成tensor,tensor转换成numpy的值
tensor([[1., 2.],
[3., 4.]])
tensor([[1., 2.],
[3., 4.]], requires_grad=True)
t_out = tensor(7.5000)
v_out = tensor(7.5000, grad_fn=)
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.]]
# 激励函数Activation function
# 卷积神经网络:推荐relu
# 循环神经网络:推荐relu or tanh
import torch
import torch.nn.functional as F
from torch.autograd import Variable
import matplotlib.pyplot as plt
# fake data
x = torch.linspace(-5, 5, 200) # x data (tensor), shape=(100, 1)
x = Variable(x) # 将x装到Variable这个篮子里面,x变成variable
x_np = x.data.numpy() # torch的数据格式不能被matplotlib识别,转换成numpy
y_relu = F.relu(x).data.numpy()
y_sigmoid = F.sigmoid(x).data.numpy()
y_tanh = F.tanh(x).data.numpy()
y_softplus = F.softplus(x).data.numpy()
# y_softmax = F.relu(x).data.numpy()
plt.figure(1, figsize=(8, 6))
plt.subplot(221)
plt.plot(x_np, y_relu, c='red', label='relu')
plt.ylim((-1, 5))
plt.legend(loc='best')
plt.subplot(222)
plt.plot(x_np, y_sigmoid, c='red', label='sigmoid')
plt.ylim((-0.2, 1.2))
plt.legend(loc='best')
plt.subplot(223)
plt.plot(x_np, y_tanh, c='red', label='tanh')
plt.ylim((-1.2, 1.2))
plt.legend(loc='best')
plt.subplot(224)
plt.plot(x_np, y_softplus, c='red', label='softplus')
plt.ylim((-0.2, 6))
plt.legend(loc='upper left')
# 回归
import torch
import torch.nn.functional as F # 激励函数都在这
import matplotlib.pyplot as plt
from torch.autograd import Variable
# 生成一些随机点
x = torch.unsqueeze(torch.linspace(-1, 1, 100), dim=1) # 把一维的数据变成二维的数据
y = x.pow(2) + 0.2 * torch.rand(x.size()) # x的平方,加上一点噪声的影响。
# torch.rand(x.size())是随机生(0,1)的随机数,形式和x相同
x, y = Variable(x), Variable(y)
class Net(torch.nn.Module): # 继承 torch 的 Module
def __init__(self, n_feature, n_hidden, n_output):
super(Net, self).__init__() # 继承 __init__ 功能
# 定义每层用什么样的形式
self.hidden = torch.nn.Linear(n_feature, n_hidden) # 隐藏层线性输出
self.predict = torch.nn.Linear(n_hidden, n_output) # 输出层线性输出
def forward(self, x): # 这同时也是 Module 中的 forward 功能
# 正向传播输入值, 神经网络分析出输出值
x = F.relu(self.hidden(x)) # 激励函数(隐藏层的线性值)
x = self.predict(x) # 输出值
return x
net = Net(n_feature=1, n_hidden=10, n_output=1)
"""
print(net) # net 的结构
Net (
(hidden): Linear (1 -> 10)
(predict): Linear (10 -> 1)
)
"""
'''
# 可视化
plt.plot()
plt.show()
'''
# optimizer 是训练的工具
optimizer = torch.optim.SGD(net.parameters(), lr=0.2) # 传入 net 的所有参数, 学习率
loss_func = torch.nn.MSELoss() # 预测值和真实值的误差计算公式 (均方差)
for t in range(100):
prediction = net(x) # 喂给 net 训练数据 x, 输出预测值
loss = loss_func(prediction, y) # 计算两者的误差
optimizer.zero_grad() # 清空上一步的残余更新参数值
loss.backward() # 误差反向传播, 计算参数更新值
optimizer.step() # 将参数更新值施加到 net 的 parameters 上
"""
# 接着上面来
if t % 5 == 0:
# plot and show learning process
plt.cla()
plt.scatter(x.data.numpy(), y.data.numpy()) # 打印散点图
plt.plot(x.data.numpy(), prediction.data.numpy(), 'r-', lw=5)
plt.text(0.5, 0, 'Loss=%.4f' % loss.data.numpy(), fontdict={'size': 20, 'color': 'red'})
plt.pause(0.1)
plt.ioff()
plt.show()
"""
# 分类
import torch
import torch.nn.functional as F # 激励函数都在这
import matplotlib.pyplot as plt
from torch.autograd import Variable
# data
n_data = torch.ones(100, 2) # 基数
x0 = torch.normal(2*n_data, 1) # class0 data (tensor), shape=(100, 2)
y0 = torch.zeros(100) # class0 lable (tensor), shape=(100, 1)
x1 = torch.normal(-2*n_data, 1) # class1 data (tensor), shape=(100, 2)
y1 = torch.ones(100) # class1 lable (tensor), shape=(100, 1)
# 注意 x, y 数据的数据形式是一定要像下面一样 (torch.cat 是在合并数据)
x = torch.cat((x0, x1), 0).type(torch.FloatTensor) # 两类数据合并 FloatTensor = 32-bit floating
y = torch.cat((y0, y1), ).type(torch.LongTensor) # 两类标签合并 LongTensor = 64-bit integer
x, y = Variable(x), Variable(y) # 放入篮子
class Net(torch.nn.Module): # 继承 torch 的 Module
def __init__(self, n_feature, n_hidden, n_output):
super(Net, self).__init__() # 继承 __init__ 功能
# 定义每层用什么样的形式
self.hidden = torch.nn.Linear(n_feature, n_hidden) # 隐藏层线性输出
self.predict = torch.nn.Linear(n_hidden, n_output) # 输出层线性输出
def forward(self, x): # 这同时也是 Module 中的 forward 功能
x = F.relu(self.hidden(x))
x = self.predict(x)
return x
net = Net(n_feature=2, n_hidden=10, n_output=2)
"""
print(net) # net 的结构
Net (
(hidden): Linear (2 -> 10)
(predict): Linear (10 -> 2)
)
"""
# optimizer 是训练的工具
optimizer = torch.optim.SGD(net.parameters(), lr=0.03) # 传入 net 的所有参数, 学习率
loss_func = torch.nn.CrossEntropyLoss() # 预测值和真实值的误差计算公式
for t in range(50):
out = net(x) # 喂给 net 训练数据 x, 输出预测值
loss = loss_func(out, y) # 计算两者的误差
optimizer.zero_grad() # 清空上一步的残余更新参数值
loss.backward() # 误差反向传播, 计算参数更新值
optimizer.step() # 将参数更新值施加到 net 的 parameters 上
# 接着上面来
if t % 2 == 0:
plt.cla()
prediction = torch.max(F.softmax(out), 1)[1]
pred_y = prediction.data.numpy().squeeze()
target_y = y.data.numpy()
plt.scatter(x.data.numpy()[:, 0], x.data.numpy()[:, 1], c=pred_y, s=100, lw=0, cmap='RdYlGn')
accuracy = sum(pred_y == target_y)/200. # 预测中有多少和真实值一样
plt.text(1.5, -4, 'Accuracy=%.2f' % accuracy, fontdict={
'size': 20, 'color': 'red'})
plt.pause(0.1)
plt.ioff() # 停止画图
plt.show()
C:\Users\lenovo\Anaconda3\envs\pytorch\lib\site-packages\ipykernel_launcher.py:62: UserWarning: Implicit dimension choice for softmax has been deprecated. Change the call to include dim=X as an argument.
### 快速搭建法
# 分类
import torch
import torch.nn.functional as F # 激励函数都在这
import matplotlib.pyplot as plt
from torch.autograd import Variable
# data
n_data = torch.ones(100, 2) # 基数
x0 = torch.normal(2*n_data, 1) # class0 data (tensor), shape=(100, 2)
y0 = torch.zeros(100) # class0 lable (tensor), shape=(100, 1)
x1 = torch.normal(-2*n_data, 1) # class1 data (tensor), shape=(100, 2)
y1 = torch.ones(100) # class1 lable (tensor), shape=(100, 1)
# 注意 x, y 数据的数据形式是一定要像下面一样 (torch.cat 是在合并数据)
x = torch.cat((x0, x1), 0).type(torch.FloatTensor) # 两类数据合并 FloatTensor = 32-bit floating
y = torch.cat((y0, y1), ).type(torch.LongTensor) # 两类标签合并 LongTensor = 64-bit integer
x, y = Variable(x), Variable(y) # 放入篮子
# method 1
class Net(torch.nn.Module): # 继承 torch 的 Module
def __init__(self, n_feature, n_hidden, n_output):
super(Net, self).__init__() # 继承 __init__ 功能
# 定义每层用什么样的形式
self.hidden = torch.nn.Linear(n_feature, n_hidden) # 隐藏层线性输出
self.predict = torch.nn.Linear(n_hidden, n_output) # 输出层线性输出
def forward(self, x): # 这同时也是 Module 中的 forward 功能
x = F.relu(self.hidden(x))
x = self.predict(x)
return x
net1 = Net(2, 10, 2)
# method 2
net2 = torch.nn.Sequential(
torch.nn.Linear(2, 10),
torch.nn.ReLU(),
torch.nn.Linear(10, 2)
)
print(net1)
print(net2)
Net(
(hidden): Linear(in_features=2, out_features=10, bias=True)
(predict): Linear(in_features=10, out_features=2, bias=True)
)
Sequential(
(0): Linear(in_features=2, out_features=10, bias=True)
(1): ReLU()
(2): Linear(in_features=10, out_features=2, bias=True)
)
# 保存和提取神经网络
# fake data
x = torch.unsqueeze(torch.linspace(-1, 1, 100), dim=1) # x data (tensor), shape=(100, 1)
y = x.pow(2) + 0.2*torch.rand(x.size()) # noisy y data (tensor), shape=(100, 1)
def save():
# 建网络
net1 = torch.nn.Sequential(
torch.nn.Linear(1, 10),
torch.nn.ReLU(),
torch.nn.Linear(10,1)
)
optimizer = torch.optim.SGD(net1.parameters(), lr=0.5)
loss_func = torch.nn.MSELoss()
# 训练
for t in range(100):
prediction = net1(x)
loss = loss_func(prediction, y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
# plot result
plt.figure(1, figsize=(10, 3)) # 创建一个画板
plt.subplot(131) # 第一个画板的第一个子图
plt.title('Net1')
plt.scatter(x.data.numpy(), y.data.numpy()) # 散点
plt.plot(x.data.numpy(), prediction.data.numpy(), 'r-', lw=3) # 描线
# 2 ways to save the net
torch.save(net1, 'net.pkl') # 保存整个网络
torch.save(net1.state_dict(), 'net_params.pkl') # 保存parameters (速度快, 占内存少)
# 还原网络
def restore_net():
# restore entire net1 to net2
net2 = torch.load('net.pkl')
prediction = net2(x) # 用于画图时的y轴坐标
# plot result
plt.subplot(132) # 第一个画板的第二个子图
plt.title('Net2')
plt.scatter(x.data.numpy(), y.data.numpy())
plt.plot(x.data.numpy(), prediction.data.numpy(), 'r-', lw=3)
# 还原参数
def restore_params():
# 新建 net3
net3 = torch.nn.Sequential(
torch.nn.Linear(1, 10),
torch.nn.ReLU(),
torch.nn.Linear(10,1)
)
# 将保存的参数复制到 net3
net3.load_state_dict(torch.load('net_params.pkl'))
prediction = net3(x)
# plot result
plt.subplot(133) # 第一个画板的第三个子图
plt.title('Net3')
plt.scatter(x.data.numpy(), y.data.numpy())
plt.plot(x.data.numpy(), prediction.data.numpy(), 'r-', lw=3)
plt.show()
# 保存
save()
# 提取整个网络
restore_net()
# 提取网络参数, 复制到新网络
restore_params()