无法将列表转换为数组:ValueError:只有一个元素张量可以转换为 Python 标量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52074153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:01:25  来源:igfitidea点击:

Cannot convert list to array: ValueError: only one element tensors can be converted to Python scalars

pythonnumpypytorchnumpy-ndarray

提问by Henning

I'm currently working with the PyTorch framework and trying to understand foreign code. I got an indices issue and wanted to print the shape of a list.
The only way of doing so (as far as Google tells me) is to convert the list into a numpy array and then getting the shape with numpy.ndarray.shape().

我目前正在使用 PyTorch 框架并试图理解外国代码。我遇到了索引问题,想打印列表的形状。
这样做的唯一方法(据 Google 告诉我)是将列表转换为 numpy 数组,然后使用 numpy.ndarray.shape() 获取形状。

But trying to convert my list into an array, I got a ValueError: only one element tensors can be converted to Python scalars.

但是尝试将我的列表转换为数组时,我得到了一个ValueError: only one element tensors can be converted to Python scalars.

My List is a converted PyTorch Tensor (list(pytorchTensor)) and looks somewhat like this:

我的列表是一个经过转换的 PyTorch Tensor ( list(pytorchTensor)),看起来有点像这样:

[tensor([[-0.2781, -0.2567, -0.2353, ..., -0.9640, -0.9855, -1.0069],
[-0.2781, -0.2567, -0.2353, ..., -1.0069, -1.0283, -1.0927],
[-0.2567, -0.2567, -0.2138, ..., -1.0712, -1.1141, -1.1784],
...,
[-0.6640, -0.6425, -0.6211, ..., -1.0712, -1.1141, -1.0927],
[-0.6640, -0.6425, -0.5997, ..., -0.9426, -0.9640, -0.9640],
[-0.6640, -0.6425, -0.5997, ..., -0.9640, -0.9426, -0.9426]]), tensor([[-0.0769, -0.0980, -0.076 9, ..., -0.9388, -0.9598, -0.9808],
[-0.0559, -0.0769, -0.0980, ..., -0.9598, -1.0018, -1.0228],
[-0.0559, -0.0769, -0.0769, ..., -1.0228, -1.0439, -1.0859],
...,
[-0.4973, -0.4973, -0.4973, ..., -1.0018, -1.0439, -1.0228],
[-0.4973, -0.4973, -0.4973, ..., -0.8757, -0.9177, -0.9177],
[-0.4973, -0.4973, -0.4973, ..., -0.9177, -0.8967, -0.8967]]), tensor([[-0.1313, -0.1313, -0.110 0, ..., -0.8115, -0.8328, -0.8753],
[-0.1313, -0.1525, -0.1313, ..., -0.8541, -0.8966, -0.9391],
[-0.1100, -0.1313, -0.1100, ..., -0.9391, -0.9816, -1.0666],
...,
[-0.4502, -0.4714, -0.4502, ..., -0.8966, -0.8966, -0.8966],
[-0.4502, -0.4714, -0.4502, ..., -0.8115, -0.8115, -0.7903],
[-0.4502, -0.4714, -0.4502, ..., -0.8115, -0.7690, -0.7690]])]

[张量([[-0.2781, -0.2567, -0.2353, ..., -0.9640, -0.9855, -1.0069],
[-0.2781, -0.2567, -0.2353, ..., -1.0069, -1.02083] ],
[-0.2567, -0.2567, -0.2138, ..., -1.0712, -1.1141, -1.1784],
...,
[-0.6640, -0.6425, -0.6211, ..., -1.0712, -1.11114] -1.0927],
[-0.6640, -0.6425, -0.5997, ..., -0.9426, -0.9640, -0.9640],
[-0.6640, -0.6425, -0.5997, ..., -0.9640, -0.9640, -26, -26 ]]), 张量([[-0.0769, -0.0980, -0.076 9, ..., -0.9388, -0.9598, -0.9808],
[-0.0559, -0.0769, -0.0980, ..., -0.9598 1.0018, -1.0228],
[-0.0559, -0.0769, -0.0769, ..., -1.0228, -1.0439, -1.0859],
...,
[-0.4973,-0.4973,-0.4973,...,-1.0018,-1.0439,-1.0228],
[-0.4973,-0.4973,-0.4973,...,-0.8757,-0.9177,-0.9177],
[ - 0.4973, -0.4973, -0.4973, ..., -0.9177, -0.8967, -0.8967]]), 张量([[-0.1313, -0.1313, -0.110 0, ..., -0.8115, -8,0.3. ],
[-0.1313, -0.1525, -0.1313, ..., -0.8541, -0.8966, -0.9391],
[-0.1100, -0.1313, -0.1100, ..., -0.9391, -, -0.9816]
...,
[-0.4502, -0.4714, -0.4502, ..., -0.8966, -0.8966, -0.8966],
[-0.4502, -0.4714, -0.4502, ..., -0.8115, -0.81709,3 ],
[-0.4502, -0.4714, -0.4502, ..., -0.8115, -0.7690, -0.7690]])]

Is there a way of getting the shape of that list without converting it into a numpy array?

有没有办法在不将其转换为 numpy 数组的情况下获取该列表的形状?

回答by Shai

It seems like you have a list of tensors. For each tensor you can see its size()(no need to convert to list/numpy). If you insist, you can convert a tensor to numpy array using numpy():

看起来你有一个张量列表。对于每个张量,您都可以看到它size()(无需转换为列表/numpy)。如果您坚持,您可以使用numpy()以下方法将张量转换为 numpy 数组:

Return a list of tensor shapes:

返回张量形状列表:

>> [t.size() for t in my_list_of_tensors]

Returns a list of numpy arrays:

返回一个 numpy 数组列表:

>> [t.numpy() for t in my_list_of_tensors]

In terms of performance, it is always best to avoid casting of tensors into numpy arrays, as it may incur sync of device/host memory. If you only need to check the shapeof a tensor, use size()function.

在性能方面,最好避免将张量转换为 numpy 数组,因为它可能会导致设备/主机内存同步。如果您只需要检查shape张量的 ,请使用size()函数。

回答by Koke Cacao

The simplest way to convert pytorch tensor to numpy array is:

将 pytorch 张量转换为 numpy 数组的最简单方法是:

nparray = tensor.numpy()

Also, for size and shape:

此外,对于尺寸和形状:

tensor_size = tensor.size()
tensor_shape = tensor.shape()
tensor_size
>>> (1080)
tensor_shape
>>> (32, 3, 128, 128)