[英]ValueError: only one element tensors can be converted to Python scalars when using torch.Tensor on list of tensors
I have a list of tensors:我有一个张量列表:
object_ids = [tensor([2., 3.]), tensor([2., 3.]), tensor([2., 3.]), tensor([2., 3.]), tensor([2., 3.]), tensor([2., 3.]), tensor([2., 3.]), tensor([2., 3.]), tensor([2., 3.]), tensor([2., 3.])]
Intuitively, it seems like I should be able to create a new tensor from this:直觉上,似乎我应该能够从中创建一个新的张量:
torch.as_tensor(object_ids, dtype=torch.float32)
But this does NOT work.但这不起作用。 Apparently, torch.as_tensor and torch.Tensor can only turn lists of scalars into new tensors.
显然,torch.as_tensor 和 torch.Tensor 只能将标量列表转换为新的张量。 it cannot turn a list of d-dim tensors into a d+1 dim tensor.
它不能将 d-dim 张量列表转换为 d+1 dim 张量。
You can use torch.stack
.您可以使用
torch.stack
。
In your example:在您的示例中:
>>> object_ids = [tensor([2., 3.]), tensor([2., 3.]), tensor([2., 3.]), tensor([2., 3.]), tensor([2., 3.]), tensor([2., 3.]), tensor([2., 3.]), tensor([2., 3.]), tensor([2., 3.]), tensor([2., 3.])]
>>> torch.stack(object_ids)
tensor([[2., 3.],
[2., 3.],
[2., 3.],
[2., 3.],
[2., 3.],
[2., 3.],
[2., 3.],
[2., 3.],
[2., 3.],
[2., 3.]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.