2017-04-08 70 views
0

我正在嘗試訓練一個帶ctc丟失的blstm語音序列識別。我設法得到的代碼運行,但訓練在第二時代,出現以下錯誤的一個具體步驟失敗:ctc blstm在訓練過程中出現keras錯誤

W tensorflow/core/framework/op_kernel.cc:993] Invalid argument: slice index 0 of dimension 0 out of bounds. 
    [[Node: ctc/scan/strided_slice = StridedSlice[Index=DT_INT32, T=DT_INT32, begin_mask=0, ellipsis_mask=0, end_mask=0, new_axis_mask=0, shrink_axis_mask=1, _device="/job:localhost/replica:0/task:0/gpu:0"](ctc/scan/Shape, ctc/scan/strided_slice/stack, ctc/scan/strided_slice/stack_1, ctc/scan/strided_slice/stack_2)]] 
Traceback (most recent call last): 
    File "speech_lstm_ctc.py", line 212, in <module> 
    callbacks=[earlystopping, checkpoint, data_gen]) 
    File "/usr/local/lib/python2.7/dist-packages/keras/legacy/interfaces.py", line 88, in wrapper 
    return func(*args, **kwargs) 
    File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1876, in fit_generator 
    class_weight=class_weight) 
    File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1620, in train_on_batch 
    outputs = self.train_function(ins) 
    File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 2073, in __call__ 
    feed_dict=feed_dict) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 767, in run 
    run_metadata_ptr) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 965, in _run 
    feed_dict_string, options, run_metadata) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1015, in _do_run 
    target_list, options, run_metadata) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1035, in _do_call 
    raise type(e)(node_def, op, message) 
tensorflow.python.framework.errors_impl.InvalidArgumentError: slice index 0 of dimension 0 out of bounds. 
    [[Node: ctc/scan/strided_slice = StridedSlice[Index=DT_INT32, T=DT_INT32, begin_mask=0, ellipsis_mask=0, end_mask=0, new_axis_mask=0, shrink_axis_mask=1, _device="/job:localhost/replica:0/task:0/gpu:0"](ctc/scan/Shape, ctc/scan/strided_slice/stack, ctc/scan/strided_slice/stack_1, ctc/scan/strided_slice/stack_2)]] 
    [[Node: ctc/scan/while/Less_1/_77 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_981_ctc/scan/while/Less_1", tensor_type=DT_BOOL, _device="/job:localhost/replica:0/task:0/cpu:0"](^_cloopctc/scan/while/TensorArrayReadV3/_17)]] 

Caused by op u'ctc/scan/strided_slice', defined at: 
    File "speech_lstm_ctc.py", line 189, in <module> 
    loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name="ctc")([y_pred, labels, input_length, label_length]) 
    File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 554, in __call__ 
    output = self.call(inputs, **kwargs) 
    File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 659, in call 
    return self.function(inputs, **arguments) 
    File "speech_lstm_ctc.py", line 141, in ctc_lambda_func 
    ctc_batch_loss = K.ctc_batch_cost(labels, y_pred, input_length, label_length) 
    File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 3258, in ctc_batch_cost 
    sparse_labels = tf.to_int32(ctc_label_dense_to_sparse(y_true, label_length)) 
    File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 3222, in ctc_label_dense_to_sparse 
    initializer=init, parallel_iterations=1) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/functional_ops.py", line 524, in scan 
    n = array_ops.shape(elems_flat[0])[0] 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/array_ops.py", line 495, in _SliceHelper 
    name=name) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/array_ops.py", line 653, in strided_slice 
    shrink_axis_mask=shrink_axis_mask) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 3688, in strided_slice 
    shrink_axis_mask=shrink_axis_mask, name=name) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 763, in apply_op 
    op_def=op_def) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2327, in create_op 
    original_op=self._default_original_op, op_def=op_def) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1226, in __init__ 
    self._traceback = _extract_stack() 

InvalidArgumentError (see above for traceback): slice index 0 of dimension 0 out of bounds. 
    [[Node: ctc/scan/strided_slice = StridedSlice[Index=DT_INT32, T=DT_INT32, begin_mask=0, ellipsis_mask=0, end_mask=0, new_axis_mask=0, shrink_axis_mask=1, _device="/job:localhost/replica:0/task:0/gpu:0"](ctc/scan/Shape, ctc/scan/strided_slice/stack, ctc/scan/strided_slice/stack_1, ctc/scan/strided_slice/stack_2)]] 
    [[Node: ctc/scan/while/Less_1/_77 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_981_ctc/scan/while/Less_1", tensor_type=DT_BOOL, _device="/job:localhost/replica:0/task:0/cpu:0"](^_cloopctc/scan/while/TensorArrayReadV3/_17)]] 

實現CTC損失的功能如下:

def ctc_lambda_func(args): 
    y_pred, labels, input_length, label_length = args 
    # the 2 is critical here since the first couple outputs of the RNN 
    # tend to be garbage: 
    y_pred = y_pred[:, 2:, :] 

    ctc_batch_loss = K.ctc_batch_cost(labels, y_pred, input_length, label_length) 

    return ctc_batch_loss 

我相信該錯誤與特定輸入無關,因爲即使使用不同的輸入,每次都出現在相同的步驟。

關於可能導致它的任何想法?

回答

0

看來導致問題的原因是批生成器實際上在某個時間點給出了一個大小爲1的小批量,並導致張量流崩潰。通過使小批量始終大於1來糾正它。