代码来自B站up爆肝杰哥
测试版本
import torch
import torchvisiondef print_hi(name):print(f'Hi, {name}') if __name__ == '__main__':print_hi('陀思妥耶夫斯基')print("HELLO pytorch {}".format(torch.__version__))print("torchvision.version:", torchvision.__version__)print("torch.cuda.is_available? ", torch.cuda.is_available())x = torch.rand(3, 6)print(x)
Hi, 陀思妥耶夫斯基
HELLO pytorch 1.12.0
torchvision.version: 0.13.0
torch.cuda.is_available? False
tensor([[0.7461, 0.6500, 0.3676, 0.5967, 0.1452, 0.3587],[0.0439, 0.9415, 0.9146, 0.8264, 0.3982, 0.3910],[0.2620, 0.0925, 0.9572, 0.6554, 0.2681, 0.0893]])
一个搭建过程
ts1=torch.randn(3,2)
ts1
tensor([[-1.3440, 1.6731],[ 0.2274, 1.1744],[-0.7912, 1.0309]])
# 转移到GPU上
# ts2=ts1.to('cuda:0')
# model = DNN().to('cuda:0')
简单搭建
import torch
import torch.nn as nn
import matplotlib.pyplot as plt
%matplotlib inlinefrom matplotlib_inline import backend_inline
backend_inline.set_matplotlib_formats('svg')
# 生成数据集
X1 = torch.rand(10000,1) # 输入特征 1
X2 = torch.rand(10000,1) # 输入特征 2
X3 = torch.rand(10000,1) # 输入特征 3
Y1 = ( (X1+X2+X3)<1 ).float() # 输出特征 1
Y2 = ( (1<(X1+X2+X3)) & ((X1+X2+X3)<2) ).float() # 输出特征 2
Y3 = ( (X1+X2+X3)>2 ).float() # 输出特征 3
Data = torch.cat([X1,X2,X3,Y1,Y2,Y3],axis=1) # 整合数据集
# Data = Data.to('cuda:0') # 把数据集搬到 GPU 上
Data.shape
torch.Size([10000, 6])
train_size=int(len(Data)*0.7)
test_size=len(Data)-train_size
Data=Data[torch.randperm(Data.size(0)),:] # 打乱样本顺序
train_Data = Data[:train_size, :]
test_Data = Data[train_size:, :]
train_Data.shape, test_Data.shape
(torch.Size([7000, 6]), torch.Size([3000, 6]))
class DNN(nn.Module):def __init__(self):''' 搭建神经网络各层 '''super(DNN, self).__init__()self.net = nn.Sequential( # 按顺序搭建各层nn.Linear(3, 5),nn.ReLU(), # 第 1 层:全连接层nn.Linear(5, 5),nn.ReLU(), # 第 2 层:全连接层nn.Linear(5, 5),nn.ReLU(), # 第 3 层:全连接层nn.Linear(5, 3) # 第 4 层:全连接层)def forward(self, x):''' 前向传播 '''y = self.net(x) # x 即输入数据return y # y 即输出数据
model=DNN()
model
DNN((net): Sequential((0): Linear(in_features=3, out_features=5, bias=True)(1): ReLU()(2): Linear(in_features=5, out_features=5, bias=True)(3): ReLU()(4): Linear(in_features=5, out_features=5, bias=True)(5): ReLU()(6): Linear(in_features=5, out_features=3, bias=True))
)
# 查看内部参数(非必要)
for name, param in model.named_parameters():print(f"参数:{name}\n 形状:{param.shape}\n 数值:{param}\n")
参数:net.0.weight形状:torch.Size([5, 3])数值:Parameter containing:
tensor([[-0.3450, -0.0658, -0.4716],[ 0.4366, -0.2939, -0.2635],[ 0.0442, -0.4962, -0.2176],[ 0.0285, -0.1640, -0.2958],[ 0.0790, 0.4858, 0.1398]], requires_grad=True)参数:net.0.bias形状:torch.Size([5])数值:Parameter containing:
tensor([ 0.5582, -0.4712, 0.4455, 0.2119, -0.2546], requires_grad=True)参数:net.2.weight形状:torch.Size([5, 5])数值:Parameter containing:
tensor([[ 0.2466, 0.1161, -0.2335, -0.3888, -0.1425],[ 0.0706, 0.0777, 0.0081, 0.0236, -0.0326],[ 0.3518, 0.0702, 0.4115, -0.2355, -0.2249],[-0.0715, -0.1902, -0.1665, -0.1604, -0.1973],[-0.0658, 0.0639, -0.1540, -0.4332, -0.3613]], requires_grad=True)参数:net.2.bias形状:torch.Size([5])数值:Parameter containing:
tensor([ 0.3787, 0.2764, 0.2568, 0.3191, -0.2946], requires_grad=True)参数:net.4.weight形状:torch.Size([5, 5])数值:Parameter containing:
tensor([[-0.1057, 0.0083, 0.0652, 0.3027, 0.1454],[ 0.1724, 0.0562, 0.3369, 0.0071, -0.1573],[-0.3154, -0.0760, 0.0859, -0.3460, -0.0201],[ 0.2442, -0.3927, -0.0244, -0.2937, 0.1900],[ 0.3870, -0.3930, 0.1015, -0.2828, -0.0344]], requires_grad=True)参数:net.4.bias形状:torch.Size([5])数值:Parameter containing:
tensor([ 0.3448, -0.2331, 0.3587, 0.3233, 0.0216], requires_grad=True)参数:net.6.weight形状:torch.Size([3, 5])数值:Parameter containing:
tensor([[ 0.3056, -0.1478, -0.1536, 0.0831, 0.2769],[-0.2593, -0.4269, 0.1112, -0.0393, -0.4057],[-0.3688, 0.3386, -0.2689, -0.2382, -0.0796]], requires_grad=True)参数:net.6.bias形状:torch.Size([3])数值:Parameter containing:
tensor([-0.0037, 0.2068, -0.0956], requires_grad=True)
# 损失函数的选择
loss_fn = nn.MSELoss()
# 优化算法的选择
learning_rate = 0.01 # 设置学习率
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
# 训练网络
epochs = 1000
losses = [] # 记录损失函数变化的列表
# 给训练集划分输入与输出
X = train_Data[:, :3] # 前 3 列为输入特征
Y = train_Data[:, -3:] # 后 3 列为输出特征
for epoch in range(epochs):Pred = model(X) # 一次前向传播(批量)loss = loss_fn(Pred, Y) # 计算损失函数losses.append(loss.item()) # 记录损失函数的变化optimizer.zero_grad() # 清理上一轮滞留的梯度loss.backward() # 一次反向传播optimizer.step() # 优化内部参数Fig = plt.figure()
plt.plot(range(epochs), losses)
plt.ylabel('loss')
plt.xlabel('epoch')
plt.show()
# 测试网络
# 给测试集划分输入与输出
X = test_Data[:, :3] # 前 3 列为输入特征
Y = test_Data[:, -3:] # 后 3 列为输出特征
with torch.no_grad(): # 该局部关闭梯度计算功能Pred = model(X) # 一次前向传播(批量)# 将每行最大的数置 1,将不是 1 的数字置 0Pred[:, torch.argmax(Pred, axis=1)] = 1Pred[Pred != 1] = 0correct = torch.sum((Pred == Y).all(1)) # 预测正确的样本数量total = Y.size(0) # 全部的样本数量print(f'测试集精准度: {100*correct/total} %')
测试集精准度: 67.19999694824219 %
# 保存网络
torch.save(model, 'model.pth')
# 把模型赋给新网络
new_model = torch.load('model.pth')
# 测试网络
# 给测试集划分输入与输出
X = test_Data[:, :3] # 前 3 列为输入特征
Y = test_Data[:, -3:] # 后 3 列为输出特征
with torch.no_grad(): # 该局部关闭梯度计算功能Pred = new_model(X) # 一次前向传播(批量)# 将每行最大的数置 1,将不是 1 的数字置 0Pred[:, torch.argmax(Pred, axis=1)] = 1Pred[Pred != 1] = 0correct = torch.sum((Pred == Y).all(1)) # 预测正确的样本数量total = Y.size(0) # 全部的样本数量print(f'测试集精准度: {100*correct/total} %')
测试集精准度: 67.19999694824219 %
一个例子:批量梯度下降
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import matplotlib.pyplot as plt
%matplotlib inline# 展示高清图
from matplotlib_inline import backend_inlinebackend_inline.set_matplotlib_formats('svg')
# 准备数据集
df = pd.read_excel('./深度学习(4):深度神经网络(DNN)/Data.xlsx', index_col=0) # 导入数据
arr = df.values # Pandas 对象退化为 NumPy 数组
arr = arr.astype(np.float32) # 转为 float32 类型数组
ts = torch.tensor(arr) # 数组转为张量
# ts = ts.to('cuda') # 把训练集搬到 cuda 上
ts.shape
torch.Size([759, 9])
# 划分训练集与测试集
train_size = int(len(ts) * 0.7) # 训练集的样本数量
test_size = len(ts) - train_size # 测试集的样本数量
ts = ts[torch.randperm(ts.size(0)), :] # 打乱样本的顺序
train_Data = ts[:train_size, :] # 训练集样本
test_Data = ts[train_size:, :] # 测试集样本
train_Data.shape, test_Data.shape
(torch.Size([531, 9]), torch.Size([228, 9]))
class DNN(nn.Module):def __init__(self):super(DNN, self).__init__()self.net = nn.Sequential(nn.Linear(8, 32), nn.Sigmoid(),nn.Linear(32, 8),nn.Sigmoid(), nn.Linear(8, 4), nn.Sigmoid(),nn.Linear(4, 1), nn.Sigmoid())def forward(self, x):y = self.net(x)return ymodel = DNN()
loss_fn = nn.BCELoss(reduction='mean')
optimizer = torch.optim.Adam(model.parameters(), lr=0.005)# 训练网络
epochs = 5000
losses = [] # 记录损失函数变化的列表
# 给训练集划分输入与输出
X = train_Data[:, :-1] # 前 8 列为输入特征
Y = train_Data[:, -1].reshape((-1, 1))
# 此处的.reshape((-1,1))将一阶张量升级为二阶张量for _ in range(epochs):Pred = model(X)loss = loss_fn(Pred, Y)losses.append(loss.item())optimizer.zero_grad()loss.backward()optimizer.step()Fig = plt.figure()
plt.plot(range(epochs), losses)
plt.ylabel('loss')
plt.xlabel('epoch')
plt.show()
# 测试网络
# 给测试集划分输入与输出
X = test_Data[:, :-1] # 前 8 列为输入特征
Y = test_Data[:, -1].reshape((-1, 1)) # 后 1 列为输出特征
with torch.no_grad(): # 该局部关闭梯度计算功能Pred = model(X) # 一次前向传播(批量)Pred[Pred >= 0.5] = 1Pred[Pred < 0.5] = 0correct = torch.sum((Pred == Y).all(1)) # 预测正确的样本total = Y.size(0) # 全部的样本数量print(f'测试集精准度: {100*correct/total} %')
测试集精准度: 69.7368392944336 %
接着上一个例子:改用小批量梯度下降
在使用小批量梯度下降时,必须使用 3 个 PyTorch 内置的实用工具(utils):
- DataSet 用于封装数据集;
- DataLoader 用于加载数据不同的批次;
- random_split 用于划分训练集与测试集。
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader, random_split
import matplotlib.pyplot as plt
%matplotlib inline# 展示高清图
from matplotlib_inline import backend_inlinebackend_inline.set_matplotlib_formats('svg')
在封装我们的数据集时,必须继承实用工具(utils)中的 DataSet 的类,这
个过程需要重写_init_、_getitem_、__len__三个方法,分别是为了加载数据集、获取数据索引、获取数据总量。
# 制作数据集
class MyData(Dataset): # 继承 Dataset 类def __init__(self, filepath):df = pd.read_excel(filepath, index_col=0) # 导入数据arr = df.values # 对象退化为数组arr = arr.astype(np.float32) # 转为 float32 类型数组ts = torch.tensor(arr) # 数组转为张量
# ts = ts.to('cuda') # 把训练集搬到 cuda 上self.X = ts[:, :-1] # 前 8 列为输入特征self.Y = ts[:, -1].reshape((-1, 1)) # 后 1 列为输出特征self.len = ts.shape[0] # 样本的总数def __getitem__(self, index):return self.X[index], self.Y[index]def __len__(self):return self.len
# 划分训练集与测试集
Data = MyData('./深度学习(4):深度神经网络(DNN)/Data.xlsx')
train_size = int(len(Data) * 0.7) # 训练集的样本数量
test_size = len(Data) - train_size # 测试集的样本数量
train_Data, test_Data = random_split(Data, [train_size, test_size])
# 批次加载器
train_loader = DataLoader(dataset=train_Data, shuffle=True, batch_size=128) # 在每个 epoch 开始时,数据会被随机打乱顺序
test_loader = DataLoader(dataset=test_Data, shuffle=False, batch_size=64)
class DNN(nn.Module):def __init__(self):super(DNN, self).__init__()self.net = nn.Sequential(nn.Linear(8, 32), nn.Sigmoid(),nn.Linear(32, 8),nn.Sigmoid(), nn.Linear(8, 4), nn.Sigmoid(),nn.Linear(4, 1), nn.Sigmoid())def forward(self, x):y = self.net(x)return y
model = DNN()
loss_fn = nn.BCELoss(reduction='mean')
optimizer = torch.optim.Adam(model.parameters(), lr=0.005)# 训练网络
epochs = 500
losses = [] # 记录损失函数变化的列表for _ in range(epochs):for (x,y) in train_loader:Pred = model(x)loss = loss_fn(Pred, y)losses.append(loss.item())optimizer.zero_grad()loss.backward()optimizer.step()Fig = plt.figure()
plt.plot(range(len(losses)), losses)
plt.show()
# 测试网络
correct = 0
total = 0
with torch.no_grad(): # 该局部关闭梯度计算功能for (x, y) in test_loader: # 获取小批次的 x 与 yPred = model(x) # 一次前向传播(小批量)Pred[Pred>=0.5] = 1Pred[Pred<0.5] = 0correct += torch.sum( (Pred == y).all(1) )total += y.size(0)
print(f'测试集精准度: {100*correct/total} %') # 可见这里小批量梯度下降比批量梯度下降效果好
测试集精准度: 76.31578826904297 %
手写数字识别
在 MNIST 中,模型的输入是一副图像,模型的输出就是一个与图像中对应的数字(0 至 9 之间的一个整数,不是独热编码)。我们不用手动将输出转换为独热编码,PyTorch 会在整个过程中自动将数据集的输出转换为独热编码.只有在最后测试网络时,我们对比测试集的预测输出与真实输出时,才需要注意一下。每个图像都是形状为28 × 28的二维数组。
在这种多分类问题中,神经网络的输出层需要一个 softmax 激活函数,它可以把输出层的数据归一化到 0-1 上,且加起来为 1,这样就模拟出了概率的意思。
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torchvision import transforms, datasets
import matplotlib.pyplot as plt
%matplotlib inline# 展示高清图
from matplotlib_inline import backend_inlinebackend_inline.set_matplotlib_formats('svg')
# 数据集转换参数
transform = transforms.Compose([transforms.ToTensor(),transforms.Normalize(0.1307, 0.3081)])train_dataset = datasets.MNIST(root='./data',train=True,transform=transform,download=False) # 我已下载,所以改成False,没下载改成True,test_dataset同理
test_dataset = datasets.MNIST(root='./data',train=False,transform=transform,download=False)
train_loader = DataLoader(dataset=train_dataset, batch_size=128, shuffle=True)
test_loader = DataLoader(dataset=test_dataset, batch_size=128, shuffle=False)
每个样本的输入都是形状为28 × 28的二维数组,那么对于 DNN 来说,输入层的神经元节点就要有28 × 28 = 784个;输出层使用独热编码,需要 10 个节点。
class DNN(nn.Module):def __init__(self):''' 搭建神经网络各层 '''super(DNN, self).__init__()self.net = nn.Sequential( # 按顺序搭建各层nn.Flatten(), # 把图像铺平成一维nn.Linear(784, 512),nn.ReLU(), # 第 1 层:全连接层nn.Linear(512, 256),nn.ReLU(), # 第 2 层:全连接层nn.Linear(256, 128),nn.ReLU(), # 第 3 层:全连接层nn.Linear(128, 64),nn.ReLU(), # 第 4 层:全连接层nn.Linear(64, 10) # 第 5 层:全连接层)def forward(self, x):''' 前向传播 '''y = self.net(x) # x 即输入数据return y # y 即输出数据
model = DNN()
loss_fn = nn.CrossEntropyLoss() # 自带 softmax 激活函数
optimizer = torch.optim.SGD(model.parameters(), lr=0.01,momentum=0.5)# 训练网络
epochs = 5
losses = [] # 记录损失函数变化的列表for _ in range(epochs):for (x,y) in train_loader:Pred = model(x)loss = loss_fn(Pred, y)losses.append(loss.item())optimizer.zero_grad()loss.backward()optimizer.step()Fig = plt.figure()
plt.plot(range(len(losses)), losses)
plt.show()
# 测试网络
correct = 0
total = 0
with torch.no_grad(): # 该局部关闭梯度计算功能for (x, y) in test_loader: # 获取小批次的 x 与 yPred = model(x) # 一次前向传播(小批量_, predicted = torch.max(Pred.data, dim=1) # 每一行最大值,最大值对应的列索引correct += torch.sum( (predicted == y) )total += y.size(0)print(f'测试集精准度: {100*correct/total} %')
测试集精准度: 95.16999816894531 %