{
  "id": 210836,
  "title": "Creating a custom loss function.",
  "url": "/competitions/cassava-leaf-disease-classification/discussion/210836",
  "author_name": "Yuri Njathi",
  "post_date": "2021-01-12T15:13:02.351000",
  "votes": 0,
  "comment_count": 0,
  "views": 0,
  "content": "<p>Based on Coursera Course : <a href=\"https://www.coursera.org/specializations/tensorflow-advanced-techniques\" target=\"_blank\">Tensorflow Advanced Techniques</a></p>\n<h2>Skeleton structure</h2>\n<p><code>def loss_function(y_true,y_pred):</code><br>\n<code>#code</code><br>\n<code>return losses</code></p>\n<h2>Huber loss function</h2>\n<p>Here is how it looks.<br>\nDid you know : The Huber loss is a loss function is less sensitive to outliers in data than the squared error loss. <br>\n Also a variant for binary classification is also sometimes used. </p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F3592731%2F0dca83807e127d9a3cd3b3ccaada9704%2FScreenshot%20from%202021-01-12%2018-48-21.png?generation=1610466606649520&amp;alt=media\" alt=\"Huber Loss\"></p>\n<p>Python code implementation : <br>\n<code>def huber_loss(y_true,y_pred):</code><br>\n<code>threshold = 1</code><br>\n<code>error = y_true - y_pred</code><br>\n<code>is_small_error = tf.abs(error) &lt;= threshold</code><br>\n<code>small_error_loss = tf.square(error)/2</code><br>\n<code>big_error_loss = threshold * (tf.abs(error)- (0.5 * threshold))</code><br>\n<code>return tf.where(is_small_error, small_error_loss, big_error_loss)</code><br>\nThat's it. I like how tensorflow has functions that show it was build from the ground up. A lot to learn.</p>",
  "messages": [
    {
      "id": 1150414,
      "postDate": "2021-01-12T15:13:02.350Z",
      "content": "<p>Based on Coursera Course : <a href=\"https://www.coursera.org/specializations/tensorflow-advanced-techniques\" target=\"_blank\">Tensorflow Advanced Techniques</a></p>\n<h2>Skeleton structure</h2>\n<p><code>def loss_function(y_true,y_pred):</code><br>\n<code>#code</code><br>\n<code>return losses</code></p>\n<h2>Huber loss function</h2>\n<p>Here is how it looks.<br>\nDid you know : The Huber loss is a loss function is less sensitive to outliers in data than the squared error loss. <br>\n Also a variant for binary classification is also sometimes used. </p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F3592731%2F0dca83807e127d9a3cd3b3ccaada9704%2FScreenshot%20from%202021-01-12%2018-48-21.png?generation=1610466606649520&amp;alt=media\" alt=\"Huber Loss\"></p>\n<p>Python code implementation : <br>\n<code>def huber_loss(y_true,y_pred):</code><br>\n<code>threshold = 1</code><br>\n<code>error = y_true - y_pred</code><br>\n<code>is_small_error = tf.abs(error) &lt;= threshold</code><br>\n<code>small_error_loss = tf.square(error)/2</code><br>\n<code>big_error_loss = threshold * (tf.abs(error)- (0.5 * threshold))</code><br>\n<code>return tf.where(is_small_error, small_error_loss, big_error_loss)</code><br>\nThat's it. I like how tensorflow has functions that show it was build from the ground up. A lot to learn.</p>",
      "rawMarkdown": "Based on Coursera Course : [Tensorflow Advanced Techniques](https://www.coursera.org/specializations/tensorflow-advanced-techniques)\n## Skeleton structure\n\n`def loss_function(y_true,y_pred):`\n`     #code `\n`     return losses`\n\n## Huber loss function\nHere is how it looks.\nDid you know : The Huber loss is a loss function is less sensitive to outliers in data than the squared error loss. \n Also a variant for binary classification is also sometimes used. \n\n![Huber Loss](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F3592731%2F0dca83807e127d9a3cd3b3ccaada9704%2FScreenshot%20from%202021-01-12%2018-48-21.png?generation=1610466606649520&alt=media)\n \nPython code implementation : \n`def huber_loss(y_true,y_pred):`\n`     threshold = 1 `\n`     error = y_true - y_pred`\n`     is_small_error = tf.abs(error) <= threshold`\n`     small_error_loss = tf.square(error)/2`\n`     big_error_loss = threshold * (tf.abs(error)- (0.5 * threshold))`\n`     return tf.where(is_small_error, small_error_loss, big_error_loss)`\nThat's it. I like how tensorflow has functions that show it was build from the ground up. A lot to learn."
    }
  ],
  "comments": [],
  "raw_markdown_by_id": {
    "1150414": "Based on Coursera Course : [Tensorflow Advanced Techniques](https://www.coursera.org/specializations/tensorflow-advanced-techniques)\n## Skeleton structure\n\n`def loss_function(y_true,y_pred):`\n`     #code `\n`     return losses`\n\n## Huber loss function\nHere is how it looks.\nDid you know : The Huber loss is a loss function is less sensitive to outliers in data than the squared error loss. \n Also a variant for binary classification is also sometimes used. \n\n![Huber Loss](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F3592731%2F0dca83807e127d9a3cd3b3ccaada9704%2FScreenshot%20from%202021-01-12%2018-48-21.png?generation=1610466606649520&alt=media)\n \nPython code implementation : \n`def huber_loss(y_true,y_pred):`\n`     threshold = 1 `\n`     error = y_true - y_pred`\n`     is_small_error = tf.abs(error) <= threshold`\n`     small_error_loss = tf.square(error)/2`\n`     big_error_loss = threshold * (tf.abs(error)- (0.5 * threshold))`\n`     return tf.where(is_small_error, small_error_loss, big_error_loss)`\nThat's it. I like how tensorflow has functions that show it was build from the ground up. A lot to learn."
  }
}