<p>@Alexis has already given the answer to the error message, but I want to clarify something about loss functions which are derived from metrics:</p>
<p>In general metrics can not be used as loss functions, but often smoothed version of metrics like the dice measure (=F1 score) <a href="https://arxiv.org/abs/1707.03237" rel="nofollow noreferrer">CH Sudre 2014</a> can be applied as loss functions. One usecase might be image segmentation.</p>
<p>(Please excuse, that I have to add this comment as an answer, since I do not have enough reputation to add a comment)</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>