{
  "id": 240365,
  "title": "Harversine loss implementation for TF users",
  "url": "/competitions/google-smartphone-decimeter-challenge/discussion/240365",
  "author_name": "",
  "post_date": "2021-05-19T13:26:43.985382800Z",
  "votes": 23,
  "comment_count": 3,
  "views": 0,
  "content": "<p>Hi kagglers, here is a quick way to implement the harversine loss function with keras:</p>\n<p>`</p>\n<pre><code>def loss(y_true, y_pred):\n    PI_ON_180 = tf.constant(np.pi / 180, dtype=tf.float32)\n    RADIUS_M = tf.constant(6_377_000, dtype = tf.float32)\n    tf.dtypes.cast(y_true, tf.float32)\n    tf.dtypes.cast(y_pred, tf.float32)\n\n    yt_rad = y_true * PI_ON_180\n    yp_rad = y_pred * PI_ON_180\n\n    delta = yt_rad - yp_rad\n    v = delta / 2\n    v = tf.sin(v)\n    v = v**2\n\n    a = v[:,1] + tf.cos(yt_rad[:,1]) * tf.cos(yp_rad[:,1]) * v[:,0] \n    c = tf.sqrt(a)\n    c = 2* tf.math.asin(c)\n    c = c*RADIUS_M\n\n    final = tf.reduce_mean(c)\n    return final\n</code></pre>\n<p>`</p>\n<p>credit : <a href=\"https://stackoverflow.com/questions/59315501/how-to-use-haversine-function-as-loss-function-while-training-model-in-tensorflo\" target=\"_blank\">here</a>.</p>",
  "messages": [
    {
      "id": "1314974",
      "postDate": "05/19/2021 13:26:43",
      "content": "<p>Hi kagglers, here is a quick way to implement the harversine loss function with keras:</p>\n<p>`</p>\n<pre><code>def loss(y_true, y_pred):\n    PI_ON_180 = tf.constant(np.pi / 180, dtype=tf.float32)\n    RADIUS_M = tf.constant(6_377_000, dtype = tf.float32)\n    tf.dtypes.cast(y_true, tf.float32)\n    tf.dtypes.cast(y_pred, tf.float32)\n\n    yt_rad = y_true * PI_ON_180\n    yp_rad = y_pred * PI_ON_180\n\n    delta = yt_rad - yp_rad\n    v = delta / 2\n    v = tf.sin(v)\n    v = v**2\n\n    a = v[:,1] + tf.cos(yt_rad[:,1]) * tf.cos(yp_rad[:,1]) * v[:,0] \n    c = tf.sqrt(a)\n    c = 2* tf.math.asin(c)\n    c = c*RADIUS_M\n\n    final = tf.reduce_mean(c)\n    return final\n</code></pre>\n<p>`</p>\n<p>credit : <a href=\"https://stackoverflow.com/questions/59315501/how-to-use-haversine-function-as-loss-function-while-training-model-in-tensorflo\" target=\"_blank\">here</a>.</p>",
      "rawMarkdown": "Hi kagglers, here is a quick way to implement the harversine loss function with keras:\n\n`\n \n    def loss(y_true, y_pred):\n        PI_ON_180 = tf.constant(np.pi / 180, dtype=tf.float32)\n        RADIUS_M = tf.constant(6_377_000, dtype = tf.float32)\n        tf.dtypes.cast(y_true, tf.float32)\n        tf.dtypes.cast(y_pred, tf.float32)\n    \n        yt_rad = y_true * PI_ON_180\n        yp_rad = y_pred * PI_ON_180\n    \n        delta = yt_rad - yp_rad\n        v = delta / 2\n        v = tf.sin(v)\n        v = v**2\n    \n        a = v[:,1] + tf.cos(yt_rad[:,1]) * tf.cos(yp_rad[:,1]) * v[:,0] \n        c = tf.sqrt(a)\n        c = 2* tf.math.asin(c)\n        c = c*RADIUS_M\n    \n        final = tf.reduce_mean(c)\n        return final\n`\n\ncredit : [here](https://stackoverflow.com/questions/59315501/how-to-use-haversine-function-as-loss-function-while-training-model-in-tensorflo).",
      "votes": null
    },
    {
      "id": "1320016",
      "postDate": "05/23/2021 17:01:19",
      "content": "<p>Hey, thank you for the post, is this the same as this competition's evaluation metric?</p>",
      "rawMarkdown": "Hey, thank you for the post, is this the same as this competition's evaluation metric?",
      "votes": null
    },
    {
      "id": "1320558",
      "postDate": "05/24/2021 06:42:22",
      "content": "<p>No. But I can modify a few line to have it  </p>",
      "rawMarkdown": "No. But I can modify a few line to have it",
      "votes": null
    },
    {
      "id": "1344519",
      "postDate": "06/11/2021 01:37:31",
      "content": "<p>Why is it loss? Isn’t haversine a distance function?</p>",
      "rawMarkdown": "Why is it loss? Isn’t haversine a distance function?",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 1320016,
      "author_name": "avivlevi815",
      "author_url": "",
      "post_date": "05/23/2021 17:01:19",
      "content": "<p>Hey, thank you for the post, is this the same as this competition's evaluation metric?</p>",
      "votes": null,
      "replies": [
        {
          "id": 1320558,
          "author_name": "ulrich07",
          "author_url": "",
          "post_date": "05/24/2021 06:42:22",
          "content": "<p>No. But I can modify a few line to have it  </p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 1344519,
      "author_name": "rushic24",
      "author_url": "",
      "post_date": "06/11/2021 01:37:31",
      "content": "<p>Why is it loss? Isn’t haversine a distance function?</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "1314974": "Hi kagglers, here is a quick way to implement the harversine loss function with keras:\n\n`\n \n    def loss(y_true, y_pred):\n        PI_ON_180 = tf.constant(np.pi / 180, dtype=tf.float32)\n        RADIUS_M = tf.constant(6_377_000, dtype = tf.float32)\n        tf.dtypes.cast(y_true, tf.float32)\n        tf.dtypes.cast(y_pred, tf.float32)\n    \n        yt_rad = y_true * PI_ON_180\n        yp_rad = y_pred * PI_ON_180\n    \n        delta = yt_rad - yp_rad\n        v = delta / 2\n        v = tf.sin(v)\n        v = v**2\n    \n        a = v[:,1] + tf.cos(yt_rad[:,1]) * tf.cos(yp_rad[:,1]) * v[:,0] \n        c = tf.sqrt(a)\n        c = 2* tf.math.asin(c)\n        c = c*RADIUS_M\n    \n        final = tf.reduce_mean(c)\n        return final\n`\n\ncredit : [here](https://stackoverflow.com/questions/59315501/how-to-use-haversine-function-as-loss-function-while-training-model-in-tensorflo).",
    "1320016": "Hey, thank you for the post, is this the same as this competition's evaluation metric?",
    "1320558": "No. But I can modify a few line to have it",
    "1344519": "Why is it loss? Isn’t haversine a distance function?"
  },
  "source": "meta"
}