pyTorch——訓練第一個分類器要點解讀
網路構建
資料載入
* 引入函式庫
import torch
import torchvision
import torchvision.transforms as transforms
*將讀入的資料進行轉化:
transform = transforms.Compose(
[transforms.ToTensor(), ***range [0, 255] -> [0.0,1.0]
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) *資料分佈歸一化到[-1,1]
*利用torch自帶的CIFAR10資料集載入訓練集
trainset = torchvision.datasets.CIFAR10(root=’./data’, train=True,
download=True, transform=transform)
*生成batch,其中:
*引數:
dataset:Dataset型別,從其中載入資料
batch_size:int,可選。每個batch載入多少樣本
shuffle:bool,可選。為True時表示每個epoch都對資料進行洗牌
sampler:Sampler,可選。從資料集中取樣樣本的方法。
num_workers:int,可選。載入資料時使用多少子程序。預設值為0,表示在主程序中載入資料。
collate_fn:callable,可選。
pin_memory:bool,可選
drop_last:bool,可選。True表示如果最後剩下不完全的batch,丟棄。False表示不丟棄。
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
shuffle=True, num_workers=2)
*載入測試集
testset = torchvision.datasets.CIFAR10(root=’./data’, train=False,
download=True, transform=transform)
*測試集batch
testloader = torch.utils.data.DataLoader(testset, batch_size=4,
shuffle=False, num_workers=2)
*定義類別
classes = (‘plane’, ‘car’, ‘bird’, ‘cat’,
‘deer’, ‘dog’, ‘frog’, ‘horse’, ‘ship’, ‘truck’)
*顯示一些訓練集中的圖片與標籤
import matplotlib.pyplot as plt
import numpy as np
def imshow(img):
img = img / 2 + 0.5 # unnormalize
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))
*# get some random training images
dataiter = iter(trainloader)
images, labels = dataiter.next()
*# show images
imshow(torchvision.utils.make_grid(images))
*# print labels
print(’ ‘.join(‘%5s’ % classes[labels[j]] for j in range(4)))
定義網路
from torch.autograd import Variable ***Variable是最核心的變數
import torch.nn as nn *神經網路庫
import torch.nn.functional as F
*定義網路單元
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 6, 5) //3 input image
// channel, 6 output channels
//5x5 square convolution
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
def forward(self, x):
//x --> conv1 --> relu --> pool -->x
x = self.pool(F.relu(self.conv1(x)))
//x --> conv2 --> relu -->pool --> x
x = self.pool(F.relu(self.conv2(x)))
//view函式將張量x變形成一維向量形式,總特徵數不變,為全連線層做準備
x = x.view(-1, 16 * 5 * 5)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
net = Net()
損失函式
***use a Classification Cross-Entropy loss and SGD with momentum
import torch.optim as optim
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
訓練過程
for epoch in range(2): #全部訓練集訓練兩次:epoch=[0,1]
running_loss = 0.0 #清空loss
for i, data in enumerate(trainloader, 0):
# get the inputs
inputs, labels = data #trainloader返回:id,image,labels
# 將inputs於labels裝進Variable中
#(autograd.Varible[data,grad,creator])
inputs, labels = Variable(inputs), Variable(labels)
# zero the parameter gradients
optimizer.zero_grad()
# forward + backward + optimize
outputs = net(inputs)
loss = criterion(outputs, labels)
#back ward to every variable recorded in Variable's grad
loss.backward()
optimizer.step() #do SGD
# print statistics
running_loss += loss.data[0]
if i % 2000 == 1999: # print every 2000 mini-batches
print('[%d, %5d] loss: %.3f' %
(epoch + 1, i + 1, running_loss / 2000))
running_loss = 0.0
print('Finished Training')
測試過程
dataiter = iter(testloader)
images, labels = dataiter.next()
# print images
imshow(torchvision.utils.make_grid(images))
print('GroundTruth: ', ' '.join('%5s' % classes[labels[j]] for j in range(4)))
outputs = net(Variable(images))
_, predicted = torch.max(outputs.data, 1)
print('Predicted: ', ' '.join('%5s' % classes[predicted[j]]
for j in range(4)))
***On the whole dataset
correct = 0
total = 0
for data in testloader:
images, labels = data
outputs = net(Variable(images))
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum()
print('Accuracy of the network on the 10000 test images: %d %%' % (
100 * correct / total))
在GPU上訓練
*將網路轉到GPU上
net.cuda()
*資料也要在GPU上
inputs, labels = Variable(inputs.cuda()), Variable(labels.cuda())