• Register
search
Log In
0 votes
969 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.
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

36.1k points
edited by

Related questions

0 votes
1 answer 137 views
137 views
I was trying to write a programm using numpy and matplotlib. I am new to python programming. The code I wrote is: import numpy import matplotlib.pyplot def ThisFunction(a): return numpy.int(a) a = numpy.arange(3, 13.5, 0.7) matplotlib.pyplot.plot(a, ... 4, in ThisFunction return numpy.int(a) TypeError: only size-1 arrays can be converted to Python scalars how can I solve this problem?
asked Mar 15 LizzyM 5.7k points
0 votes
1 answer 1.2K views
1.2K views
Problem : I am getting below error: TypeError: only length-1 arrays can be converted to Python scalars How can I fix it?
asked Oct 21, 2019 peterlaw 6.9k points
0 votes
1 answer 258 views
258 views
Problem : I am having below error in a title, and I don't know what's wrong. It was working when I used np.hstack instead of the np.append, but I want to make this faster, so using append. time_list the list of floats heights is the 1d np.array of ... <module> axe_x.append(time_list[np.arange(j+n,j+(time_interval-n))]) TypeError: only integer arrays with one element can be converted to an index
asked Jan 24 jwilliam 3.9k points
2 votes
2 answers 49 views
49 views
Problem: I wrote the following code in python using numpy. But it gives me some error message. The code I wrote is: import numpy as np arrayone = np.array(['Human', 'Birds', 'Groots', 'Oats']) arraytwo = np.array(['Beach', 'Toast']) arraythree ... concatenate TypeError: only integer scalar arrays can be converted to a scalar index Can anyone sort out this error and solve this. Thanks in advance.
asked May 6 samhaz 5k points
1 vote
1 answer 17 views
17 views
Problem: I am doing a online course on python. I got an assginement from there. So I was trying to write a programm using numpy and matplotlib. I am new to python programming. The code I wrote is: import numpy import matplotlib.pyplot def ThisFunction(a): return ... .int(a) TypeError: only size-1 arrays can be converted to Python scalars Do anyone know why this error is happening, please help.
asked Apr 3 LizzyM 5.7k points