使用Keras进行深入学习:如何理解Lambda层和Lambda函数?
我的代码如下:使用Keras进行深入学习:如何理解Lambda层和Lambda函数?,lambda,keras,Lambda,Keras,我的代码如下: labels = Input(name='the_labels', shape=[1], dtype='float32') input_length = Input(name='input_length', shape=[1], dtype='int64') label_length = Input(name='label_length', shape=[1], dtype='int64') loss_out = Lambda(ctc_lambda_func, output_s
labels = Input(name='the_labels', shape=[1], dtype='float32')
input_length = Input(name='input_length', shape=[1], dtype='int64')
label_length = Input(name='label_length', shape=[1], dtype='int64')
loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name="ctc")([output, labels, input_length, label_length])
model = Model(input=[net_input, labels, input_length, label_length], output=[loss_out])
model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer=optimizer, metrics=[])
我的ctc_lambda_func定义如下:
def ctc_lambda_func(args):
y_pred, labels, input_length, label_length = args
# the 2 is critical here since the first couple outputs of the RNN
# tend to be garbage:
shift = 2
y_pred = y_pred[:, shift:, :]
input_length -= shift
return K.ctc_batch_cost(labels, y_pred, input_length, label_length)
我不明白:
(ctc_lambda_func,output_shape=(1,),name=“ctc”)
是函数“lambda”的参数,但是的参数是什么([输出,标签,输入长度,标签长度])
loss={'ctc':lambda y_true,y_pred:y_pred}
是loss函数。我发现损失函数列表如下,但没有ctc。
- 均方误差(y_true,y_pred)均方绝对误差(y_true, y_pred)
- 平均绝对百分比误差(y_真,y_pred)
- 均方对数误差(y_真,y_pred)
- 方形铰链(y_真,y_pred)
- 铰链(y_真,y_前)
- 分类铰链(y_真,y_前)
- logcosh(y_true,y_pred)
- 范畴交叉熵(y_真,y_pred)
- 稀疏分类交叉熵(y_真,y_pred)
- 二元交叉熵(y_真,y_pred)
- kullback_-leibler_散度(y_-true,y_-pred)
- 泊松(y_真,y_pred) -余弦近似(y_真,y_pred)
我是Keras和Python方面的新手。如果您能给我一些解释,我将不胜感激。这不是Keras库的正确用法。该代码似乎绕过了API
([output,labels,input_length,label_length])
是传递给自定义函数的张量,在这个函数中为loss函数。这种复杂解决方案背后的原因是API不仅允许对丢失函数进行(输出、标签)
签名y_pred
绕过Keras内置的损失函数。loss={'ctc':…
对于'ctc'
层,即Lambda,绕过了loss您可以实现自定义丢失函数,但这不是API的预期用途。对于具有多个参数的丢失解决方案,请查看此项。欢迎使用此项。请查看此项以改进您的问题(尤其是格式和校对):这将使别人更容易理解和帮助你。