Dear list,
I am currently working with the keras package for R. With it, I want to fit a Neural Network model with multiple outputs that optimizes a single custom loss function that I create. That is, instead of having a custom loss function applied to each outcome and them having the network optimize the sum of said custom loss, I want my custom function generate at once one single loss value for the whole model.
For instance, suppose I have the following silly custom loss function:
custom_loss <- function(y_pred, y_true)
{
result <- abs(y_pred[1] - y_true[1]) * abs(y_pred[2] - y_true[2])
return(result)
}
With a model like:
build_model <- function()
{
input_tensor <- layer_input(shape = dim(train_data)[[2]])
base_model <- input_tensor %>%
layer_dense(units = 64, activation = "relu") %>%
layer_dense(units = 64, activation = "relu")
y1_prediction <- base_model %>% layer_dense(units = 1, name="y1")
y2_prediction <- base_model %>% layer_dense(units = 1, name="y2")
model <- keras_model(input_tensor, list(y1_prediction, y2_prediction))
model %>% compile(optimizer = "rmsprop", loss = custom_loss)
}
Given what I read about achieving a single custom loss function with Keras in Python, my expectation would be that the specification above would be enough.
However, the keras package is still calling "custom_loss" for each of the outputs and then optimizing the sum of losses:
Epoch 1/200
20/20 [==============================] - 2s 3ms/step - loss: 2266.0321 - y1_loss: 2265.9965 - y2_loss: 0.0357
Epoch 2/200
20/20 [==============================] - 0s 5ms/step - loss: 1795.3417 - y1_loss: 1795.3154 - y2_loss: 0.0264
How could I use the keras package in R to properly set this up, such that I would end up having my multi-output network calculate only one single custom loss function and optimize it (instead of the sum of losses)?
Many thanks for any help you may provide!
Kind regards,
Louis