shahir asfahan
shahir asfahan

Reputation: 53

Custom loss function Keras for y_pred above a certain threshold value only

how to write a custom loss function in keras regression where MAE is calculated for y_pred above a certain threshold only. For eg. y_true = [10 , 14 , 23 , 30 , 5 , 4] ,
y_pred = [8 , 12 , 27 , 38 , 10 , 8]

How to write a custom loss function where MAE(Mean absolute error) of y_pred values above 20 only are calculated i.e MAE for y_pred > 20 i.e [23,30] which comes to 6 [(27-23) + (38-30)]/2

This problem has arisen as i need models that predict correctly only the highest range of predictions so that i can use only those data points only that return the highest predictions as rest of the lower predictions data is not useful for me. something like -

def custom_loss(y_pred, y_true):
    for y_pred > 20:
        result =MAE(y_pred , y_true)
    return result

Upvotes: 2

Views: 868

Answers (2)

Marco Cerliani
Marco Cerliani

Reputation: 22031

try in this way

def custom_loss(y_true, y_pred):
    
    y_pred = y_pred[y_pred>20]
    y_true = y_true[y_pred>20]
    
    return tf.reduce_mean(tf.abs(y_true-y_pred))

n_sample = 1000
X = np.random.uniform(0,5, (n_sample,10))
y = np.random.randint(0,50, n_sample)

inp = Input((10,))
x = Dense(32, activation='relu')(inp)
out = Dense(1)(x)
model = Model(inp, out)

model.compile('adam',loss=custom_loss)
model.fit(X,y, epochs=10)

this loss may result in nan because can happens that in batches all predictions are under 20 so pay attention to the magnitude of the predictions

Upvotes: 2

M Z
M Z

Reputation: 4799

For Keras loss functions, as far as I understand you should stick to using Keras backend functions. Take a look here, I've linked the Tensorflow Keras backend documentation.

There is a function that might be useful for you called map_fn, which allows you to map a function over all y_pred y_true pairs, but there's probably several different ways to approach this and the documentation should help!

Upvotes: 0

Related Questions