1. softmax
使最后输出结果是一个概率分布,和为1.
2. Cross Entropy
CrossEntropyLoss = LogSoftmax + NLLLoss
损失函数,其中已经包括了求softmax。
3. MNIST数据集分类
transform=transforms.Compose([transforms.ToTensor(),#Convert the PIL Image to Tensor.
transforms.Normalize((0.1307,),(0.3081,))])#The parameters are mean and std respectively.
Design model
import numpy as np
import torch
from torch.utils.data import DataLoader #For constructing DataLoader
from torchvision import transforms #For constructing DataLoader
from torchvision import datasets #For constructing DataLoader
import torch.nn.functional as F #For using function relu()
import torch.optim as optim #For constructing Optimizer
batch_size=64
transform=transforms.Compose([transforms.ToTensor(),#Convert the PIL Image to Tensor.
transforms.Normalize((0.1307,),(0.3081,))])#The parameters are mean and std respectively.
train_dataset = datasets.MNIST(root='../dataset/mnist',train=True,transform=transform,download=True)
test_dataset = datasets.MNIST(root='../dataset/mnist',train=False,transform=transform,download=True)
train_loader = DataLoader(dataset=train_dataset,batch_size=batch_size,shuffle=True)
test_loader = DataLoader(dataset=test_dataset,batch_size=batch_size,shuffle=False)
class Net(torch.nn.Module):
def __init__(self):
super(Net,self).__init__()
self.linear1=torch.nn.Linear(784,512)
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)#把每一张图片的像素都拼接起来(n张图*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)#最后一层就是10层,不做变换
model=Net()
criterion=torch.nn.CrossEntropyLoss()
optimizer=torch.optim.SGD(model.parameters(),lr=0.01,momentum=0.5)#SGD with momentum
def train(epoch):
running_loss=0.0
for batch_idex,data in enumerate(train_loader,0):
inputs,target=data
optimizer.zero_grad()
#forward + backward + update
outputs=model(inputs)
loss=criterion(outputs,target)
loss.backward()
optimizer.step()
running_loss+=loss.item()
if batch_idex%300==299:
print('[%d,%5d loss:%.3f]' % (epoch+1,batch_idex+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)
predicted=torch.argmax(outputs.data,dim=1)
total+=labels.size(0)
correct += (predicted == labels).sum().item()
print('Accuracy on test set: %d %%' % (100 * correct / total))
#print(correct)
if __name__ == '__main__':
for epoch in range(10):
train(epoch)
test()