偶然间我们在使用pytorch将一个list转换成为tensor的时间大概会遇到这个标题:
报错内容:
ValueErrornly one element tensors can be converted to Python scalars
大概:
TypeError: only integer tensors of a single element can be converted to an index
x = torch.tensor([1,2,3])a = [x,x]print(torch.tensor(a))修改为:
x = torch.tensor([1,2,3])a = [x.tolist(),x.tolist()]print(torch.tensor(a))大概:
x = torch.tensor([1,2,3])a = [x,x]print(torch.tensor([item.numpy() for item in a]))固然会带来一些性能标题。由于如许子转换黑白常耗时间的。
他大概会发出告诫:
UserWarning: Creating a tensor from a list of numpy.ndarrays is extremely slow. Please consider converting the list to a single numpy.ndarray with numpy.array() before converting to a tensor.
要只管制止如许的操纵。 |