• Register
Welcome to Kodlogs, programming questions and answer website.
0 votes
79 views

Problem :

I am trying to convert my list into an array using Python, 
But I got below error
 ValueError: only one element tensors can be converted to Python scalars.
by (6.9k points)  

1 Answer

0 votes

Solution :

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

Returns a list of tensor shapes:

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

Returns a list of numpy arrays:

>> [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, If you only need to check the shape of a tensor, use size() function.

Further Readings:

https://github.com/onnx/onnx/issues/2158

by (21.9k points)  
edited by
...