Customizing a single Loss function for Multiple Outputs using Keras in R

496 views
Skip to first unread message

Louis Ashwall

unread,
Apr 29, 2021, 12:01:14 AM4/29/21
to Keras-users
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)?

For convenience, a full minimally reproducible example can be found here: https://file.re/2021/04/29/min-example-1/.

Many thanks for any help you may provide!

Kind regards,

Louis

morteza mohammady

unread,
Apr 29, 2021, 9:23:55 AM4/29/21
to Louis Ashwall, Keras-users

Hey,
I think Your loss function uses just one output but the output of tour network is y1_predict and y2_predict. 
You need to write the loss as following:
Model.compile(optimizer=..., loses=[customloss,customloss],weights=[0.1,0.2])

Weights are [1,1] by default.

Also, the loss is weighted average of all samples in each batch.

I hope my answer is useful.


--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/7d77f459-8b8f-4820-b826-1b1b6ce5ba38n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages