簡體   English   中英

sess.run中的Tensorflow unhashable類型'list'

[英]Tensorflow unhashable type 'list' in sess.run

這些帖子實際上有成千上萬,但我還沒有看到一個解決我確切問題的帖子。 如果存在,請隨時關閉。

我知道列表在Python中是可變的。 因此,我們無法將列表存儲為字典中的鍵。

我有以下代碼(因為它無關緊要而忽略了大量的代碼):

with tf.Session() as sess:
    sess.run(init)
    step = 1

    while step * batch_size < training_iterations:
            for batch_x, batch_y in batch(train_x, train_y, batch_size):

            batch_x = np.reshape(batch_x, (batch_x.shape[0],
                                           1,
                                           batch_x.shape[1]))
            batch_x.astype(np.float32)

            batch_y = np.reshape(batch_y, (batch_y.shape[0], 1))
            batch_y.astype(np.float32)

            sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})
            if step % display_step == 0:
                # Calculate batch accuracy
                acc = sess.run(accuracy,
                               feed_dict={x: batch_x, y: batch_y})
                # Calculate batch loss
                loss = sess.run(cost, feed_dict={x: batch_x, y: batch_y})
                print("Iter " + str(step*batch_size) +
                      ", Minibatch Loss= " +
                      "{:.6f}".format(loss) + ", Training Accuracy= " +
                      "{:.5f}".format(acc))
        step += 1
    print("Optimization Finished!")

train_x[batch_size, num_features] numpy矩陣

train_y[batch_size, num_results] numpy矩陣

我的圖表中有以下占位符:

x = tf.placeholder(tf.float32, shape=(None, num_steps, num_input))
y = tf.placeholder(tf.float32, shape=(None, num_res))

所以我很自然地需要轉換我的train_xtrain_y以獲得tensorflow所期望的正確格式。

我這樣做有以下幾點:

 batch_x = np.reshape(batch_x, (batch_x.shape[0],
                                1,
                                batch_x.shape[1]))

 batch_y = np.reshape(batch_y, (batch_y.shape[0], 1))

這個結果給了我兩個numpy.ndarray

batch_x的維度為[batch_size, timesteps, features] batch_y的維度為[batch_size, num_results]

如圖所示。

現在,當我傳遞這些重新形成的numpy.ndarray我得到TypeError: Unhashable type list以下行中的TypeError: Unhashable type list

sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})

這對我來說很奇怪,因為啟動python:

import numpy as np
a = np.zeros((10,3,4))
{a : 'test'}
TypeError: unhashable type: 'numpy.ndarray`

您可以看到我收到完全不同的錯誤消息。

在我的代碼中,我對數據執行了一系列轉換:

x = tf.transpose(x, [1, 0, 2])
x = tf.reshape(x, [-1, num_input])
x = tf.split(0, num_steps, x)


lstm_cell = rnn_cell.BasicLSTMCell(num_hidden, forget_bias=forget_bias)
outputs, states = rnn.rnn(lstm_cell, x, dtype=tf.float32)

並且列表出現的唯一位置是切片之后,這導致rnn.rnn期望的T大小的張量列表。

我在這里完全失敗了。 我覺得我正盯着解決方案,我看不到它。 有人可以幫我從這里出去嗎?

謝謝!

我覺得這里有點傻,但我相信別人會有這個問題。

tf.split導致列表的上面的行是問題。

我沒有將它們拆分成單獨的函數,並直接修改x(如我的代碼所示)並且從未更改過名稱。 因此,當代碼在sess.run運行時,x不再是預期的張sess.run符,而是圖形中轉換后的張量列表。

重命名x每個轉換解決了問題。

我希望這可以幫助別人。

如果feed_dict={x: batch_x, y: batch_y}中的xy出於某種原因列表feed_dict={x: batch_x, y: batch_y}也會發生此錯誤。 在我的情況下,我將它們拼錯為XY ,這些是我的代碼中的列表。

我不小心將變量x設置為代碼中的python列表。

為什么會拋出這個錯誤? 因為_, loss = sess.run([optimizer, cost], feed_dict={x: batch_x, y: batch_y})batch_xbatch_y其中一個是列表或元組。 它們必須是tensor ,因此打印兩個變量類型以查找代碼的錯誤。

暫無
暫無

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

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