Scale out to between 0 and 1 before computing loss fucnction in keras
Use Lambda layer
def scale(tensor):
    tensor = (tensor - K.min(tensor)+1e-20)/(K.max(tensor)-K.min(tensor)+1e-10)
    return tensor

scaled_density_pred = Lambda(scale, name=&quot;scale_layer&quot;)(density_pred)