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

PyTorch?device與cuda.device用法介紹

 更新時間:2022年04月02日 16:03:47   作者:研究生不遲到  
這篇文章主要介紹了PyTorch?device與cuda.device用法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

1 查看當(dāng)前的device

輸入情況:

import torch
print("Default Device : {}".format(torch.Tensor([4, 5, 6]).device))

輸出情況:

Default Device : cpu

2 cpu設(shè)備可以使用“cpu:0”來指定

輸入情況

device = torch.Tensor([1, 2, 3], device="cpu:0").device
print("Device Type: {}".format(device))

輸出情況

Device Type: cpu

3 gpu設(shè)備可以使用“cuda:0”來指定

輸入情況

gpu = torch.device("cuda:0")
print("GPU Device:【{}:{}】".format(gpu.type, gpu.index))

輸出情況

GPU Device:【cuda:0】

4 查詢CPU和GPU設(shè)備數(shù)量

輸入情況

print("Total GPU Count :{}".format(torch.cuda.device_count()))
print("Total CPU Count :{}".format(torch.cuda.os.cpu_count()))

輸出情況

Total GPU Count :1
Total CPU Count :8

5 從CPU設(shè)備上轉(zhuǎn)換到GPU設(shè)備

5.1 torch.Tensor方法默認使用CPU設(shè)備

輸入情況

data = torch.Tensor([[1, 4, 7], [3, 6, 9], [2, 5, 8]])
print(data.shape)

輸出情況

torch.Size([3, 3])

5.2 使用to方法將cpu的Tensor轉(zhuǎn)換到GPU設(shè)備上

輸入情況:

data_gpu = data.to(torch.device("cuda:0"))
print(data_gpu.device)

輸出情況:

cuda:0

5.3 使用.cuda方法將cpu的Tensor轉(zhuǎn)換到GPU設(shè)備上

輸入情況:

data_gpu2 = data.cuda(torch.device("cuda:0"))
# 如果只有一塊gpu的話  直接寫成這樣:data_gpu2 = data.cuda()
print(data_gpu2.device)

輸出情況:

cuda:0

到此這篇關(guān)于PyTorch device與cuda.device用法的文章就介紹到這了,更多相關(guān)PyTorch device使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論