测试pytorch中是否可以调用GPU

测试代码

import torch
flag = torch.cuda.is_available()
print(flag)

ngpu= 1
# Decide which device we want to run on
device = torch.device("cuda:0" if (torch.cuda.is_available() and ngpu > 0) else "cpu")
print(device)
print(torch.cuda.get_device_name(0))
print(torch.rand(3,3).cuda()) 

在这里插入图片描述
其中,cuda:0是指第一个GPU。

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐