我正在尝试根据这个指南构建基于LSTM的RNN:http://monik.in/a-noobs-guide-to-implementing-rnn-lstm-using-tensorflow/我的输入是一个大小为89102*39的ndarray(89102行,39个特征)。数据有3个标签 – 0,1,2。看起来我的占位符定义有问题,但我并不确定问题出在哪里。
我的代码是:
data = tf.placeholder(tf.float32, [None, 1000, 39]) target = tf.placeholder(tf.float32, [None, 3]) cell = tf.nn.rnn_cell.LSTMCell(self.num_hidden) val, state = tf.nn.dynamic_rnn(cell, data, dtype=tf.float32) val = tf.transpose(val, [1, 0, 2]) last = tf.gather(val, int(val.get_shape()[0]) - 1) weight = tf.Variable(tf.truncated_normal([self.num_hidden, int(target.get_shape()[1])])) bias = tf.Variable(tf.constant(0.1, shape=[target.get_shape()[1]])) prediction = tf.nn.softmax(tf.matmul(last, weight) + bias) cross_entropy = -tf.reduce_sum(target * tf.log(tf.clip_by_value(prediction, 1e-10, 1.0))) optimizer = tf.train.AdamOptimizer() minimize = optimizer.minimize(cross_entropy) mistakes = tf.not_equal(tf.argmax(target, 1), tf.argmax(prediction, 1)) error = tf.reduce_mean(tf.cast(mistakes, tf.float32)) init_op = tf.initialize_all_variables() sess = tf.Session() sess.run(init_op) batch_size = 1000 no_of_batches = int(len(train_input) / batch_size) epoch = 5000 for i in range(epoch): ptr = 0 for j in range(no_of_batches): inp, out = train_input[ptr:ptr + batch_size], train_output[ptr:ptr + batch_size] ptr += batch_size sess.run(minimize, {data: inp, target: out}) print( "Epoch - ", str(i))
我得到以下错误:
File , line 133, in execute_graphsess.run(minimize, {data: inp, target: out}) File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 789, in run run_metadata_ptr) File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 975, in _run % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))ValueError: Cannot feed value of shape (1000, 39) for Tensor 'Placeholder:0', which has shape '(1000, 89102, 39)'
有什么想法可能会导致这个问题吗?
回答:
如这里所指出的,dynamic_rnn
函数接受形状为
[batch_size, truncated_backprop_length, input_size]
在你提供的链接中,占位符的形状是
data = tf.placeholder(tf.float32, [None, 20,1])
这意味着他们选择了truncated_backprop_length=20
和input_size=1
。
他们的数据是以下3D
数组:
[ array([[0],[0],[1],[0],[0],[1],[0],[1],[1],[0],[0],[0],[1],[1],[1],[1],[1],[1],[0],[0]]), array([[1],[1],[0],[0],[0],[0],[1],[1],[1],[1],[1],[0],[0],[1],[0],[0],[0],[1],[0],[1]]), .....]
根据你的代码,看起来train_input
是一个2D
数组而不是3D
数组。因此,你需要将其转换为3D
数组。为此,你需要决定要为truncated_backprop_length
和input_size
使用哪些参数。之后,你需要适当地定义data
。
例如,如果你希望truncated_backprop_length
和input_size
分别为39和1,你可以这样做
import numpy as nptrain_input=np.reshape(train_input,(len(train_input),39,1))data = tf.placeholder(tf.float32, [None, 39,1])
我根据上述讨论更改了你的代码,并在一些我生成的随机数据上运行它。它运行时没有抛出错误。请看下面的代码: