Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 801
Sumit Avatar asked Jul 13 '18 00:07

Sumit


People also ask

What is loss function in Keras?

A loss function is one of the two arguments required for compiling a Keras model: from tensorflow import keras from tensorflow.keras import layers model = keras. Sequential() model. add(layers. Dense(64, kernel_initializer='uniform', input_shape=(10,))) model.

Why do we need custom loss function?

A custom loss function can improve the models performance significantly, and can be really useful in solving some specific problems. To create a custom loss, you have to take care of some rules. The loss function must only take two values, that are true labels, and predicted labels.

What is Categorical_crossentropy in Keras?

categorical_crossentropy: Used as a loss function for multi-class classification model where there are two or more output labels. The output label is assigned one-hot category encoding value in form of 0s and 1. The output label, if present in integer form, is converted into categorical encoding using keras.


1 Answers

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.

like image 189
Daniel Möller Avatar answered Sep 30 '22 16:09

Daniel Möller