简体   繁体   English

ValueError:在张量列表上使用 torch.Tensor 时,只能将一个元素张量转换为 Python 标量

[英]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.

相关问题 ValueError: 将列表转换为浮点 Torch 张量时,只能将一个元素张量转换为 Python 标量 - ValueError: only one element tensors can be converted to Python scalars when converting list to float Torch tensor ValueError: 只有一个元素张量可以转换为 Python 标量 - ValueError: only one element tensors can be converted to Python scalars ValueError:在将列表转换为 numpy 数组时,只能将一个元素张量转换为 Python 标量 - ValueError: only one element tensors can be converted to Python scalars while converting list to numpy array 无法将列表转换为数组:ValueError:只有一个元素张量可以转换为 Python 标量 - Cannot convert list to array: ValueError: only one element tensors can be converted to Python scalars 在我的 PyTorch 训练迭代器中,如何解决 ValueError:只有一个元素张量可以转换为 Python 标量? - In my PyTorch train iterator, how do I resolve the ValueError: only one element tensors can be converted to Python scalars? 训练回归量时ValueError:只有一个元素张量可以转换为Python Scarlars - When training regressor ValueError: only one element tensors can be converted to Python Scarlars Pytorch 数学计算(只能将一个元素张量转换为 Python 标量) - Pytorch math calculation ( only one element tensors can be converted to Python scalars) TypeError: Merge 只能在张量列表上调用,而不是 keras 中的单个张量 - TypeError: Merge can only be called on a list of tensors, not a single tensor in keras 在张量列表上使用 torch.cat - Using torch.cat on list of tensors 无法使用割炬创建张量 - Unable to create a tensor using torch.Tensor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM