Only one element tensors can be converted to Python scalars

Borislav Hadzhiev

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.

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.])`

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)`

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'>`

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:

- Use a list comprehension to iterate over the list.
- 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)`

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.

`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.

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)`

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:

- Convert a NumPy array to 0 or 1 based on threshold in Python
- How to get the length of a 2D Array in Python
- TypeError: 'numpy.ndarray' object is not callable in Python
- TypeError: Object of type ndarray is not JSON serializable
- IndexError: too many indices for array in Python [Solved]
- How to replace None with NaN in Pandas DataFrame
- FutureWarning: pandas.Int64Index is deprecated and will be removed from pandas in a future version
- You are trying to merge on int64 and object columns [Fixed]
- Python: Can't call numpy() on Tensor that requires grad