4

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
DarkCygnus
  • 7,420
  • 4
  • 36
  • 59
Francesco Pegoraro
  • 778
  • 13
  • 33
  • Related? https://stackoverflow.com/questions/46858016/keras-custom-loss-function-to-pass-arguments-other-than-y-true-and-y-pred – sladomic Jan 19 '18 at 17:14

1 Answers1

2

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.

DarkCygnus
  • 7,420
  • 4
  • 36
  • 59
  • Do you think it is possible to get the batch in my custom_loss? I would like to make a new prediction directly from the loss – Francesco Pegoraro Jan 19 '18 at 21:24
  • hey @FrancescoPegoraro glad this worked for you :) so, by batch you mean your training batch? Or the gradent update done per that batch? – DarkCygnus Jan 19 '18 at 21:30
  • Sorry @DarkCygnus I forgot to say thank you! Anyway I mean training batch, I know it may sound weird... – Francesco Pegoraro Jan 19 '18 at 21:33
  • @FrancescoPegoraro hmm not really sure. You want to use the current samples on this batch as some part of the calculation of your loss (that is the X corresponding to that y_pred)? – DarkCygnus Jan 19 '18 at 21:40
  • Yes, I would like to implement adversarial learning, like in this [paper](https://nlp.stanford.edu/courses/cs224n/2015/reports/20.pdf). For that, the loss of the model on an **example** should be the "classic" loss + the loss of the model on the prediction of **example + ∇( loss)** . Recap: L(θ,x,y) + L(θ, x + ∇W L(θ, x, y)) – Francesco Pegoraro Jan 19 '18 at 21:59
  • 1
    @FrancescoPegoraro I'll give it some thought, and reply to you later... meanwhile, some [answer](https://stackoverflow.com/a/45195260/3908170) I wrote a while ago that may be useful (as to manage to pass extra variables to your custom loss function, you could adapt it to your needs by passing those batch elements somehow) – DarkCygnus Jan 19 '18 at 23:18