johnnyp
johnnyp

Reputation: 99

Asymetric metrics in Keras

I am trying to predict the evolution of a function with artificial neural network and Keras. The thing is that I want the output of the neural net to be conservative, i.e I can accept underestimation of the value (to a certain extend) but overestimation is much more of a problem.

I would like to use as a metrics :

I think this might be feasible in Keras but I confess I have no clue how to do it. Does someone know how to do the trick ?

Thanks

Upvotes: 1

Views: 68

Answers (1)

Marco Cerliani
Marco Cerliani

Reputation: 22031

I think that the simplest way to do this is to create a custom metric with tf.keras.backend.switch

here a dummy example:

X = np.random.uniform(0,1, (100,30))
y = np.random.uniform(0,1, (100,1))

def custom_metric(true, pred):
    abs_error = tf.abs(true - pred)
    error = tf.keras.backend.switch(pred < true, abs_error/2, abs_error*2)
    return tf.reduce_mean(error)
    
inp = Input((30,))
x = Dense(32)(inp)
out = Dense(1)(x)

model = Model(inp, out)
model.compile('adam', 'mse', metrics=custom_metric)
model.fit(X,y, epochs=3)

you can also modify it according to your needs

Upvotes: 1

Related Questions