2021-07-27
代码
从今天起每天的一个,虽然现在是模仿,但是很开心,谢谢
"""卷积网络中的输入层与传统神经网络有些区别,需重新设计,训练模块基本一致"""
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as f
from torchvision import datasets, transforms
import matplotlib.pyplot as plt
import numpy as np
"""
分别构建训练集和测试集(验证集)
DataLoader来迭代取数据
"""
input_size = 28
num_classes = 10
num_epochs = 3
batch_size = 64
train_dataset = datasets.MNIST(root='./data',
train=True,
transform=transforms.ToTensor(),
download=True)
test_dataset = datasets.MNIST(root='./data',
train=False,
transform=transforms.ToTensor())
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size,
shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size=batch_size,
shuffle=True)
"""
一般卷积层,relu层,池化层可以写成一个套餐
注意卷积最后结果还是一个特征图,需要把特征图转换成向量才能做分类或者回归任务
"""
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(
in_channels=1,
out_channels=16,
kernel_size=5,
stride=1,
padding=2,
),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2),
)
self.conv2 = nn.Sequential(
nn.Conv2d(16, 32, 5, 1, 1),
nn.ReLU(),
nn.MaxPool2d(2),
)
self.out = nn.Linear(32 * 6 * 6, 10)
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = x.view(x.size(0), -1)
output = self.out(x)
return output
def accuracy(predictions, labels):
pred = torch.max(predictions.data, 1)[1]
rights = pred.eq(labels.data.view_as(pred)).sum()
return rights, len(labels)
'1、实例化'
net = CNN()
'2、损失函数'
criterion = nn.CrossEntropyLoss()
'3、定义优化器,普通的随机的梯度下降法'
optizer = optim.Adam(net.parameters(), lr=0.001)
'4、开始循环训练'
for epoch in range(num_epochs):
'5、当前epoch的结果保存下来'
train_rights = []
for batch_idx, (data, target) in enumerate(train_loader):
net.train()
output = net(data)
loss = criterion(output, target)
loss.backward()
optizer.step()
right = accuracy(output, target)
train_rights.append(right)
if batch_idx % 100 == 0:
net.eval()
val_rights = []
for (data, target) in test_loader:
output = net(data)
right = accuracy(output, target)
val_rights.append(right)
train_r = (sum([tup[0] for tup in train_rights]), sum([tup[1] for tup in train_rights]))
val_r = (sum([tup[0] for tup in val_rights]), sum([tup[1] for tup in val_rights]))
print('当前epoch:{} [{} / {} ({:.0f}%)]\t损失:{:.6f}\t训练集准确率:{:.2f}\t测试集准确率:{:.2f}'.format(epoch,
batch_idx * batch_size,
len(train_loader.dataset),
100. * batch_idx / len(train_loader),
loss.data,
100. * train_r[0].numpy() / train_r[1],
100. * val_r[0].numpy() / val_r[1]))
结果图
|