{"id":68,"date":"2024-06-09T12:49:20","date_gmt":"2024-06-09T12:49:20","guid":{"rendered":"https:\/\/skharlanov.com\/?p=68"},"modified":"2024-06-09T15:55:25","modified_gmt":"2024-06-09T15:55:25","slug":"mae-l1-normalization","status":"publish","type":"post","link":"https:\/\/skharlanov.com\/index.php\/2024\/06\/09\/mae-l1-normalization\/","title":{"rendered":"MAE \/ L1 normalization"},"content":{"rendered":"\n<p>This is loss metric for machine learning models.<br>It has different names: <\/p>\n\n\n\n<ul>\n<li>Mean absolute error<\/li>\n\n\n\n<li>Mean absolute difference<\/li>\n\n\n\n<li>L1 normalization (math jargon)<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-1 wp-block-group-is-layout-flex\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Computing MAE<\/h2>\n\n\n\n<p>To compute MAE we need to:<\/p>\n\n\n\n<ul>\n<li>calculate absolute difference between predictions and real values.<\/li>\n\n\n\n<li>compute average of it.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-group has-global-padding is-content-justification-left is-layout-constrained wp-container-core-group-is-layout-2 wp-block-group-is-layout-constrained\">\n<figure class=\"wp-block-table is-style-regular\"><table><thead><tr><th>Actual<\/th><th>Predicted<\/th><th>Error<\/th><\/tr><\/thead><tbody><tr><td>10<\/td><td>20<\/td><td>-10<\/td><\/tr><tr><td>23<\/td><td>13<\/td><td>10<\/td><\/tr><tr><td>5<\/td><td>6<\/td><td>-1<\/td><\/tr><\/tbody><\/table><\/figure>\n<\/div>\n\n\n\n<p>Total absolute error: 21<br>Mean absolute error: 7<\/p>\n\n\n\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-3 wp-block-group-is-layout-flex\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Why absolute values?<\/h2>\n\n\n\n<p>If we compute non absolute difference, then negative errors can cancel out positive errors:<\/p>\n\n\n\n<div class=\"wp-block-group has-global-padding is-content-justification-left is-layout-constrained wp-container-core-group-is-layout-4 wp-block-group-is-layout-constrained\">\n<figure class=\"wp-block-table\"><table><thead><tr><th>Actual<\/th><th>Predicted<\/th><th>Error<\/th><\/tr><\/thead><tbody><tr><td>10<\/td><td>20<\/td><td>-10<\/td><\/tr><tr><td>23<\/td><td>13<\/td><td>10<\/td><\/tr><\/tbody><\/table><\/figure>\n<\/div>\n\n\n\n<p>Total error: 0<br>Mean error: 0<\/p>\n\n\n\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-5 wp-block-group-is-layout-flex\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading is-style-default\">Python code to compute MAE from scratch<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">def mae(predictions, targets):\n    # Calculate the absolute differences\n    absolute_differences = [abs(p - t) for p, t in zip(predictions, targets)]\n\n    # Calculate the mean of the absolute differences\n    mean_absolute_error = sum(absolute_differences) \/ len(absolute_differences)\n\n    return mean_absolute_error\n\n# Example usage\npredictions = [3.0, 5.0, 2.5, 6.0]\ntargets = [3.5, 4.5, 2.0, 5.0]\nresult = mae(predictions, targets)\nprint(\"MAE:\", result)<\/code><\/pre>\n\n\n\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-6 wp-block-group-is-layout-flex\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Python code to compute MAE with PyTorch<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">F.l1_loss(predictions, targets)<\/code><\/pre>\n\n\n\n<p>&nbsp;Module<code> torch.nn.functional<\/code>, which the <code>PyTorch <\/code>team recommends importing as&nbsp;<code>F<\/code><\/p>\n\n\n\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-7 wp-block-group-is-layout-flex\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Math formula<\/h2>\n\n\n\n\n\n\n\n<p class=\"has-text-align-left\">$$\\text{MAE} = \\frac{1}{n} \\sum_{i=1}^{n} |y_i &#8211; \\hat{y}_i|$$<\/p>\n\n\n\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-8 wp-block-group-is-layout-flex\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Specifics<\/h2>\n\n\n\n<p>Doesn&#8217;t have such strong punishment for big differences as <a href=\"https:\/\/skharlanov.com\/index.php\/2024\/06\/09\/rmse-loss-metric-for-regression\/\" title=\"RMSE \u2013 loss metric for regression\">RMSE \/ L2 normalization<\/a><br>MAE is used less frequently than <a href=\"https:\/\/skharlanov.com\/index.php\/2024\/06\/09\/rmse-loss-metric-for-regression\/\" title=\"RMSE \u2013 loss metric for regression\">RMSE \/ L2 normalization<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is loss metric for machine learning models.It has different names: Computing MAE To compute MAE we need to: Actual Predicted Error 10 20 -10 23 13 10 5 6 -1 Total absolute error: 21Mean absolute error: 7 Why absolute values? If we compute non absolute difference, then negative errors can cancel out positive errors: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[12],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/skharlanov.com\/index.php\/wp-json\/wp\/v2\/posts\/68"}],"collection":[{"href":"https:\/\/skharlanov.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/skharlanov.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/skharlanov.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/skharlanov.com\/index.php\/wp-json\/wp\/v2\/comments?post=68"}],"version-history":[{"count":59,"href":"https:\/\/skharlanov.com\/index.php\/wp-json\/wp\/v2\/posts\/68\/revisions"}],"predecessor-version":[{"id":186,"href":"https:\/\/skharlanov.com\/index.php\/wp-json\/wp\/v2\/posts\/68\/revisions\/186"}],"wp:attachment":[{"href":"https:\/\/skharlanov.com\/index.php\/wp-json\/wp\/v2\/media?parent=68"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/skharlanov.com\/index.php\/wp-json\/wp\/v2\/categories?post=68"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/skharlanov.com\/index.php\/wp-json\/wp\/v2\/tags?post=68"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}