# Only one element tensors can be converted to Python scalars

Last updated: Apr 11, 2024
3 min

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

Here is an example of how the error occurs.

main.py
```Copied!```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
```Copied!```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
```Copied!```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
```Copied!```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`.

## #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
```Copied!```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
```Copied!```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
```Copied!```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.