{"id":7397,"date":"2023-09-07T10:26:15","date_gmt":"2023-09-07T18:26:15","guid":{"rendered":"https:\/\/live-cometml.pantheonsite.io\/?p=7397"},"modified":"2025-04-24T17:14:20","modified_gmt":"2025-04-24T17:14:20","slug":"guide-to-loss-functions-for-machine-learning-models","status":"publish","type":"post","link":"https:\/\/www.comet.com\/site\/blog\/guide-to-loss-functions-for-machine-learning-models\/","title":{"rendered":"Guide to Loss Functions for Machine Learning Models"},"content":{"rendered":"\n<link rel=\"canonical\" href=\"https:\/\/www.comet.com\/site\/blog\/guide-to-loss-functions-for-machine-learning-models\">\n\n\n\n<div class=\"eo ep eq er es\">\n<div class=\"ab ca\">\n<div class=\"ch bg dx dy dz ea\">\n<figure class=\"lr ls lt lu lv lw lo lp paragraph-image\">\n<div class=\"lx ly hb lz bg ma\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg mb mc c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/0*ag5QD6HncPkXdd0P\" alt=\"\" width=\"700\" height=\"1050\"><\/figure><div class=\"lo lp lq\"><picture><\/picture><\/div>\n<\/div><figcaption class=\"md me mf lo lp mg mh be b bf z gi\" data-selectable-paragraph=\"\">Photo by <a class=\"af mi\" href=\"https:\/\/unsplash.com\/@alexkixa?utm_source=medium&amp;utm_medium=referral\" target=\"_blank\" rel=\"noopener ugc nofollow\">Alexandre Debi\u00e8ve<\/a> on <a class=\"af mi\" href=\"https:\/\/unsplash.com\/?utm_source=medium&amp;utm_medium=referral\" target=\"_blank\" rel=\"noopener ugc nofollow\">Unsplash<\/a><\/figcaption><\/figure>\n<p id=\"5e5b\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\">In machine learning, a loss function is used to measure the loss, or cost, of a specific machine learning model. These loss functions calculate the amount of error in a specific machine learning model using some mathematical formula and measure the performance of that specific model.<\/p>\n<p id=\"40e1\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\">There are various loss functions that are used in machine learning for regression and classification problems. In the process of the machine learning model building, our aim is to minimize the loss\/cost function and therefore increase the accuracy of the model.<\/p>\n<p id=\"fa25\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\">In this article, we will go through various loss functions used in machine learning models for regression and classification problems. Let\u2019s get started!<\/p>\n<h1 id=\"c21a\" class=\"ne nf ev be ng nh ni fv nj nk nl fy nm nn no np nq nr ns nt nu nv nw nx ny nz bj\" data-selectable-paragraph=\"\">Loss functions for Regression<\/h1>\n<h2 id=\"26b6\" class=\"oa nf ev be ng ob oc od nj oe of og nm mr oh oi oj mv ok ol om mz on oo op oq bj\" data-selectable-paragraph=\"\">Mean Squared Error<\/h2>\n<p id=\"4185\" class=\"pw-post-body-paragraph mj mk ev be b ft or mm mn fw os mp mq mr ot mt mu mv ou mx my mz ov nb nc nd eo bj\" data-selectable-paragraph=\"\">The mean squared error (MSE) measures the amount of error in machine learning regression models by calculating the average squared distance between the observed and predicted values in our data.<\/p>\n<p id=\"d703\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\">This loss function penalizes the model for large errors by squaring them and therefore making the model <strong class=\"be ow\">less<\/strong> <strong class=\"be ow\">robust<\/strong> to the outliers. We should not use this loss function when the dataset is prone to too many outliers.<\/p>\n<p id=\"40f4\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\">For a good model\/estimator, the value of the MSE should be closer to zero.<\/p>\n<p id=\"2e10\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\">If we have a data point Yi and the predicted value for this point is \u0176i, then the mean squared error can be calculated as:<\/p>\n<figure class=\"lr ls lt lu lv lw lo lp paragraph-image\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg mb mc c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:605\/1*Xru-2k-DxCH76LBv6UiPhg.png\" alt=\"\" width=\"605\" height=\"159\"><\/figure><div class=\"lo lp ox\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/format:webp\/1*Xru-2k-DxCH76LBv6UiPhg.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/format:webp\/1*Xru-2k-DxCH76LBv6UiPhg.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/format:webp\/1*Xru-2k-DxCH76LBv6UiPhg.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/format:webp\/1*Xru-2k-DxCH76LBv6UiPhg.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/format:webp\/1*Xru-2k-DxCH76LBv6UiPhg.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/format:webp\/1*Xru-2k-DxCH76LBv6UiPhg.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1210\/format:webp\/1*Xru-2k-DxCH76LBv6UiPhg.png 1210w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 605px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/1*Xru-2k-DxCH76LBv6UiPhg.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/1*Xru-2k-DxCH76LBv6UiPhg.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/1*Xru-2k-DxCH76LBv6UiPhg.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/1*Xru-2k-DxCH76LBv6UiPhg.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/1*Xru-2k-DxCH76LBv6UiPhg.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/1*Xru-2k-DxCH76LBv6UiPhg.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1210\/1*Xru-2k-DxCH76LBv6UiPhg.png 1210w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 605px\" data-testid=\"og\"><\/picture><\/div>\n<\/figure>\n<p id=\"b563\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\">Where n is the total number of observations in our dataset.<\/p>\n<p id=\"ba9b\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\"><strong class=\"be ow\">Example<\/strong>:<\/p>\n<pre class=\"lr ls lt lu lv oy oz pa pb ax pc bj\"><span id=\"7e1b\" class=\"oa nf ev oz b hj pd pe l hz pf\" data-selectable-paragraph=\"\"><strong class=\"oz ew\">import<\/strong> numpy <strong class=\"oz ew\">as<\/strong> np<\/span><span id=\"eff9\" class=\"oa nf ev oz b hj pg pe l hz pf\" data-selectable-paragraph=\"\">def <strong class=\"oz ew\">mean_squared_error<\/strong>(true_val, pred):\n    squared_error = np.square(true_val - pred)\n    sum_squared_error = np.sum(squared_error)\n    mse_loss = sum_squared_error \/ true.size\n\n    <strong class=\"oz ew\">return<\/strong> mse_loss<\/span><\/pre>\n<p id=\"9335\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\">In machine learning, we aim to lower the MSE value to increase the accuracy of the model.<\/p>\n<h2 id=\"c8cf\" class=\"oa nf ev be ng ob oc od nj oe of og nm mr oh oi oj mv ok ol om mz on oo op oq bj\" data-selectable-paragraph=\"\">Mean Absolute Error<\/h2>\n<p id=\"99fc\" class=\"pw-post-body-paragraph mj mk ev be b ft or mm mn fw os mp mq mr ot mt mu mv ou mx my mz ov nb nc nd eo bj\" data-selectable-paragraph=\"\">The mean absolute error (MAE) measures the amount of error in machine learning models by calculating the total absolute difference between the actual value and the predicted value in our data. This error is also known as<strong class=\"be ow\"> L1 loss<\/strong>.<\/p>\n<p id=\"6f8b\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\">This mean absolute error loss function is <strong class=\"be ow\">more robust<\/strong> to the outliers in comparison with the MSE loss function. Therefore, we can use this loss function when the dataset is prone to too many outliers.<\/p>\n<p id=\"5658\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\">If we have data point Yi and the corresponding predicted value for this data point is \u0176i, then the mean absolute error can be calculated as:<\/p>\n<figure class=\"lr ls lt lu lv lw lo lp paragraph-image\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg mb mc c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:584\/1*CyxisErQIN3igHXeA3liWg.png\" alt=\"\" width=\"584\" height=\"152\"><\/figure><div class=\"lo lp ph\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/format:webp\/1*CyxisErQIN3igHXeA3liWg.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/format:webp\/1*CyxisErQIN3igHXeA3liWg.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/format:webp\/1*CyxisErQIN3igHXeA3liWg.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/format:webp\/1*CyxisErQIN3igHXeA3liWg.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/format:webp\/1*CyxisErQIN3igHXeA3liWg.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/format:webp\/1*CyxisErQIN3igHXeA3liWg.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1168\/format:webp\/1*CyxisErQIN3igHXeA3liWg.png 1168w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 584px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/1*CyxisErQIN3igHXeA3liWg.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/1*CyxisErQIN3igHXeA3liWg.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/1*CyxisErQIN3igHXeA3liWg.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/1*CyxisErQIN3igHXeA3liWg.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/1*CyxisErQIN3igHXeA3liWg.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/1*CyxisErQIN3igHXeA3liWg.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1168\/1*CyxisErQIN3igHXeA3liWg.png 1168w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 584px\" data-testid=\"og\"><\/picture><\/div>\n<\/figure>\n<p id=\"1250\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\">Where n is the total number of observations in our dataset.<\/p>\n<p id=\"2899\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\"><strong class=\"be ow\">Example<\/strong>:<\/p>\n<pre class=\"lr ls lt lu lv oy oz pa pb ax pc bj\"><span id=\"d9d0\" class=\"oa nf ev oz b hj pd pe l hz pf\" data-selectable-paragraph=\"\"><strong class=\"oz ew\">from<\/strong> sklearn.metrics <strong class=\"oz ew\">import<\/strong> mean_absolute_error<\/span><span id=\"fa81\" class=\"oa nf ev oz b hj pg pe l hz pf\" data-selectable-paragraph=\"\">#sample data\nactual <strong class=\"oz ew\">=<\/strong> [2, 3, 5, 5, 9]\npredicted <strong class=\"oz ew\">=<\/strong> [3, 3, 8, 7, 6]<\/span><span id=\"00cc\" class=\"oa nf ev oz b hj pg pe l hz pf\" data-selectable-paragraph=\"\">#calculate MAE<\/span><span id=\"94fc\" class=\"oa nf ev oz b hj pg pe l hz pf\" data-selectable-paragraph=\"\">error <strong class=\"oz ew\">=<\/strong> <strong class=\"oz ew\">mean_absolute_error<\/strong>(actual, predicted)\nprint(error)<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"eo ep eq er es\">\n<div class=\"ab ca\">\n<div class=\"ch bg dx dy dz ea\">\n<blockquote class=\"pq\"><p id=\"dc5e\" class=\"pr ps ev be pt pu pv pw px py pz nd gi\" data-selectable-paragraph=\"\">Struggling to track and reproduce complex experiment parameters? Artifacts are just one of the many tools in the Comet toolbox to help ease model management. <a class=\"af mi\" href=\"https:\/\/www.comet.com\/site\/blog\/debugging-your-machine-learning-models-with-comet-artifacts\/\" target=\"_blank\" rel=\"noopener ugc nofollow\">Read our PetCam scenario to learn more.<\/a><\/p><\/blockquote>\n<\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"eo ep eq er es\">\n<div class=\"ab ca\">\n<div class=\"ch bg dx dy dz ea\">\n<h2 id=\"68ba\" class=\"oa nf ev be ng ob oc od nj oe of og nm mr oh oi oj mv ok ol om mz on oo op oq bj\" data-selectable-paragraph=\"\">Mean Absolute Percentage Error<\/h2>\n<p id=\"d400\" class=\"pw-post-body-paragraph mj mk ev be b ft or mm mn fw os mp mq mr ot mt mu mv ou mx my mz ov nb nc nd eo bj\" data-selectable-paragraph=\"\">The mean absolute percentage error (MAPE) loss function measures the amount of error by taking the absolute difference between the actual value and the predicted value in our data and then dividing it by the actual value. We apply the absolute percentage to this value and then average it across our dataset.<\/p>\n<p id=\"4d1b\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\">This loss function measures the error better than the MSE as it does not penalize large errors. This loss function is commonly used as it normalizes all the errors on a common scale and is easy to interpret.<\/p>\n<p id=\"5c80\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\">If we have data point Yi and the corresponding predicted value for this data point is \u0176i, then the mean absolute percentage error can be calculated as:<\/p>\n<figure class=\"lr ls lt lu lv lw lo lp paragraph-image\">\n<div class=\"lx ly hb lz bg ma\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg mb mc c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*6gP58Rm5942V_Xc9q2WZIg.png\" alt=\"\" width=\"700\" height=\"230\"><\/figure><div class=\"lo lp qa\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/format:webp\/1*6gP58Rm5942V_Xc9q2WZIg.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/format:webp\/1*6gP58Rm5942V_Xc9q2WZIg.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/format:webp\/1*6gP58Rm5942V_Xc9q2WZIg.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/format:webp\/1*6gP58Rm5942V_Xc9q2WZIg.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/format:webp\/1*6gP58Rm5942V_Xc9q2WZIg.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/format:webp\/1*6gP58Rm5942V_Xc9q2WZIg.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/format:webp\/1*6gP58Rm5942V_Xc9q2WZIg.png 1400w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/1*6gP58Rm5942V_Xc9q2WZIg.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/1*6gP58Rm5942V_Xc9q2WZIg.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/1*6gP58Rm5942V_Xc9q2WZIg.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/1*6gP58Rm5942V_Xc9q2WZIg.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/1*6gP58Rm5942V_Xc9q2WZIg.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/1*6gP58Rm5942V_Xc9q2WZIg.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/1*6gP58Rm5942V_Xc9q2WZIg.png 1400w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\" data-testid=\"og\"><\/picture><\/div>\n<\/div>\n<\/figure>\n<p id=\"50c0\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\">Where n is the total number of observations in our dataset.<\/p>\n<p id=\"ee29\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\"><strong class=\"be ow\">Example:<\/strong><\/p>\n<pre class=\"lr ls lt lu lv oy oz pa pb ax pc bj\"><span id=\"9f21\" class=\"oa nf ev oz b hj pd pe l hz pf\" data-selectable-paragraph=\"\"><strong class=\"oz ew\">import<\/strong> numpy <strong class=\"oz ew\">as<\/strong> np<\/span><span id=\"ab10\" class=\"oa nf ev oz b hj pg pe l hz pf\" data-selectable-paragraph=\"\">def <strong class=\"oz ew\">mean_absolute_percentage_error<\/strong>(actual_value, pred):\n    abs_error = (np.abs(actual_value - pred)) \/ actual_value\n    sum_abs_error = np.sum(abs_error)\n    mape_loss_perc = (sum_abs_error \/ true.size) * 100<\/span><span id=\"9763\" class=\"oa nf ev oz b hj pg pe l hz pf\" data-selectable-paragraph=\"\">    <strong class=\"oz ew\">return<\/strong> mape_loss_perc<\/span><\/pre>\n<p id=\"8a42\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\">The smaller the value of MAPE, the better will be the accuracy of the model.<\/p>\n<h2 id=\"1abf\" class=\"oa nf ev be ng ob oc od nj oe of og nm mr oh oi oj mv ok ol om mz on oo op oq bj\" data-selectable-paragraph=\"\">Mean Squared Logarithmic Error<\/h2>\n<p id=\"6e49\" class=\"pw-post-body-paragraph mj mk ev be b ft or mm mn fw os mp mq mr ot mt mu mv ou mx my mz ov nb nc nd eo bj\" data-selectable-paragraph=\"\">The mean squared logarithmic error (MSLE) measures the amount of error in the machine learning models by taking the ratio between the actual value and the predicted value in our data.<\/p>\n<p id=\"c52f\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\">As the name suggests, this loss function is a variation of mean squared error (MSE). We use this loss function when we do not want to significantly penalize the model (as mean squared error) for large errors than the smaller errors.<\/p>\n<p id=\"0561\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\">If we have data point Yi and the corresponding predicted value for this data point is \u0176i, then the mean absolute logarithmic error can be calculated as:<\/p>\n<figure class=\"lr ls lt lu lv lw lo lp paragraph-image\">\n<div class=\"lx ly hb lz bg ma\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg mb mc c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*bFqPWRZ9nSoV5Z8GkitZKA.png\" alt=\"\" width=\"700\" height=\"147\"><\/figure><div class=\"lo lp qb\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/format:webp\/1*bFqPWRZ9nSoV5Z8GkitZKA.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/format:webp\/1*bFqPWRZ9nSoV5Z8GkitZKA.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/format:webp\/1*bFqPWRZ9nSoV5Z8GkitZKA.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/format:webp\/1*bFqPWRZ9nSoV5Z8GkitZKA.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/format:webp\/1*bFqPWRZ9nSoV5Z8GkitZKA.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/format:webp\/1*bFqPWRZ9nSoV5Z8GkitZKA.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/format:webp\/1*bFqPWRZ9nSoV5Z8GkitZKA.png 1400w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/1*bFqPWRZ9nSoV5Z8GkitZKA.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/1*bFqPWRZ9nSoV5Z8GkitZKA.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/1*bFqPWRZ9nSoV5Z8GkitZKA.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/1*bFqPWRZ9nSoV5Z8GkitZKA.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/1*bFqPWRZ9nSoV5Z8GkitZKA.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/1*bFqPWRZ9nSoV5Z8GkitZKA.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/1*bFqPWRZ9nSoV5Z8GkitZKA.png 1400w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\" data-testid=\"og\"><\/picture><\/div>\n<\/div>\n<\/figure>\n<p id=\"babc\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\">Where n is the total number of observations in our dataset.<\/p>\n<p id=\"3a4d\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\"><strong class=\"be ow\">Example<\/strong>:<\/p>\n<pre class=\"lr ls lt lu lv oy oz pa pb ax pc bj\"><span id=\"1cb3\" class=\"oa nf ev oz b hj pd pe l hz pf\" data-selectable-paragraph=\"\"><strong class=\"oz ew\">from<\/strong> sklearn.metrics <strong class=\"oz ew\">import<\/strong> mean_squared_log_error<\/span><span id=\"c458\" class=\"oa nf ev oz b hj pg pe l hz pf\" data-selectable-paragraph=\"\">#sample data\nactual = [3, 5, 2.5, 7, 11]\npredicted = [2.5, 5, 4, 8, 10.5]<\/span><span id=\"0bb9\" class=\"oa nf ev oz b hj pg pe l hz pf\" data-selectable-paragraph=\"\">#calculate MSLE<\/span><span id=\"a0ce\" class=\"oa nf ev oz b hj pg pe l hz pf\" data-selectable-paragraph=\"\">error = <strong class=\"oz ew\">mean_squared_log_error<\/strong>(actual, predicted)\nprint(error)<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"eo ep eq er es\">\n<div class=\"ab ca\">\n<div class=\"ch bg dx dy dz ea\">\n<h1 id=\"7236\" class=\"ne nf ev be ng nh qc fv nj nk qd fy nm nn qe np nq nr qf nt nu nv qg nx ny nz bj\" data-selectable-paragraph=\"\">Loss functions for Classification<\/h1>\n<h2 id=\"f37c\" class=\"oa nf ev be ng ob oc od nj oe of og nm mr oh oi oj mv ok ol om mz on oo op oq bj\" data-selectable-paragraph=\"\">Binary Cross-Entropy Loss<\/h2>\n<p id=\"65f0\" class=\"pw-post-body-paragraph mj mk ev be b ft or mm mn fw os mp mq mr ot mt mu mv ou mx my mz ov nb nc nd eo bj\" data-selectable-paragraph=\"\">This binary cross-entropy loss is a default loss function for binary classifiers in machine learning algorithms. It measures the performance of a classification model by comparing the value of each predicted probability with the actual class output such as <strong class=\"be ow\">0 <\/strong>or<strong class=\"be ow\"> 1<\/strong>.<\/p>\n<p id=\"7469\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\">If the difference between the predicted probability and the value of actual class output increases, then the cross-entropy loss increases as well.<\/p>\n<p id=\"542b\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\">Suppose a classifier predicts a probability of 0.09 and the actual observation class is 1, then it would result in a high cross-entropy loss value. The value of this loss function for a model should be closer to 0 ideally, to achieve high accuracy.<\/p>\n<p id=\"c7fe\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\">Here\u2019s the formula for the cross-entropy loss function:<\/p>\n<figure class=\"lr ls lt lu lv lw lo lp paragraph-image\">\n<div class=\"lx ly hb lz bg ma\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg mb mc c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*HhHMqpMjGD_r7xXRxchong.png\" alt=\"\" width=\"700\" height=\"145\"><\/figure><div class=\"lo lp qh\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/format:webp\/1*HhHMqpMjGD_r7xXRxchong.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/format:webp\/1*HhHMqpMjGD_r7xXRxchong.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/format:webp\/1*HhHMqpMjGD_r7xXRxchong.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/format:webp\/1*HhHMqpMjGD_r7xXRxchong.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/format:webp\/1*HhHMqpMjGD_r7xXRxchong.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/format:webp\/1*HhHMqpMjGD_r7xXRxchong.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/format:webp\/1*HhHMqpMjGD_r7xXRxchong.png 1400w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/1*HhHMqpMjGD_r7xXRxchong.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/1*HhHMqpMjGD_r7xXRxchong.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/1*HhHMqpMjGD_r7xXRxchong.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/1*HhHMqpMjGD_r7xXRxchong.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/1*HhHMqpMjGD_r7xXRxchong.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/1*HhHMqpMjGD_r7xXRxchong.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/1*HhHMqpMjGD_r7xXRxchong.png 1400w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\" data-testid=\"og\"><\/picture><\/div>\n<\/div>\n<\/figure>\n<p id=\"e637\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\">Here, h\u03b8(xi) is the probability of class 1 and (1-h\u03b8(xi)) is the probability of class 0.<\/p>\n<p id=\"fcc1\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\"><strong class=\"be ow\">Example<\/strong>:<\/p>\n<pre class=\"lr ls lt lu lv oy oz pa pb ax pc bj\"><span id=\"2a63\" class=\"oa nf ev oz b hj pd pe l hz pf\" data-selectable-paragraph=\"\">def <strong class=\"oz ew\">binary_cross_entropy<\/strong>(actual, predicted):\n\tsum_score = 0.0\n\tfor i in <strong class=\"oz ew\">range<\/strong>(len(actual)):\n\t\tsum_score += actual[i] * log(1e-15 + predicted[i])\n\tmean_sum_score = 1.0 \/ len(actual) * sum_score<\/span><span id=\"604d\" class=\"oa nf ev oz b hj pg pe l hz pf\" data-selectable-paragraph=\"\">\t<strong class=\"oz ew\">return<\/strong> -mean_sum_score<\/span><\/pre>\n<h2 id=\"c541\" class=\"oa nf ev be ng ob oc od nj oe of og nm mr oh oi oj mv ok ol om mz on oo op oq bj\" data-selectable-paragraph=\"\">Hinge Loss<\/h2>\n<p id=\"9c6b\" class=\"pw-post-body-paragraph mj mk ev be b ft or mm mn fw os mp mq mr ot mt mu mv ou mx my mz ov nb nc nd eo bj\" data-selectable-paragraph=\"\">The hinge loss is an alternative to the cross-entropy loss function. It was initially developed for the performance evaluation of the support vector machine (SVM) algorithm to calculate the maximum margin from the hyperplane to the labelled class.<\/p>\n<p id=\"d6e5\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\">It is used with binary classification where the target values fall in the range of <strong class=\"be ow\">-1<\/strong> to <strong class=\"be ow\">1<\/strong>. This loss function penalizes the wrong predictions by assigning more errors when there is more difference between the actual and predicted value class.<\/p>\n<p id=\"a57c\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\">Here\u2019s the formula for the hinge loss function:<\/p>\n<figure class=\"lr ls lt lu lv lw lo lp paragraph-image\">\n<div class=\"lx ly hb lz bg ma\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg mb mc c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*yC2qfgfXby3ZFTDXcHeCLg.png\" alt=\"\" width=\"700\" height=\"109\"><\/figure><div class=\"lo lp qi\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/format:webp\/1*yC2qfgfXby3ZFTDXcHeCLg.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/format:webp\/1*yC2qfgfXby3ZFTDXcHeCLg.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/format:webp\/1*yC2qfgfXby3ZFTDXcHeCLg.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/format:webp\/1*yC2qfgfXby3ZFTDXcHeCLg.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/format:webp\/1*yC2qfgfXby3ZFTDXcHeCLg.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/format:webp\/1*yC2qfgfXby3ZFTDXcHeCLg.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/format:webp\/1*yC2qfgfXby3ZFTDXcHeCLg.png 1400w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/1*yC2qfgfXby3ZFTDXcHeCLg.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/1*yC2qfgfXby3ZFTDXcHeCLg.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/1*yC2qfgfXby3ZFTDXcHeCLg.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/1*yC2qfgfXby3ZFTDXcHeCLg.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/1*yC2qfgfXby3ZFTDXcHeCLg.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/1*yC2qfgfXby3ZFTDXcHeCLg.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/1*yC2qfgfXby3ZFTDXcHeCLg.png 1400w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\" data-testid=\"og\"><\/picture><\/div>\n<\/div>\n<\/figure>\n<p id=\"f561\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\">Where <strong class=\"be ow\">Sj<\/strong> is the actual value and <strong class=\"be ow\">Syi<\/strong> is the predicted value.<\/p>\n<p id=\"e35c\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\"><strong class=\"be ow\">Example<\/strong>:<\/p>\n<pre class=\"lr ls lt lu lv oy oz pa pb ax pc bj\"><span id=\"a75d\" class=\"oa nf ev oz b hj pd pe l hz pf\" data-selectable-paragraph=\"\"><strong class=\"oz ew\">import<\/strong> numpy <strong class=\"oz ew\">as<\/strong> np<\/span><span id=\"5a42\" class=\"oa nf ev oz b hj pg pe l hz pf\" data-selectable-paragraph=\"\">def <strong class=\"oz ew\">hinge_loss<\/strong>(y, y_pred):\n    l = 0\n    size = np.size(y)\n    for i in range(size):\n        l = l + max(0, 1 - y[i] * y_pred[i])<\/span><span id=\"920a\" class=\"oa nf ev oz b hj pg pe l hz pf\" data-selectable-paragraph=\"\">    <strong class=\"oz ew\">return<\/strong> l \/ size<\/span><\/pre>\n<h1 id=\"bfbf\" class=\"ne nf ev be ng nh ni fv nj nk nl fy nm nn no np nq nr ns nt nu nv nw nx ny nz bj\" data-selectable-paragraph=\"\">Conclusion<\/h1>\n<p id=\"d0cb\" class=\"pw-post-body-paragraph mj mk ev be b ft or mm mn fw os mp mq mr ot mt mu mv ou mx my mz ov nb nc nd eo bj\" data-selectable-paragraph=\"\">That\u2019s all from this article. In this article, we learned about various loss functions in machine learning that are used with regression and classification problems. To achieve better outcomes, it is important to choose the loss function that is the best fitting for our data.<\/p>\n<p id=\"0dbe\" class=\"pw-post-body-paragraph mj mk ev be b ft ml mm mn fw mo mp mq mr ms mt mu mv mw mx my mz na nb nc nd eo bj\" data-selectable-paragraph=\"\">Thanks for reading!<\/p>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Photo by Alexandre Debi\u00e8ve on Unsplash In machine learning, a loss function is used to measure the loss, or cost, of a specific machine learning model. These loss functions calculate the amount of error in a specific machine learning model using some mathematical formula and measure the performance of that specific model. There are various [&hellip;]<\/p>\n","protected":false},"author":88,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"customer_name":"","customer_description":"","customer_industry":"","customer_technologies":"","customer_logo":"","footnotes":""},"categories":[6,7],"tags":[],"coauthors":[185],"class_list":["post-7397","post","type-post","status-publish","format-standard","hentry","category-machine-learning","category-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.9 (Yoast SEO v25.9) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Guide to Loss Functions for Machine Learning Models - Comet<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.comet.com\/site\/blog\/guide-to-loss-functions-for-machine-learning-models\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Guide to Loss Functions for Machine Learning Models\" \/>\n<meta property=\"og:description\" content=\"Photo by Alexandre Debi\u00e8ve on Unsplash In machine learning, a loss function is used to measure the loss, or cost, of a specific machine learning model. These loss functions calculate the amount of error in a specific machine learning model using some mathematical formula and measure the performance of that specific model. There are various [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.comet.com\/site\/blog\/guide-to-loss-functions-for-machine-learning-models\/\" \/>\n<meta property=\"og:site_name\" content=\"Comet\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/cometdotml\" \/>\n<meta property=\"article:published_time\" content=\"2023-09-07T18:26:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-04-24T17:14:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/0*ag5QD6HncPkXdd0P\" \/>\n<meta name=\"author\" content=\"Pralabh Saxena\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Cometml\" \/>\n<meta name=\"twitter:site\" content=\"@Cometml\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Pralabh Saxena\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Guide to Loss Functions for Machine Learning Models - Comet","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.comet.com\/site\/blog\/guide-to-loss-functions-for-machine-learning-models\/","og_locale":"en_US","og_type":"article","og_title":"Guide to Loss Functions for Machine Learning Models","og_description":"Photo by Alexandre Debi\u00e8ve on Unsplash In machine learning, a loss function is used to measure the loss, or cost, of a specific machine learning model. These loss functions calculate the amount of error in a specific machine learning model using some mathematical formula and measure the performance of that specific model. There are various [&hellip;]","og_url":"https:\/\/www.comet.com\/site\/blog\/guide-to-loss-functions-for-machine-learning-models\/","og_site_name":"Comet","article_publisher":"https:\/\/www.facebook.com\/cometdotml","article_published_time":"2023-09-07T18:26:15+00:00","article_modified_time":"2025-04-24T17:14:20+00:00","og_image":[{"url":"https:\/\/miro.medium.com\/v2\/resize:fit:700\/0*ag5QD6HncPkXdd0P","type":"","width":"","height":""}],"author":"Pralabh Saxena","twitter_card":"summary_large_image","twitter_creator":"@Cometml","twitter_site":"@Cometml","twitter_misc":{"Written by":"Pralabh Saxena","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.comet.com\/site\/blog\/guide-to-loss-functions-for-machine-learning-models\/#article","isPartOf":{"@id":"https:\/\/www.comet.com\/site\/blog\/guide-to-loss-functions-for-machine-learning-models\/"},"author":{"name":"Pralabh Saxena","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/661df331deec9788343ef011c9467cc8"},"headline":"Guide to Loss Functions for Machine Learning Models","datePublished":"2023-09-07T18:26:15+00:00","dateModified":"2025-04-24T17:14:20+00:00","mainEntityOfPage":{"@id":"https:\/\/www.comet.com\/site\/blog\/guide-to-loss-functions-for-machine-learning-models\/"},"wordCount":969,"publisher":{"@id":"https:\/\/www.comet.com\/site\/#organization"},"image":{"@id":"https:\/\/www.comet.com\/site\/blog\/guide-to-loss-functions-for-machine-learning-models\/#primaryimage"},"thumbnailUrl":"https:\/\/miro.medium.com\/v2\/resize:fit:700\/0*ag5QD6HncPkXdd0P","articleSection":["Machine Learning","Tutorials"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.comet.com\/site\/blog\/guide-to-loss-functions-for-machine-learning-models\/","url":"https:\/\/www.comet.com\/site\/blog\/guide-to-loss-functions-for-machine-learning-models\/","name":"Guide to Loss Functions for Machine Learning Models - Comet","isPartOf":{"@id":"https:\/\/www.comet.com\/site\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.comet.com\/site\/blog\/guide-to-loss-functions-for-machine-learning-models\/#primaryimage"},"image":{"@id":"https:\/\/www.comet.com\/site\/blog\/guide-to-loss-functions-for-machine-learning-models\/#primaryimage"},"thumbnailUrl":"https:\/\/miro.medium.com\/v2\/resize:fit:700\/0*ag5QD6HncPkXdd0P","datePublished":"2023-09-07T18:26:15+00:00","dateModified":"2025-04-24T17:14:20+00:00","breadcrumb":{"@id":"https:\/\/www.comet.com\/site\/blog\/guide-to-loss-functions-for-machine-learning-models\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.comet.com\/site\/blog\/guide-to-loss-functions-for-machine-learning-models\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/blog\/guide-to-loss-functions-for-machine-learning-models\/#primaryimage","url":"https:\/\/miro.medium.com\/v2\/resize:fit:700\/0*ag5QD6HncPkXdd0P","contentUrl":"https:\/\/miro.medium.com\/v2\/resize:fit:700\/0*ag5QD6HncPkXdd0P"},{"@type":"BreadcrumbList","@id":"https:\/\/www.comet.com\/site\/blog\/guide-to-loss-functions-for-machine-learning-models\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.comet.com\/site\/"},{"@type":"ListItem","position":2,"name":"Guide to Loss Functions for Machine Learning Models"}]},{"@type":"WebSite","@id":"https:\/\/www.comet.com\/site\/#website","url":"https:\/\/www.comet.com\/site\/","name":"Comet","description":"Build Better Models Faster","publisher":{"@id":"https:\/\/www.comet.com\/site\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.comet.com\/site\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.comet.com\/site\/#organization","name":"Comet ML, Inc.","alternateName":"Comet","url":"https:\/\/www.comet.com\/site\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/#\/schema\/logo\/image\/","url":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2025\/01\/logo_comet_square.png","contentUrl":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2025\/01\/logo_comet_square.png","width":310,"height":310,"caption":"Comet ML, Inc."},"image":{"@id":"https:\/\/www.comet.com\/site\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/cometdotml","https:\/\/x.com\/Cometml","https:\/\/www.youtube.com\/channel\/UCmN63HKvfXSCS-UwVwmK8Hw"]},{"@type":"Person","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/661df331deec9788343ef011c9467cc8","name":"Pralabh Saxena","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/image\/af2f89cb395a3afe9b42605f70d9c6a7","url":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2023\/09\/1689749938719-96x96.jpg","contentUrl":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2023\/09\/1689749938719-96x96.jpg","caption":"Pralabh Saxena"},"url":"https:\/\/www.comet.com\/site\/blog\/author\/pralabh-saxena2014gmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/7397","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/users\/88"}],"replies":[{"embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/comments?post=7397"}],"version-history":[{"count":1,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/7397\/revisions"}],"predecessor-version":[{"id":15556,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/7397\/revisions\/15556"}],"wp:attachment":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/media?parent=7397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/categories?post=7397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/tags?post=7397"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/coauthors?post=7397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}