Only one element tensors can be converted to Python scalars

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
3 min

banner

# Only one element tensors can be converted to Python scalars

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.

valueerror only one element tensors can be converted to python scalars

Here is an example of how the error occurs.

main.py
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.

main.py
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.])

converting one element tensors to python scalars

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.

main.py
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)

using torch stack before converting to python scalars

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.

main.py
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'>

convert output tensor to numpy array

The tensor.numpy() method returns the tensor as a NumPy ndarray.

# Use the size() method if you need to get the sizes of a list of tensors

If you need to get the sizes of a list of tensors:

  1. Use a list comprehension to iterate over the list.
  2. Use the tensor.size() method to get the size of each tensor.
main.py
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)

get size of each tensor

We used a list comprehension to iterate over the list of tensors.

List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

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.

main.py
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.

# Use the numpy() method to convert a list of tensors to a list of NumPy arrays

If you need to convert a list of tensors to a list of NumPy arrays, call the numpy() method on each tensor.

main.py
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)

call numpy method on each tensor

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev