Programmers learn & share
0 votes

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)   | 797 views

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:

by (36.1k points)  
edited by
2,183 questions
2,487 answers
241 users