title: How to use F1 Score with Keras model?
body: <p>For some reason I get error message when trying to specify f1 score with Keras model:</p>
  
  <pre><code>model.compile(optimizer='adam', loss='mse', metrics=['accuracy', 'f1_score'])
  </code></pre>
  
  <p>I get this error:</p>
  
  <pre><code>ValueError: Unknown metric function:f1_score
  </code></pre>
  
  <p>After providing 'f1_score' function in the same file where I use 'model.compile' like this:</p>
  
  <pre><code>def f1_score(y_true, y_pred):
  
      # Count positive samples.
      c1 = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
      c2 = K.sum(K.round(K.clip(y_pred, 0, 1)))
      c3 = K.sum(K.round(K.clip(y_true, 0, 1)))
  
      # If there are no true samples, fix the F1 score at 0.
      if c3 == 0:
          return 0
  
      # How many selected items are relevant?
      precision = c1 / c2
  
      # How many relevant items are selected?
      recall = c1 / c3
  
      # Calculate f1_score
      f1_score = 2 * (precision * recall) / (precision + recall)
      return f1_score 
  
  model.compile(optimizer='adam', loss='mse', metrics=['accuracy', f1_score])
  </code></pre>
  
  <p>Model compiles all right and can be saved to a file:</p>
  
  <pre><code>model.save(model_path) # works ok
  </code></pre>
  
  <p>Yet loading it in another program, :</p>
  
  <pre><code>from keras import models 
  model = models.load_model(model_path)
  </code></pre>
  
  <p>fails with an error:</p>
  
  <pre><code>ValueError: Unknown metric function:f1_score
  </code></pre>
  
  <p>Specifying 'f1_score' in the same file this time does not help, Keras does not see it. What's wrong? How to use F1 Score with Keras model?</p>