<p>As others have stated, precision/recall is not directly usable as a loss function. However, better proxy loss functions have been found that help with a whole family of precision/recall related functions (e.g. ROC AUC, precision at fixed recall, etc.)</p>
<p>The research paper <a href="https://arxiv.org/pdf/1608.04802.pdf" rel="nofollow noreferrer">Scalable Learning of Non-Decomposable Objectives</a> covers this with a method to sidestep the combinatorial optimization by the use of certain calculated bounds, and some Tensorflow code by the authors is available at the <a href="https://git.dst.etit.tu-chemnitz.de/external/tf-models/-/tree/master/research/global_objectives" rel="nofollow noreferrer">tensorflow/models</a> repository. Additionally, there is a followup question <a href="https://stackoverflow.com/questions/54286334/use-tensorflow-loss-global-objectives-recall-at-precision-loss-with-keras-not">on StackOverflow</a> that has an answer that adapts this into a usable Keras loss function.</p>
<p>Special thanks to Francois Chollet and other participants on the <a href="https://github.com/keras-team/keras/issues/1732" rel="nofollow noreferrer">Keras issue thread here</a> that turned up that research paper. You may also find that thread provides other useful insights into the problem at hand.</p>
Is there an optimizer in keras based on precision or recall instead of loss?
<p>I am developping a segmentation neural network with only two classes, 0 and 1 (0 is the background and 1 the object that I want to find on the image). On each image, there are about 80% of 1 and 20% of 0. As you can see, the dataset is unbalanced and it makes the results wrong. My accuracy is 85% and my loss is low, but that is only because my model is good at finding the background !</p>

<p>I would like to base the optimizer on another metric, like precision or recall which is more usefull in this case.</p>

<p>Does anyone know how to implement this ?</p>