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。
查看有幾個可用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