1.使用torch.Tensor将list转为Tensor时遇到问题:ValueError: only one element tensors can be converted to Python scalars。
情况说明:list由Tensor组成,使用torch.Tensor将该list也转为Tensor.
当list中的元素为多维Tensor时,是不可以使用torch.Tensor的,比如下面👇这种情况:
>>> a = [[1],[2]]
>>> a = torch.Tensor(a)
>>> b = [[3],[4]]
>>> b = torch.Tensor(b)
>>> c = [a,b]
>>> torch.Tensor(c)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: only one element tensors can be converted to Python scalars
>>>
问题解决:将torch.Tensor换成torch.stack即可