title: How to calculate F1 Macro in Keras?
body: <p>i've tried to use the codes given from Keras before they're removed. Here's the code :</p>
  
  <pre><code>def precision(y_true, y_pred):
      true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
      predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
      precision = true_positives / (predicted_positives + K.epsilon())
      return precision
  
  def recall(y_true, y_pred):
      true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
      possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
      recall = true_positives / (possible_positives + K.epsilon())
      return recall
  
  def fbeta_score(y_true, y_pred, beta=1):
      if beta &lt; 0:
          raise ValueError('The lowest choosable beta is zero (only precision).')
  
      # If there are no true positives, fix the F score at 0 like sklearn.
      if K.sum(K.round(K.clip(y_true, 0, 1))) == 0:
          return 0
  
      p = precision(y_true, y_pred)
      r = recall(y_true, y_pred)
      bb = beta ** 2
      fbeta_score = (1 + bb) * (p * r) / (bb * p + r + K.epsilon())
      return fbeta_score
  
  def fmeasure(y_true, y_pred):
      return fbeta_score(y_true, y_pred, beta=1)
  </code></pre>
  
  <p>From what i saw (i'm an amateur in this), it seems like they use the correct formula. But, when i tried to use it as a metrics in the training process, I got exactly equal output for val_accuracy, val_precision, val_recall, and val_fmeasure. I do believe that it might happen even if the formula correct, but i believe it is unlikely. Any explanation for this issue? Thank you</p>