色哟哟视频在线观看-色哟哟视频在线-色哟哟欧美15最新在线-色哟哟免费在线观看-国产l精品国产亚洲区在线观看-国产l精品国产亚洲区久久

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫(xiě)文章/發(fā)帖/加入社區(qū)
會(huì)員中心
电子发烧友
开通电子发烧友VIP会员 尊享10大特权
海量资料免费下载
精品直播免费看
优质内容免费畅学
课程9折专享价
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

基于PyTorch的深度學(xué)習(xí)入門(mén)教程之DataParallel使用多GPU

ss ? 來(lái)源:雁回晴空 ? 作者:雁回晴空 ? 2021-02-15 09:55 ? 次閱讀

前言

本文參考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ò)分類器

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


聲明:本文內(nèi)容及配圖由入駐作者撰寫(xiě)或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問(wèn)題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • gpu
    gpu
    +關(guān)注

    關(guān)注

    28

    文章

    4909

    瀏覽量

    130632
  • Data
    +關(guān)注

    關(guān)注

    0

    文章

    63

    瀏覽量

    38607
  • 深度學(xué)習(xí)
    +關(guān)注

    關(guān)注

    73

    文章

    5554

    瀏覽量

    122465
  • pytorch
    +關(guān)注

    關(guān)注

    2

    文章

    809

    瀏覽量

    13756
收藏 0人收藏

    評(píng)論

    相關(guān)推薦
    熱點(diǎn)推薦

    GPU深度學(xué)習(xí)中的應(yīng)用 GPUs在圖形設(shè)計(jì)中的作用

    隨著人工智能技術(shù)的飛速發(fā)展,深度學(xué)習(xí)作為其核心部分,已經(jīng)成為推動(dòng)技術(shù)進(jìn)步的重要力量。GPU(圖形處理單元)在深度學(xué)習(xí)中扮演著至關(guān)重要的角色,
    的頭像 發(fā)表于 11-19 10:55 ?1438次閱讀

    PyTorch GPU 加速訓(xùn)練模型方法

    深度學(xué)習(xí)領(lǐng)域,GPU加速訓(xùn)練模型已經(jīng)成為提高訓(xùn)練效率和縮短訓(xùn)練時(shí)間的重要手段。PyTorch作為一個(gè)流行的深度
    的頭像 發(fā)表于 11-05 17:43 ?1228次閱讀

    如何使用 PyTorch 進(jìn)行強(qiáng)化學(xué)習(xí)

    強(qiáng)化學(xué)習(xí)(Reinforcement Learning, RL)是一種機(jī)器學(xué)習(xí)方法,它通過(guò)與環(huán)境的交互來(lái)學(xué)習(xí)如何做出決策,以最大化累積獎(jiǎng)勵(lì)。PyTorch 是一個(gè)流行的開(kāi)源機(jī)器
    的頭像 發(fā)表于 11-05 17:34 ?900次閱讀

    Pytorch深度學(xué)習(xí)訓(xùn)練的方法

    掌握這 17 種方法,用最省力的方式,加速你的 Pytorch 深度學(xué)習(xí)訓(xùn)練。
    的頭像 發(fā)表于 10-28 14:05 ?540次閱讀
    <b class='flag-5'>Pytorch</b><b class='flag-5'>深度</b><b class='flag-5'>學(xué)習(xí)</b>訓(xùn)練的方法

    GPU深度學(xué)習(xí)應(yīng)用案例

    GPU深度學(xué)習(xí)中的應(yīng)用廣泛且重要,以下是一些GPU深度學(xué)習(xí)應(yīng)用案例: 一、圖像識(shí)別 圖像識(shí)別是
    的頭像 發(fā)表于 10-27 11:13 ?1082次閱讀

    深度學(xué)習(xí)GPU加速效果如何

    圖形處理器(GPU)憑借其強(qiáng)大的并行計(jì)算能力,成為加速深度學(xué)習(xí)任務(wù)的理想選擇。
    的頭像 發(fā)表于 10-17 10:07 ?521次閱讀

    pytorch和python的關(guān)系是什么

    PyTorch已經(jīng)成為了一個(gè)非常受歡迎的框架。本文將介紹PyTorch和Python之間的關(guān)系,以及它們?cè)?b class='flag-5'>深度學(xué)習(xí)領(lǐng)域的應(yīng)用。 Python簡(jiǎn)介 Python是一種高級(jí)、解釋型、通用
    的頭像 發(fā)表于 08-01 15:27 ?3064次閱讀

    PyTorch深度學(xué)習(xí)開(kāi)發(fā)環(huán)境搭建指南

    PyTorch作為一種流行的深度學(xué)習(xí)框架,其開(kāi)發(fā)環(huán)境的搭建對(duì)于深度學(xué)習(xí)研究者和開(kāi)發(fā)者來(lái)說(shuō)至關(guān)重要。在Windows操作系統(tǒng)上搭建
    的頭像 發(fā)表于 07-16 18:29 ?2231次閱讀

    pytorch中有神經(jīng)網(wǎng)絡(luò)模型嗎

    當(dāng)然,PyTorch是一個(gè)廣泛使用的深度學(xué)習(xí)框架,它提供了許多預(yù)訓(xùn)練的神經(jīng)網(wǎng)絡(luò)模型。 PyTorch中的神經(jīng)網(wǎng)絡(luò)模型 1. 引言 深度
    的頭像 發(fā)表于 07-11 09:59 ?1456次閱讀

    PyTorch的介紹與使用案例

    PyTorch是一個(gè)基于Python的開(kāi)源機(jī)器學(xué)習(xí)庫(kù),它主要面向深度學(xué)習(xí)和科學(xué)計(jì)算領(lǐng)域。PyTorch由Meta Platforms(原Fa
    的頭像 發(fā)表于 07-10 14:19 ?764次閱讀

    tensorflow和pytorch哪個(gè)更簡(jiǎn)單?

    : TensorFlow和PyTorch都是用于深度學(xué)習(xí)和機(jī)器學(xué)習(xí)的開(kāi)源框架。TensorFlow由Google Brain團(tuán)隊(duì)開(kāi)發(fā),而PyTorc
    的頭像 發(fā)表于 07-05 09:45 ?1331次閱讀

    PyTorch的特性和使用方法

    使用Python重新寫(xiě)了很多內(nèi)容,使其更加靈活易用。它不僅是一個(gè)擁有自動(dòng)求導(dǎo)功能的深度神經(jīng)網(wǎng)絡(luò)框架,還可以看作是一個(gè)加入了GPU支持的NumPy。PyTorch支持動(dòng)態(tài)圖,允許在運(yùn)行時(shí)構(gòu)建計(jì)算圖,這使得模型開(kāi)發(fā)和調(diào)試過(guò)程更加直觀
    的頭像 發(fā)表于 07-02 14:27 ?1067次閱讀

    如何使用PyTorch建立網(wǎng)絡(luò)模型

    PyTorch是一個(gè)基于Python的開(kāi)源機(jī)器學(xué)習(xí)庫(kù),因其易用性、靈活性和強(qiáng)大的動(dòng)態(tài)圖特性,在深度學(xué)習(xí)領(lǐng)域得到了廣泛應(yīng)用。本文將從PyTorch
    的頭像 發(fā)表于 07-02 14:08 ?765次閱讀

    TensorFlow與PyTorch深度學(xué)習(xí)框架的比較與選擇

    深度學(xué)習(xí)作為人工智能領(lǐng)域的一個(gè)重要分支,在過(guò)去十年中取得了顯著的進(jìn)展。在構(gòu)建和訓(xùn)練深度學(xué)習(xí)模型的過(guò)程中,深度
    的頭像 發(fā)表于 07-02 14:04 ?1496次閱讀

    新手小白怎么學(xué)GPU云服務(wù)器跑深度學(xué)習(xí)?

    新手小白想用GPU云服務(wù)器跑深度學(xué)習(xí)應(yīng)該怎么做? 用個(gè)人主機(jī)通常pytorch可以跑但是LexNet,AlexNet可能就直接就跑不動(dòng),如何實(shí)現(xiàn)更經(jīng)濟(jì)便捷的實(shí)現(xiàn)
    發(fā)表于 06-11 17:09
    主站蜘蛛池模板: 成年人免费观看的视频 | 亚洲色大成网站www久久九九 | 亚洲国产成人精品无码区5566 | 欧美视频毛片在线播放 | 狼群资源网中文字幕 | 亚洲精品成人 | 宿舍BL 纯肉各种PLAY H | 日韩欧美一区二区三区在线 | 我的漂亮朋友在线观看全集免费 | 动漫成年美女黄漫网站 | 久久国产精品无码视欧美 | 性色AV一区二区三区V视界影院 | 国产GV天堂亚洲国产GV刚刚碰 | 久久综合九色综合国产 | 少男同志freedeos| 国产精品第八页 | 国产成人精品自线拍 | 91免费精品国自产拍在线可以看 | 2019天天射干网站 | 亚洲精品中文字幕无码A片蜜桃 | 男男女女爽爽爽视频免费 | 久久精品国产视频澳门 | 久久这里只有精品国产精品99 | 岛国大片在线观看完整版 | 国产精品久久久久久久久久久 | 国产精品嫩草99AV在线 | 亚洲中文有码字幕日本 | 亚洲一区免费观看 | 日韩一级精品久久久久 | 亚洲haose在线观看 | 扒开她的黑森林让我添动态图 | 男子扒开美女尿口做羞羞的事 | 人人澡人人爽人人精品 | 国产亚洲精品品视频在线 | 嫩草影院地址一地址二 | 国产AV麻豆出品在线播放 | 青青草原国产在线观看 | 欧美506070| 成品片a免人看免费 | 99视频精品国产在线视频 | 日日干夜夜艹 |

    電子發(fā)燒友

    中國(guó)電子工程師最喜歡的網(wǎng)站

    • 2931785位工程師會(huì)員交流學(xué)習(xí)
    • 獲取您個(gè)性化的科技前沿技術(shù)信息
    • 參加活動(dòng)獲取豐厚的禮品