【问题标题】:Tensorflow unhashable type 'list' in sess.runsess.run 中的 Tensorflow 不可散列类型“列表”
【发布时间】:2017-03-14 01:08:07
【问题描述】:

实际上有数以千计的此类帖子,但我还没有看到一个解决我的确切问题的帖子。如果存在,请随时关闭它。

我知道列表在 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

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 大小的张量列表。

我在这里完全不知所措。我觉得我正盯着解决方案看,我看不到它。有人可以帮我从这里出去吗?

谢谢!

【问题讨论】:

    标签: python list numpy multidimensional-array tensorflow


    【解决方案1】:

    我不小心将变量x 设置为代码中的python 列表。

    为什么会抛出这个错误?因为_, loss = sess.run([optimizer, cost], feed_dict={x: batch_x, y: batch_y})batch_xbatch_y 之一是列表或元组。它们必须是tensor,因此打印出这两个变量类型以查看代码有什么问题。

    【讨论】:

      【解决方案2】:

      如果feed_dict={x: batch_x, y: batch_y} 中的xy 出于某种原因在列表中,也会出现此错误。就我而言,我将它们拼写为 XY,这些是我代码中的列表。

      【讨论】:

      • 这没有提供问题的答案。一旦你有足够的reputation,你就可以comment on any post;相反,provide answers that don't require clarification from the asker。 - From Review
      • @yivi 这可能不是 OP 在标题中收到错误消息的原因,但这绝对是该错误的可能解释。我刚刚犯了完全相同的错字,这个问题是错误消息的第一个谷歌结果,多亏了这个答案,我最终没有浪费很多时间来追踪一个愚蠢的错误。从我的立场来看,这是一个有效且有用的答案。
      • 感谢它帮助我解决了问题,确实我还有其他变量名为 X 和 Y。这被用作 sess.run 中的键
      【解决方案3】:

      我觉得这里有点傻,但我相信其他人会遇到这个问题。

      tf.split 导致列表的上面那一行是问题所在。

      我没有将这些拆分成单独的函数,而是直接修改了 x(如我的代码所示)并且从未更改过名称。所以当代码在sess.run 运行时,x 不再是预期的张量占位符,而是图中转换后的张量列表。

      重命名x 的每个转换解决了这个问题。

      我希望这对某人有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-05
        • 2017-07-11
        • 2018-04-24
        • 1970-01-01
        • 2022-01-11
        • 2020-03-28
        • 2014-02-16
        相关资源
        最近更新 更多