在前面的理論講解和網(wǎng)絡實現(xiàn)中,我們斷斷續(xù)續(xù)的學習了 Tensorflow 和 keras 兩個著名的深度學習框架。當然主要還是 Tensorflow,keras 的底層計算都是以 Tensorflow 為后端的。在正式進入下一環(huán)節(jié)的學習前,筆者先給 pytorch 入個門,至于系統(tǒng)的學習,還是需要依靠各種項目實戰(zhàn)來鍛煉。 
pytorch 是一款可以媲美于 Tensorflow 優(yōu)秀的深度學習計算框架,但又相比于 Tensorflow 在語法上更具備靈活性。pytorch 原生于一款小眾語言 lua,而后基于 python 版本后具備了強大的生命力。作為一款基于 python 的深度學習計算庫,pytorch 提供了高于 numpy 的強大的張量計算能力和兼具靈活度和速度的深度學習研究功能。 
下面筆者就以 pytorch 基本張量運算、自動求導機制和基于 LeNet-5 的訓練實例對 pytorch 進行一個快速的入門和上手。 torch 的張量運算 和學習 Tensorflow 中的張量 tensor 一樣,torch 的張量運算也可以理解為 numpy 科學計算的加強版。底層的計算邏輯基本一致,torch 張量的強大之處可以利用 GPU 來加速運算。 創(chuàng)建一個 2x3 的矩陣: x = torch.Tensor(2, 3)print(x)

獲取矩陣的大?。?/span> print(x.size())
torch.Size([2, 3]) 執(zhí)行張量運算: y = torch.rand(2, 3)print(x + y)

或者是提供一種指定輸出張量的運算語法: result = torch.Tensor(2, 3)torch.add(x, y, out = result)print(result)
當然 torch 也可以方便的與 numpy 數(shù)組進行轉換。 torch 張量轉為 numpy 數(shù)組: a = torch.ones(5).numpy()print(a)
[1. 1. 1. 1. 1.] numpy 數(shù)組轉為 torch 張量: import numpy as npprint(torch.from_numpy(np.ones(5)))

使用 .cuda 方法將 tensor 在 GPU 上運行: if torch.cuda.is_available(): x = x.cuda() y = y.cuda() x + y
由上述操作可見,torch 的張量運算和 numpy 一樣非常簡單,相較于 tensorflow 的張量運算要更加靈活。 自動求導 在神經(jīng)網(wǎng)絡的反向傳播中涉及了大量的求導運算,pytorch 中求導的核心計算模塊 autograd 可以幫助我們快速實現(xiàn)復雜的求導運算。而求導運算又是建立在 torch 變量 Variable 基礎之上的,Variable 對 torch 的 Tensor 進行了包裝,當神經(jīng)網(wǎng)絡的結構和前向計算完成后,可以方便的對變量調用 backward 方法執(zhí)行反向計算。 創(chuàng)建一個 Variable: from torch.autograd import Variablex = Variable(torch.ones(2, 3), requires_grad = True)print(x)

執(zhí)行梯度計算: y = x + 2z = y * y * 5out = z.mean()out.backward()print(x.grad)

需要注意的是 torch 計算梯度時關于目標變量的梯度計算的表達方式為 Variable.grad 。 基于 torch 的 LeNet-5 訓練 cifar10 LeNet-5 網(wǎng)絡我們在第 14 講的時候已經(jīng)對論文進行了詳細的解讀和實現(xiàn)。參看第 14 講的鏈接:深度學習筆記14:CNN經(jīng)典論文研讀之Le-Net5及其Tensorflow實現(xiàn) 數(shù)據(jù)準備和轉換: transform = transforms.Compose( [transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)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)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')
torchvision 是用來服務于 torch 包的,用于生成、轉換和準備預訓練模型。cifar10 數(shù)據(jù)集:

定義 LeNet-5 網(wǎng)絡結構: from torch.autograd import 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) 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) self.fc3 = nn.Linear(84, 10) def forward(self, x): x = self.pool(F.relu(self.conv1(x))) x = self.pool(F.relu(self.conv2(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 xnet = Net()
定義損失函數(shù)和優(yōu)化器: import torch.optim as optimcriterion = nn.CrossEntropyLoss()optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
訓練 LeNet-5: for epoch in range(5): running_loss = 0.0 for i, data in enumerate(trainloader, 0): # get input data inputs, labels = data # variable the data inputs, labels = Variable(inputs), Variable(labels) # gradients zeros optimizer.zero_grad() # forward + backward + optimize outputs = net(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step() # print model train info running_loss += loss.data[0] if i % 2000 == 1999: print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 2000)) running_loss = 0.0
print('Finished Training')

在測試集上展示訓練效果: import matplotlib.pyplot as plt def imshow(img): img = img / 2 + 0.5 npimg = img.numpy() plt.imshow(np.transpose(npimg, (1, 2, 0))) # test the net on test datasets# ground truth dataiter = iter(testloader)images, labels = dataiter.next() # print image imshow(torchvision.utils.make_grid(images))print('GroundTruth: ', ' '.join('%5s' % classes[labels[j]] for j in range(4)))

看看 LeNet-5 的預測結果:
# the net predict result outputs = net(Variable(images))_, predicted = torch.max(outputs.data, 1)print('Predicted: ', ' '.join('%5s' % classes[predicted[j]] for j in range(4)))
(‘Predicted: ‘, ‘ cat ship ship plane’) 貌似訓練效果很好。再來看一下模型在全部測試集上的表現(xiàn): # test on all test data 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))
Accuracy of the network on the 10000 test images: 61 % 準確率達到 61%,已經(jīng)遠超隨機猜測的10%的準確率了。 再看看模型在每一類別上的分類準確率的表現(xiàn): class_correct = list(0. for i in range(10))class_total = list(0. for i in range(10)) for data in testloader: images, labels = data outputs = net(Variable(images)) _, predicted = torch.max(outputs.data, 1) c = (predicted == labels).squeeze() for i in range(4): label = labels[i] class_correct[label] += c[i] class_total[label] += 1
for i in range(10): print('Accuracy of %5s : %2d %%' % ( classes[i], 100 * class_correct[i] / class_total[i]))

可見模型在貓和鳥等小型動物上分類效果較差,在車船飛機等大型物體上效果較好。該實例來自于 pytorch 的官方 tutorial 60 分鐘快速上手文檔,能夠讓大家非??焖俚娜腴T學習 pytorch 。 在對神經(jīng)網(wǎng)絡的基本原理有深刻理解的基礎上,對比之前的 tensorflow 和 keras,相信大家都能快速掌握 pytorch 。 參考資料: http://pytorch.org/tutorials/ http://pytorch./cn/tutorials/
|