所以目前我正处于文本分类问题上,但是我什至无法在Tensorflow中设置模型。我有一批长度为70(使用填充)的句子,我使用的嵌入大小为300的embedding_lookup。这里是嵌入代码:
embedding = tf.constant(embedding_matrix, name="embedding")
inputs = tf.nn.embedding_lookup(embedding, input_.input_data)
因此,现在输入的形状应为 [batch_size,s句子长度,embedding_size] ,这并不奇怪。现在可悲的是我为我的LSTMCell得到了ValueError,因为它期望ndim = 2,显然输入是ndim = 3。我还没有找到更改LSTM层的预期输入形状的方法。这是我的LSTMCell初始化的代码:
for i in range(num_layers):
cells.append(LSTMCell(num_units, forget_bias, state_is_tuple, reuse=reuse, name='lstm_{}'.format(i))
cell = tf.contrib.rnn.MultiRNNCell(cells, state_is_tuple=True)
该错误在单元格的调用函数中触发,如下所示:
for step in range(self.num_steps):
if step > 0: tf.get_variable_scope().reuse_variables()
(cell_output, state) = cell(inputs[:, step, :], state)
类似的问题,但没有帮助:Understanding Tensorflow LSTM Input shape
答案 0 :(得分:0)
我可以自己解决问题。看起来,LSTMCell实现相对于LSTM的实际工作方式更为实用和基础。 Keras LSTM层负责处理我在使用TensorFlow时需要考虑的事项。我使用的示例来自以下官方TensorFlow示例:
https://github.com/tensorflow/models/tree/master/tutorials/rnn/ptb
由于我们要为LSTM图层提供序列,因此我们需要逐个单词地为单元格提供单词。当单元的调用创建两个输出(单元输出和单元状态)时,我们对所有句子中的所有单词使用循环,以馈送单元并重用单元状态。这样,我们为图层创建了输出,然后可以将其用于进一步的操作。此代码如下所示:
self._initial_state = cell.zero_state(config.batch_size, data_type())
state = self._initial_state
outputs = []
with tf.variable_scope("RNN"):
for time_step in range(self.num_steps):
if time_step > 0: tf.get_variable_scope().reuse_variables()
(cell_output, state) = cell(inputs[:, time_step, :], state)
outputs.append(cell_output)
output = tf.reshape(tf.concat(outputs, 1), [-1, config.hidden_size])
num_steps 表示我们将要使用的句子中的单词数量。