Last updated: Apr 11, 2024
Reading timeยท3 min
The PyTorch "ValueError: only one element tensors can be converted to Python scalars " occurs when you try to convert tensors that contain multiple elements to Python scalars.
To solve the error, stack the tensors before converting them to Python scalars.
Here is an example of how the error occurs.
import torch a_list = [torch.tensor([3., 4])] * 3 # โ๏ธ ValueError: only one element tensors can be converted to Python scalars x = torch.FloatTensor(a_list)
The torch.FloatTensor()
class can only convert a list of tensors if they
contain only one element.
For example, the following code sample is valid.
import torch a_list = [torch.tensor(3.)] * 3 # ๐๏ธ [tensor(3.), tensor(3.), tensor(3.)] print(a_list) x = torch.FloatTensor(a_list) print(x) # ๐๏ธ tensor([3., 3., 3.])
Notice that the tensors in the list only contain one element.
This is valid and works as expected.
If your tensors contain multiple elements, use the torch.stack() method.
import torch a_list = [torch.tensor([3., 4])] * 3 # ๐๏ธ [tensor([3., 4.]), tensor([3., 4.]), tensor([3., 4.])] print(a_list) x = torch.stack(a_list) # tensor([[3., 4.], # [3., 4.], # [3., 4.]]) print(x)
The torch.stack() method concatenates a sequence of tensors along a new dimension.
Note that all tensors must be of the same size.
The method returns the output tensor.
If you want to convert the result to a NumPy array, call the numpy()
method on
the tensor.
a_list = [torch.tensor([3., 4])] * 3 # [tensor([3., 4.]), tensor([3., 4.]), tensor([3., 4.])] print(a_list) x = torch.stack(a_list).numpy() # tensor([[3., 4.], # [3., 4.], # [3., 4.]]) print(x) print(type(x)) # ๐๏ธ <class 'numpy.ndarray'>
The
tensor.numpy()
method returns the tensor as a NumPy ndarray
.
size()
method if you need to get the sizes of a list of tensorsIf you need to get the sizes of a list of tensors:
import torch a_list = [torch.tensor([3., 4])] * 3 sizes = [t.size() for t in a_list] # ๐๏ธ [torch.Size([2]), torch.Size([2]), torch.Size([2])] print(sizes)
We used a list comprehension to iterate over the list of tensors.
On each iteration, we call the size()
method on the tensor.
The tensor.size()
method returns the size of the tensor.
There is also a shape
attribute that returns the size of the tensor.
import torch a_list = [torch.tensor([3., 4])] * 3 sizes = [t.shape for t in a_list] # ๐๏ธ [torch.Size([2]), torch.Size([2]), torch.Size([2])] print(sizes)
The code sample achieves the same result using the tensor.shape attribute.
numpy()
method to convert a list of tensors to a list of NumPy arraysIf you need to convert a list of tensors to a list of NumPy arrays, call the
numpy()
method on each tensor.
import torch a_list = [torch.tensor([3., 4])] * 3 a_list = [torch.tensor([3., 4])] * 3 arrays = [t.numpy() for t in a_list] # ๐๏ธ [array([3., 4.], dtype=float32), # array([3., 4.], dtype=float32), # array([3., 4.], dtype=float32)] print(arrays)
Note that it is more performant to use the size()
method if you only need to
get the size of each tensor.
Converting each tensor to a NumPy array is not as performant.
You can learn more about the related topics by checking out the following tutorials: