我正在尝试创建一个具有架构的神经网络:输入 - >嵌入 - > LSTM - >输出。但是,我从LSTM小区收到错误说:
Traceback (most recent call last):
File "RNN.py", line 192, in <module>
dtype=tf.float32)
File "anaconda2-4.4.0/lib/python2.7/site-packages/tensorflow/python/ops/rnn.py", line 598, in dynamic_rnn
dtype=dtype)
File "anaconda2-4.4.0/lib/python2.7/site-packages/tensorflow/python/ops/rnn.py", line 761, in _dynamic_rnn_loop
swap_memory=swap_memory)
File "anaconda2-4.4.0/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2775, in while_loop
result = context.BuildLoop(cond, body, loop_vars, shape_invariants)
File "anaconda2-4.4.0/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2604, in BuildLoop
pred, body, original_loop_vars, loop_vars, shape_invariants)
File "anaconda2-4.4.0/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2554, in _BuildLoop
body_result = body(*packed_vars_for_body)
File "anaconda2-4.4.0/lib/python2.7/site-packages/tensorflow/python/ops/rnn.py", line 744, in _time_step
skip_conditionals=True)
File "anaconda2-4.4.0/lib/python2.7/site-packages/tensorflow/python/ops/rnn.py", line 236, in _rnn_step
new_output, new_state = call_cell()
File "anaconda2-4.4.0/lib/python2.7/site-packages/tensorflow/python/ops/rnn.py", line 732, in <lambda>
call_cell = lambda: cell(input_t, state)
File "anaconda2-4.4.0/lib/python2.7/site-packages/tensorflow/python/ops/rnn_cell_impl.py", line 180, in __call__
return super(RNNCell, self).__call__(inputs, state)
File "anaconda2-4.4.0/lib/python2.7/site-packages/tensorflow/python/layers/base.py", line 450, in __call__
outputs = self.call(inputs, *args, **kwargs)
File "anaconda2-4.4.0/lib/python2.7/site-packages/tensorflow/contrib/rnn/python/ops/core_rnn_cell.py", line 226, in call
output, res_state = self._cell(inputs, state)
File "anaconda2-4.4.0/lib/python2.7/site-packages/tensorflow/python/ops/rnn_cell_impl.py", line 180, in __call__
return super(RNNCell, self).__call__(inputs, state)
File "anaconda2-4.4.0/lib/python2.7/site-packages/tensorflow/python/layers/base.py", line 450, in __call__
outputs = self.call(inputs, *args, **kwargs)
File "anaconda2-4.4.0/lib/python2.7/site-packages/tensorflow/python/ops/rnn_cell_impl.py", line 938, in call
cur_inp, new_state = cell(cur_inp, cur_state)
File "anaconda2-4.4.0/lib/python2.7/site-packages/tensorflow/python/ops/rnn_cell_impl.py", line 774, in __call__
output, new_state = self._cell(inputs, state, scope)
File "anaconda2-4.4.0/lib/python2.7/site-packages/tensorflow/python/ops/rnn_cell_impl.py", line 180, in __call__
return super(RNNCell, self).__call__(inputs, state)
File "anaconda2-4.4.0/lib/python2.7/site-packages/tensorflow/python/layers/base.py", line 450, in __call__
outputs = self.call(inputs, *args, **kwargs)
File "anaconda2-4.4.0/lib/python2.7/site-packages/tensorflow/python/ops/rnn_cell_impl.py", line 554, in call
input_size = inputs.get_shape().with_rank(2)[1]
File "anaconda2-4.4.0/lib/python2.7/site-packages/tensorflow/python/framework/tensor_shape.py", line 637, in with_rank
raise ValueError("Shape %s must have rank %d" % (self, rank))
ValueError: Shape (9, 600, 256) must have rank 2
500是以下代码中的vocabsize
。 word_embedding_size
为256。batch_size
为9。
这是我建立的模型:
inputs = tf.placeholder(tf.float32, [batch_size, maxlength, vocabsize])
lengths = tf.placeholders(tf.int32, [batch_size])
embeddings = tf.Variable(tf.random_uniform([vocabsize, word_embedding_size], -1, 1)
lstm = {}
lstm[0] = tf.contrib.rnn.LSTMCell(hidden_layer_size, state_is_tuple=True, initializer=tf.contrib.layers.xavier_initializer(seed=random_seed))
lstm[0] = tf.contrib.rnn.DropoutWrapper(lstm[0], output_keep_prob=0.5)
lstm[0] = tf.contrib.rnn.MultiRNNCell(cells=[lstm[0]] * 1, state_is_tuple=True)
output_layer = {}
output_layer[0] = Layer.W(1 * hidden_layer_size, 20, 'OutputLayer')
output_bias = {}
output_bias[0] = Layer.b(20, 'OutputBias')
outputs = {}
fstate = {}
rnn_inputs = tf.nn.embedding_lookup(embeddings, inputs)
with tf.variable_scope("lstm0"):
# create the rnn graph at run time
outputs[0], fstate[0] = tf.nn.dynamic_rnn(lstm[0], run_inputs,
sequence_length=lengths,
dtype=tf.float32)
logits = {}
logits[0] = tf.matmul(tf.concat([f.h for f in fstate[0]], 1), output_layer[0]) + output_bias[0]
loss = {}
result = refblock = tf.nn.softmax(logits[0])
我不明白错误是什么,因为我认为tf.nn.dynamic_rnn
应该处理我的3D输入并以适当的格式传递给LSTM单元格,但看起来它不起作用,而我不明白为什么。
如果有人能帮助我解决问题,那就太棒了!
由于