Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unhashable type: 'numpy.ndarray' error in tensorflow

data = pd.read_excel("/Users/madhavthaker/Downloads/Reduced_Car_Data.xlsx")

train = np.random.rand(len(data)) < 0.8

data_train = data[train]
data_test = data[~train]


x_train = data_train.ix[:,0:3].values
y_train = data_train.ix[:,-1].values
x_test = data_test.ix[:,0:3].values
y_test = data_test.ix[:,-1].values

y_label = tf.placeholder(shape=[None,1], dtype=tf.float32, name='y_label')
x = tf.placeholder(shape=[None,3], dtype=tf.float32, name='x')
W = tf.Variable(tf.random_normal([3,1]), name='weights')
b = tf.Variable(tf.random_normal([1]), name='bias')
y = tf.matmul(x,W)  + b

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    summary_op = tf.summary.merge_all()
    #Fit all training data
    for epoch in range(1000):
        sess.run(train, feed_dict={x: x_train, y_label: y_train})

        # Display logs per epoch step
        if (epoch+1) % display_step == 0:
            c = sess.run(loss, feed_dict={x: x_train, y_label:y_train})
            print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(c), \
                "W=", sess.run(W), "b=", sess.run(b))

    print("Optimization Finished!")
    training_cost = sess.run(loss, feed_dict={x: x_train, y_label: y_train})
    print("Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n')

Here is the error:

x---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-37-50102cbac823> in <module>()
      6     #Fit all training data
      7     for epoch in range(1000):
----> 8         sess.run(train, feed_dict={x: x_train, y_label: y_train})
      9 
     10         # Display logs per epoch step

TypeError: unhashable type: 'numpy.ndarray'

Here are the shapes of both of the numpy arrays that I am inputting:

y_train.shape = (78,)
x_train.shape = (78, 3)

I have no idea what is causing this. All of my shapes match up and I shouldn't have any issues. Let me know if you need any more information.

Edit: From my comment on one of the answers below, it seems as though I had to specify a specific size for my placeholders. None was not satisfactory. When I changed that and re-ran my code, everything worked fine. Still not quite sure why that is.

like image 344
madsthaks Avatar asked Mar 28 '17 23:03

madsthaks


People also ask

How do I fix Unhashable type NumPy Ndarray?

We can solve this by adding each array element instead of the array object into the set. This should add all the elements of the array to the set.

What does Unhashable type NumPy Ndarray mean?

by Suf | Programming, Python, Tips. The error TypeError: unhashable type: 'numpy. ndarray' occurs when trying to get a hash of a NumPy ndarray. For example, using an ndarray as a key in a Python dictionary because you can only use hashable data types as a key.

How do I fix Unhashable type error?

This error occurs when trying to hash a list, which is an unhashable object. For example, using a list as a key in a Python dictionary will cause this error since dictionaries only accept hashable data types as a key. The standard way to solve this issue is to cast a list to a tuple, which is a hashable data type.

What is Unhashable type in Python?

TypeError: unhashable type: 'list' usually means that you are trying to use a list as an hash argument. This means that when you try to hash an unhashable object it will result an error. For ex. when you use a list as a key in the dictionary , this cannot be done because lists can't be hashed.


2 Answers

In my case, the problem was naming the input parameter the same as the placeholder variable. This, of course, replaces your tensorflow variable with the input variable; resulting in a different key for the feed_dict.

A tensorflow variable is hashable, but your input parameter (np.ndarray) isn't. The unhashable error is therefore a result of you trying to pass your parameter as the key instead of a tensorflow variable. Some code to visualize what I'm trying to say:

a = tf.placeholder(dtype=tf.float32, shape=[1,2,3])
b = tf.identity(a)

with tf.Session() as sess:
    your_var = np.ones((1,2,3))
    a = your_var
    sess.run(b, feed_dict={a: a})

Hopes this helps anyone stumbling upon this problem in the future!

like image 188
Andreas Forslöw Avatar answered Nov 05 '22 14:11

Andreas Forslöw


Please carefully check the datatype you feed "x_train/y_train" and the tensor "x/y_label" you defined by 'tf.placeholder(...)'

I have met the same problem with you. And the reason is x_train in my code is "np.float64", but what I defined by tf.placeholder() is tf.float32. The date type float64 and float32 is mismatching.

like image 39
zero Avatar answered Nov 05 '22 15:11

zero