본문 바로가기

TIL/개인공부

[Pytorch] numpy에서 torch, torch에서 numpy

반응형

 

torch에서 numpy

a = torch.randn(1,3)
print(a)
# tensor([[0.5519, 0.1323, 0.1297]])

b = a.numpy()
print(b)
# [[0.55190253 0.13231923 0.12974031]]

numpy에서 torch

a = np.ones(3)
print(a)
# [1. 1. 1.]

b = torch.from_numpy(a)
print(b)
# tensor([1., 1., 1.], dtype=torch.float64)
반응형