CTC,Connectionist temporal classification。從字面上理解它是用來解決時序類數據的分類問題。語音識別端到端解決方案中應用的技術。主要是解決以下兩個問題
- 解決語音輸入和標簽的對齊問題。對於一段語音輸入,將其轉化為聲學頻譜圖,傳統的聲學模型需要對其頻譜圖上的每一幀對應的發音因素,而采用CTC作為損失函數,只需要一個輸入序列和輸出序列即可。
- CTC是一種損失函數,用來衡量輸入的序列經過神經網絡之后,和真實的輸出相差有多少。對於nihao這個發音,不同的人有不同的發音方式,可能是nnnnniiiihhhaaaooo... 等等,CTC能衡量長度不一的輸入經過神經網絡后與實際結果的損失值大小。
Keras中CTC實現
from keras import backend as K
from keras.models import Model
from keras.layers import (Input, Lambda)
from keras.optimizers import SGD
from keras.callbacks import ModelCheckpoint
import os
def ctc_lambda_func(args):
y_pred, labels, input_length, label_length = args
return K.ctc_batch_cost(labels, y_pred, input_length, label_length)
def add_ctc_loss(input_to_softmax):
the_labels = Input(name='the_labels', shape=(None,), dtype='float32')
input_lengths = Input(name='input_length', shape=(1,), dtype='int64')
label_lengths = Input(name='label_length', shape=(1,), dtype='int64')
output_lengths = Lambda(input_to_softmax.output_length)(input_lengths)
# CTC loss is implemented in a lambda layer
loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name='ctc')(
[input_to_softmax.output, the_labels, output_lengths, label_lengths])
model = Model(
inputs=[input_to_softmax.input, the_labels, input_lengths, label_lengths],
outputs=loss_out)
return model