檢測(cè)pytorch是否使用GPU的方法小結(jié)
利用gpustat或nvidia-smi實(shí)時(shí)監(jiān)控GPU使用率
安裝gpustat
apt install gpustat
啟動(dòng)gpustat
watch -n1 --color gpustat --color
每秒輸出實(shí)時(shí)監(jiān)測(cè)結(jié)果,如下圖:

也可利用nvidia-smi實(shí)時(shí)監(jiān)控,會(huì)顯示更多的參數(shù)
$ watch -n 1 nvidia-smi --query-gpu=index,gpu_name,memory.total,memory.used,memory.free,temperature.gpu,pstate,utilization.gpu,utilization.memory --format=csv

輸出torch對(duì)應(yīng)的設(shè)備
首先在python里檢查,也是大家用的最多的方式,檢查GPU是否可用(但實(shí)際并不一定真的在用)
torch.cuda.is_available()
更嚴(yán)謹(jǐn)一些,在程序運(yùn)行的時(shí)候查看是否真的在使用GPU,插入代碼,在運(yùn)行時(shí)輸出torch對(duì)應(yīng)的設(shè)備,如果這里輸出的是CPU,肯定就沒(méi)有在GPU上運(yùn)行了。
# setting device on GPU if available, else CPU
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print('Using device:', device)
print()
#Additional Info when using cuda
if device.type == 'cuda':
print(torch.cuda.get_device_name(0))
print('Memory Usage:')
print('Allocated:', round(torch.cuda.memory_allocated(0)/1024**3,1), 'GB')
print('Cached: ', round(torch.cuda.memory_reserved(0)/1024**3,1), 'GB')
使用簡(jiǎn)單全連接網(wǎng)絡(luò)檢測(cè)GPU情況
可以直接運(yùn)行一個(gè)簡(jiǎn)單的全連接網(wǎng)絡(luò),查看GPU的使用情況:
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchsummary import summary
from torchvision import models
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 6, 5)
self.conv2 = nn.Conv2d(6, 16, 5)
#此處的16*5*5為conv2經(jīng)過(guò)pooling之后的尺寸,即為fc1的輸入尺寸,在這里寫(xiě)死了,因此后面的輸入圖片大小不能任意調(diào)整
self.fc1 = nn.Linear(16*5*5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
x = F.max_pool2d(F.relu(self.conv2(x)), 2)
x = x.view(-1, self.num_flat_features(x))
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
def num_flat_features(self, x):
size = x.size()[1:]
num_features = 1
for s in size:
num_features *= s
return num_features
net = Net()
print(net)
params = list(net.parameters())
print (len(params))
print(params[0].size())
print(params[1].size())
print(params[2].size())
print(params[3].size())
print(params[4].size())
print(params[5].size())
print(params[6].size())
print(params[7].size())
print(params[8].size())
print(params[9].size())
input = torch.randn(1, 1, 32, 32)
out = net(input)
print(out)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
vgg = net.to(device)
summary(vgg, (1, 32, 32))
后記
我的問(wèn)題最后解決了,但是用的不是這三種方法,是因?yàn)槲业姆?wù)器是阿里云服務(wù)器,安裝完驅(qū)動(dòng)之后還要添加License,但是客服沒(méi)說(shuō),導(dǎo)致此后GPU利用率一直是0%,添加License之后就好了。。。
以上就是檢測(cè)pytorch是否使用GPU的方法小結(jié)的詳細(xì)內(nèi)容,更多關(guān)于檢測(cè)pytorch GPU方法的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
opencv中顏色空間轉(zhuǎn)換函數(shù)cv2.cvtColor()使用
本文主要介紹了opencv中顏色空間轉(zhuǎn)換函數(shù)cv2.cvtColor()使用,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
PyCharm2018 安裝及破解方法實(shí)現(xiàn)步驟
這篇文章主要介紹了PyCharm2018 安裝及破解方法實(shí)現(xiàn)步驟,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-09-09
樹(shù)莓派實(shí)現(xiàn)移動(dòng)拍照
這篇文章主要為大家詳細(xì)介紹了樹(shù)莓派實(shí)現(xiàn)移動(dòng)拍照,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06
python使用Streamlit庫(kù)制作Web可視化頁(yè)面
一談到Web頁(yè)面,可能大家首先想到就是HTML,CSS或JavaScript。 本次小F就給大家介紹一下如何用Python制作一個(gè)數(shù)據(jù)可視化網(wǎng)頁(yè),使用到的是Streamlit庫(kù)。輕松的將一個(gè)Excel數(shù)據(jù)文件轉(zhuǎn)換為一個(gè)Web頁(yè)面,提供給所有人在線查看。2021-05-05
python爬蟲(chóng) 爬取58同城上所有城市的租房信息詳解
這篇文章主要介紹了python爬蟲(chóng) 爬取58同城上所有城市的租房信息詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
python利用openpyxl拆分多個(gè)工作表的工作簿的方法
這篇文章主要介紹了python利用openpyxl拆分多個(gè)工作表的工作簿的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
python實(shí)現(xiàn)selenium網(wǎng)絡(luò)爬蟲(chóng)的方法小結(jié)
這篇文章主要介紹了python實(shí)現(xiàn)selenium網(wǎng)絡(luò)爬蟲(chóng)的方法小結(jié),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03

