繁体   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