# Is it possible to make F1_Score differentiable and use it directly as a Loss function?

One of the metrics that is widely used in binary classification is the F1 score:

$$F_1 = 2\cdot \frac{recall \cdot precision}{recall+precision}$$

The problem of the F1-score is that it is not differentiable and so we cannot use it as a loss function to compute gradients and update the weights when training the model. The F1-score needs binary predictions (0/1) to be measured.

I am seeing it a lot. Let's say I am using per example a Linear regression or a gradient boosting.

Is there any way that it can be minimized directly?

Yes there is, let's take $$F_1$$ score base definition, with : $$F_1 = 2 \times \frac{precision \times recall} {precision + recall} \\ F_1 = \frac{2 \times TP} {2 \times TP + FP + FN}$$ And this is the same as the Sørensen-Dice coefficient, also known as Dice coefficient or Bray-Curtis distance. This is a statistical indicator that measures the similarity of two samples :
$$Dice(X,Y) = \frac{2|X \cap Y|}{|X| + |Y|}$$
Concerning the implementation of this loss, we can approximate $$|X \cap Y|$$ as the sum of the matrix obtained using Hadamard product ($$\odot$$, also known as the element-wise product) between the ground truth ($$y$$) and the prediction ($$\hat{y}$$). We can then define $$L_{Dice}$$ as follows:
\begin{align*} L_{Dice} &= 1 - Dice \\ L_{Dice}\left(y, \hat{y}\right) &= 1 - \frac{ 2\sum y \odot \hat{y}} {\sum y + \sum \hat{y}} \end{align*}