欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2021年06月01日 10:42:20   作者:學(xué)渣渣渣渣渣  
考慮到各種運(yùn)算只能在cpu或者gpu運(yùn)算,不能混和運(yùn)算,本文介紹常用的幾種把數(shù)據(jù)挪到gpu或者直接在gpu創(chuàng)建數(shù)據(jù)再進(jìn)行運(yùn)算的方法

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

查看有幾個(gè)可用GPU。

torch.cuda.device_count()

查看GPU算力

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

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

# 里面輸入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 的哪個(gè)版本。

有一條指令可以查看

import torch
print(torch.version.cuda)

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

相關(guān)文章

最新評(píng)論