Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras: Calculating loss as *median* across datapoints instead of mean

The Keras losses page says that if we have a custom loss function, then "the actual optimized objective is the mean of the output array across all datapoints." Is there any way we can optimize the median of the output array across all datapoints (instead of the mean)?

like image 813
samjk Avatar asked Oct 18 '22 11:10

samjk


1 Answers

In order to do this, you would need to go down to tensorflow level

import keras
import tensorflow


def pick_median(arg_tensor):
    the_upper_tensor = tensorflow.contrib.distributions.percentile(arg_tensor, 50, interpolation='higher')
    the_lower_tensor = tensorflow.contrib.distributions.percentile(arg_tensor, 50, interpolation='lower')

    final_tensor = (the_upper_tensor + the_lower_tensor) / 2
    # print(the_count.eval(session=keras.backend.get_session()))

    return final_tensor

Here is how you would define, let's say, median_squared_error loss function:

def median_squared_error(arg_y_true,
                         arg_y_pred):
    final_tensor = keras.backend.square(arg_y_pred - arg_y_true)
    final_tensor = pick_median(arg_tensor=final_tensor)
    return final_tensor
like image 199
OlDor Avatar answered Oct 21 '22 02:10

OlDor