{"id":181,"date":"2024-06-09T15:52:55","date_gmt":"2024-06-09T15:52:55","guid":{"rendered":"https:\/\/skharlanov.com\/?p=181"},"modified":"2024-06-09T15:59:26","modified_gmt":"2024-06-09T15:59:26","slug":"rmse-loss-metric-for-regression","status":"publish","type":"post","link":"https:\/\/skharlanov.com\/index.php\/2024\/06\/09\/rmse-loss-metric-for-regression\/","title":{"rendered":"RMSE \/ L2 normalization"},"content":{"rendered":"\n<p>This is loss metric for machine learning models.<br>Has two names:<\/p>\n\n\n\n<ul>\n<li>Root mean squared error<\/li>\n\n\n\n<li>L2 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 RMSE<\/h2>\n\n\n\n<p>To compute RMSE we need to<\/p>\n\n\n\n<ul>\n<li>calculate difference between predictions and real values.<\/li>\n\n\n\n<li>square it.<\/li>\n\n\n\n<li>compute the average.<\/li>\n\n\n\n<li>take the square root of the resulting average.<\/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\"><table><thead><tr><th>Actual<\/th><th>Predicted<\/th><th>Error<\/th><th>Powered error<\/th><\/tr><\/thead><tbody><tr><td>10<\/td><td>20<\/td><td>-10<\/td><td>100<\/td><\/tr><tr><td>3<\/td><td>8<\/td><td>-5<\/td><td>25<\/td><\/tr><tr><td>6<\/td><td>1<\/td><td>5<\/td><td>25<\/td><\/tr><\/tbody><\/table><\/figure>\n<\/div>\n\n\n\n<p>Total powered error: 150<br>Mean powered error: 50<br>Root mean squared error: 7.07<\/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\">Python code to compute RMSE &#8211; SciKit<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">from sklearn.metrics import mean_squared_error\nrmse = np.sqrt(mean_squared_error(targets, predictions))\nprint('RMSE: %f' % (rmse))<\/code><\/pre>\n\n\n\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-4 wp-block-group-is-layout-flex\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Python code to compute RMSE &#8211; PyTorch<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">F.mse_loss(predictions, targets).sqrt()<\/code><\/pre>\n\n\n\n<p>Actually <code>PyTorch<\/code> computes <code>mse<\/code> and we should add square root by ourself.<\/p>\n\n\n\n<p>Module <code>torch.nn.functional<\/code>, which the PyTorch 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-5 wp-block-group-is-layout-flex\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Python code to compute RMSE from scratch<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">import math\n\ndef rmse(predictions, targets):\n    # Calculate the squared differences\n    squared_differences = [(p - t) ** 2 for p, t in zip(predictions, targets)]\n\n    # Calculate the mean of the squared differences\n    mean_squared_error = sum(squared_differences) \/ len(squared_differences)\n\n    # Return the square root of the mean squared error\n    return math.sqrt(mean_squared_error)\n\n# Example usage\npredictions = [3.0, 5.0, 2.5, 6.0]\ntargets = [3.5, 4.5, 2.0, 5.0]\nresult = rmse(predictions, targets)\nprint(\"RMSE:\", 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\">Math formula<\/h2>\n\n\n\n\n\n\n\n<p>$$\\text{RMSE} = \\sqrt{\\frac{1}{n} \\sum_{i=1}^{n} (y_i &#8211; \\hat{y}_i)^2}$$<\/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\">Specifics<\/h2>\n\n\n\n<p>Because all differences are squared, function strongly punish standalone big differences.<\/p>\n\n\n\n<p>In order to apply a softer penalty, one can use <a href=\"https:\/\/skharlanov.com\/index.php\/2024\/06\/09\/mae-l1-normalization\/\" title=\"MAE - L1 normalization\">MAE \/ L1 normalization<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is loss metric for machine learning models.Has two names: Computing RMSE To compute RMSE we need to Actual Predicted Error Powered error 10 20 -10 100 3 8 -5 25 6 1 5 25 Total powered error: 150Mean powered error: 50Root mean squared error: 7.07 Python code to compute RMSE &#8211; SciKit Python code [&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\/181"}],"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=181"}],"version-history":[{"count":4,"href":"https:\/\/skharlanov.com\/index.php\/wp-json\/wp\/v2\/posts\/181\/revisions"}],"predecessor-version":[{"id":188,"href":"https:\/\/skharlanov.com\/index.php\/wp-json\/wp\/v2\/posts\/181\/revisions\/188"}],"wp:attachment":[{"href":"https:\/\/skharlanov.com\/index.php\/wp-json\/wp\/v2\/media?parent=181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/skharlanov.com\/index.php\/wp-json\/wp\/v2\/categories?post=181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/skharlanov.com\/index.php\/wp-json\/wp\/v2\/tags?post=181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}