{
  "id": 188556,
  "title": "anyone have created the custom Performance metrics for TF? ",
  "url": "/competitions/rsna-str-pulmonary-embolism-detection/discussion/188556",
  "author_name": "Saurabh dubey",
  "post_date": "2020-10-03T21:22:03.617000",
  "votes": 2,
  "comment_count": 0,
  "views": 0,
  "content": "<p>```<br>\nimport keras.backend as K</p>\n<p>def f1_score(y_true, y_pred):</p>\n<pre><code># Count positive samples.\nc1 = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\nc2 = K.sum(K.round(K.clip(y_pred, 0, 1)))\nc3 = K.sum(K.round(K.clip(y_true, 0, 1)))\n\n# If there are no true samples, fix the F1 score at 0.\nif c3 == 0:\n    return 0\n\n# How many selected items are relevant?\nprecision = c1 / c2\n\n# How many relevant items are selected?\nrecall = c1 / c3\n\n# Calculate f1_score\nf1_score = 2 * (precision * recall) / (precision + recall)\nreturn f1_score```\n</code></pre>\n<p>But not able implement it for the competiton metrics, any resources or advice will be welcomed</p>\n<p>Thank you</p>",
  "messages": [
    {
      "id": 1036513,
      "postDate": "2020-10-03T21:22:03.617Z",
      "content": "<p>```<br>\nimport keras.backend as K</p>\n<p>def f1_score(y_true, y_pred):</p>\n<pre><code># Count positive samples.\nc1 = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\nc2 = K.sum(K.round(K.clip(y_pred, 0, 1)))\nc3 = K.sum(K.round(K.clip(y_true, 0, 1)))\n\n# If there are no true samples, fix the F1 score at 0.\nif c3 == 0:\n    return 0\n\n# How many selected items are relevant?\nprecision = c1 / c2\n\n# How many relevant items are selected?\nrecall = c1 / c3\n\n# Calculate f1_score\nf1_score = 2 * (precision * recall) / (precision + recall)\nreturn f1_score```\n</code></pre>\n<p>But not able implement it for the competiton metrics, any resources or advice will be welcomed</p>\n<p>Thank you</p>",
      "rawMarkdown": "```\nimport keras.backend as K\n\ndef f1_score(y_true, y_pred):\n\n    # Count positive samples.\n    c1 = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    c2 = K.sum(K.round(K.clip(y_pred, 0, 1)))\n    c3 = K.sum(K.round(K.clip(y_true, 0, 1)))\n\n    # If there are no true samples, fix the F1 score at 0.\n    if c3 == 0:\n        return 0\n\n    # How many selected items are relevant?\n    precision = c1 / c2\n\n    # How many relevant items are selected?\n    recall = c1 / c3\n\n    # Calculate f1_score\n    f1_score = 2 * (precision * recall) / (precision + recall)\n    return f1_score```\n\nBut not able implement it for the competiton metrics, any resources or advice will be welcomed\n\nThank you",
      "votes": 2
    }
  ],
  "comments": [],
  "raw_markdown_by_id": {
    "1036513": "```\nimport keras.backend as K\n\ndef f1_score(y_true, y_pred):\n\n    # Count positive samples.\n    c1 = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    c2 = K.sum(K.round(K.clip(y_pred, 0, 1)))\n    c3 = K.sum(K.round(K.clip(y_true, 0, 1)))\n\n    # If there are no true samples, fix the F1 score at 0.\n    if c3 == 0:\n        return 0\n\n    # How many selected items are relevant?\n    precision = c1 / c2\n\n    # How many relevant items are selected?\n    recall = c1 / c3\n\n    # Calculate f1_score\n    f1_score = 2 * (precision * recall) / (precision + recall)\n    return f1_score```\n\nBut not able implement it for the competiton metrics, any resources or advice will be welcomed\n\nThank you"
  }
}