title: Keras Custom Loss Function using with confusion matrix
body: <p>I was trying to create a custom loss function in Keras for a binary classification problem based on a confusion matrix. More precisely, the following equation should be minimized: `</p>
  <p>Min: 10xFalsePositive + 500xFalse Negative</p>
  <p>I wrote the following custom loss function:</p>
  <pre><code>def custom_loss_func(y_train, y_pred):
      cm=metrics.confusion_matrix(y_train,y_pred)
      cm1 = pd.DataFrame(cm.reshape((1,4)), columns=['TN', 'FP', 'FN', 'TP'])
      cm1 = cm1.fillna(0)
      TC= 10*cm1.FP + 500*cm1.FN
      return float (TC)
  
  # Compiling the ANN
  ann.compile(optimizer = 'adam', loss = my_loss_fn, metrics = ['accuracy'])
  
  # Fitting the ANN to the Training set
  ann.fit(x_train,y_train,batch_size=32,epochs = 50, class_weight= class_weights)
  </code></pre>
  <p>When I run the code I get the following error:</p>
  <pre><code>NotImplementedError: Cannot convert a symbolic tf.Tensor (my_loss_fn/strided_slice:0) to a numpy array. 
  This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported.
  </code></pre>
  <p>I also tried a native Tensorflow implementation:</p>
  <pre><code>def my_loss_fn(y_train, y_pred):
      FP = tf.keras.metrics.FalsePositives()
      FP.update_state(y_train, y_pred)
      FP=m.result().numpy()
  
      FN = tf.keras.metrics.FalseNegatives()
      FN.update_state(y_train, y_pred)
      FN=n.result().numpy()
  
      # Converted as Keras Tensors
      FP = tf.Variable(m)
      FN = tf.Variable(n)
  
      loss = 500*FN + 10*FP
  
      return loss
  </code></pre>
  <p>This one gave me the follwing error:</p>
  <pre><code>ValueError(f&quot;No gradients provided for any variable: {variable}. &quot;
  
   f&quot;Provided `grads_and_vars` is {grads_and_vars}.&quot;)
    if vars_with_empty_grads:
   logging.warning(
   (&quot;Gradients do not exist for variables %s when minimizing the loss. &quot;
   &quot;If you're using `model.compile()`, did you forget to provide a `loss`&quot;
   &quot;argument?&quot;),
   ([v.name for v in vars_with_empty_grads]))
  </code></pre>
  <p>Could anyone give me some help on this problem?</p>