torch tensor
阿新 • • 發佈:2018-12-31
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()