前言
本文參考PyTorch官網(wǎng)的教程,分為五個(gè)基本模塊來(lái)介紹PyTorch。為了避免文章過(guò)長(zhǎng),這五個(gè)模塊分別在五篇博文中介紹。
Part1:PyTorch簡(jiǎn)單知識(shí)
Part2:PyTorch的自動(dòng)梯度計(jì)算
Part3:使用PyTorch構(gòu)建一個(gè)神經(jīng)網(wǎng)絡(luò)
Part4:訓(xùn)練一個(gè)神經(jīng)網(wǎng)絡(luò)分類(lèi)器
Part5:數(shù)據(jù)并行化
本文是關(guān)于Part5的內(nèi)容。
Part5:數(shù)據(jù)并行化
本文中,將會(huì)講到DataParallel使用多GPU。
在PyTorch中使用GPU比較簡(jiǎn)單,可以這樣把模型放到GPU上。
model.gpu()
還可以復(fù)制所有的tensors到GPU上。
mytensor = my_tensor.gpu()
請(qǐng)注意,單純調(diào)用mytensor.gpu()不會(huì)拷貝tensor到GPU上。你需要把它分配給一個(gè)新的tensor,然后在GPU上使用這個(gè)新的tensor。
前向和反向傳播可以在多個(gè)GPU上運(yùn)行。但是,PyTorch默認(rèn)只使用一個(gè)GPU。你可以使用DataParallel使得你的模型可以在過(guò)個(gè)GPU上并行運(yùn)算。
model = nn.DataParallel(model)
1 Package導(dǎo)入和參數(shù)設(shè)置
導(dǎo)入PyTorch的模塊并且設(shè)置參數(shù)。
2 虛擬數(shù)據(jù)集
制作虛擬(隨機(jī))數(shù)據(jù)集,只需要執(zhí)行g(shù)etitem。
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, 100), batch_size=batch_size, shuffle=True)
3 簡(jiǎn)單模型
作為實(shí)例,我們的模型只是獲取輸入,進(jìn)行線性運(yùn)算,給出結(jié)果。但是,你可以把DataParallel應(yīng)用到任何模型(CNN,RNN,Capsule Net 等等)。
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(" In Model: input size", input.size(), "output size", output.size()) return output
4 創(chuàng)建模型和數(shù)據(jù)并行
這是本篇教程的核心內(nèi)容。我們需要制作一個(gè)模型實(shí)例,并檢查是否有多個(gè)GPU。如果有多GPU,可以使用nn.DataParallel打包我們的model。之后,我們可以把利用model.gpu()把模型放到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) if torch.cuda.is_available(): model.cuda()
5 運(yùn)行模型
for data in rand_loader: if torch.cuda.is_available(): input_var = Variable(data.cuda()) else: input_var = Variable(data) output = model(input_var) print("Outside: input size", input_var.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])
6 結(jié)果
(1)如果有2 GPUs,可以看到
# on 2 GPUs Let's use 2 GPUs! In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2]) In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2]) Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2]) In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2]) In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2]) Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2]) In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2]) In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2]) Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2]) In Model: input size torch.Size([5, 5]) output size torch.Size([5, 2]) In Model: input size torch.Size([5, 5]) output size torch.Size([5, 2]) Outside: input size torch.Size([10, 5]) output_size torch.Size([10, 2])
(2)如果有3 GPUs,可以看到
Let's use 3 GPUs! In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) In Model: input size torch.Size([10, 5]) output size torch.Size([10, 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]) In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) In Model: input size torch.Size([10, 5]) output size torch.Size([10, 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]) In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2]) Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2]) Outside: input size torch.Size([10, 5]) output_size torch.Size([10, 2])
(3)如果有8 GPUs,可以看到
Let's use 8 GPUs! In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2]) In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2]) Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2]) In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2]) In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2]) In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2]) In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2]) In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2]) Outside: input size torch.Size([10, 5]) output_size torch.Size([10, 2])
7 總結(jié)
DataParallel將數(shù)據(jù)自動(dòng)分割送到不同的GPU上處理,在每個(gè)模塊完成工作后,DataParallel再收集整合這些結(jié)果返回。
責(zé)任編輯:xj
-
gpu
+關(guān)注
關(guān)注
28文章
4673瀏覽量
128593 -
Data
+關(guān)注
關(guān)注
0文章
62瀏覽量
38225 -
深度學(xué)習(xí)
+關(guān)注
關(guān)注
73文章
5463瀏覽量
120890 -
pytorch
+關(guān)注
關(guān)注
2文章
802瀏覽量
13115
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論