Use layer output in keras custom loss

那年仲夏 提交于 2021-02-09 02:47:33

问题


I am developing a custom loss function in Keras and I need the first layer output.

How can I retrieve it?

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

回答1:


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.

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.



来源:https://stackoverflow.com/questions/48345755/use-layer-output-in-keras-custom-loss

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!