1

如何在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

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

4

0 回答 0