日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

PyTorch?device與cuda.device用法介紹_python

作者:研究生不遲到 ? 更新時間: 2022-06-06 編程語言

1 查看當前的device

輸入情況:

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

輸出情況:

Default Device : cpu

2 cpu設備可以使用“cpu:0”來指定

輸入情況

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

輸出情況

Device Type: cpu

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

輸入情況

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

輸出情況

GPU Device:【cuda:0】

4 查詢CPU和GPU設備數量

輸入情況

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設備上轉換到GPU設備

5.1 torch.Tensor方法默認使用CPU設備

輸入情況

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

輸出情況

torch.Size([3, 3])

5.2 使用to方法將cpu的Tensor轉換到GPU設備上

輸入情況:

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

輸出情況:

cuda:0

5.3 使用.cuda方法將cpu的Tensor轉換到GPU設備上

輸入情況:

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

輸出情況:

cuda:0

原文鏈接:https://blog.csdn.net/weixin_42521185/article/details/123913221

欄目分類
最近更新