<p>This pre-print describes a differentiable loss function for the Matthews Correlation Coefficient. They derive an analytical form for its gradient, based on differentiable versions of TP, FP, etc.  Then they use it to train their Convolutional Neural Network.</p>
<p><a href="https://arxiv.org/abs/2010.13454" rel="nofollow noreferrer">https://arxiv.org/abs/2010.13454</a></p>
<p>I think these formulas are exactly what you need.   They provide code in PyTorch, too.</p>
Matthews correlation coefficient as a loss in keras
<p>I try to write a custom loss function for keras with tf backend.
I get the following error</p>

<blockquote>
  <p>ValueError: An operation has <code>None</code> for gradient. Please make sure
  that all of your ops have a gradient defined (i.e. are
  differentiable). Common ops without gradient: K.argmax, K.round,
  K.eval.</p>
</blockquote>

<pre><code>def matthews_correlation(y_true, y_pred):
    y_pred_pos = K.round(K.clip(y_pred, 0, 1))
    y_pred_neg = 1 - y_pred_pos

    y_pos = K.round(K.clip(y_true, 0, 1))
    y_neg = 1 - y_pos

    tp = K.sum(y_pos * y_pred_pos)
    tn = K.sum(y_neg * y_pred_neg)

    fp = K.sum(y_neg * y_pred_pos)
    fn = K.sum(y_pos * y_pred_neg)

    numerator = (tp * tn - fp * fn)
    denominator = K.sqrt((tp + fp) * (tp + fn) * (tn + fp) * (tn + fn))

    return 1.0 - numerator / (denominator + K.epsilon())
</code></pre>

<p>If I use this function as a metric and not as the loss function it works. How can I use this function as a loss?</p>

<p>After removing K.round I get following error:</p>

<blockquote>
  <p>InvalidArgumentError: Can not squeeze dim[0], expected a dimension of 1,
  got 8
     [[{{node loss_9/dense_10_loss/Squeeze}} = Squeeze[T=DT_FLOAT,
  squeeze_dims=[-1],
  _device="/job:localhost/replica:0/task:0/device:GPU:0"]
  (_arg_dense_10_sample_weights_0_2/_2445)]]
     [[{{node loss_9/add_12/_2467}} = _Recvclient_terminated=false,
  recv_device="/job:localhost/replica:0/task:0/device:CPU:0",
  send_device="/job:localhost/replica:0/task:0/device:GPU:0",
  send_device_incarnation=1, tensor_name="edge_6418_loss_9/add_12",
  tensor_type=DT_FLOAT,
  _device="/job:localhost/replica:0/task:0/device:CPU:0"]]</p>
</blockquote>