本文的主要目的是针对初学者,完成第一个网络模型训练的操作示意,在此基础上,后学者可以按照套路训练不同的模型。 代码如下:
import torch
import torch.nn as nn
from torch.nn import Sequential, Conv2d, MaxPool2d, Linear, Flatten
# 定义网络
class MyCifar10(nn.Module):
def __init__(self):
super(MyCifar10, self).__init__()
self.model = Sequential(
Conv2d(3, 32, 5, padding=2),
MaxPool2d(2),
Conv2d(32, 32, kernel_size=5, padding=2),
MaxPool2d(2),
Conv2d(32, 64, kernel_size=5, padding=2),
MaxPool2d(2),
Flatten(),
# 64*4*4
Linear(64 * 4 * 4, 64),
Linear(64, 10)
)
def forward(self, x):
return self.model(x)
# 测试网络
if __name__ == '__main__':
model = MyCifar10()
input = torch.ones(64, 3, 32, 32)
output = model(input)
print(output.shape)
import torch
import torchvision
from torch.nn import Sequential, Conv2d, MaxPool2d, Linear, Flatten
from torch.utils.data import DataLoader
import torch.nn as nn
from torch.utils.tensorboard import SummaryWriter
from MyCifar10Model import MyCifar10
# 用cifar10的测试和训练集数据
train_data = torchvision.datasets.CIFAR10("cifar10", train=True, transform=torchvision.transforms.ToTensor(),
download=True)
test_data = torchvision.datasets.CIFAR10("cifar10", train=False, transform=torchvision.transforms.ToTensor(),
download=True)
train_dataloader = DataLoader(train_data, batch_size=64)
test_dataloader = DataLoader(test_data, batch_size=64)
# create net model
model = MyCifar10()
# define loss func
loss_fn = nn.CrossEntropyLoss()
# optimizer
learn_rate = 1e-2
optimizer = torch.optim.SGD(model.parameters(), learn_rate)
epochs = 2
train_step = 0
test_step = 0
# 画图
writer = SummaryWriter("log7")
# 只针对一些特殊的层起作用
model.train()
for i in range(epochs):
print(">>>>>>>>>>>>>>>>第{}轮训练开始".format(i+1))
for data in train_dataloader:
imgs, targets = data
outputs = model(imgs)
# 优化器
loss = loss_fn(outputs, targets)
optimizer.zero_grad()
loss.backward()
optimizer.step()
train_step +=1
if train_step % 100 == 0:
print("----------训练次数{}, loss:{}".format(train_step, loss.item()))
writer.add_scalar("train_loss", loss.item(), train_step)
# 测试
# eval也只是针对特殊的层起作用
model.eval()
total_loss = 0
total_accuracy = 0
with torch.no_grad():
for data in test_dataloader:
imgs, targets = data
outputs = model(imgs)
loss = loss_fn(outputs, targets)
total_loss += loss.item()
accuracy = (outputs.argmax(1) == targets).sum()
total_accuracy += accuracy
print("测试集上的loss:{}".format(total_loss))
writer.add_scalar("test_loss", total_loss, test_step)
writer.add_scalar("accuracy", total_accuracy, test_step)
test_step +=1
# 保存每一轮训练的模型
torch.save(model, "modules/my_model_{}".format(i))
writer.close()