本文介绍了在keras自定义损失中使用图层输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在Keras中开发自定义损失函数,我需要第一层输出.
I am developing a custom loss function in Keras and I need the first layer output.
我如何找回它?
def custom_loss(y_true, y_pred):
cross = K.mean(K.binary_crossentropy(y_true, y_pred), axis = 1)
layer_output = model.get_layer_output(1) # this is what i'd like to use
return cross + perturb
推荐答案
检查文档您可以使用model.get_layer()
方法检索图层.然后,您可以传递所需的索引,也可以传递层的名称.
Checking the docs you can retrieve a layer by using the model.get_layer()
method. You can then pass the desired index or well pass the name of the layer.
获取图层后,您可以使用layer.output
属性轻松获取其输出,如在此处在文档上.
After getting a layer you can easily obtain its output by using the layer.output
attribute, as explained here on the docs.
将两者结合起来就可以得到所需图层的输出.
Combining both you can obtain the output of your desired layer.
这篇关于在keras自定义损失中使用图层输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!