【发布时间】: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_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)
列表出现的唯一位置是在切片之后,这会产生rnn.rnn 期望的T 大小的张量列表。
我在这里完全不知所措。我觉得我正盯着解决方案看,我看不到它。有人可以帮我从这里出去吗?
谢谢!
【问题讨论】:
标签: python list numpy multidimensional-array tensorflow