自用
import torch
import numpy as np
a = torch.ones(3,4)
b = a.to("cuda")
print(a)
print(b)
结果:
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]], device='cuda:0')
注意:.to()不仅可以转移device,还可以修改数据类型,比如:a.to(torch.double)
rot = torch.ones((1, 1, 1, 1, 1)).cuda()
print(rot)
结果:
tensor([[[[[1.]]]]], device='cuda:0')
dtype = torch.cuda.FloatTensor
x = torch.rand(2,2).type(dtype)
print(x)
结果:
tensor([[0.2392, 0.3246],
[0.7091, 0.1741]], device='cuda:0')
a = np.array([1,2,3,4])
b = torch.from_numpy(a).cuda()
print(b)
结果:
tensor([1, 2, 3, 4], device='cuda:0', dtype=torch.int32)
https://www.pythonf.cn/read/149404#3_cpugpu_106