pytoch tensor常见坑

2021-05-08  本文已影响0人  CNforinsist

ValueError: only one element tensors can be converted to Python scalars

1.先创建一个list,然后list append 的元素为tensor,再将list整体转为tensor时就会出现上述错,

解决办法:

1.先把list的tensor转换为numpy,然后再把list转换为tensor;

2.torch.tensor([item.cpu().detach().numpy() for item in 【list】]).to(self.device)

上一篇下一篇

猜你喜欢

热点阅读