(1) CPU张量 ----> GPU张量,使用Tensor.cuda()
(2) GPU张量 ----> CPU张量,使用Tensor.cpu()
我们可以通过torch.cuda.is_available()函数来判断当前的环境是否支持GPU,如果支持,则返回GPU类型,否则返回CPU类型。
def commonType(Tensor):
if torch.cuda.is_available():
cuda = "cuda:0"
return Tensor.cuda(cuda)
else:
return Tensor
更多:https://blog.youkuaiyun.com/HowardWood/article/details/79508648