Keras CTC损失输入

时间:2017-04-18 10:02:56

标签: tensorflow keras

我尝试使用CTC进行语音识别,并尝试了CTC示例here。在该示例中,CTC Lambda层的输入是softmax层(y_pred)的输出。 Lambda图层调用ctc_batch_cost内部调用Tensorflow的ctc_loss,但Tensorflow ctc_loss documentation表示ctc_loss函数在内部执行softmax,因此您不需要&inner #39;首先需要softmax你的输入。我认为正确的用法是将Lambda传递给ctc_loss图层,这样您只需在内部div函数中应用softmax一次。我试过这个例子,但它确实有效。我应该遵循示例还是Tensorflow文档?

1 个答案:

答案 0 :(得分:6)

您发布的代码中使用的损失与您链接的代码不同。代码中使用的损失是found here

keras代码在调用ctc_loss之前执行一些预处理,使其适合所需的格式。除了要求输入不是softmax-ed之外,tensorflow的ctc_loss还期望dims为NUM_TIME, BATCHSIZE, FEATURES。 Keras的ctc_batch_cost做了这两件事in this line.

它会使log()摆脱softmax缩放,并且它也会使dim混乱,使其形状正确。当我说摆脱softmax缩放时,它显然不会恢复原始张量,而是softmax(log(softmax(x))) = softmax(x)。见下文:

def softmax(x):
"""Compute softmax values for each sets of scores in x."""
e_x = np.exp(x - np.max(x))
return e_x / e_x.sum()


x = [1,2,3]
y = softmax(x)
z = np.log(y) # z =/= x (obviously) BUT
yp = softmax(z) # yp = y #####
相关问题