keras的CTC损失

时间:2017-10-03 10:46:52

标签: python tensorflow machine-learning keras ctc

如何在keras中使用tensorflows CTC损失函数?

我试过这样做:

def ctc_loss(y_true,y_pred):
    return(tf.nn.ctc_loss(y_pred, y_true, 64, 
         preprocess_collapse_repeated=False, ctc_merge_repeated=False, 
         time_major=True))

使用的模型是:

def build_model():
    encoder_input = Input(shape=(512,64), name='encoder_in')
    x = Bidirectional(GRU(64, implementation=2, 
                      return_sequences=True),merge_mode='concat')(encoder_input)
    x = GRU(128, implementation=2, return_sequences=True)(x)
    x = LSTM(64, implementation=2, return_sequences=True)(x) 
    model = Model(inputs=encoder_input,outputs=x)
    #model.load_weights('Tuned.h5')
    model.compile(loss=ctc_loss, optimizer='adam', metrics=['acc'])
    model.summary()
    return model

但我一直收到错误:

File "C:\local\Anaconda3-4.1.1-Windows-x86_64\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\ops\ctc_ops.py", line 139, in ctc_loss
    raise TypeError("Expected labels (first argument) to be a SparseTensor")
TypeError: Expected labels (first argument) to be a SparseTensor

我该如何解决这个问题?如何将输出张量转换为稀疏?

0 个答案:

没有答案