深度学习笔记2:使用pytorch构建神经网络
基本组件:输入层、隐藏层、输出层、激活函数、损失函数和学习率
在这里使用简单数据集利用pytorch构建神经网络,利用张量对象操作和梯度值计算更新网络权重。
参考博客:PyTorch深度学习实战(3)——使用PyTorch构建神经网络_pytorch 神经网络-CSDN博客
1.使用pytorch构建神经网络
import torchx = [[1,2],[3,4],[5,6],[7,8]]
y = [[3],[7],[11],[15]]
#将列表转为张量对象
X = torch.tensor(x).float()
Y = torch.tensor(y).float()
#将xy数据 注册到device
device = 'cuda' if torch.cuda.is_available() else 'cpu'
X = X.to(device)
Y = Y.to(device)
#定义神经网络
from torch import nn
#创建神经网络架构类Myneuralnet,继承自nn.module,nn.module是pytorch中所有神经网络模块的基类
class MyNeuralNet(nn.Module):#定义神经网络的结构,初始化函数,定义网络的各个层,调用super()._init__()来确保类继承nn.module的属性和方法def __init__(self):#调用父类的初始化函数super().__init__()#定义网络层,自动注册了参数self.input_to_hidden_layer = nn.Linear(2,8)#全连接self.hidden_layer_activation = nn.ReLU()#激活函数self.hidden_to_output_layer = nn.Linear(8,1)#全连接#定义前向传播函数,将初始化后的网络组件连接到一起def forward(self,x):x = self.input_to_hidden_layer(x)x = self.hidden_layer_activation(x)x = self.hidden_to_output_layer(x)return x
print(nn.Linear(2,7))#不知道为什么有问题
#访问每个神经网络组建的初始权重
mynet = MyNeuralNet().to(device)
print(mynet.input_to_hidden_layer.weight)
#获得神经网络所有参数
mynet.parameters()
#根据上一行代码返回的生成器对象,通过生成器循环获得所有参数
for param in mynet.parameters():print(param)
#定义损失函数
loss_func = nn.MSELoss()
#将输入值传递给神经网络对象,计算给定输入的损失函数
_y = mynet(X)
loss_value = loss_func(_y,Y)
print(loss_value)
#定义优化器使用SGD算法更新网络参数
from torch.optim import SGD
opt = SGD(mynet.parameters(),lr=0.001)
#一个epoch的训练
opt.zero_grad()#将梯度清零
loss_value = loss_func(mynet(X),Y)#计算损失
loss_value.backward()#反向传播
opt.step()#更新权重
#使用for循环重复执行上述步骤重复50个epoch,在loos_history列表中记录每个epoch的损失值
loss_history = []
for _ in range(50):opt.zero_grad()loss_value = loss_func(mynet(X), Y)loss_value.backward()opt.step()loss_history.append(loss_value.item()) # 使用item()方法获取张量的数值并添加到列表#绘制损失随着epoch的变化
import matplotlib.pyplot as plt
plt.plot(loss_history)
plt.title('loss variation over increasing epochs')
plt.xlabel('epochs')
plt.ylabel('loss value')
plt.show()
神经网络数据加载
from torch.utils.data import Dataset, DataLoader
import torch
import torch.nn as nn
#导入数据,将数据转换为浮点数,并注册到对应设备
x = [[1,2],[3,4],[5,6],[7,8]]
y = [[3],[7],[11],[15]]X = torch.tensor(x).float()
Y = torch.tensor(y).float()device = 'cuda' if torch.cuda.is_available() else 'cpu'
X = X.to(device)
Y = Y.to(device)
#定义数据集类,继承自Dataset
class MyDataset(Dataset):def __init__(self,x,y):self.x = torch.tensor(x).float()self.y = torch.tensor(y).float()def __len__(self):return len(self.x)#获取指定数据样本def __getitem__(self,ix):return self.x[ix],self.y[ix]
#自定义类的实例
ds = MyDataset(x,y)
#通过dataloader传递数据集实例,从原始输入输出张量对象中获取batch_size
dl = DataLoader(ds,batch_size=2,shuffle=True)
#循环遍历d1后去批量数据信息
for x,y in dl:print(x,y)#定义神经网络类
class MyNeuralNet(nn.Module):def __init__(self):super().__init__()self.input_to_hidden_layer = nn.Linear(2,8)self.hidden_layer_activation = nn.ReLU()self.hidden_to_output_layer = nn.Linear(8,1)def forward(self,x):x = self.input_to_hidden_layer(x)x = self.hidden_layer_activation(x)x = self.hidden_to_output_layer(x)return x
#定义模型对象mynet损失函数和优化器
mynet = MyNeuralNet().to(device)
loss_func = nn.MSELoss()
from torch.optim import SGD
opt = torch.optim.SGD(mynet.parameters(),lr=0.001)
#循环遍历数据点最小化损失值
import time
loss_history = []
start_time = time.time()
for _ in range(50):for data in dl:x,y = dataopt.zero_grad()loss_value = loss_func(mynet(x),y)loss_value.backward()opt.step()loss_history.append(loss_value)
end_time = time.time()
print(end_time - start_time)
模型测试
#模型测试
val_x = [[8,9],[10,11],[1.5,2.5]]
#将数据转换为张量对象
val_x = torch.tensor(val_x).float()
#将张量对象注册到设备
val_x = val_x.to(device)
#将输入数据传递给模型进行预测
mynet(val_x)
#获取中间层值 直接调用神经网络层
print(mynet.hidden_layer_activation(mynet.input_to_hidden_layer(val_x)))
#另一种方法:在forward中查看
class MyneralNet(nn.Module):def __init__(self):super().__init__()self.input_to_hidden_layer = nn.Linear(2,8)self.hidden_layer_activation = nn.ReLU()self.hidden_to_output_layer = nn.Linear(8,1)def forward(self,x):hidden1 = self.input_to_hidden_layer(x)hidden2 = self.hidden_layer_activation(hidden1)x = self.hidden_to_output_layer(hidden2)return x,hidden2
#访问隐藏层
print(mynet(val_x)[1])
2.使用Sequential类创建神经网络
#使用Sequential类创建神经网络,只需要利用nn.sequential()函数将网络的各个层按照顺序堆叠起来
import torch
import torch.nn as nn
from torch.optim import SGD
import numpy as np
from torch.utils.data import Dataset,DataLoader
device = 'cuda' if torch.cuda.is_available() else 'cpu'
#定义数据集与数据集类x = [[1,2],[3,4],[5,6],[7,8]]
y = [[3],[7],[11],[15]]class MyDataset(Dataset):def __init__(self,x,y):self.x = torch.tensor(x).float().to(device)self.y = torch.tensor(y).float().to(device)self.length = len(x)def __getitem__(self,idx):return self.x[idx],self.y[idx]def __len__(self):return self.length
#定义数据集ds和数据加载器dl
ds = MyDataset(x,y)
dl = DataLoader(ds,batch_size=2,shuffle=True)
#使用nn模块中的Sequential类创建神经网络
mynet = nn.Sequential(nn.Linear(2,8),nn.ReLU(),nn.Linear(8,1)
).to(device)
#打印模型摘要summary
from torchsummary import summary
summary(mynet,(1,2))
#定义损失函数和优化器
loss_func = nn.MSELoss()
opt = SGD(mynet.parameters(),lr=0.001)
#训练模型
loss_history = []
import time
start = time.time()
for _ in range(50):for data in dl:x,y = dataopt.zero_grad()loss_value = loss_func(mynet(x),y)loss_value.backward()opt.step()loss_history.append(loss_value)
end = time.time()
print(end - start)
#在验证集上测试
val_x = [[8,9],[10,11],[1.5,2.5]]
val_x = torch.tensor(val_x).float().to(device)
print(mynet(val_x))
3.保存和加载模型
#模型状态
print(mynet.state_dict())
#模型保存
torch.save(mynet.state_dict(),'mynet.pt')
#模型加载
model = nn.Sequential(nn.Linear(2,8),nn.ReLU(),nn.Linear(8,1)
).to(device)
#从磁盘加载模型参数
state_dict = torch.load('mynet.pt')
#加载state_dict到模型,并将其注册到device上,执行预测任务
model.load_state_dict(state_dict)
model.to(device)
val = [[8,9],[10,11],[1.5,2.5]]
val = torch.tensor(val).float().to(device)
print(model(val))