Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keras: Smooth L1 loss

Try to customize loss function(smooth L1 loss) in keras like below

ValueError: Shape must be rank 0 but is rank 5 for 'cond/Switch' (op: 'Switch') with input shapes: [?,24,24,24,?], [?,24,24,24,?].

from keras import backend as K
import numpy as np


def smooth_L1_loss(y_true, y_pred):
    THRESHOLD = K.variable(1.0)
    mae = K.abs(y_true-y_pred)
    flag = K.greater(mae, THRESHOLD)
    loss = K.mean(K.switch(flag, (mae - 0.5), K.pow(mae, 2)), axis=-1)
    return loss
like image 363
yuan zhou Avatar asked Dec 03 '22 13:12

yuan zhou


1 Answers

I know I'm two years late to the party, but if you are using tensorflow as keras backend you can use tensorflow's Huber loss (which is essentially the same) like so:

import tensorflow as tf


def smooth_L1_loss(y_true, y_pred):
    return tf.losses.huber_loss(y_true, y_pred)
like image 61
felixwege Avatar answered May 16 '23 09:05

felixwege