有成千上万的这些帖子,但我还没有看到一个解决我的确切问题。如果已经有了,请随意关闭它。
我知道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_x
和train_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)
只有在切片后,列表才会出现,这导致了一个T
大小的张量列表,是rnn.rnn
所期望的。
我完全不知所措。感觉就站在解决方案前面却看不见它。有人能帮我吗?
谢谢!