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()))输出情况
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)输出情况:
5.3 使用.cuda方法将cpu的Tensor转换到GPU设备上
输入情况:
data_gpu2 = data.cuda(torch.device("cuda:0")) # 如果只有一块gpu的话 直接写成这样:data_gpu2 = data.cuda() print(data_gpu2.device)输出情况:
到此这篇关于PyTorch device与cuda.device用法的文章就介绍到这了,更多相关PyTorch device使用内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
查看更多关于PyTorch device与cuda.device用法介绍的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did99024