濮阳杆衣贸易有限公司

主頁 > 知識庫 > pytorch 如何用cuda處理數(shù)據(jù)

pytorch 如何用cuda處理數(shù)據(jù)

熱門標(biāo)簽:地圖標(biāo)注面積 小蘇云呼電話機器人 儋州電話機器人 北京電銷外呼系統(tǒng)加盟 北瀚ai電銷機器人官網(wǎng)手機版 朝陽手機外呼系統(tǒng) 佛山400電話辦理 市場上的電銷機器人 所得系統(tǒng)電梯怎樣主板設(shè)置外呼

1 設(shè)置GPU的一些操作

設(shè)置在os端哪些GPU可見,如果不可見,那肯定是不能夠調(diào)用的~

import os
GPU = '0,1,2'
os.environ['CUDA_VISIBLE_DEVICES'] =GPU

torch.cuda.is_available()查看cuda是否可用。

if torch.cuda.is_available():
         torch.backends.cudnn.benchmark = True
        '''
        如果網(wǎng)絡(luò)的輸入數(shù)據(jù)維度或類型上變化不大,設(shè)置 torch.backends.cudnn.benchmark = true 
        可以增加運行效率;
  如果網(wǎng)絡(luò)的輸入數(shù)據(jù)在每次 iteration 都變化的話,會導(dǎo)致 cnDNN 每次都會去尋找一遍最優(yōu)配置,
  這樣反而會降低運行效率。
  這下就清晰明了很多了。
  
        Benchmark模式會提升計算速度,但是由于計算中有隨機性,每次網(wǎng)絡(luò)前饋結(jié)果略有差異。
   torch.backends.cudnn.benchmark = True
     如果想要避免這種結(jié)果波動,設(shè)置:
  torch.backends.cudnn.deterministic = True
        '''

這句話也很常見,設(shè)置默認(rèn)的device,優(yōu)先gpu。

device = 'cuda' if torch.cuda.is_available() else 'cpu'

cpu挪到gpu

# 也可以是 device = torch.device('cuda:0')
device = torch.device('cuda')
a = torch.tensor([1,2,3])
b = a.to(device )
print(a)
print(b)

out:

tensor([1, 2, 3])

tensor([1, 2, 3], device='cuda:0')

判斷變量是否基于GPU。

a.is_cuda

查看有幾個可用GPU。

torch.cuda.device_count()

查看GPU算力

# 返回gpu最大和最小計算能力,是一個tuple
torch.cuda.get_device_capability()

設(shè)置默認(rèn)哪一個GPU運算。

# 里面輸入int類型的數(shù)字
torch.cuda.set_device()

抓取指定gpu的全名。

if torch.cuda.is_available():
    device = torch.device('cuda')
    print('Using GPU: ', torch.cuda.get_device_name(0))

out:

'GeForce GTX 1050'

2 直接在gpu創(chuàng)建

方法一:

a = torch.ones(3,4,device="cuda")
print(a)

out:

tensor([[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]], device='cuda:0')

方法二:

a = torch.cuda.FloatTensor(3, 4)
print(a)

out:

tensor([[-1., -1., -1., -1.],
        [-1., -1., -1., -1.],
        [-1., -1., -1., -1.]], device='cuda:0')

3 從cpu轉(zhuǎn)移到gpu

方法一:tensor.to()

a = torch.ones(3,4)
b = a.to("cuda")
print(a)
print(b)

out:

tensor([[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]])
tensor([[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]], device='cuda:0')

注意:.to()不僅可以轉(zhuǎn)移device,還可以修改數(shù)據(jù)類型,比如:a.to(torch.double)

方法二:tensor.cuda()

a = torch.tensor([1., 2.]).cuda()

方法三:tensor.type()

dtype = torch.cuda.FloatTensor
x = torch.rand(2,2).type(dtype)

方法四:torch.from_numpy(np_labels).cuda()

wm_labels = torch.from_numpy(np_labels).cuda()

4 在cuda中訓(xùn)練模型

在默認(rèn)情況下,模型參數(shù)的優(yōu)化(即訓(xùn)練)是在cpu上進(jìn)行的,如果想要挪到GPU,得做如下修改:

import torch.nn as nn
#假設(shè)前面已經(jīng)定義好了模型
#創(chuàng)建模型
Hidnet = UnetGenerator_mnist()
#把模型放入GPU
Hidnet = nn.DataParallel(Hidnet.cuda())
#查看模型參數(shù)
list(Hidnet.parameters())[0]

out:

Parameter containing:
tensor([[[[ 0.1315,  0.0562,  0.1186],
          [-0.1158,  0.1394, -0.0399],
          [ 0.1728,  0.1051, -0.1034]],

         [[ 0.1702, -0.1208, -0.1134],
          [-0.1449,  0.1912,  0.1727],
          [ 0.1562,  0.1601,  0.1055]],

         [[ 0.1031, -0.0062, -0.0068],
          [-0.0453,  0.1150,  0.0366],
          [ 0.0680, -0.1234, -0.0988]]]], device='cuda:0', requires_grad=True)

可以看到 device=‘cuda:0' 啦

pytorch 查看cuda 版本

由于pytorch的whl 安裝包名字都一樣,所以我們很難區(qū)分到底是基于cuda 的哪個版本。

有一條指令可以查看

import torch
print(torch.version.cuda)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • pytorch model.cuda()花費時間很長的解決
  • pytorch中.to(device) 和.cuda()的區(qū)別說明
  • PyTorch CUDA環(huán)境配置及安裝的步驟(圖文教程)
  • Linux安裝Pytorch1.8GPU(CUDA11.1)的實現(xiàn)
  • 將pytorch的網(wǎng)絡(luò)等轉(zhuǎn)移到cuda

標(biāo)簽:定西 商丘 寧夏 龍巖 金融催收 江蘇 云南 酒泉

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《pytorch 如何用cuda處理數(shù)據(jù)》,本文關(guān)鍵詞  pytorch,如,何用,cuda,處理,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《pytorch 如何用cuda處理數(shù)據(jù)》相關(guān)的同類信息!
  • 本頁收集關(guān)于pytorch 如何用cuda處理數(shù)據(jù)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    连云港市| 黎川县| 卢氏县| 仁化县| 宁海县| 漯河市| 江门市| 调兵山市| 康定县| 江山市| 宁陕县| 常熟市| 澄江县| 新民市| 济源市| 周至县| 北宁市| 溆浦县| 拜城县| 新兴县| 金昌市| 潼关县| 冷水江市| 平利县| 汉中市| 新丰县| 荥阳市| 永丰县| 山西省| 和田县| 安福县| 耿马| 扶绥县| 仙游县| 沙田区| 凤庆县| 丹阳市| 屯昌县| 遂宁市| 剑阁县| 大竹县|