目录
1.笔记
1.1 softmax分类器
分类器需要满足
因为指数函数都会大于0,所以有:
1.2 Loss函数
在pytorch中torch.nn.CrossEntropyLoss()里会将softmax和Loss合在一起,所以在结构的最后一层不需要激活函数。
LogSoftmax + NLLLoss == CrossEntropyLoss
1.3 手写识别数据集模型结构图
2.代码及结果
由于一直使用的是jupyter notebook进行,在写代码导入torchvision时发现不存在,但是之前已经下好pytorch了,后来发现原来之前一直在base环境下进行的,而我的pytorch安装在创建的虚拟环境中,后面安装了ipykernel包,创建时直接在有pytorch中的环境里进行。
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(),transforms.Normalize((0.1307,),(0.3081,))])
train_dataset = datasets.MNIST(root='../data/minist',train=True,download=True,transform=transform)#数据直接从网络下载到.py所在的上一级里创建了data/minist/MINIST里
train_loader = DataLoader(train_dataset,shuffle=True,batch_size=batch_size)
test_dataset = datasets.MNIST(root='../data/minist',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.l1 = torch.nn.Linear(784,512)#数据为28x28
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)#改变张量模型 -1表示不确定弄成几行,自动分,若是2x28x28会分成2x784
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)#最后一层的激活函数合在CELoss里
model = Net()
criterion = torch.nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(),lr=0.01,momentum=0.5)#momentum冲量
def train(epoch):
running_loss = 0.0
for batch_idx, data in enumerate(train_loader,0):
inputs, target = data
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs,target)
loss.backward()
optimizer.step()
#每循环三百次看一次loss
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)#输出所在行最大值为1,我们不关注最大的概率为多少(_表示),关注的是最大概率对应的是0-9中的几
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Accuracy on test set: %d %%' % (100 * correct / total))
if __name__ == '__mian__':#简单说,当前是主程序而不是被调用时运行下面代码,我在jupyter notebook中运行时没有加入这行代码
for epoch in range(10):
train(epoch)#训练十次
test()
结果:
[1, 300] loss: 2.196 [1, 600] loss: 0.855 [1, 900] loss: 0.426 Accuracy on test set: 89 % [2, 300] loss: 0.327 [2, 600] loss: 0.261 [2, 900] loss: 0.221 Accuracy on test set: 94 % [3, 300] loss: 0.193 [3, 600] loss: 0.168 [3, 900] loss: 0.155 Accuracy on test set: 95 % [4, 300] loss: 0.137 [4, 600] loss: 0.121 [4, 900] loss: 0.116 Accuracy on test set: 96 % [5, 300] loss: 0.097 [5, 600] loss: 0.096 [5, 900] loss: 0.094 Accuracy on test set: 96 % [6, 300] loss: 0.081 [6, 600] loss: 0.075 [6, 900] loss: 0.073 Accuracy on test set: 97 % [7, 300] loss: 0.062 [7, 600] loss: 0.062 [7, 900] loss: 0.064 Accuracy on test set: 97 % [8, 300] loss: 0.047 [8, 600] loss: 0.048 [8, 900] loss: 0.054 Accuracy on test set: 97 % [9, 300] loss: 0.040 [9, 600] loss: 0.039 [9, 900] loss: 0.043 Accuracy on test set: 97 % [10, 300] loss: 0.031 [10, 600] loss: 0.032 [10, 900] loss: 0.038 Accuracy on test set: 97 %