训练集、测试集均为FashionMNIST数据,
b
a
t
c
h
s
i
z
e
=
256
batchsize=256
batchsize=256,
e
p
o
c
h
e
s
=
10
epoches=10
epoches=10,
l
r
=
0.8
lr=0.8
lr=0.8
代码部分
from torchvision.transforms import transforms
import torch
from torch import nn
from torch.nn import init
import numpy as np
import torchvision
class FlattenLayer(torch.nn.Module):
'''
将输入的图片维度进行转变shape(256,100,100,3)->shape(256,3*100*100)
'''
def __init__(self):
super(FlattenLayer,self).__init__()
def forward(self,X):
return X.view(X.shape[0],-1)
def load_data_fashion_mnist(batch_size,resize=None,root="~/Datasets/FashionMNIST"):
'''
下载数据集,返回两个批量数据迭代器,train_iter,test_iter
'''
trans = []
if resize:
trans.append(torchvision.transforms.Resize(size=resize))
trans.append(torchvision.transforms.ToTensor())
transform = torchvision.transforms.Compose(trans)
mnist_train = torchvision.datasets.FashionMNIST(root=root,train=True,download=True,transform=transform)
mnist_test = torchvision.datasets.FashionMNIST(root=root,train=False,download=True,transform=transform)
train_iter = torch.utils.data.DataLoader(mnist_train,batch_size=batch_size,shuffle=True,num_workers=4)
test_iter = torch.utils.data.DataLoader(mnist_test,batch_size=batch_size,shuffle=True,num_workers=4)
return train_iter,test_iter
def evaluate_accuracy(data_iter,net,device = torch.device("cuda" if torch.cuda.is_available() else "cpu")):
'''
对测试集进行训练,计算准确率
'''
acc_sum, n = 0.0, 0
with torch.no_grad():
for a_batchsize_imagedata,a_batchsize_labeldata in data_iter:
if isinstance(net,torch.nn.Module):
net.eval()
a_batchsize_labeldata_hat = net(a_batchsize_imagedata.to(device))
acc_sum += (a_batchsize_labeldata_hat.argmax(dim=1)==a_batchsize_labeldata.to(device)).float().sum().item()
net.train()
n += a_batchsize_labeldata.shape[0]
return acc_sum/n
def train(num_epoch,train_iter,test_iter,net,loss,optimizer):
'''
训练数据
'''
for epoch in range(num_epoch):
train_loss_sum,train_acc_sum,n = 0.0,0.0,0
for a_batchsize_imagedata,a_batchsize_labeldata in train_iter:
a_batchsize_labeldata_hat = net(a_batchsize_imagedata)
l = loss(a_batchsize_labeldata_hat,a_batchsize_labeldata).sum()
if optimizer is not None:
optimizer.zero_grad()
l.backward()
optimizer.step()
train_loss_sum += l.item()
train_acc_sum += (a_batchsize_labeldata_hat.argmax(dim=1)==a_batchsize_labeldata).sum().item()
n += a_batchsize_labeldata.shape[0]
test_acc = evaluate_accuracy(test_iter,net)
print("epcoch {:} loss {:.4f}% train_acc {:.4f}% test_acc {:.4f}%".format(epoch+1,train_loss_sum/n*100,train_acc_sum/n*100, test_acc*100))
num_inputs, num_outputs, num_hiddens = 784, 10 ,256
net = nn.Sequential(
FlattenLayer(),
nn.Linear(num_inputs,num_hiddens),
nn.ReLU(),
nn.Linear(num_hiddens,num_outputs)
)
for params in net.parameters():
init.normal_(params,mean=0,std=0.01)
batchsize=256
train_iter, test_iter = load_data_fashion_mnist(batch_size=batchsize)
loss = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(params=net.parameters(),lr=0.8)
num_epochs= 10
train(num_epochs,train_iter,test_iter,net,loss,optimizer)
测试结果
epcoch 1 loss 0.7313% train_acc 58.3450% test_acc 67.6800% epcoch 2 loss 0.2502% train_acc 75.6733% test_acc 75.2200% epcoch 3 loss 0.2038% train_acc 80.6483% test_acc 79.9600% epcoch 4 loss 0.1696% train_acc 84.2433% test_acc 83.8300% epcoch 5 loss 0.1590% train_acc 85.0917% test_acc 82.2700% epcoch 6 loss 0.1482% train_acc 86.1000% test_acc 85.0800% epcoch 7 loss 0.1425% train_acc 86.5967% test_acc 82.5100% epcoch 8 loss 0.1367% train_acc 87.1317% test_acc 82.9800% epcoch 9 loss 0.1334% train_acc 87.3833% test_acc 85.7100% epcoch 10 loss 0.1292% train_acc 87.8100% test_acc 85.1500%
|