深度學習之pytorch(二) 數據并行

又是好久沒更新博客,最近瑣事纏身,寫文檔寫到吐。沒時間學習新的知識,剛空閑下來立刻就學習之前忘得差不多得Pytorch。Pytorch和tensorflow差不多,具體得就不多啰嗦了,覺得還有疑問的童鞋可以自行去官網學習,官網網址包括:

1.https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html

2.https://pytorch-cn.readthedocs.io/zh/latest/package_references/torch-nn/#class-torchnndataparallelmodule-device_idsnone-output_devicenone-dim0source

?

個人覺得值得一說的是關于數據并行這部分:

根據官網的教程代碼總結如下所示:

# -*- coding: utf-8 -*-
"""
Created on Tue Apr 16 14:32:58 2019@author: kofzh
"""import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader# Parameters and DataLoaders
input_size = 5
output_size = 2batch_size = 30
data_size = 100device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")class RandomDataset(Dataset):def __init__(self, size, length):self.len = lengthself.data = torch.randn(length, size)def __getitem__(self, index):return self.data[index]def __len__(self):return self.lenrand_loader = DataLoader(dataset=RandomDataset(input_size, data_size),batch_size=batch_size, shuffle=True)class Model(nn.Module):# Our modeldef __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 outputmodel = 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 GPUsmodel = nn.DataParallel(model)model.to(device)
for data in rand_loader:input = data.to(device)output = model(input)print("Outside: input size", input.size(),"output_size", output.size())

對于上述代碼分別是 batchsize= 30在CPU和4路GPU環境下運行得出的結果:

CPU with batchsize= 30結果:

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])

4路?GPU with batchsize= 30?結果:

Let's use 4 GPUs!In Model: input size torch.Size([8, 5]) output size torch.Size([8, 2])In Model: input size torch.Size([8, 5]) output size torch.Size([8, 2])In Model: input size torch.Size([6, 5]) output size torch.Size([6, 2])In Model: input size torch.Size([8, 5]) output size torch.Size([8, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])In Model: input size    In Model: input sizetorch.Size([8, 5])  In Model: input size torch.Size([8, 5])output size      In Model: input size torch.Size([8, 5]) output size torch.Size([8, 2])torch.Size([6, 5]) output size torch.Size([6, 2])output size torch.Size([8, 2])torch.Size([8, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])In Model: input size    In Model: input size torch.Size([8, 5]) output size torch.Size([8, 2])
torch.Size([8, 5]) output size torch.Size([8, 2])In Model: input size torch.Size([8, 5]) output size torch.Size([8, 2])In Model: input size torch.Size([6, 5]) output size torch.Size([6, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])In Model: input size    In Model: input sizetorch.Size([3, 5])  In Model: input size torch.Size([3, 5]) output size torch.Size([3, 2])torch.Size([3, 5]) output size torch.Size([3, 2])output size torch.Size([3, 2])In Model: input size torch.Size([1, 5]) output size torch.Size([1, 2])
Outside: input size torch.Size([10, 5]) output_size torch.Size([10, 2])

對于上述代碼分別是 batchsize= 25在CPU和4路GPU環境下運行得出的結果:

CPU with batchsize= 25結果:

 In Model: input size torch.Size([25, 5]) output size torch.Size([25, 2])
Outside: input size torch.Size([25, 5]) output_size torch.Size([25, 2])In Model: input size torch.Size([25, 5]) output size torch.Size([25, 2])
Outside: input size torch.Size([25, 5]) output_size torch.Size([25, 2])In Model: input size torch.Size([25, 5]) output size torch.Size([25, 2])
Outside: input size torch.Size([25, 5]) output_size torch.Size([25, 2])In Model: input size torch.Size([25, 5]) output size torch.Size([25, 2])
Outside: input size torch.Size([25, 5]) output_size torch.Size([25, 2])

4路?GPU with batchsize= 25?結果:

Let's use 4 GPUs!In Model: input size    In Model: input size torch.Size([7, 5]) output size torch.Size([7, 2])torch.Size([7, 5]) output size torch.Size([7, 2])In Model: input size    In Model: input size torch.Size([7, 5]) output size torch.Size([7, 2])torch.Size([4, 5]) output size torch.Size([4, 2])
Outside: input size torch.Size([25, 5]) output_size torch.Size([25, 2])In Model: input size torch.Size([7, 5]) output size torch.Size([7, 2])In Model: input size torch.Size([7, 5]) output size torch.Size([7, 2])In Model: input size    In Model: input size torch.Size([7, 5]) output size torch.Size([7, 2])torch.Size([4, 5]) output size torch.Size([4, 2])
Outside: input size torch.Size([25, 5]) output_size torch.Size([25, 2])In Model: input size    In Model: input size  torch.Size([7, 5]) output size torch.Size([7, 2])In Model: input size torch.Size([7, 5]) output size torch.Size([7, 2])
torch.Size([7, 5]) output size torch.Size([7, 2])In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
Outside: input size torch.Size([25, 5]) output_size torch.Size([25, 2])In Model: input size torch.Size([7, 5]) output size torch.Size([7, 2])In Model: input size    In Model: input size torch.Size([7, 5]) output size torch.Size([7, 2])torch.Size([7, 5]) output size torch.Size([7, 2])In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])

個人總結:batchsize在實際訓練時對于每個GPU的分配基本上采用的是平均分配數據的原則,每個GPU分配的數據相加 =?batchsize。

備注:

1.官網上的CPU結果圖是錯誤的,可能是圖截錯了,在此對于某些博客直接抄襲官網的結果圖表示鄙視。

2.本文針對單服務器多GPU的情況,請不要混淆。

如有錯誤,敬請指正!

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/256529.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/256529.shtml
英文地址,請注明出處:http://en.pswp.cn/news/256529.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

JS 轉換數字為大寫

1 function toUpper(n) {2 n n;3 var unit 十百千萬;4 var num 一二三四五六七八九 ;5 var array new Array();6 for (var in.length; i > 0; i--){7 var numIndex parseInt(n.charAt(i-1))-1;8 if(n…

ANSYS——ANSYS后處理操作技巧與各類問題良心大總結

目錄 1.ANSYS后處理時如何按灰度輸出云圖? 2 將云圖輸出為JPG 3.怎么在計算結果實體云圖中切面? 4.非線性計算過程中收斂曲線實時顯示 5.運用命令流進行計算時,一個良好的習慣是: 6.應力圖中左側的文字中,SMX與SMN分別代表最大值和最小值 7.在非…

容器的綜合應用:文本查詢程序

需求 程序讀取用戶指定的任意文本文件,允許用戶從該文件中查找單詞。查詢結果是該單詞出現的次數,并列出每次出現所在行,如果某單詞在同一行中多次出現,程序將只顯示該行一次。行號按升序顯示,即第 7 行應該在第 9 行之…

Anaconda 安裝操作及遇到的坑

最近剛用Pytorch,編譯開源代碼的時候發現缺少n個package,原來是之前在Anaconda3 創建的虛擬環境各自是獨立的,tensorflow下安裝的不能在別的環境下使用,所以要重新安裝。然而關鍵是國內各種屏蔽資源,無法FQ去直接下載安…

IE瀏覽器歷史版本圖標大全

上個月IE團隊慶祝了IE的15周歲生日, 并曬了曬IE各個歷史版本的圖標: Internet Explorer 1.0 圖標 Internet Explorer 2.0 圖標 Internet Explorer 3.0 圖標 Internet Explorer 4.0 圖標 Internet Explorer 5.0 圖標 Internet Explorer 6.0 圖標 Internet…

7.Mybatis關聯表查詢(這里主要講的是一對一和一對多的關聯查詢)

視頻地址:http://edu.51cto.com/sd/be679 在Mybatis中的管理表查詢這里主要介紹的是一對一和一對多的關聯查詢的resultMap的管理配置查詢,當然你也可以用包裝類來實現。不過這里不說,做關聯查詢的步驟可以簡單的總結為以下的幾步:…

ANSYS——查看某一截面的云圖分布(也叫做切片圖)

1.確定截面的位置 此處以圖中紅色處截面為例 2.將工作平面經過坐標變化移動到指定截面處(工作平面的XY平面與截面重合) 工作平面坐標系默認是與總體坐標系重合的,這里是先平移再進行旋轉

深度學習之keras (一) 初探

之前一段時間里,學習過tensorflow和Pytorch也寫了點心得,目前是因為項目原因用了一段時間Keras,覺得很不錯啊,至少從入門來說對新手極度友好,由于keras是基于tensoflow的基礎,相當于tensorflow的高級API吧&…

swift:高級運算符(位運算符、溢出運算符、優先級和結合性、運算符重載函數)...

swift:高級運算符 http://www.cocoachina.com/ios/20140612/8794.html 除了基本操作符中所講的運算符,Swift還有許多復雜的高級運算符,包括了C語和Objective-C中的位運算符和移位運算。 不同于C語言中的數值計算,Swift的數值計算默…

收集、報告或保存系統活動信息:sar命令

2019獨角獸企業重金招聘Python工程師標準>>> 索引 sar命令的使用常用方法 查看網絡設備(網卡)的狀態信息查看socket使用情況查看cpu使用情況(默認)查看內存和交換空間使用情況查看內存的統計信息查看tty設備的活動狀態查看等待運行的進程數和…

【GOF23設計模式】原型模式

【GOF23設計模式】原型模式 來源:http://www.bjsxt.com/ 一、【GOF23設計模式】_原型模式、prototype、淺復制、深復制、Cloneable接口 淺復制 1 package com.test.prototype;2 3 import java.util.Date;4 5 /**6 * 淺復制7 */8 public class Sheep implements C…

ANSYS——自定義的梁截面中心(法線節點)的偏置,詳細全面

目錄 1、ANSYS梁的確定 2.關于梁截面的一些名詞 總體坐標系 梁截面坐標系 梁單元的坐標系

Deepfacelab 小白教程

不小心入了AI換臉的坑,但是感覺AI換臉很有意思,第一次感覺科研使我快樂。 目錄 一、AI換臉軟件簡介 二、Deepfacelab下載安裝 三、Deepfacelab Demo實現 四、Deepfacelab 填坑 五、總結 一、AI換臉軟件簡介 這個沒有具體使用過,目前我…

Underscore.js 的模板功能

Underscore是一個非常實用的JavaScript庫,提供許多編程時需要的功能的支持,他在不擴展任何JavaScript的原生對象的情況下提供很多實用的功能。 無論你寫一段小的js代碼,還是寫一個大型的HTML5應用,underscore都能幫上忙。目前&…

ANSYS——查看剖面圖的應力分布云圖以及工作平面的相關設置

剖面圖和切片圖其實差不多,只是切片圖只有一個截面,而剖面圖是切去一部分保留另一部分模型,不但可以看到截面處應力分布還可以看到剩余模型的應力分布 切片應力云圖可見:https://blog.csdn.net/qq_45769063/article/details/106357700 1.剖面云圖的查看 首先將工作平面的…

2016.8.2

高端內存映射方式 高端內存映射分為三種:永久映射、臨時映射和非連續動態內存映射。高端內存一般是指896MB以上的頁框,這段區間內核一般不能直接訪問。 1.永久映射 永久內核映射允許內核建立高端頁框到內核地址空間的長期映射。它們使用主內核頁表中的一…

深度學習之pytorch(三) C++調用

玩深度學習,個人覺得基于anaconda的python適合開發與測試,C適合實際的工程部署!而pytorch官方有編譯好的libtorch,特別方便,適合于我這樣的伸手黨和手殘黨(win10下編譯tensorflow編譯了好久都沒通過,好憂傷…

ANSYS入門——模態分析步驟與實例詳解

目錄 一、ANSYS求解模態分析步驟 建模 施加載荷和求解

javascript庫之Mustache庫使用說明

一、簡單示例 代碼: 1 function show(t) { 2 $("#content").html(t); 3 } 4 5 var view { 6 title: YZF, 7 cacl: function () { 8 return …

Light OJ 1007

求區間歐拉函數平方和。。。 最后因為longlong 范圍爆了WA 了&#xff0c; 0.0 #include<bits/stdc.h> using namespace std; const int maxn 5000000 131; typedef unsigned long long LL;bool Com[maxn]; LL Num[maxn], Prim[maxn / 3]; int Cnt;void INIT() {Num[1]…