发布时间:2023-03-16 09:30
import torch
from torchvision import transforms
from torchvision import datasets
from torch.utils.data import DataLoader
import torch.nn.functional as F
import torch.optim as optim
batch_size = 64
transform = transforms.Compose([
transforms.ToTensor(), # 将PIL格式图像转换成Tensor矩阵向量(维度28x28转换成1x28x28,1:为RGB通道)【 [0...255]--->[0,1] 】
transforms.Normalize((0.1307, ), (0.3081, )) # 均一化处理(均值、标准差)
])
# 训练集数据
train_dataset = datasets.MNIST(root='../dataset/mnist/',
train=True,
download=True,
transform=transform)
# 加载训练集数据
train_loader = DataLoader(train_dataset,
shuffle=True,
batch_size=batch_size)
# 测试集数据集
test_dataset = datasets.MNIST(root='../dataset/mnist/',
train=False,
download=True,
transform=transform)
# 加载测试集数据集
test_loader = DataLoader(test_dataset,
shuffle=False,
batch_size=batch_size)
class Net(torch.nn.Module):
def __init__(self):
super(Net, self).__init__()
self.linear1 = torch.nn.Linear(784, 512) # 将PIL格式图像转换成Tensor矩阵向量(维度28x28转换成1x28x28,1:为RGB通道)28x28=784
self.linear2 = torch.nn.Linear(512, 256)
self.linear3 = torch.nn.Linear(256, 128)
self.linear4 = torch.nn.Linear(128, 64)
self.linear5 = torch.nn.Linear(64, 10)
def forward(self, x):
x = x.view(-1, 784) # -1:自动检测矩阵有有多少行,列指定为784
x = F.relu(self.linear1(x))
x = F.relu(self.linear2(x))
x = F.relu(self.linear3(x))
x = F.relu(self.linear4(x))
return self.linear5(x)
model = Net()
###################3 构建损失函数、优化器###############################
criterion = torch.nn.CrossEntropyLoss() # 交叉熵损失
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.5) # 参数优化
#####################4 循环训练 #########################
def train(epoch):
running_loss = 0.0
for batch_idx, data in enumerate(train_loader, 0):
# 准备数据(input:输入,target:实际值)
inputs, target = data
# 梯度清0
optimizer.zero_grad()
# 前向传播
outputs = model(inputs)
# 交叉熵损失函数计算
loss = criterion(outputs, target)
# 反向传播
loss.backward()
# 参数优化
optimizer.step()
# 累计loss
running_loss += loss.item()
# 数据集一共有batch_idx个数据,每隔300个打印一次平均损失函数值
if batch_idx % 300 ==299:
print('[%d, %5d] loss: %.3f' % (epoch + 1, batch_idx + 1, running_loss / 300))
running_loss = 0.0
def test():
correct = 0
total = 0
with torch.no_grad():
for data in test_loader:
images, labels = data
outputs = model(images)
_, predicted = torch.max(outputs.data, dim=1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Accuracy on test set: %d %%' % (100 * correct / total))
if __name__ == '__main__':
for epoch in range(10):
train(epoch)
test()