五月综合激情婷婷六月,日韩欧美国产一区不卡,他扒开我内裤强吻我下面视频 ,无套内射无矿码免费看黄,天天躁,日日躁,狠狠躁

新聞動態(tài)

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

發(fā)布日期:2022-03-27 15:07 | 文章來源:站長之家

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)驗,希望能給大家一個參考,也希望大家多多支持本站。

海外服務(wù)器租用

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部