b = np.ones(5)
a = torch.from_numpy(b)
np.add(b, 1, out = b)
CUDA Tensor
张量可以使用 .to 方法移到任何设备上
if torch.cuda.is_available():
device = torch.device("cuda") # a CUDA device object
y = torch.ones_like(x, device=device) # directly create a tensor on GPU
x = x.to(device) # or just use strings ``.to("cuda")``
z = x + y
print(z)
print(z.to("cpu", torch.double)) # ``.to`` can also change dtype together!