使用Tensorflow 0.12时,MultiRNNCell
的工作方式发生了变化,对于初学者,state_is_tuple
现在默认设置为True
,此外,还有对此进行讨论:
state_is_tuple
:如果True
,则接受和返回的状态为n
- 元组,其中n = len(cells)
。如果False
,则状态都沿列轴连接。后一种行为很快就会被弃用。
我想知道我是如何使用GRU单元的多层RNN的,到目前为止我的代码是:
def _run_rnn(self, inputs):
# embedded inputs are passed in here
self.initial_state = tf.zeros([self._batch_size, self._hidden_size], tf.float32)
cell = tf.nn.rnn_cell.GRUCell(self._hidden_size)
cell = tf.nn.rnn_cell.DropoutWrapper(cell, output_keep_prob=self._dropout_placeholder)
cell = tf.nn.rnn_cell.MultiRNNCell([cell] * self._num_layers, state_is_tuple=False)
outputs, last_state = tf.nn.dynamic_rnn(
cell = cell,
inputs = inputs,
sequence_length = self.sequence_length,
initial_state = self.initial_state
)
return outputs, last_state
我的输入查找单词ID并返回相应的嵌入向量。现在,运行上面的代码我受到以下错误的欢迎:
ValueError: Dimension 1 in both shapes must be equal, but are 100 and 200 for 'rnn/while/Select_1' (op: 'Select') with input shapes: [?], [64,100], [64,200]
我有?
的地方在我的占位符中:
def _add_placeholders(self):
self.input_placeholder = tf.placeholder(tf.int32, shape=[None, self._max_steps])
self.label_placeholder = tf.placeholder(tf.int32, shape=[None, self._max_steps])
self.sequence_length = tf.placeholder(tf.int32, shape=[None])
self._dropout_placeholder = tf.placeholder(tf.float32)
答案 0 :(得分:2)
您的主要问题在于initial_state
的设置。由于您的州现在是一个元组(更具体地说是LSTMStateTuple
),因此您无法直接将其分配给tf.zeros
。而是使用,
self.initial_state = cell.zero_state(self._batch_size, tf.float32)
请查看documentation了解更多信息。
要在代码中使用此功能,您需要在feed_dict
中传递此 tensor 。做这样的事情,
state = sess.run(model.initial_state)
for batch in batches:
# Logic to add input placeholder in `feed_dict`
feed_dict[model.initial_state] = state
# Note I'm re-using `state` below
(loss, state) = sess.run([model.loss, model.final_state], feed_dict=feed_dict)