问题
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