使用glstm(Group LSTM)单元在tensorflow中构建双向rnn

时间:2017-07-21 10:25:52

标签: python tensorflow ocr

我使用cnn + lstm + ctc网络(基于https://arxiv.org/pdf/1507.05717.pdf)来进行中文场景文本识别。对于大量的课程(3500+),网络很难训练。我听说使用LSTM组(https://arxiv.org/abs/1703.10722,O。Kuchaiev和B. Ginsburg" LSTM Networks的分解技巧",ICLR 2017研讨会。)可以减少参数数量并加速培训,所以我已尝试在我的代码中使用它。

我使用双层双向lstm。这是使用tf.contrib.rnn.LSTMCell

的原始代码
rnn_outputs, _, _ = 
tf.contrib.rnn.stack_bidirectional_dynamic_rnn(
[tf.contrib.rnn.LSTMCell(num_units=self.num_hidden, state_is_tuple=True) for _ in range(self.num_layers)],
[tf.contrib.rnn.LSTMCell(num_units=self.num_hidden, state_is_tuple=True) for _ in range(self.num_layers)], 
self.rnn_inputs, dtype=tf.float32, sequence_length=self.rnn_seq_len, scope='BDDLSTM')

训练非常缓慢。 100小时后,测试集的预测值仍为39%。

现在我想使用tf.contrib.rnn.GLSTMCell。当我用这个GLSTMCell替换LSTMCell时

rnn_outputs, _, _ = tf.contrib.rnn.stack_bidirectional_dynamic_rnn(
[tf.contrib.rnn.GLSTMCell(num_units=self.num_hidden, num_proj=self.num_proj, number_of_groups=4) for _ in range(self.num_layers)],
[tf.contrib.rnn.GLSTMCell(num_units=self.num_hidden, num_proj=self.num_proj, number_of_groups=4) for _ in range(self.num_layers)],
self.rnn_inputs, dtype=tf.float32, sequence_length=self.rnn_seq_len, scope='BDDLSTM')

我收到以下错误

/home/frisasz/miniconda2/envs/dl/bin/python "/media/frisasz/DATA/FSZ_Work/deep learning/IDOCR_/work/train.py"
Traceback (most recent call last):
  File "/media/frisasz/DATA/FSZ_Work/deep learning/IDOCR_/work/train.py", line 171, in <module>
    train(train_dir='/media/frisasz/Windows/40T/', val_dir='../../0000/40V/')
  File "/media/frisasz/DATA/FSZ_Work/deep learning/IDOCR_/work/train.py", line 41, in train
    FLAGS.momentum)
  File "/media/frisasz/DATA/FSZ_Work/deep learning/IDOCR_/work/model.py", line 61, in __init__
    self.logits = self.rnn_net()
  File "/media/frisasz/DATA/FSZ_Work/deep learning/IDOCR_/work/model.py", line 278, in rnn_net
    self.rnn_inputs, dtype=tf.float32, sequence_length=self.rnn_seq_len, scope='BDDLSTM')
  File "/home/frisasz/miniconda2/envs/dl/lib/python2.7/site-packages/tensorflow/contrib/rnn/python/ops/rnn.py", line 220, in stack_bidirectional_dynamic_rnn
    dtype=dtype)
  File "/home/frisasz/miniconda2/envs/dl/lib/python2.7/site-packages/tensorflow/python/ops/rnn.py", line 375, in bidirectional_dynamic_rnn
    time_major=time_major, scope=fw_scope)
  File "/home/frisasz/miniconda2/envs/dl/lib/python2.7/site-packages/tensorflow/python/ops/rnn.py", line 574, in dynamic_rnn
    dtype=dtype)
  File "/home/frisasz/miniconda2/envs/dl/lib/python2.7/site-packages/tensorflow/python/ops/rnn.py", line 737, in _dynamic_rnn_loop
    swap_memory=swap_memory)
  File "/home/frisasz/miniconda2/envs/dl/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2770, in while_loop
    result = context.BuildLoop(cond, body, loop_vars, shape_invariants)
  File "/home/frisasz/miniconda2/envs/dl/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2599, in BuildLoop
    pred, body, original_loop_vars, loop_vars, shape_invariants)
  File "/home/frisasz/miniconda2/envs/dl/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2549, in _BuildLoop
    body_result = body(*packed_vars_for_body)
  File "/home/frisasz/miniconda2/envs/dl/lib/python2.7/site-packages/tensorflow/python/ops/rnn.py", line 720, in _time_step
    skip_conditionals=True)
  File "/home/frisasz/miniconda2/envs/dl/lib/python2.7/site-packages/tensorflow/python/ops/rnn.py", line 206, in _rnn_step
    new_output, new_state = call_cell()
  File "/home/frisasz/miniconda2/envs/dl/lib/python2.7/site-packages/tensorflow/python/ops/rnn.py", line 708, in <lambda>
    call_cell = lambda: cell(input_t, state)
  File "/home/frisasz/miniconda2/envs/dl/lib/python2.7/site-packages/tensorflow/python/ops/rnn_cell_impl.py", line 180, in __call__
    return super(RNNCell, self).__call__(inputs, state)
  File "/home/frisasz/miniconda2/envs/dl/lib/python2.7/site-packages/tensorflow/python/layers/base.py", line 441, in __call__
    outputs = self.call(inputs, *args, **kwargs)
  File "/home/frisasz/miniconda2/envs/dl/lib/python2.7/site-packages/tensorflow/contrib/rnn/python/ops/rnn_cell.py", line 2054, in call
    R_k = _linear(x_g_id, 4 * self._group_shape[1], bias=False)
  File "/home/frisasz/miniconda2/envs/dl/lib/python2.7/site-packages/tensorflow/python/ops/rnn_cell_impl.py", line 1005, in _linear
    "but saw %s" % (shape, shape[1]))
ValueError: linear expects shape[1] to be provided for shape (?, ?), but saw ?

Process finished with exit code 1

我不确定GLSTMCell是否可以简单地替换tf.contrib.rnn.stack_bidirectional_dynamic_rnn()中的LSTMCell(或其他有助于构建rnn的函数)。我没有找到任何使用GLSTMCell的例子。有人知道用GLSTMCell构建双向rnn的正确方法吗?

1 个答案:

答案 0 :(得分:1)

我尝试使用bidirectional_dynamic_rnn构建双向GLSTM时出现完全相同的错误。

在我的情况下,问题来自GLSTM只能在以静态方式定义时使用的事实:当计算图形时,您不能有未定义的形状参数(例如batch_size)。

因此,尝试在图形中定义将在GLSTM单元格中的某个点结束的所有形状,它应该可以正常工作。