-
[Pytorch] tensor로 구성된 list를 tensor로 만드는 방법Debug 2023. 8. 21. 02:47
▶ 현상
List Object를 tensor 타입으로 만들고 싶을 때, 리스트 구성 요소가 tensor일 때 torch.Tensor class를 생성하며 인자로 list를 넘겨주면 에러가 발생한다.
import torch a = list(torch.zeros(1, 3)) tensor_a = torch.Tensor(a) # ValueError: only one element tensors can be converted to Python scalars
▶ 발생 에러
ValueError: only one element tensors can be converted to Python scalars
▶ 해결 방법
tensor_a = torch.stack(a, dim=0) # tensor([[0., 0., 0.]])