「免費學習 60+ 節公開課:投票頁面,點擊講師頭像」
作者 | Soumith Chintala
中文翻譯、注釋製作 | 黃海廣
配置環境:PyTorch 1.3,python 3.7,
主機:顯卡:一塊1080ti;內存:32g(註:絕大部分代碼不需要GPU)
目錄
一、Pytorch是什麼?
二、AUTOGRAD
三、神經網絡
四、訓練一個分類器
五、數據並行
一、PyTorch 是什麼
他是一個基於Python的科學計算包,目標用戶有兩類
為了使用GPU來替代numpy
一個深度學習研究平台:提供最大的靈活性和速度
開始
張量(Tensors)
張量類似於numpy的ndarrays,不同之處在於張量可以使用GPU來加快計算。
from __future__ import print_function
import torch
構建一個未初始化的5*3的矩陣:
x = torch.Tensor(5, 3)
print(x)
tensor([[ 0.0000e+00, 0.0000e+00, 1.3004e-42],
[ 0.0000e+00, 7.0065e-45, 0.0000e+00],
[-3.8593e+35, 7.8753e-43, 0.0000e+00],
[ 0.0000e+00, 1.8368e-40, 0.0000e+00],
[-3.8197e+35, 7.8753e-43, 0.0000e+00]])
構建一個零矩陣,使用long的類型
x = torch.zeros(5, 3, dtype=torch.long)
print(x)
tensor([[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
從數據中直接構建一個張量(tensor):
x = torch.tensor([5.5, 3])
print(x)
tensor([5.5000, 3.0000])
或者在已有的張量(tensor)中構建一個張量(tensor). 這些方法將重用輸入張量(tensor)的屬性,例如, dtype,除非用戶提供新值
x = x.new_ones(5, 3, dtype=torch.double) # new_* methods take in sizes
print(x)
x = torch.randn_like(x, dtype=torch.float) # 覆蓋類型!
print(x) # result 的size相同
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], dtype=torch.float64)
tensor([[ 1.1701, -0.8342, -0.6769],
[-1.3060, 0.3636, 0.6758],
[ 1.9133, 0.3494, 1.1412],
[ 0.9735, -0.9492, -0.3082],
[ 0.9469, -0.6815, -1.3808]])
獲取張量(tensor)的大小
print(x.size)
torch.Size([5, 3])
** 注意 **
torch.Size
實際上是一個元組,所以它支持元組的所有操作。
操作
張量上的操作有多重語法形式,下面我們以加法為例進行講解。
語法1
y = torch.rand(5, 3)
print(x + y)
tensor([[ 1.7199, -0.1819, -0.1543],
[-0.5413, 1.1591, 1.4098],
[ 2.0421, 0.5578, 2.0645],
[ 1.7301, -0.3236, 0.4616],
[ 1.2805, -0.4026, -0.6916]])
語法二
print(torch.add(x, y))
tensor([[ 1.7199, -0.1819, -0.1543],
[-0.5413, 1.1591, 1.4098],
[ 2.0421, 0.5578, 2.0645],
[ 1.7301, -0.3236, 0.4616],
[ 1.2805, -0.4026, -0.6916]])
語法三:
給出一個輸出張量作為參數
result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)
tensor([[ 1.7199, -0.1819, -0.1543],
[-0.5413, 1.1591, 1.4098],
[ 2.0421, 0.5578, 2.0645],
[ 1.7301, -0.3236, 0.4616],
[ 1.2805, -0.4026, -0.6916]])
語法四:
原地操作(in-place)
# 把x加到y上
y.add_(x)
print(y)
tensor([[ 1.7199, -0.1819, -0.1543],
[-0.5413, 1.1591, 1.4098],
[ 2.0421, 0.5578, 2.0645],
[ 1.7301, -0.3236, 0.4616],
[ 1.2805, -0.4026, -0.6916]])
注意
任何在原地(in-place)改變張量的操作都有一個_
後綴。例如x.copy_(y)
,x.t_
操作將改變x.
你可以使用所有的numpy
索引操作。
你可以使用各種類似標準NumPy的花哨的索引功能
print(x[:, 1])
tensor([-0.8342, 0.3636, 0.3494, -0.9492, -0.6815])
調整大小:如果要調整張量/重塑張量,可以使用torch.view
:
x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8) # -1的意思是沒有指定維度
print(x.size, y.size, z.size)
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
如果你有一個單元素張量,使用.item
將值作為Python數字
x = torch.randn(1)
print(x)
print(x.item)
tensor([0.3441])
0.34412217140197754
稍後閱讀
這裡:(https://pytorch.org/docs/stable/torch.html)
描述了一百多種張量操作,包括轉置,索引,數學運算,線性代數,隨機數等。
Numpy橋
把一個torch張量轉換為numpy數組或者反過來都是很簡單的。
Torch張量和numpy數組將共享潛在的內存,改變其中一個也將改變另一個。
把Torch張量轉換為numpy數組
a = torch.ones(5)
print(a)
tensor([1., 1., 1., 1., 1.])
b = a.numpy
print(b)
print(type(b))
[ 1. 1. 1. 1. 1.]
通過如下操作,我們看一下numpy數組的值如何在改變。
a.add_(1)
print(a)
print(b)
tensor([2., 2., 2., 2., 2.])
[ 2. 2. 2. 2. 2.]
把numpy數組轉換為torch張量
看看改變numpy數組如何自動改變torch張量。
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print(a)
print(b)
[ 2. 2. 2. 2. 2.]
tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
所有在CPU上的張量,除了字符張量,都支持在numpy之間轉換。
CUDA張量
可以使用.to方法將張量移動到任何設備上。
# let us run this cell only if CUDA is available
# We will use ``torch.device`` objects to move tensors in and out of GPU
if torch.cuda.is_available:
device = torch.device("cuda") # a CUDA device object
y = torch.ones_like(x, device=device) # directly create a tensor on GPU
x = x.to(device) # or just use strings ``.to("cuda")``
z = x + y
print(z)
print(z.to("cpu", torch.double)) # ``.to`` can also change dtype together!
tensor([1.3441], device='cuda:0')
tensor([1.3441], dtype=torch.float64)
本章的官方代碼:
Jupyter notebook:
https://pytorch.org/tutorials/_downloads/3c2b25b8a9f72db7780a6bf9b5fc9f62/tensor_tutorial.ipynb
二、Autograd: 自動求導(automatic differentiation)
PyTorch 中所有神經網絡的核心是autograd
包.我們首先簡單介紹一下這個包,然後訓練我們的第一個神經網絡.
autograd
包為張量上的所有操作提供了自動求導.它是一個運行時定義的框架,這意味著反向傳播是根據你的代碼如何運行來定義,並且每次疊代可以不同.
接下來我們用一些簡單的示例來看這個包:
張量(Tensor)
torch.Tensor
是包的核心類。如果將其屬性.requires_grad
設置為True,則會開始跟蹤其上的所有操作。完成計算後,您可以調用.backward
並自動計算所有梯度。此張量的梯度將累積到.grad
屬性中。
要阻止張量跟蹤歷史記錄,可以調用.detach
將其從計算歷史記錄中分離出來,並防止將來的計算被跟蹤。
要防止跟蹤歷史記錄(和使用內存),您還可以使用torch.no_grad包裝代碼塊:在評估模型時,這可能特別有用,因為模型可能具有requires_grad = True
的可訓練參數,但我們不需要梯度。
還有一個類對於autograd實現非常重要 - Function。
Tensor和Function互相連接並構建一個非循環圖構建一個完整的計算過程。每個張量都有一個.grad_fn
屬性,該屬性引用已創建Tensor的Function(除了用戶創建的Tensors - 它們的grad_fn
為None
)。
如果要計算導數,可以在Tensor上調用.backward
。如果Tensor是標量(即它包含一個元素數據),則不需要為backward
指定任何參數,但是如果它有更多元素,則需要指定一個梯度參數,該參數是匹配形狀的張量。
import torch
創建一個張量並設置requires_grad = True
以跟蹤它的計算
x = torch.ones(2, 2, requires_grad=True)
print(x)
tensor([[1., 1.],
[1., 1.]], requires_grad=True)
在張量上執行操作:
y = x + 2
print(y)
tensor([[3., 3.],
[3., 3.]], grad_fn=
)
因為y是通過一個操作創建的,所以它有grad_fn,而x是由用戶創建,所以它的grad_fn為None.
print(y.grad_fn)
print(x.grad_fn)
None
在y上執行操作
z = y * y * 3
out = z.mean
print(z, out)
tensor([[27., 27.],
[27., 27.]], grad_fn=
) tensor(27., grad_fn= )
.requires\\_grad_(...)
就地更改現有的Tensor的requires_grad
標誌。如果沒有給出,輸入標誌默認為False。
a = torch.randn(2, 2)
a = ((a * 3) / (a - 1))
print(a.requires_grad)
a.requires_grad_(True)
print(a.requires_grad)
b = (a * a).sum
print(b.grad_fn)
False
True
梯度(Gradients)
現在我們來執行反向傳播,out.backward
相當於執行out.backward(torch.tensor(1.))
out.backward
輸出out對x的梯度d(out)/dx:
print(x.grad)
tensor([[4.5000, 4.5000],
[4.5000, 4.5000]])
你應該得到一個值全為4.5的矩陣,我們把張量out稱為"". 則:, ,並且 ,所以,, 因此 在數學上,如果你有一個向量值函數 ,則相對於的梯度是雅可比矩陣:
一般來說,torch.autograd是一個計算雅可比向量積的引擎。也就是說,給定任何向量,計算乘積。如果恰好是標量函數的梯度,即 然後根據鏈式法則,雅可比向量乘積將是相對於的梯度
雅可比向量積的這種特性使得將外部梯度饋送到具有非標量輸出的模型中非常方便。
現在讓我們來看一個雅可比向量積的例子:
x = torch.randn(3, requires_grad=True)
y = x * 2
while y.data.norm < 1000:
y = y * 2
print(y)
tensor([ 384.5854, -13.6405, -1049.2870], grad_fn=
)
現在在這種情況下,y不再是標量。torch.autograd
無法直接計算完整雅可比行列式,但如果我們只想要雅可比向量積,只需將向量作為參數向後傳遞:
v = torch.tensor([0.1, 1.0, 0.0001], dtype=torch.float)
y.backward(v)
print(x.grad)
tensor([5.1200e+01, 5.1200e+02, 5.1200e-02])
您還可以通過torch.no_grad代碼,在張量上使用.requires_grad = True來停止使用跟蹤歷史記錄。
print(x.requires_grad)
print((x ** 2).requires_grad)
with torch.no_grad:
print((x ** 2).requires_grad)
True
True
False
關於autograd
和Function
的文檔在http://pytorch.org/docs/autograd
本章的官方代碼:
Jupyter notebook:
https://pytorch.org/tutorials/_downloads/009cea8b0f40dfcb55e3280f73b06cc2/autograd_tutorial.ipynb
三、神經網絡
可以使用torch.nn
包來構建神經網絡.
你已知道autograd
包,nn
包依賴autograd
包來定義模型並求導.一個nn.Module
包含各個層和一個forward(input)
方法,該方法返回output
.
例如,我們來看一下下面這個分類數字圖像的網絡.
它是一個簡單的前饋神經網絡,它接受一個輸入,然後一層接著一層的輸入,直到最後得到結果。
神經網絡的典型訓練過程如下:
定義神經網絡模型,它有一些可學習的參數(或者權重);
在數據集上疊代;
通過神經網絡處理輸入;
計算損失(輸出結果和正確值的差距大小)
將梯度反向傳播會網絡的參數;
更新網絡的參數,主要使用如下簡單的更新原則:
weight = weight - learning_rate * gradient
定義網絡
我們先定義一個網絡:
import torch
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__
# 1 input image channel, 6 output channels, 5x5 square convolution
# kernel
self.conv1 = nn.Conv2d(1, 6, 5)
self.conv2 = nn.Conv2d(6, 16, 5)
# an affine operation: y = Wx + b
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
# Max pooling over a (2, 2) window
x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
# If the size is a square you can only specify a single number
x = F.max_pool2d(F.relu(self.conv2(x)), 2)
x = x.view(-1, self.num_flat_features(x))
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
def num_flat_features(self, x):
size = x.size[1:] # all dimensions except the batch dimension
num_features = 1
for s in size:
num_features *= s
return num_features
net = Net
print(net)
Net(
(conv1): Conv2d(1, 6, kernel_size=(5, 5), stride=(1, 1))
(conv2): Conv2d(6, 16, kernel_size=(5, 5), stride=(1, 1))
(fc1): Linear(in_features=400, out_features=120, bias=True)
(fc2): Linear(in_features=120, out_features=84, bias=True)
(fc3): Linear(in_features=84, out_features=10, bias=True)
)
你只需定義forward
函數,backward
函數(計算梯度)在使用autograd
時自動為你創建.你可以在forward
函數中使用Tensor
的任何操作。
net.parameters
返回模型需要學習的參數。
params = list(net.parameters)
print(len(params))
print(params[0].size)
10
torch.Size([6, 1, 5, 5])
forward
的輸入和輸出都是autograd.Variable
.注意:這個網絡(LeNet)期望的輸入大小是32*32.如果使用MNIST數據集來訓練這個網絡,請把圖片大小重新調整到32*32.
input = torch.randn(1, 1, 32, 32)
out = net(input)
print(out)
tensor([[-0.1217, 0.0449, -0.0392, -0.1103, -0.0534, -0.1108, -0.0565, 0.0116,
0.0867, 0.0102]], grad_fn=
)
將所有參數的梯度緩存清零,然後進行隨機梯度的的反向傳播.
net.zero_grad
out.backward(torch.randn(1, 10))
注意
torch.nn
只支持小批量輸入,整個torch.nn
包都只支持小批量樣本,而不支持單個樣本例如,
nn.Conv2d
將接受一個4維的張量,每一維分別是(樣本數*通道數*高*寬).如果你有單個樣本,只需使用
input.unsqueeze(0)
來添加其它的維數.
在繼續之前,我們回顧一下到目前為止見過的所有類.
回顧
torch.Tensor
-支持自動編程操作(如backward()
)的多維數組。同時保持梯度的張量。nn.Module
-神經網絡模塊.封裝參數,移動到GPU上運行,導出,加載等nn.Parameter
-一種張量,當把它賦值給一個Module
時,被自動的註冊為參數.autograd.Function
-實現一個自動求導操作的前向和反向定義, 每個張量操作都會創建至少一個Function
節點,該節點連接到創建張量並對其歷史進行編碼的函數。
現在,我們包含了如下內容:
定義一個神經網絡
處理輸入和調用
backward
剩下的內容:
計算損失值
更新神經網絡的權值
損失函數
一個損失函數接受一對(output, target)作為輸入(output為網絡的輸出,target為實際值),計算一個值來估計網絡的輸出和目標值相差多少。
在nn包中有幾種不同的損失函數.一個簡單的損失函數是:nn.MSELoss
,它計算輸入和目標之間的均方誤差。
例如:
output = net(input)
target = torch.randn(10) # a dummy target, for example
target = target.view(1, -1) # make it the same shape as output
criterion = nn.MSELoss
loss = criterion(output, target)
print(loss)
tensor(0.5663, grad_fn=
)
現在,你反向跟蹤loss
,使用它的.grad_fn
屬性,你會看到向下面這樣的一個計算圖:
input -> conv2d -> relu -> maxpool2d -> conv2d -> relu -> maxpool2d -> view -> linear -> relu -> linear -> relu -> linear -> MSELoss -> loss
所以, 當你調用loss.backward
,整個圖被區分為損失以及圖中所有具有requires_grad = True
的張量,並且其.grad
張量的梯度累積。
為了說明,我們反向跟蹤幾步:
print(loss.grad_fn) # MSELoss
print(loss.grad_fn.next_functions[0][0]) # Linear
print(loss.grad_fn.next_functions[0][0].next_functions[0][0])
反向傳播
為了反向傳播誤差,我們所需做的是調用loss.backward
.你需要清除已存在的梯度,否則梯度將被累加到已存在的梯度。
現在,我們將調用loss.backward
,並查看conv1層的偏置項在反向傳播前後的梯度。
net.zero_grad # zeroes the gradient buffers of all parameters
print('conv1.bias.grad before backward')
print(net.conv1.bias.grad)
loss.backward
print('conv1.bias.grad after backward')
print(net.conv1.bias.grad)
conv1.bias.grad before backward
tensor([0., 0., 0., 0., 0., 0.])
conv1.bias.grad after backward
tensor([ 0.0006, -0.0164, 0.0122, -0.0060, -0.0056, -0.0052])
稍後閱讀:
神經網絡包包含了各種用來構成深度神經網絡構建塊的模塊和損失函數,一份完整的文檔查看[這裡]:(https://pytorch.org/docs/nn)
唯一剩下的內容:
更新網絡的權重
更新權重
實踐中最簡單的更新規則是隨機梯度下降(SGD).
weight=weight−learning_rate∗gradient
我們可以使用簡單的Python代碼實現這個規則。
learning_rate = 0.01
for f in net.parameters:
f.data.sub_(f.grad.data * learning_rate)
然而,當你使用神經網絡是,你想要使用各種不同的更新規則,比如SGD,Nesterov-SGD
,Adam
,RMSPROP
等.為了能做到這一點,我們構建了一個包torch.optim
實現了所有的這些規則.使用他們非常簡單:
import torch.optim as optim
# create your optimizer
optimizer = optim.SGD(net.parameters, lr=0.01)
# in your training loop:
optimizer.zero_grad # zero the gradient buffers
output = net(input)
loss = criterion(output, target)
loss.backward
optimizer.step # Does the update
注意
觀察如何使用optimizer.zero_grad
手動將梯度緩衝區設置為零。這是因為梯度是反向傳播部分中的說明那樣是累積的。
本章的官方代碼:
Jupyter notebook:
https://pytorch.org/tutorials/_downloads/97abb4c06a586d45ef3fc4b4b9634406/neural_networks_tutorial.ipynb
四、訓練一個分類器
你已經學會如何去定義一個神經網絡,計算損失值和更新網絡的權重。
你現在可能在思考:數據哪裡來呢?
關於數據
通常,當你處理圖像,文本,音頻和視頻數據時,你可以使用標準的Python包來加載數據到一個numpy數組中.然後把這個數組轉換成torch.*Tensor
。
對於圖像,有諸如Pillow,OpenCV包等非常實用
對於音頻,有諸如scipy和librosa包
對於文本,可以用原始Python和Cython來加載,或者使用NLTK和SpaCy 對於視覺,我們創建了一個
torchvision
包,包含常見數據集的數據加載,比如Imagenet,CIFAR10,MNIST等,和圖像轉換器,也就是torchvision.datasets
和torch.utils.data.DataLoader
。
這提供了巨大的便利,也避免了代碼的重複。
在這個教程中,我們使用CIFAR10數據集,它有如下10個類別:』airplane』,』automobile』,』bird』,』cat』,』deer』,』dog』,』frog』,』horse』,’ship』,』truck』。這個數據集中的圖像大小為3*32*32,即,3通道,32*32像素。
訓練一個圖像分類器
我們將按照下列順序進行:
使用
torchvision
加載和歸一化CIFAR10訓練集和測試集.定義一個卷積神經網絡
定義損失函數
在訓練集上訓練網絡
在測試集上測試網絡
1. 加載和歸一化CIFAR0
使用torchvision
加載CIFAR10是非常容易的。
import torch
import torchvision
import torchvision.transforms as transforms
torchvision的輸出是[0,1]的PILImage圖像,我們把它轉換為歸一化範圍為[-1, 1]的張量。
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')
#這個過程有點慢,會下載大約340mb圖片數據。
Files already downloaded and verified
Files already downloaded and verified
我們展示一些有趣的訓練圖像。
import matplotlib.pyplot as plt
import numpy as np
# functions to show an image
def imshow(img):
img = img / 2 + 0.5 # unnormalize
npimg = img.numpy
plt.imshow(np.transpose(npimg, (1, 2, 0)))
plt.show
# 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)))
plane deer dog plane
2. 定義一個卷積神經網絡
從之前的神經網絡一節複製神經網絡代碼,並修改為接受3通道圖像取代之前的接受單通道圖像。
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 x
net = Net
3. 定義損失函數和優化器
我們使用交叉熵作為損失函數,使用帶動量的隨機梯度下降。
import torch.optim as optim
criterion = nn.CrossEntropyLoss
optimizer = optim.SGD(net.parameters, lr=0.001, momentum=0.9)
4. 訓練網絡
這是開始有趣的時刻,我們只需在數據疊代器上循環,把數據輸入給網絡,並優化。
for epoch in range(2): # loop over the dataset multiple times
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
# get the inputs
inputs, labels = data
# zero the parameter gradients
optimizer.zero_grad
# forward + backward + optimize
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward
optimizer.step
# print statistics
running_loss += loss.item
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')
[1, 2000] loss: 2.286
[1, 4000] loss: 1.921
[1, 6000] loss: 1.709
[1, 8000] loss: 1.618
[1, 10000] loss: 1.548
[1, 12000] loss: 1.496
[2, 2000] loss: 1.435
[2, 4000] loss: 1.409
[2, 6000] loss: 1.373
[2, 8000] loss: 1.348
[2, 10000] loss: 1.326
[2, 12000] loss: 1.313
Finished Training
5. 在測試集上測試網絡
我們在整個訓練集上訓練了兩次網絡,但是我們還需要檢查網絡是否從數據集中學習到東西。
我們通過預測神經網絡輸出的類別標籤並根據實際情況進行檢測,如果預測正確,我們把該樣本添加到正確預測列表。
第一步,顯示測試集中的圖片一遍熟悉圖片內容。
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)))
GroundTruth: cat ship ship plane
現在我們來看看神經網絡認為以上圖片是什麼?
outputs = net(images)
輸出是10個標籤的機率。一個類別的機率越大,神經網絡越認為他是這個類別。所以讓我們得到最高機率的標籤。
_, predicted = torch.max(outputs, 1)
print('Predicted: ', ' '.join('%5s' % classes[predicted[j]]
for j in range(4)))
Predicted: cat ship ship plane
這結果看起來非常的好。
接下來讓我們看看網絡在整個測試集上的結果如何。
correct = 0
total = 0
with torch.no_grad:
for data in testloader:
images, labels = data
outputs = net(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum.item
print('Accuracy of the network on the 10000 test images: %d %%' % (
100 * correct / total))
Accuracy of the network on the 10000 test images: 54 %
結果看起來好於偶然,偶然的正確率為10%,似乎網絡學習到了一些東西。
那在什麼類上預測較好,什麼類預測結果不好呢?
class_correct = list(0. for i in range(10))
class_total = list(0. for i in range(10))
with torch.no_grad:
for data in testloader:
images, labels = data
outputs = net(images)
_, predicted = torch.max(outputs, 1)
c = (predicted == labels).squeeze
for i in range(4):
label = labels[i]
class_correct[label] += c[i].item
class_total[label] += 1
for i in range(10):
print('Accuracy of %5s : %2d %%' % (
classes[i], 100 * class_correct[i] / class_total[i]))
Accuracy of plane : 52 %
Accuracy of car : 63 %
Accuracy of bird : 43 %
Accuracy of cat : 33 %
Accuracy of deer : 36 %
Accuracy of dog : 46 %
Accuracy of frog : 68 %
Accuracy of horse : 62 %
Accuracy of ship : 80 %
Accuracy of truck : 63 %
接下來幹什麼?
我們如何在GPU上運行神經網絡呢?
在GPU上訓練
你是如何把一個Tensor轉換GPU上,你就如何把一個神經網絡移動到GPU上訓練。這個操作會遞歸遍歷有所模塊,並將其參數和緩衝區轉換為CUDA張量。
device = torch.device("cuda:0" if torch.cuda.is_available else "cpu")
# Assume that we are on a CUDA machine, then this should print a CUDA device:
#假設我們有一台CUDA的機器,這個操作將顯示CUDA設備。
print(device)
cuda:0
接下來假設我們有一台CUDA的機器,然後這些方法將遞歸遍歷所有模塊並將其參數和緩衝區轉換為CUDA張量:
net.to(device)
Net(
(conv1): Conv2d(3, 6, kernel_size=(5, 5), stride=(1, 1))
(pool): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(conv2): Conv2d(6, 16, kernel_size=(5, 5), stride=(1, 1))
(fc1): Linear(in_features=400, out_features=120, bias=True)
(fc2): Linear(in_features=120, out_features=84, bias=True)
(fc3): Linear(in_features=84, out_features=10, bias=True)
)
請記住,你也必須在每一步中把你的輸入和目標值轉換到GPU上:
inputs, labels = inputs.to(device), labels.to(device)
為什麼我們沒注意到GPU的速度提升很多?那是因為網絡非常的小。
實踐:
嘗試增加你的網絡的寬度(第一個nn.Conv2d
的第2個參數, 第二個nn.Conv2d
的第一個參數,他們需要是相同的數字),看看你得到了什麼樣的加速。
實現的目標:
深入了解了PyTorch的張量庫和神經網絡
訓練了一個小網絡來分類圖片
本章的官方代碼:
Jupyter notebook:
https://pytorch.org/tutorials/_downloads/17a7c7cb80916fcdf921097825a0f562/cifar10_tutorial.ipynb
五、數據並行(選讀)
作者:Sung Kim和Jenny Kang
在這個教程里,我們將學習如何使用數據並行(DataParallel)來使用多GPU。
PyTorch非常容易的就可以使用GPU,你可以用如下方式把一個模型放到GPU上:
device = torch.device("cuda:0")
model.to(device)
然後你可以複製所有的張量到GPU上:
mytensor = my_tensor.to(device)
請注意,只調用mytensor.gpu
並沒有複製張量到GPU上。你需要把它賦值給一個新的張量並在GPU上使用這個張量。
在多GPU上執行前向和反向傳播是自然而然的事。然而,PyTorch默認將只是用一個GPU。你可以使用DataParallel
讓模型並行運行來輕易的讓你的操作在多個GPU上運行。
model = nn.DataParallel(model)
這是這篇教程背後的核心,我們接下來將更詳細的介紹它。
導入和參數
導入PyTorch模塊和定義參數。
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
# Parameters and DataLoaders
input_size = 5
output_size = 2
batch_size = 30
data_size = 100
設備:
device = torch.device("cuda:0" if torch.cuda.is_available else "cpu")
虛擬數據集
製作一個虛擬(隨機)數據集,你只需實現__getitem__
。
class RandomDataset(Dataset):
def __init__(self, size, length):
self.len = length
self.data = torch.randn(length, size)
def __getitem__(self, index):
return self.data[index]
def __len__(self):
return self.len
rand_loader = DataLoader(dataset=RandomDataset(input_size, data_size),
batch_size=batch_size, shuffle=True)
簡單模型
作為演示,我們的模型只接受一個輸入,執行一個線性操作,然後得到結果。然而,你能在任何模型(CNN,RNN,Capsule Net等)上使用DataParallel
。
我們在模型內部放置了一條列印語句來檢測輸入和輸出向量的大小。請注意批等級為0時列印的內容。
class Model(nn.Module):
# Our model
def __init__(self, input_size, output_size):
super(Model, self).__init__
self.fc = nn.Linear(input_size, output_size)
def forward(self, input):
output = self.fc(input)
print("\\tIn Model: input size", input.size,
"output size", output.size)
return output
創建一個模型和數據並行
這是本教程的核心部分。首先,我們需要創建一個模型實例和檢測我們是否有多個GPU。如果我們有多個GPU,我們使用nn.DataParallel
來包裝我們的模型。然後通過model.to(device)
把模型放到GPU上。
model = Model(input_size, output_size)
if torch.cuda.device_count > 1:
print("Let's use", torch.cuda.device_count, "GPUs!")
# dim = 0 [30, xxx] -> [10, ...], [10, ...], [10, ...] on 3 GPUs
model = nn.DataParallel(model)
model.to(device)
Model(
(fc): Linear(in_features=5, out_features=2, bias=True)
)
運行模型
現在我們可以看輸入和輸出張量的大小。
for data in rand_loader:
input = data.to(device)
output = model(input)
print("Outside: input size", input.size,
"output_size", output.size)
In Model: input size torch.Size([30, 5]) output size torch.Size([30, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
In Model: input size torch.Size([30, 5]) output size torch.Size([30, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
In Model: input size torch.Size([30, 5]) output size torch.Size([30, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
Outside: input size torch.Size([10, 5]) output_size torch.Size([10, 2])
結果
當我們對30個輸入和輸出進行批處理時,我們和期望的一樣得到30個輸入和30個輸出,但是如果你有多個GPU,你得到如下的結果。
2個GPU
如果你有2個GPU,你將看到:
3個GPU:
如果你有3個GPU,你將看到:
8個GPU:
如果你有8個GPU,你將看到:
總結
DataParallel
自動的劃分數據,並將作業發送到多個GPU上的多個模型。在每個模型完成作業後,DataParallel
收集併合並結果返回給你。
更多信息請看這裡:
http://pytorch.org/tutorials/beginner/former_torchies/parallelism_tutorial.html
本章的官方代碼:
Jupyter notebook:
https://pytorch.org/tutorials/_downloads/63ecfdf27b96977f3a89015f60065a2b/data_parallel_tutorial.ipynb
翻譯版本github下載:
https://github.com/fengdu78/machine_learning_beginner/tree/master/PyTorch_beginner
原文連結:
https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html
(*本文為AI科技大本營轉載文章,轉載請聯繫作者)
所有CSDN 用戶都可參與投票和抽獎活動
加入福利群,每周還有精選學習資料、技術圖書等福利發送