1.概念
softMax:概率分布,将每个项的值/累加的值=每个结果出现的概率值 NLLLoss:就是把输出与Label对应的那个值拿出来,再去掉负号,再求均值 CrossEntropyLoss:就是把Softmax–Log–NLLLoss合并成一步
2.代码实现
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
# prepare dataset
'''
Normalize:均值和方差
'''
batch_size = 64
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))]) # 归一化
train_dataset = datasets.MNIST(root='../dataset/mnist/', train=True, download=True, transform=transform)#trans转化
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)
# design model using class
class Net(torch.nn.Module):
def __init__(self):
super(Net, self).__init__()
self.l1 = torch.nn.Linear(784, 512)
self.l2 = torch.nn.Linear(512, 256)
self.l3 = torch.nn.Linear(256, 128)
self.l4 = torch.nn.Linear(128, 64)
self.l5 = torch.nn.Linear(64, 10)
def forward(self, x):
x = x.view(-1, 784) # 将(N,1,28,28)--->二维矩阵;-1自动求N N*784
x = F.relu(self.l1(x))
x = F.relu(self.l2(x))
x = F.relu(self.l3(x))
x = F.relu(self.l4(x))
return self.l5(x) # 最后一层不做激活,不进行非线性变换
model = Net()
# construct loss and optimizer
criterion = torch.nn.CrossEntropyLoss() # 交叉熵损失
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.5)# momentum冲量
# training cycle forward, backward, update
def train(epoch):
running_loss = 0.0
for batch_idx, data in enumerate(train_loader, 0):
# 获得一个批次的数据和标签
inputs, target = data
optimizer.zero_grad()
# 获得模型预测结果(64, 10)
outputs = model(inputs)
# 交叉熵代价函数outputs(64,10),target(64)
loss = criterion(outputs, target)
loss.backward()
optimizer.step()
running_loss += loss.item()
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()
3.结果展示
[1, 300] loss: 2.196
[1, 600] loss: 0.929
[1, 900] loss: 0.412
accuracy on test set: 88 %
[2, 300] loss: 0.312
[2, 600] loss: 0.271
[2, 900] loss: 0.232
accuracy on test set: 93 %
[3, 300] loss: 0.189
[3, 600] loss: 0.181
[3, 900] loss: 0.162
accuracy on test set: 95 %
[4, 300] loss: 0.136
[4, 600] loss: 0.128
[4, 900] loss: 0.122
accuracy on test set: 96 %
[5, 300] loss: 0.098
[5, 600] loss: 0.104
[5, 900] loss: 0.093
accuracy on test set: 96 %
[6, 300] loss: 0.080
[6, 600] loss: 0.077
[6, 900] loss: 0.077
accuracy on test set: 96 %
[7, 300] loss: 0.061
[7, 600] loss: 0.064
[7, 900] loss: 0.062
accuracy on test set: 97 %
[8, 300] loss: 0.050
[8, 600] loss: 0.055
[8, 900] loss: 0.051
accuracy on test set: 97 %
[9, 300] loss: 0.039
[9, 600] loss: 0.041
[9, 900] loss: 0.047
accuracy on test set: 97 %
[10, 300] loss: 0.036
[10, 600] loss: 0.033
[10, 900] loss: 0.034
accuracy on test set: 97 %
|