-
[pytorch] numpy를 tensor로 변환하기Study Bits 2023. 9. 21. 22:12
torch.Tensor()
입력된 numpy array가 복제되어 tensor가 만들어진다.
torch.from_numpy(), as_tensor()
입력된 numpy array를 tensor로 바꿔 반환된다. 데이터가 복제되지 않고 메모리 버퍼가 공유되기 때문에, array 변경시 tensor 데이터도 변경된다.
import torch import numpy as np a = np.zeros([1, 2, 3, 4]) tensor_a = torch.from_numpy(a) tensor_a = torch.as_tensor(a)