3

I have a VAE model that I've broken down into the encoder and decoder parts, and implemented a custom loss. A simplified example is as below

input = Input(shape=(self.image_height, self.image_width, self.image_channel))
encoded = build_encoder(input)
decoded = build_decoder(encoded)
model = Model(input, decoded)

The loss (simplified) is

loss = K.mean(decoded[0] + decoded[1] + encoded[0]**2)
model.add_loss(loss)
model.compile(optimizer=self.optimizer)

My main problem is that I want to use Keras' modelcheckpoint function, which would then require me to set custom metrics. However, everything I have seen online is similar to https://keras.io/metrics/#custom_metrics. This only takes in y_true and y_pred, and modify the validation loss from there. How would I implement it in my example model, where the loss is calculated from multiple inputs, not only the final output of "decoded"?

Andy Wei
  • 618
  • 7
  • 22
  • Possible duplicate of [add\_loss function in keras](https://stackoverflow.com/questions/50063613/add-loss-function-in-keras) – dennlinger Oct 03 '18 at 08:13

1 Answers1

1

Well apparently you can still use the variables (keras layers) without passing those into the custom loss function.

So for my example, the loss can be calculated as

def custom_loss(y_true, y_pred):
    return K.mean(decoded[0] + decoded[1] + encoded[0]**2)

model.compile(optimizer=self.optimizer, loss=custom_loss)

y_true and y_pred is never used, but the actual required inputs can still be called (as long as they are in the same scope as the custom loss function of course).

Andy Wei
  • 618
  • 7
  • 22