<p>It appears approaches for optimising directly for these types of metrics have been devised and used successfully, improving scoring and or training times:</p>

<p><a href="https://www.kaggle.com/c/human-protein-atlas-image-classification/discussion/77289" rel="noreferrer">https://www.kaggle.com/c/human-protein-atlas-image-classification/discussion/77289</a></p>

<p><a href="https://www.kaggle.com/c/human-protein-atlas-image-classification/discussion/70328" rel="noreferrer">https://www.kaggle.com/c/human-protein-atlas-image-classification/discussion/70328</a></p>

<p><a href="https://www.kaggle.com/rejpalcz/best-loss-function-for-f1-score-metric" rel="noreferrer">https://www.kaggle.com/rejpalcz/best-loss-function-for-f1-score-metric</a></p>

<p>One such method involves using the sums of probabilities, in place of counts, for the sets of true positives, false positives, and false negative metrics. For example F-beta loss (the generalisation of F1) can be calculated in with Torch in Python as follows:</p>

<pre><code>def forward(self, y_logits, y_true):
    y_pred = self.sigmoid(y_logits)
    TP = (y_pred * y_true).sum(dim=1)
    FP = ((1 - y_pred) * y_true).sum(dim=1)
    FN = (y_pred * (1 - y_true)).sum(dim=1)
    fbeta = (1 + self.beta**2) * TP / ((1 + self.beta**2) * TP + (self.beta**2) * FN + FP + self.epsilon)
    fbeta = fbeta.clamp(min=self.epsilon, max=1 - self.epsilon)
    return 1 - fbeta.mean()
</code></pre>

<p>An alternative method is described in this paper:</p>

<p><a href="https://arxiv.org/abs/1608.04802" rel="noreferrer">https://arxiv.org/abs/1608.04802</a></p>

<p>The approach taken optimises for a lower bound on the statistic. Other metrics such as AUROC and AUCPR are also discussed. An implementation in TF of such an approach can be found here:</p>

<p><a href="https://github.com/tensorflow/models/tree/master/research/global_objectives" rel="noreferrer">https://github.com/tensorflow/models/tree/master/research/global_objectives</a></p>
How to use F-score as error function to train neural networks?
<p>I am pretty new to neural networks. I am training a network in tensorflow, but the number of positive examples is much much less than negative examples in my dataset (it is a medical dataset). 
So, I know that F-score calculated from precision and recall is a good measure of how well the model is trained. 
I have used error functions like cross-entropy loss or MSE before, but they are all based on accuracy calculation (if I am not wrong). But how do I use this F-score as an error function? Is there a tensorflow function for that? Or I have to create a new one?</p>

<p>Thanks in advance. </p>