Skip to content Skip to sidebar Skip to footer

What Is The Solution Python Gives Me "valueerror: Setting An Array Element With A Sequence."

I am running the code below but it's giving me an error about arrays. I have tried to find a solution and somehow understand the problem but I couldn't solve the problem. Here is m

Solution 1:

One action that produces this error is assigning a list to an array element:

In [498]: x=np.zeros(3)
In [499]: x
Out[499]: array([ 0.,  0.,  0.])
In [500]: x[0] = [1,2,3]
....
ValueError: setting an array element with a sequence.

Since the error is in a np.asarray(subfeed_val, dtype=subfeed_dtype) statement it is more likely that it is doing something like:

In [502]: np.array([[1,2,3],[1,2]], dtype=int)
ValueError: setting an array element with a sequence.

It's still the problem of trying to put a sequence of numbers into one slot.

Looking further up the error stack, the error is in:

sess.run([optimizer, cross_entropy], feed_dict={x: x_train, y:[t fortin y_train.as_matrix()]})

I don't think it has to do with the assignment to c.

sess.run is a tensorflow function that I know nothing about.

================

the error stack, properly formatted is

Traceback (most recent call last): 
File "NeuralNet.py", line 92, in train_neural_network(x) 
File "NeuralNet.py", line 83, in train_neural_network
   _, c = sess.run([optimizer, cross_entropy], feed_dict={x: x_train, y:[t for t in y_train.as_matrix()]}) 
File "/home/jusuf/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 717, in    
    runrun_metadata_ptr) 
File "/home/jusuf/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 888, in 
   _run np_val = np.asarray(subfeed_val, dtype=subfeed_dtype) 
File "/home/jusuf/anaconda3/lib/python3.5/site-packages/numpy/core/numeric.py", line 482, in 
   asarray returnarray(a, dtype, copy=False, order=order) 
ValueError: setting an array element with a sequence.

I'd suggest reviewing the tensorflow documentation, and make sure that inputs to this function are correct. Focus on allowed types, and if arrays, pay attention to dimensions, shape, and dtype.

Solution 2:

Error message is;

ValueError: setting an array element with a sequence.

Explains that: You're trying to set an array element with a sequence. Well, where is the mistake, see below;

c = sess.run([optimizer, cross_entropy]

So tell me what is the "c" . It's probably a float, integer or whatever. But I am pretty sure it is not an array. That's why you are receiving an exception above.

But if you want to print out that array, you can do directly;

print(sess.run([optimizer, cross_entropy]) instead of run print(c)

As far as I saw from your code, you are not using "c" anywhere.

Post a Comment for "What Is The Solution Python Gives Me "valueerror: Setting An Array Element With A Sequence.""