torch tensor

tensor

跟numpy 很像。

x = torch.zeros(5, 3, dtype=torch.long)
print(x)
x = x.new_ones(5, 3, dtype=torch.double)      # new_* methods take in sizes
print(x)

x = torch.randn_like(x, dtype=torch.float)    # override dtype!
print(x)                                      # result has the same size

CUDA TENSORS

# let us run this cell only if CUDA is available
# We will use ``torch.device`` objects to move tensors in and out of GPU
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!

与numpy转换

先放在cpu,再转为numpy
此时对他的处理与torch tensor会牵连在一起。

a=x.cpu().numpy()

猜你喜欢

转载自blog.csdn.net/qq_35608277/article/details/85334159