Search code examples
pythonrkerasloss-function

Custom Loss Function in R Keras


I want to calculate weighted mean squared error, where weights is one vector in the data. I wrote a custom code based on the suggestions available on stack overflow.

The function is provided below:

weighted_mse <- function(y_true, y_pred,weights){
  # convert tensors to R objects
  K        <- backend()
  y_true   <- K$eval(y_true)
  y_pred   <- K$eval(y_pred)
  weights  <- K$eval(weights)

  # calculate the metric
  loss <- sum(weights*((y_true - y_pred)^2)) 

  # convert to tensor
  return(K$constant(loss))
  }

However, I am not sure how to pass the custom function to the compiler. It would be great if someone can help me. Thank you.

model      <- model %>% compile(
                loss = 'mse', 
                optimizer = 'rmsprop',
                metrics = 'mse')

Regards


Solution

  • You can't eval in loss funtions. This will break the graph.

    You should just use the sample_weight parameter of the fit method: https://keras.rstudio.com/reference/fit.html

    ##not sure if this is valid R, but 
    ##at some point you will call `fit` for training with `X_train` and `Y_train`, 
    ##so, just add the weights.
    history <- model$fit(X_train, Y_train, ..., sample_weight = weights)
    

    That's all (don't use a custom loss).


    Just for knowledge - Passing loss functions to compile

    Only works for functions taking y_true and y_pred. (Not necessary if you're using sample_weights)

    model      <- model %>% compile(
                loss = weighted_mse, 
                optimizer = 'rmsprop',
                metrics = 'mse')
    

    But this won't work, you need something similar to the wrapper created by @spadarian.

    Also, it will be very complicated to keep a correlation between your data and the weights, both because Keras will divide your data in batches and also because the data will be shuffled.