神经网络分类模型训练
常用包:
import torch
import numpy as np
import torch.nn as nn
import torch.utils.data as Data
import torchvision
1,加载数据集:
对minist:
train_data = torchvision.datasets.MNIST(
root='/Users/wangpeng/Desktop/all/CS/Courses/Deep Learning/mofan_PyTorch/mnist/', # mnist has been downloaded before, use it directly
train=True, # this is training data
transform=torchvision.transforms.ToTensor(), # Converts a PIL.Image or numpy.ndarray to
# torch.FloatTensor of shape (C x H x W) and normalize in the range [0.0, 1.0]
download=False,
)
2,将数据集才分为训练集和测试集,train_test_split()
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2)
3,将训练/测试数据集安装batchsize大小分组
DataLoader:参数(数据集,batchsize, shuffle = True不能整分时将不足的丢弃)
import torch.utils.data as Data
train_loader = Data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True)
4,搭模型
class 模型名字(nn.Module):
def __init__(self,需传入的模型结构参数):
nn.LSTM(in_dim, hidden_dim, n_layer, batch_first=True)
nn.Linear(hidden_dim, n_class)
**多个全连接层时**:
nn.Sequential(
nn.Linear(hidden_dim * self.num_dir, hidden_dim),
nn.LeakyReLU(True),
nn.Dropout(),
nn.Linear(hidden_dim, hidden_dim),
nn.LeakyReLU(True),
nn.Dropout(),
nn.Linear(hidden_dim, num_classes),
)
训练数据传入模型,在模型中流转计算
def forward(self, x):
out, _ = self.lstm(x)
5, 模型初始化
model = LSTMnet(相关参数值28, 64, 2, 10) # 图片大小28*28,lstm的每个隐藏层64个节点,2层隐藏层
if torch.cuda.is_available(): cuda(GPU)是否可用
model = model.cuda()
optimizer = torch.optim.Adam(model.parameters(), lr=LR) **优化器**
criterion = nn.CrossEntropyLoss() **损失函数**
6训练阶段model.train()
def train(epoch):
model.train() #注意这
for epoch in range(EPOCH): 整个数据集训练次数
for iteration, (train_x, train_y) in enumerate(train_loader): 一个batch size训练一次
train_x = train_x.squeeze() 数据的一些处理
output = model(train_x) 数据输入模型
loss = criterion(output, train_y) # cross entropy loss 输出结果与真实标签之间的loss,使用交叉熵损失真实标签不用one-hot
optimizer.zero_grad() # clear gradients for this training step清空优化器里的梯度
loss.backward() # backpropagation, compute gradients 损失函数反向传播,计算梯度
optimizer.step() #使用计算的梯度更新模型参数
7,测试阶段model.eval()
def test(test_data_loader):
model.eval()
test_loss = 0.
for data, target in test_data_loader:
with torch.no_grad():
data, target = Variable(data.float()), Variable(target.long())
if cuda:
data, target = data.cuda(), target.cuda()
output = model(data) 测试数据输入模型
test_loss += criterion(output, target).item() # sum up batch loss 计算损失
pred, correct, num_instance, conf_mat = get_acc(output, target)