Skip to content
Advertisement

keras apply threshold for loss function

I am developing a Keras model. My dataset is badly unbalanced, so I want to set a threshold for training and testing. If I’m not mistaken, when doing a backward propagation, neural network checks the predicted values with the original ones and calculate the error and based on the error, set new weights for neurons.

As I know, Keras uses 0.5 for the threshold. I know there are ways to apply custom metrics (as recall and precision) with custom threshold, but that threshold is only used for calculating the recall, and it is not applied in the loss function. To be more clear, If I want to set 0.85 as my threshold, the neural network would use 0.5 as threshold to calculate loss and 0.85 for recall.

Is there any ways to set this threshold for training as well?

Advertisement

Answer

There is no such a thing as a threshold for loss.

A loss function must be “differentiable”, thus it must be a “continuous” function.
The best you can do is to set “class weights”, such as these examples: Higher loss penalty for true non-zero predictions

Advertisement