diff --git a/src/model_modules/loss.py b/src/model_modules/loss.py new file mode 100644 index 0000000000000000000000000000000000000000..bcb85282d0fa15f18ebd65a89e4020c2a0170224 --- /dev/null +++ b/src/model_modules/loss.py @@ -0,0 +1,22 @@ +"""Collection of different customised loss functions.""" + +from keras import backend as K + +from typing import Callable + + +def l_p_loss(power: int) -> Callable: + """ + Calculate the L<p> loss for given power p. + + L1 (p=1) is equal to mean absolute error (MAE), L2 (p=2) is to mean squared error (MSE), ... + + :param power: set the power of the error calculus + + :return: loss for given power + """ + + def loss(y_true, y_pred): + return K.mean(K.pow(K.abs(y_pred - y_true), power), axis=-1) + + return loss