Search code examples
rkerasloss-function

Writing a loss function in R studio using Keras


So I am trying to write a custom loss function in Rstudio in Keras. I basically wanna penalize more when the function produces undervalued prediction. However I cannot figure out how to access the members of a tensor.

This is so far what I have tried:

myloss <- function(y_true, y_pred){

    penalize = k_flatten(y_pred) - k_flatten(y_true);
    penalize_pos = penalize >= 0
    penalize_neg = penalize < 0
    # I cannot find a mask function to turn penalize_pos into actual indecies
    #tried this but did not work
    A = penalize$eval()[penalize_pos$eval()]
    B = penalize$eval()[penalize_neg$eval()]

    return(sum(abs(A) + abs(B)*10))

}

I was wondering if you have any suggestion. Thanks.


Solution

  • I was having this same problem. I know it is way late but here is a solution I found while searching. This website has a nice tutorial I found useful.

    Like the Python functions, the custom loss functions for R need to operate on tensor objects rather than R primitives. In order to perform these operations, you need to get a reference to the backend using backend(). In my system configuration, this returns a reference to tensorflow.

    It also includes the following code snippet:

    # Mean Log Absolute Error
    MLAE <- function( y_true, y_pred ) {
      K <- backend()
      K$mean( K$abs( K$log( K$relu(y_true *1000 ) + 1 ) - 
          K$log( K$relu(y_pred*1000 ) + 1)))
    }
    # Mean Squared Log Absolute Error
    MSLAE <- function( y_true, y_pred ) {
      K <- backend()
      K$mean( K$pow( K$abs( K$log( K$relu(y_true *1000 ) + 1 ) - 
        K$log( K$relu(y_pred*1000 ) + 1)), 2))
    }
    

    Notice the K <- backend() call, which allows you to operate on tensor objects.