CTC是2006年的論文Connectionist Temporal Classification: Labelling Unsegmented Sequence Data with Recurrent Neural Networks中提到的,論文地址: http://www.cs.toronto.edu/~graves/icml_2006.pdf
論文中CTC的定義是這樣的:把對未分割的序列數據label的任務叫做Temporal Classification,把使用RNNs對未分割的序列數據label叫做Connectionist Temporal Classification(CTC) 。與之相對的是,把對數據序列的每一個time-step或者frame獨立label 叫做framewise classification
tensorflow中的相關實現在 /tensorflow/python/ops/ctc_ops.py
1. ctc_loss, 計算ctc loss
def ctc_loss(labels, inputs, sequence_length, preprocess_collapse_repeated=False, ctc_merge_repeated=True, time_major=True):
這個類執行softmax操作,所以輸入應該是LSTM輸出的線性映射
inputs, 最內部維度大小是num_classes,代表“num_labels +1” 個類別,其中num_labels是真實的balebs的數目,最大值“num_labels-1”是為blank label保留的
例如,如果一個單詞包含3個labels ‘[a, b, c]’,則num_classes =4, 且labels的索引號是 ‘{a:0, b:1, c:2, blank:3}’
至於參數 preprocess_collapse_repeated 和 ctc_merge_repeated:
如果 preprocess_collapse_repeated = True ,在計算ctc之前,重復的labels會被合並為一個labels。這種預處理對下面這種情況是有用的:如果訓練數據是強制對齊得到的,會包含不必要的重復。
如果 ctc_merge_repeated = False,那么伴隨ctc計算的深入,重復的非blank將不會被合並,會被解釋為獨立的labels。這是ctc的簡化的非標准的版本
具體見下表
- preprocess_collapse_repeated = False,ctc_merge_repeated = True:經典CTC,輸出的真實的重復的中間帶有blanks類別,也可以通過解碼器解碼,輸出不帶有blanks的重復類別
- preprocess_collapse_repeated = True,ctc_merge_repeated = False:因為在training之前,input 的labels已經合並重復項了,所以不會輸出重復的類
- preprocess_collapse_repeated = False,ctc_merge_repeated = False:輸出重復的中間帶有blank的類別,但是通常不需要解碼器合並重復項
- preprocess_collapse_repeated = True,ctc_merge_repeated = True: 未測試,非常可能不會學會輸出重復類
參數:
labels: int32 SparseTensor, 標准的輸出,稀疏矩陣
inputs: 3-D float tensor . 計算得到的logits。 如果time_major = False, shape:batch_size x max_time x num_classes. 如果 time_major = True, shape:max_time x batch_size x num_classes
sequence_length: 1-D int32 向量, batch_size
輸出:
1-D float tensor,size:[batch], 概率的負對數
2. ctc_beam_search_decoder: 對輸入的logits執行beam search 解碼
def ctc_beam_search_decoder(inputs, sequence_length, beam_width=100, top_paths=1, merge_repeated=True):
如果 merge_repeated = True, 在輸出的beam中合並重復類。這意味着如果一個beam中的連續項( consecutive entries) 相同,只有第一個提交。即,如果top path 是‘A B B B ’,返回值是‘A B’(當merge_repeated = True),‘A B B B ’ (當merge_repeated = False)
參數:
inputs: 3-D float tensor , shape:max_time x batch_size x num_classes
sequence_length: 1-D int32 向量, batch_size
beam_width: int scalar>=0
top_paths: int scalar>=0, <= beam_width, 輸出解碼后的數目
輸出:
元組:(decoded, log_prob)
其中:
decoded : a list of length top_paths, 每一個是一個稀疏矩陣
log_prob : matrix , shape (batch_size x top_paths)