簡體   English   中英

unhashable類型:tensorflow中的'numpy.ndarray'錯誤

[英]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')

這是錯誤:

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'

以下是我輸入的兩個numpy數組的形狀:

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

我不知道造成這種情況的原因。 我的所有形狀都匹配,我不應該有任何問題。 如果您需要更多信息,請與我們聯系。

編輯:從我對下面其中一個答案的評論,似乎我必須為我的占位符指定一個特定的大小。 None人不滿意。 當我改變它並重新運行我的代碼時,一切正常。 仍然不太確定為什么會這樣。

在我的例子中,問題是將輸入參數命名為與占位符變量相同。 當然,這會用輸入變量替換tensorflow變量; 導致feed_dict的不同鍵。

tensorflow變量是可清除的,但您的輸入參數(np.ndarray)不是。 因此,不可用的錯誤是您嘗試將參數作為鍵而不是tensorflow變量傳遞的結果。 一些代碼可視化我想說的內容:

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})

希望這可以幫助任何人在將來絆倒這個問題!

請仔細檢查您輸入“x_train / y_train”的數據類型以及由'tf.placeholder(...)'定義的張量“x / y_label”

我遇到了同樣的問題。 而原因就是x_train在我的代碼是“NP。float64”,但我通過tf.placeholder定義(什么)為TF。 float32 日期類型float64和float32不匹配。

我認為問題在於定義字典。 字典鍵必須是“可散列類型”,例如數字,字符串或元組是常見的。 列表或數組不起作用:

In [256]: {'x':np.array([1,2,3])}
Out[256]: {'x': array([1, 2, 3])}
In [257]: x=np.array([1,2,3])
In [258]: {x:np.array([1,2,3])}
...
TypeError: unhashable type: 'numpy.ndarray'

我不知道張量流是否足以知道它們是什么:

y_label = tf.placeholder(shape=[None,1], dtype=tf.float32, name='y_label')
x = tf.placeholder(shape=[None,3], dtype=tf.float32, name='x')

該錯誤表明它們是numpy數組,而不是字符串。 x是否具有name屬性?

或者字典應該指定為:

{'x': x_train, 'y_label': y_train}

奇怪,我也有這個問題。 在我關閉python shell並從文件中運行代碼后,即使在shell中也沒有成功重現它(它只是沒有錯誤)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM