[英]Tensorflow unhashable type 'list' in sess.run
There are literally thousands of these posts but I haven't seen one yet that addresses my exact problem. 这些帖子实际上有成千上万,但我还没有看到一个解决我确切问题的帖子。 Please feel free to close this if one exists. 如果存在,请随时关闭。
I understand that lists are mutable in Python. 我知道列表在Python中是可变的。 As a result, we cannot store a list as a key in a dictionary. 因此,我们无法将列表存储为字典中的键。
I have the following code (a ton of it is left out because it is irrelevant): 我有以下代码(因为它无关紧要而忽略了大量的代码):
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
is a [batch_size, num_features]
numpy matrix train_x
是[batch_size, num_features]
numpy矩阵
train_y
is a [batch_size, num_results]
numpy matrix train_y
是[batch_size, num_results]
numpy矩阵
I have the following placeholders in my graph: 我的图表中有以下占位符:
x = tf.placeholder(tf.float32, shape=(None, num_steps, num_input))
y = tf.placeholder(tf.float32, shape=(None, num_res))
So naturally I need to transform my train_x
and train_y
to get to the correct format tensorflow expects. 所以我很自然地需要转换我的train_x
和train_y
以获得tensorflow所期望的正确格式。
I do that with the following: 我这样做有以下几点:
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))
This result gives me two numpy.ndarray
: 这个结果给了我两个numpy.ndarray
:
batch_x
is of dimensions [batch_size, timesteps, features]
batch_y
is of dimensions [batch_size, num_results]
batch_x
的维度为[batch_size, timesteps, features]
batch_y
的维度为[batch_size, num_results]
As expected by our graph. 如图所示。
Now when I pass these reshaped numpy.ndarray
I get TypeError: Unhashable type list
on the following line: 现在,当我传递这些重新形成的numpy.ndarray
我得到TypeError: Unhashable type list
以下行中的TypeError: Unhashable type list
:
sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})
This seems strange to me because firing up python: 这对我来说很奇怪,因为启动python:
import numpy as np
a = np.zeros((10,3,4))
{a : 'test'}
TypeError: unhashable type: 'numpy.ndarray`
You can see I get an entirely different error message. 您可以看到我收到完全不同的错误消息。
Further in my code I perform a series of transformations on the data: 在我的代码中,我对数据执行了一系列转换:
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)
And the only place a list occurs is after slicing, which results in a T
sized list of tensors that rnn.rnn
expects. 并且列表出现的唯一位置是切片之后,这导致rnn.rnn
期望的T
大小的张量列表。
I am at a complete loss here. 我在这里完全失败了。 I feel like I'm staring right at the solution and I can't see it. 我觉得我正盯着解决方案,我看不到它。 Can anyone help me out here? 有人可以帮我从这里出去吗?
Thank you! 谢谢!
I feel kind of silly here but I am sure someone else will have this problem. 我觉得这里有点傻,但我相信别人会有这个问题。
The line above where the tf.split
results in a list is the problem. tf.split
导致列表的上面的行是问题。
I did not split these into separate functions, and modified x directly (as shown in my code) and never changed the names. 我没有将它们拆分成单独的函数,并直接修改x(如我的代码所示)并且从未更改过名称。 So when the code ran in sess.run
, x was no longer a tensor placeholder as expected but rather a list of tensors after transformation in the graph. 因此,当代码在sess.run
运行时,x不再是预期的张sess.run
符,而是图形中转换后的张量列表。
Renaming each transformation of x
solved the problem. 重命名x
每个转换解决了问题。
I hope this helps someone. 我希望这可以帮助别人。
This error also occurs if x
and y
in feed_dict={x: batch_x, y: batch_y}
are for some reason lists. 如果feed_dict={x: batch_x, y: batch_y}
中的x
和y
出于某种原因列表feed_dict={x: batch_x, y: batch_y}
也会发生此错误。 In my case I misspelled them as X
and Y
and these were lists in my code. 在我的情况下,我将它们拼错为X
和Y
,这些是我的代码中的列表。
I accidentally set the variable x
as a python list in the code. 我不小心将变量x
设置为代码中的python列表。
Why it threw this error? 为什么会抛出这个错误? because of _, loss = sess.run([optimizer, cost], feed_dict={x: batch_x, y: batch_y})
, batch_x
or batch_y
one of either is a list or tuple. 因为_, loss = sess.run([optimizer, cost], feed_dict={x: batch_x, y: batch_y})
, batch_x
或batch_y
其中一个是列表或元组。 They must be a tensor
, so print the two variables types out for looking what's wrong with the code. 它们必须是tensor
,因此打印两个变量类型以查找代码的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.