Difference between revisions of "Whitening and standardization"
(Created blank page) |
|||
| Line 1: | Line 1: | ||
<!DOCTYPE html> | |||
<html lang="en-US"> | |||
<head> | |||
<meta charset="UTF-8" /> | |||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | |||
<link rel="pingback" href="https://machinelearningmastery.com/xmlrpc.php" /> | |||
<meta name='robots' content='index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1' /> | |||
<!-- Mobile viewport scale --> | |||
<meta content="initial-scale=1.0, maximum-scale=1.0, user-scalable=yes" name="viewport"/> | |||
<!-- This site is optimized with the Yoast SEO plugin v17.1 - https://yoast.com/wordpress/plugins/seo/ --> | |||
<title>How to use Data Scaling Improve Deep Learning Model Stability and Performance</title><link rel="stylesheet" href="https://machinelearningmastery.com/wp-content/cache/min/1/7ab4e5ffb1c7994e88de46542752e00b.css" media="all" data-minify="1" /> | |||
<link rel="canonical" href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/" /> | |||
<meta property="og:locale" content="en_US" /> | |||
<meta property="og:type" content="article" /> | |||
<meta property="og:title" content="How to use Data Scaling Improve Deep Learning Model Stability and Performance" /> | |||
<meta property="og:description" content="Deep learning neural networks learn how to map inputs to outputs from examples in a training dataset. The weights of […]" /> | |||
<meta property="og:url" content="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/" /> | |||
<meta property="og:site_name" content="Machine Learning Mastery" /> | |||
<meta property="article:publisher" content="https://www.facebook.com/MachineLearningMastery/" /> | |||
<meta property="article:author" content="https://www.facebook.com/MachineLearningMastery/" /> | |||
<meta property="article:published_time" content="2019-02-03T18:00:34+00:00" /> | |||
<meta property="article:modified_time" content="2020-08-25T00:18:53+00:00" /> | |||
<meta property="og:image" content="https://machinelearningmastery.com/wp-content/uploads/2018/11/Box-and-Whisker-Plots-of-Mean-Squared-Error-With-Unscaled-Normalized-and-Standardized-Input-Variables-for-the-Regression-Problem.png" /> | |||
<meta property="og:image:width" content="1280" /> | |||
<meta property="og:image:height" content="960" /> | |||
<meta name="twitter:label1" content="Written by" /> | |||
<meta name="twitter:data1" content="Jason Brownlee" /> | |||
<meta name="twitter:label2" content="Est. reading time" /> | |||
<meta name="twitter:data2" content="26 minutes" /> | |||
<script type="application/ld+json" class="yoast-schema-graph">{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://machinelearningmastery.com/#organization","name":"Machine Learning Mastery","url":"https://machinelearningmastery.com/","sameAs":["https://www.facebook.com/MachineLearningMastery/","https://www.linkedin.com/company/machine-learning-mastery","https://twitter.com/TeachTheMachine"],"logo":{"@type":"ImageObject","@id":"https://machinelearningmastery.com/#logo","inLanguage":"en-US","url":"https://machinelearningmastery.com/wp-content/uploads/2016/09/cropped-icon.png","contentUrl":"https://machinelearningmastery.com/wp-content/uploads/2016/09/cropped-icon.png","width":512,"height":512,"caption":"Machine Learning Mastery"},"image":{"@id":"https://machinelearningmastery.com/#logo"}},{"@type":"WebSite","@id":"https://machinelearningmastery.com/#website","url":"https://machinelearningmastery.com/","name":"Machine Learning Mastery","description":"Making developers awesome at machine learning","publisher":{"@id":"https://machinelearningmastery.com/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://machinelearningmastery.com/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#primaryimage","inLanguage":"en-US","url":"https://machinelearningmastery.com/wp-content/uploads/2018/11/Box-and-Whisker-Plots-of-Mean-Squared-Error-With-Unscaled-Normalized-and-Standardized-Input-Variables-for-the-Regression-Problem.png","contentUrl":"https://machinelearningmastery.com/wp-content/uploads/2018/11/Box-and-Whisker-Plots-of-Mean-Squared-Error-With-Unscaled-Normalized-and-Standardized-Input-Variables-for-the-Regression-Problem.png","width":1280,"height":960,"caption":"Box and Whisker Plots of Mean Squared Error With Unscaled, Normalized and Standardized Input Variables for the Regression Problem"},{"@type":"WebPage","@id":"https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#webpage","url":"https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/","name":"How to use Data Scaling Improve Deep Learning Model Stability and Performance","isPartOf":{"@id":"https://machinelearningmastery.com/#website"},"primaryImageOfPage":{"@id":"https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#primaryimage"},"datePublished":"2019-02-03T18:00:34+00:00","dateModified":"2020-08-25T00:18:53+00:00","breadcrumb":{"@id":"https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/"]}]},{"@type":"BreadcrumbList","@id":"https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://machinelearningmastery.com/"},{"@type":"ListItem","position":2,"name":"Blog","item":"https://machinelearningmastery.com/blog/"},{"@type":"ListItem","position":3,"name":"How to use Data Scaling Improve Deep Learning Model Stability and Performance"}]},{"@type":"Article","@id":"https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#article","isPartOf":{"@id":"https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#webpage"},"author":{"@id":"https://machinelearningmastery.com/#/schema/person/e2d0ff4828d406a3b47e5a3c9a0591e8"},"headline":"How to use Data Scaling Improve Deep Learning Model Stability and Performance","datePublished":"2019-02-03T18:00:34+00:00","dateModified":"2020-08-25T00:18:53+00:00","mainEntityOfPage":{"@id":"https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#webpage"},"wordCount":3566,"commentCount":125,"publisher":{"@id":"https://machinelearningmastery.com/#organization"},"image":{"@id":"https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#primaryimage"},"thumbnailUrl":"https://machinelearningmastery.com/wp-content/uploads/2018/11/Box-and-Whisker-Plots-of-Mean-Squared-Error-With-Unscaled-Normalized-and-Standardized-Input-Variables-for-the-Regression-Problem.png","articleSection":["Deep Learning Performance"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#respond"]}]},{"@type":"Person","@id":"https://machinelearningmastery.com/#/schema/person/e2d0ff4828d406a3b47e5a3c9a0591e8","name":"Jason Brownlee","image":{"@type":"ImageObject","@id":"https://machinelearningmastery.com/#personlogo","inLanguage":"en-US","url":"https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=96&d=mm&r=g","contentUrl":"https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=96&d=mm&r=g","caption":"Jason Brownlee"},"description":"Jason Brownlee, PhD is a machine learning specialist who teaches developers how to get results with modern machine learning methods via hands-on tutorials.","sameAs":["http://MachineLearningMastery.com","https://www.facebook.com/MachineLearningMastery/","https://www.linkedin.com/company/machine-learning-mastery","https://twitter.com/teachthemachine"]}]}</script> | |||
<!-- / Yoast SEO plugin. --> | |||
<link rel='dns-prefetch' href='//cdn.jsdelivr.net' /> | |||
<link rel='dns-prefetch' href='//ads.adthrive.com' /> | |||
<link rel='dns-prefetch' href='//www.google-analytics.com' /> | |||
<link rel='dns-prefetch' href='//loadeu.exelator.com' /> | |||
<link rel='dns-prefetch' href='//sync.crwdcntrl.net' /> | |||
<link rel='dns-prefetch' href='//gdpr-wrapper.privacymanager.io' /> | |||
<link rel='dns-prefetch' href='//securepubads.g.doubleclick.net' /> | |||
<link rel='dns-prefetch' href='//gdpr.privacymanager.io' /> | |||
<link rel='dns-prefetch' href='//sb.scorecardresearch.com' /> | |||
<link rel='dns-prefetch' href='//confiant-integrations.global.ssl.fastly.net' /> | |||
<link rel="alternate" type="application/rss+xml" title="Machine Learning Mastery » Feed" href="https://feeds.feedburner.com/MachineLearningMastery" /> | |||
<link rel="alternate" type="application/rss+xml" title="Machine Learning Mastery » Comments Feed" href="https://machinelearningmastery.com/comments/feed/" /> | |||
<link rel="alternate" type="application/rss+xml" title="Machine Learning Mastery » How to use Data Scaling Improve Deep Learning Model Stability and Performance Comments Feed" href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/feed/" /> | |||
<style type="text/css"> | |||
img.wp-smiley, | |||
img.emoji { | |||
display: inline !important; | |||
border: none !important; | |||
box-shadow: none !important; | |||
height: 1em !important; | |||
width: 1em !important; | |||
margin: 0 .07em !important; | |||
vertical-align: -0.1em !important; | |||
background: none !important; | |||
padding: 0 !important; | |||
} | |||
</style> | |||
<!--[if lt IE 9]> | |||
<link href="https://machinelearningmastery.com/wp-content/themes/canvas-new/css/non-responsive.css" rel="stylesheet" type="text/css" /> | |||
<style type="text/css">.col-full, #wrapper { width: 960px; max-width: 960px; } #inner-wrapper { padding: 0; } body.full-width #header, #nav-container, body.full-width #content, body.full-width #footer-widgets, body.full-width #footer { padding-left: 0; padding-right: 0; } body.fixed-mobile #top, body.fixed-mobile #header-container, body.fixed-mobile #footer-container, body.fixed-mobile #nav-container, body.fixed-mobile #footer-widgets-container { min-width: 960px; padding: 0 1em; } body.full-width #content { width: auto; padding: 0 1em;}</style> | |||
<![endif]--> | |||
<script type="f3886dae12b0536ad361ea93-text/javascript" src='https://machinelearningmastery.com/wp-includes/js/jquery/jquery.min.js?ver=3.6.0' id='jquery-core-js' defer></script> | |||
<script type="f3886dae12b0536ad361ea93-text/javascript" id='ssb-front-js-js-extra'> | |||
/* <![CDATA[ */ | |||
var SSB = {"ajax_url":"https:\/\/machinelearningmastery.com\/wp-admin\/admin-ajax.php","fb_share_nonce":"0f0d514a34"}; | |||
/* ]]> */ | |||
</script> | |||
<link rel="https://api.w.org/" href="https://machinelearningmastery.com/wp-json/" /><link rel="alternate" type="application/json" href="https://machinelearningmastery.com/wp-json/wp/v2/posts/6939" /><link rel="EditURI" type="application/rsd+xml" title="RSD" href="https://machinelearningmastery.com/xmlrpc.php?rsd" /> | |||
<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="https://machinelearningmastery.com/wp-includes/wlwmanifest.xml" /> | |||
<link rel='shortlink' href='https://machinelearningmastery.com/?p=6939' /> | |||
<style type="text/css"> | |||
.mpcs-classroom .nav-back i, | |||
.mpcs-classroom .navbar-section a.btn, | |||
.mpcs-classroom .navbar-section a, | |||
.mpcs-classroom .navbar-section button { | |||
color: rgba(255, 255, 255) !important; | |||
} | |||
.mpcs-classroom .navbar-section .dropdown .menu a { | |||
color: rgba(44, 54, 55) !important; | |||
} | |||
.mpcs-classroom .mpcs-progress-ring { | |||
background-color: rgba(29, 166, 154) !important; | |||
} | |||
.mpcs-classroom .mpcs-course-filter .dropdown .btn span, | |||
.mpcs-classroom .mpcs-course-filter .dropdown .btn i, | |||
.mpcs-classroom .mpcs-course-filter .input-group .input-group-btn, | |||
.mpcs-classroom .mpcs-course-filter .input-group .mpcs-search, | |||
.mpcs-classroom .mpcs-course-filter .input-group input[type=text], | |||
.mpcs-classroom .mpcs-course-filter .dropdown a, | |||
.mpcs-classroom .pagination, | |||
.mpcs-classroom .pagination i, | |||
.mpcs-classroom .pagination a { | |||
color: rgba(44, 54, 55) !important; | |||
border-color: rgba(44, 54, 55) !important; | |||
} | |||
/* body.mpcs-classroom a{ | |||
color: rgba(); | |||
} */ | |||
#mpcs-navbar, | |||
#mpcs-navbar button#previous_lesson_link, | |||
#mpcs-navbar button#previous_lesson_link:hover { | |||
background: rgba(44, 54, 55); | |||
} | |||
.course-progress .user-progress, | |||
.btn-green, | |||
#mpcs-navbar button:not(#previous_lesson_link){ | |||
background: rgba(29, 166, 154, 0.9); | |||
} | |||
.btn-green:hover, | |||
#mpcs-navbar button:not(#previous_lesson_link):focus, | |||
#mpcs-navbar button:not(#previous_lesson_link):hover{ | |||
background: rgba(29, 166, 154); | |||
} | |||
.btn-green{border: rgba(29, 166, 154)} | |||
.course-progress .progress-text, | |||
.mpcs-lesson i.mpcs-circle-regular { | |||
color: rgba(29, 166, 154) | |||
} | |||
#mpcs-main #bookmark, .mpcs-lesson.current{background: rgba(29, 166, 154, 0.3)} | |||
.mpcs-instructor .tile-subtitle{ | |||
color: rgba(29, 166, 154, 1) | |||
} | |||
</style> | |||
<style media="screen"> | |||
.simplesocialbuttons.simplesocialbuttons_inline .ssb-fb-like { | |||
margin: ; | |||
} | |||
/*inline margin*/ | |||
.simplesocialbuttons.simplesocialbuttons_inline.simplesocial-simple-icons button{ | |||
margin: ; | |||
} | |||
/*margin-digbar*/ | |||
</style> | |||
<!-- Open Graph Meta Tags generated by Simple Social Buttons 3.2.3 --> | |||
<meta property="og:title" content="How to use Data Scaling Improve Deep Learning Model Stability and Performance - Machine Learning Mastery" /> | |||
<meta property="og:description" content="Deep learning neural networks learn how to map inputs to outputs from examples in a training dataset. | |||
The weights of the model are initialized to small random values and updated via an optimization algorithm in response to estimates of error on the training dataset. | |||
Given the use of small weights in the model and the use of error between predictions and expected" /> | |||
<meta property="og:url" content="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/" /> | |||
<meta property="og:site_name" content="Machine Learning Mastery" /> | |||
<meta property="og:image" content="https://machinelearningmastery.com/wp-content/uploads/2018/11/Box-and-Whisker-Plots-of-Mean-Squared-Error-With-Unscaled-Normalized-and-Standardized-Input-Variables-for-the-Regression-Problem.png" /> | |||
<meta name="twitter:card" content="summary_large_image" /> | |||
<meta name="twitter:description" content="Deep learning neural networks learn how to map inputs to outputs from examples in a training dataset. | |||
The weights of the model are initialized to small random values and updated via an optimization algorithm in response to estimates of error on the training dataset. | |||
Given the use of small weights in the model and the use of error between predictions and expected" /> | |||
<meta name="twitter:title" content="How to use Data Scaling Improve Deep Learning Model Stability and Performance - Machine Learning Mastery" /> | |||
<meta property="twitter:image" content="https://machinelearningmastery.com/wp-content/uploads/2018/11/Box-and-Whisker-Plots-of-Mean-Squared-Error-With-Unscaled-Normalized-and-Standardized-Input-Variables-for-the-Regression-Problem.png" /> | |||
<style id="wplmi-inline-css" type="text/css"> span.wplmi-user-avatar { width: 16px;display: inline-block !important;flex-shrink: 0; } img.wplmi-elementor-avatar { border-radius: 100%;margin-right: 3px; } | |||
</style> | |||
<link rel="preload" as="font" href="https://machinelearningmastery.com/wp-content/themes/canvas-new/includes/fonts/fontawesome-webfont.woff2?v=4.5.0" crossorigin> | |||
<!-- Custom CSS Styling --> | |||
<style type="text/css"> | |||
#logo .site-title, #logo .site-description { display:none; } | |||
body {background-repeat:no-repeat;background-position:top left;background-attachment:scroll;border-top:0px solid #000000;} | |||
#header {background-repeat:no-repeat;background-position:left top;margin-top:0px;margin-bottom:0px;padding-top:10px;padding-bottom:10px;border:0px solid ;} | |||
#logo .site-title a {font:bold 40px/1em "Helvetica Neue", Helvetica, sans-serif;color:#222222;} | |||
#logo .site-description {font:normal 13px/1em "Helvetica Neue", Helvetica, sans-serif;color:#999999;} | |||
body, p { font:normal 14px/1.5em "Helvetica Neue", Helvetica, sans-serif;color:#555555; } | |||
h1 { font:bold 28px/1.2em "Helvetica Neue", Helvetica, sans-serif;color:#222222; }h2 { font:bold 24px/1.2em "Helvetica Neue", Helvetica, sans-serif;color:#222222; }h3 { font:bold 20px/1.2em "Helvetica Neue", Helvetica, sans-serif;color:#222222; }h4 { font:bold 16px/1.2em "Helvetica Neue", Helvetica, sans-serif;color:#222222; }h5 { font:bold 14px/1.2em "Helvetica Neue", Helvetica, sans-serif;color:#222222; }h6 { font:bold 12px/1.2em "Helvetica Neue", Helvetica, sans-serif;color:#222222; } | |||
.page-title, .post .title, .page .title {font:bold 28px/1.1em "Helvetica Neue", Helvetica, sans-serif;color:#222222;} | |||
.post .title a:link, .post .title a:visited, .page .title a:link, .page .title a:visited {color:#222222} | |||
.post-meta { font:normal 12px/1.5em "Helvetica Neue", Helvetica, sans-serif;color:#999999; } | |||
.entry, .entry p{ font:normal 15px/1.5em "Helvetica Neue", Helvetica, sans-serif;color:#555555; } | |||
.post-more {font:normal 13px/1.5em "Helvetica Neue", Helvetica, sans-serif;color:;border-top:0px solid #e6e6e6;border-bottom:0px solid #e6e6e6;} | |||
#post-author, #connect {border-top:1px solid #e6e6e6;border-bottom:1px solid #e6e6e6;border-left:1px solid #e6e6e6;border-right:1px solid #e6e6e6;border-radius:5px;-moz-border-radius:5px;-webkit-border-radius:5px;background-color:#fafafa} | |||
.nav-entries a, .woo-pagination { font:normal 13px/1em "Helvetica Neue", Helvetica, sans-serif;color:#888; } | |||
.woo-pagination a, .woo-pagination a:hover {color:#888!important} | |||
.widget h3 {font:bold 14px/1.2em "Helvetica Neue", Helvetica, sans-serif;color:#555555;border-bottom:1px solid #e6e6e6;} | |||
.widget_recent_comments li, #twitter li { border-color: #e6e6e6;} | |||
.widget p, .widget .textwidget { font:normal 13px/1.5em "Helvetica Neue", Helvetica, sans-serif;color:#555555; } | |||
.widget {font:normal 13px/1.5em "Helvetica Neue", Helvetica, sans-serif;color:#555555;border-radius:0px;-moz-border-radius:0px;-webkit-border-radius:0px;} | |||
#tabs .inside li a, .widget_woodojo_tabs .tabbable .tab-pane li a { font:bold 12px/1.5em "Helvetica Neue", Helvetica, sans-serif;color:#555555; } | |||
#tabs .inside li span.meta, .widget_woodojo_tabs .tabbable .tab-pane li span.meta { font:300 11px/1.5em "Helvetica Neue", Helvetica, sans-serif;color:#999999; } | |||
#tabs ul.wooTabs li a, .widget_woodojo_tabs .tabbable .nav-tabs li a { font:300 11px/2em "Helvetica Neue", Helvetica, sans-serif;color:#999999; } | |||
@media only screen and (min-width:768px) { | |||
ul.nav li a, #navigation ul.rss a, #navigation ul.cart a.cart-contents, #navigation .cart-contents #navigation ul.rss, #navigation ul.nav-search, #navigation ul.nav-search a { font:bold 15px/1.2em "Helvetica Neue", Helvetica, sans-serif;color:#ffffff; } #navigation ul.rss li a:before, #navigation ul.nav-search a.search-contents:before { color:#ffffff;} | |||
#navigation ul.nav > li a:hover, #navigation ul.nav > li:hover a, #navigation ul.nav li ul li a, #navigation ul.cart > li:hover > a, #navigation ul.cart > li > ul > div, #navigation ul.cart > li > ul > div p, #navigation ul.cart > li > ul span, #navigation ul.cart .cart_list a, #navigation ul.nav li.current_page_item a, #navigation ul.nav li.current_page_parent a, #navigation ul.nav li.current-menu-ancestor a, #navigation ul.nav li.current-cat a, #navigation ul.nav li.current-menu-item a { color:#eeeeee!important; } | |||
#navigation ul.nav > li a:hover, #navigation ul.nav > li:hover, #navigation ul.nav li ul, #navigation ul.cart li:hover a.cart-contents, #navigation ul.nav-search li:hover a.search-contents, #navigation ul.nav-search a.search-contents + ul, #navigation ul.cart a.cart-contents + ul, #navigation ul.nav li.current_page_item a, #navigation ul.nav li.current_page_parent a, #navigation ul.nav li.current-menu-ancestor a, #navigation ul.nav li.current-cat a, #navigation ul.nav li.current-menu-item a{background-color:#84abc7!important} | |||
#navigation ul.nav li ul, #navigation ul.cart > li > ul > div { border: 0px solid #dbdbdb; } | |||
#navigation ul.nav > li:hover > ul { left: 0; } | |||
#navigation ul.nav > li { border-right: 0px solid #dbdbdb; }#navigation ul.nav > li:hover > ul { left: 0; } | |||
#navigation { box-shadow: none; -moz-box-shadow: none; -webkit-box-shadow: none; }#navigation ul li:first-child, #navigation ul li:first-child a { border-radius:0px 0 0 0px; -moz-border-radius:0px 0 0 0px; -webkit-border-radius:0px 0 0 0px; } | |||
#navigation {background:#84abc7;border-top:0px solid #dbdbdb;border-bottom:0px solid #dbdbdb;border-left:0px solid #dbdbdb;border-right:0px solid #dbdbdb;border-radius:0px; -moz-border-radius:0px; -webkit-border-radius:0px;} | |||
#top ul.nav li a { font:normal 12px/1.6em "Helvetica Neue", Helvetica, sans-serif;color:#ddd; } | |||
} | |||
#footer, #footer p { font:normal 13px/1.4em "Helvetica Neue", Helvetica, sans-serif;color:#999999; } | |||
#footer {border-top:1px solid #dbdbdb;border-bottom:0px solid ;border-left:0px solid ;border-right:0px solid ;border-radius:0px; -moz-border-radius:0px; -webkit-border-radius:0px;} | |||
.magazine #loopedSlider .content h2.title a { font:bold 24px/1em Arial, sans-serif;color:#ffffff; } | |||
.wooslider-theme-magazine .slide-title a { font:bold 24px/1em Arial, sans-serif;color:#ffffff; } | |||
.magazine #loopedSlider .content .excerpt p { font:300 13px/1.5em Arial, sans-serif;color:#cccccc; } | |||
.wooslider-theme-magazine .slide-content p, .wooslider-theme-magazine .slide-excerpt p { font:300 13px/1.5em Arial, sans-serif;color:#cccccc; } | |||
.magazine .block .post .title a {font:bold 18px/1.2em Helvetica Neue, Helvetica, sans-serif;color:#222222; } | |||
#loopedSlider.business-slider .content h2 { font:bold 24px/1em Arial, sans-serif;color:#ffffff; } | |||
#loopedSlider.business-slider .content h2.title a { font:bold 24px/1em Arial, sans-serif;color:#ffffff; } | |||
.wooslider-theme-business .has-featured-image .slide-title { font:bold 24px/1em Arial, sans-serif;color:#ffffff; } | |||
.wooslider-theme-business .has-featured-image .slide-title a { font:bold 24px/1em Arial, sans-serif;color:#ffffff; } | |||
#wrapper #loopedSlider.business-slider .content p { font:300 13px/1.5em Arial, sans-serif;color:#cccccc; } | |||
.wooslider-theme-business .has-featured-image .slide-content p { font:300 13px/1.5em Arial, sans-serif;color:#cccccc; } | |||
.wooslider-theme-business .has-featured-image .slide-excerpt p { font:300 13px/1.5em Arial, sans-serif;color:#cccccc; } | |||
.archive_header { font:bold 18px/1em Arial, sans-serif;color:#222222; } | |||
.archive_header {border-bottom:1px solid #e6e6e6;} | |||
.archive_header .catrss { display:none; } | |||
</style> | |||
<!-- Custom Favicon --> | |||
<link rel="shortcut icon" href="https://machinelearningmastery.com/wp-content/uploads/2019/09/icon-16x16.png"/> | |||
<!-- Options Panel Custom CSS --> | |||
<style type="text/css"> | |||
#logo img { | |||
max-width: 100%; | |||
height: auto; | |||
} | |||
</style> | |||
<!-- Woo Shortcodes CSS --> | |||
<!-- Custom Stylesheet --> | |||
<!-- Theme version --> | |||
<meta name="generator" content="Canvas 5.9.21" /> | |||
<meta name="generator" content="WooFramework 6.2.9" /> | |||
<link rel="icon" href="https://machinelearningmastery.com/wp-content/uploads/2016/09/cropped-icon-32x32.png" sizes="32x32" /> | |||
<link rel="icon" href="https://machinelearningmastery.com/wp-content/uploads/2016/09/cropped-icon-192x192.png" sizes="192x192" /> | |||
<link rel="apple-touch-icon" href="https://machinelearningmastery.com/wp-content/uploads/2016/09/cropped-icon-180x180.png" /> | |||
<meta name="msapplication-TileImage" content="https://machinelearningmastery.com/wp-content/uploads/2016/09/cropped-icon-270x270.png" /> | |||
<style type="text/css" id="wp-custom-css"> | |||
.display-posts-listing.image-left .listing-item { | |||
overflow: hidden; | |||
margin-bottom: 30px; | |||
width: 100%; | |||
} | |||
.display-posts-listing.image-left .image { | |||
float: left; | |||
margin: 0 10px 0 0; | |||
} | |||
.display-posts-listing.image-left .attachment-thumbnail { | |||
height: auto; | |||
width: auto; | |||
max-width: 50px; | |||
max-height: 50px; | |||
border-radius: 50%; | |||
} | |||
.display-posts-listing.image-left .title { | |||
display: block; | |||
} | |||
.display-posts-listing.image-left .excerpt-dash { | |||
display: none; | |||
} | |||
.display-posts-listing.image-left { | |||
margin: 0 0 40px 0; | |||
} </style> | |||
<noscript><style id="rocket-lazyload-nojs-css">.rll-youtube-player, [data-lazy-src]{display:none !important;}</style></noscript> <!-- Facebook Pixel Code --> | |||
<script type="f3886dae12b0536ad361ea93-text/javascript"> | |||
!function(f,b,e,v,n,t,s) | |||
{if(f.fbq)return;n=f.fbq=function(){n.callMethod? | |||
n.callMethod.apply(n,arguments):n.queue.push(arguments)}; | |||
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0'; | |||
n.queue=[];t=b.createElement(e);t.async=!0; | |||
t.src=v;s=b.getElementsByTagName(e)[0]; | |||
s.parentNode.insertBefore(t,s)}(window, document,'script', | |||
'https://machinelearningmastery.com/wp-content/cache/busting/facebook-tracking/fbpix-events-en_US-2.9.5.js'); | |||
fbq('init', '834324500844861'); | |||
fbq('track', 'PageView'); | |||
</script> | |||
<noscript><img height="1" width="1" style="display:none" | |||
src="https://www.facebook.com/tr?id=834324500844861&ev=PageView&noscript=1" | |||
/></noscript> | |||
<!-- End Facebook Pixel Code --> | |||
</head> | |||
<body class="post-template-default single single-post postid-6939 single-format-standard chrome alt-style-default two-col-left width-960 two-col-left-960"> | |||
<div id="wrapper"> | |||
<div id="inner-wrapper"> | |||
<div style="text-align: center; padding: 10px; background-color: #FFF555;"> | |||
<span style="font-size: 125%;color: black;"> <a href="/super-bundle/?utm_campaign=Machine%20Learning%20Mastery%20Super%20Bundle&utm_source=website&utm_medium=banner">Click to get the 20-book Super Bundle! (Save $250)</a></span> | |||
</div> | |||
<h3 class="nav-toggle icon"><a href="#navigation">Navigation</a></h3> | |||
<header id="header" class="col-full"> | |||
<div id="logo"> | |||
<a href="https://machinelearningmastery.com/" title="Making developers awesome at machine learning"><img width="480" height="80" src="https://machinelearningmastery.com/wp-content/uploads/2019/09/Header_smaller_text_better-1.png" alt="Machine Learning Mastery" /></a> | |||
<span class="site-title"><a href="https://machinelearningmastery.com/">Machine Learning Mastery</a></span> | |||
<span class="site-description">Making developers awesome at machine learning</span> | |||
</div> | |||
<div class="header-widget"> | |||
<div id="custom_html-34" class="widget_text widget widget_custom_html"><div class="textwidget custom-html-widget"><div style="text-align: right;"> | |||
<a href="https://machinelearningmastery.lpages.co/bdl-mini-course/">Click to Take the FREE Deep Learning Performane Crash-Course</a> | |||
</div></div></div><div id="search-4" class="widget widget_search"><div class="search_main"> | |||
<form method="get" class="searchform" action="https://machinelearningmastery.com/" > | |||
<input type="text" class="field s" name="s" value="Search..." onfocus="if (!window.__cfRLUnblockHandlers) return false; if (this.value == 'Search...') {this.value = '';}" onblur="if (!window.__cfRLUnblockHandlers) return false; if (this.value == '') {this.value = 'Search...';}" data-cf-modified-f3886dae12b0536ad361ea93-="" /> | |||
<input type="hidden" name="post_type" value="post" /> | |||
<button type="submit" class="fa fa-search submit" name="submit" value="Search"></button> | |||
</form> | |||
<div class="fix"></div> | |||
</div></div> </div> | |||
</header> | |||
<nav id="navigation" class="col-full" role="navigation"> | |||
<section class="menus"> | |||
<a href="https://machinelearningmastery.com" class="nav-home"><span>Home</span></a> | |||
<h3>Main Menu</h3><ul id="main-nav" class="nav fl"><li id="menu-item-6503" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-6503"><a href="https://machinelearningmastery.com/start-here/">Get Started</a></li> | |||
<li id="menu-item-6501" class="menu-item menu-item-type-post_type menu-item-object-page current_page_parent menu-item-6501"><a href="https://machinelearningmastery.com/blog/">Blog</a></li> | |||
<li id="menu-item-6506" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-6506"><a href="#">Topics</a> | |||
<ul class="sub-menu"> | |||
<li id="menu-item-6508" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6508"><a href="https://machinelearningmastery.com/category/deep-learning/">Deep Learning (keras)</a></li> | |||
<li id="menu-item-7921" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-7921"><a href="https://machinelearningmastery.com/category/deep-learning-for-computer-vision/">Computer Vision</a></li> | |||
<li id="menu-item-6509" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6509"><a href="https://machinelearningmastery.com/category/deep-learning-time-series/">Neural Net Time Series</a></li> | |||
<li id="menu-item-6515" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6515"><a href="https://machinelearningmastery.com/category/natural-language-processing/">NLP (Text)</a></li> | |||
<li id="menu-item-8576" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-8576"><a href="https://machinelearningmastery.com/category/generative-adversarial-networks/">GANs</a></li> | |||
<li id="menu-item-6511" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6511"><a href="https://machinelearningmastery.com/category/lstm/">LSTMs</a></li> | |||
<li id="menu-item-7106" class="menu-item menu-item-type-taxonomy menu-item-object-category current-post-ancestor current-menu-parent current-post-parent menu-item-7106"><a href="https://machinelearningmastery.com/category/better-deep-learning/">Better Deep Learning</a></li> | |||
<li id="menu-item-6512" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6512"><a href="https://machinelearningmastery.com/category/machine-learning-algorithms/">Intro to Algorithms</a></li> | |||
<li id="menu-item-6507" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6507"><a href="https://machinelearningmastery.com/category/algorithms-from-scratch/">Code Algorithms</a></li> | |||
<li id="menu-item-6520" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6520"><a href="https://machinelearningmastery.com/category/time-series/">Intro to Time Series</a></li> | |||
<li id="menu-item-6516" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6516"><a href="https://machinelearningmastery.com/category/python-machine-learning/">Python (scikit-learn)</a></li> | |||
<li id="menu-item-11055" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-11055"><a href="https://machinelearningmastery.com/category/ensemble-learning/">Ensemble Learning</a></li> | |||
<li id="menu-item-9966" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-9966"><a href="https://machinelearningmastery.com/category/imbalanced-classification/">Imbalanced Learning</a></li> | |||
<li id="menu-item-10674" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-10674"><a href="https://machinelearningmastery.com/category/data-preparation/">Data Preparation</a></li> | |||
<li id="menu-item-6517" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6517"><a href="https://machinelearningmastery.com/category/r-machine-learning/">R (caret)</a></li> | |||
<li id="menu-item-6522" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6522"><a href="https://machinelearningmastery.com/category/weka-machine-learning/">Weka (no code)</a></li> | |||
<li id="menu-item-6510" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6510"><a href="https://machinelearningmastery.com/category/linear-algebra/">Linear Algebra</a></li> | |||
<li id="menu-item-6519" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6519"><a href="https://machinelearningmastery.com/category/statistical-methods/">Statistics</a></li> | |||
<li id="menu-item-12825" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-12825"><a href="https://machinelearningmastery.com/category/optimization/">Optimization</a></li> | |||
<li id="menu-item-8989" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-8989"><a href="https://machinelearningmastery.com/category/probability/">Probability</a></li> | |||
<li id="menu-item-6523" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6523"><a href="https://machinelearningmastery.com/category/xgboost/">XGBoost</a></li> | |||
</ul> | |||
</li> | |||
<li id="menu-item-6502" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-6502"><a href="https://machinelearningmastery.com/products/">EBooks</a></li> | |||
<li id="menu-item-6500" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-6500"><a href="https://machinelearningmastery.com/faq/">FAQ</a></li> | |||
<li id="menu-item-6504" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-6504"><a href="https://machinelearningmastery.com/about/">About</a></li> | |||
<li id="menu-item-6505" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-6505"><a href="https://machinelearningmastery.com/contact/">Contact</a></li> | |||
</ul> <div class="side-nav"> | |||
</div><!-- /#side-nav --> | |||
</section><!-- /.menus --> | |||
<a href="#top" class="nav-close"><span>Return to Content</span></a> | |||
</nav> | |||
<!-- #content Starts --> | |||
<div id="content" class="col-full"> | |||
<div id="main-sidebar-container"> | |||
<!-- #main Starts --> | |||
<section id="main"> | |||
<article class="post-6939 post type-post status-publish format-standard has-post-thumbnail hentry category-better-deep-learning"> | |||
<header> | |||
<h1 class="title entry-title">How to use Data Scaling Improve Deep Learning Model Stability and Performance</h1> </header> | |||
<div class="post-meta"><span class="small">By</span> <span class="author vcard"><span class="fn"><a href="https://machinelearningmastery.com/author/jasonb/" title="Posts by Jason Brownlee" rel="author">Jason Brownlee</a></span></span> <span class="small">on</span> <abbr class="date time published updated" title="2019-02-04T05:00:34+1100">February 4, 2019</abbr> <span class="small">in</span> <span class="categories"><a href="https://machinelearningmastery.com/category/better-deep-learning/" title="View all items in Deep Learning Performance">Deep Learning Performance</a></span> </div> | |||
<section class="entry"> | |||
<div class="simplesocialbuttons simplesocial-simple-icons simplesocialbuttons_inline simplesocialbuttons-align-left post-6939 post simplesocialbuttons-inline-no-animation"> | |||
<button class="ssb_tweet-icon" data-href="https://twitter.com/share?text=How+to+use+Data+Scaling+Improve+Deep+Learning+Model+Stability+and+Performance&url=https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/" rel="nofollow" onclick="if (!window.__cfRLUnblockHandlers) return false; javascript:window.open(this.dataset.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;" data-cf-modified-f3886dae12b0536ad361ea93-=""> | |||
<span class="icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 72 72"><path fill="none" d="M0 0h72v72H0z"/><path class="icon" fill="#fff" d="M68.812 15.14c-2.348 1.04-4.87 1.744-7.52 2.06 2.704-1.62 4.78-4.186 5.757-7.243-2.53 1.5-5.33 2.592-8.314 3.176C56.35 10.59 52.948 9 49.182 9c-7.23 0-13.092 5.86-13.092 13.093 0 1.026.118 2.02.338 2.98C25.543 24.527 15.9 19.318 9.44 11.396c-1.125 1.936-1.77 4.184-1.77 6.58 0 4.543 2.312 8.552 5.824 10.9-2.146-.07-4.165-.658-5.93-1.64-.002.056-.002.11-.002.163 0 6.345 4.513 11.638 10.504 12.84-1.1.298-2.256.457-3.45.457-.845 0-1.666-.078-2.464-.23 1.667 5.2 6.5 8.985 12.23 9.09-4.482 3.51-10.13 5.605-16.26 5.605-1.055 0-2.096-.06-3.122-.184 5.794 3.717 12.676 5.882 20.067 5.882 24.083 0 37.25-19.95 37.25-37.25 0-.565-.013-1.133-.038-1.693 2.558-1.847 4.778-4.15 6.532-6.774z"/></svg></span><i class="simplesocialtxt">Tweet </i></button> | |||
<button class="ssb_fbshare-icon" target="_blank" data-href="https://www.facebook.com/sharer/sharer.php?u=https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/" onclick="if (!window.__cfRLUnblockHandlers) return false; javascript:window.open(this.dataset.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;" data-cf-modified-f3886dae12b0536ad361ea93-=""> | |||
<span class="icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" class="_1pbq" color="#ffffff"><path fill="#ffffff" fill-rule="evenodd" class="icon" d="M8 14H3.667C2.733 13.9 2 13.167 2 12.233V3.667A1.65 1.65 0 0 1 3.667 2h8.666A1.65 1.65 0 0 1 14 3.667v8.566c0 .934-.733 1.667-1.667 1.767H10v-3.967h1.3l.7-2.066h-2V6.933c0-.466.167-.9.867-.9H12v-1.8c.033 0-.933-.266-1.533-.266-1.267 0-2.434.7-2.467 2.133v1.867H6v2.066h2V14z"></path></svg></span> | |||
<span class="simplesocialtxt">Share </span> </button> | |||
<button class="ssb_linkedin-icon" data-href="https://www.linkedin.com/cws/share?url=https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/" onclick="if (!window.__cfRLUnblockHandlers) return false; javascript:window.open(this.dataset.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;" data-cf-modified-f3886dae12b0536ad361ea93-=""> | |||
<span class="icon"> <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="15px" height="14.1px" viewBox="-301.4 387.5 15 14.1" enable-background="new -301.4 387.5 15 14.1" xml:space="preserve"> <g id="XMLID_398_"> <path id="XMLID_399_" fill="#FFFFFF" d="M-296.2,401.6c0-3.2,0-6.3,0-9.5h0.1c1,0,2,0,2.9,0c0.1,0,0.1,0,0.1,0.1c0,0.4,0,0.8,0,1.2 c0.1-0.1,0.2-0.3,0.3-0.4c0.5-0.7,1.2-1,2.1-1.1c0.8-0.1,1.5,0,2.2,0.3c0.7,0.4,1.2,0.8,1.5,1.4c0.4,0.8,0.6,1.7,0.6,2.5 c0,1.8,0,3.6,0,5.4v0.1c-1.1,0-2.1,0-3.2,0c0-0.1,0-0.1,0-0.2c0-1.6,0-3.2,0-4.8c0-0.4,0-0.8-0.2-1.2c-0.2-0.7-0.8-1-1.6-1 c-0.8,0.1-1.3,0.5-1.6,1.2c-0.1,0.2-0.1,0.5-0.1,0.8c0,1.7,0,3.4,0,5.1c0,0.2,0,0.2-0.2,0.2c-1,0-1.9,0-2.9,0 C-296.1,401.6-296.2,401.6-296.2,401.6z"/> <path id="XMLID_400_" fill="#FFFFFF" d="M-298,401.6L-298,401.6c-1.1,0-2.1,0-3,0c-0.1,0-0.1,0-0.1-0.1c0-3.1,0-6.1,0-9.2 c0-0.1,0-0.1,0.1-0.1c1,0,2,0,2.9,0h0.1C-298,395.3-298,398.5-298,401.6z"/> <path id="XMLID_401_" fill="#FFFFFF" d="M-299.6,390.9c-0.7-0.1-1.2-0.3-1.6-0.8c-0.5-0.8-0.2-2.1,1-2.4c0.6-0.2,1.2-0.1,1.8,0.2 c0.5,0.4,0.7,0.9,0.6,1.5c-0.1,0.7-0.5,1.1-1.1,1.3C-299.1,390.8-299.4,390.8-299.6,390.9L-299.6,390.9z"/> </g> </svg> </span> | |||
<span class="simplesocialtxt">Share</span> </button> | |||
</div> | |||
<p id="last-modified-info">Last Updated on August 25, 2020</p> | |||
<p>Deep learning neural networks learn how to map inputs to outputs from examples in a training dataset.</p> | |||
<p>The weights of the model are initialized to small random values and updated via an optimization algorithm in response to estimates of error on the training dataset.</p> | |||
<p>Given the use of small weights in the model and the use of error between predictions and expected values, the scale of inputs and outputs used to train the model are an important factor. Unscaled input variables can result in a slow or unstable learning process, whereas unscaled target variables on regression problems can result in exploding gradients causing the learning process to fail.</p> | |||
<p>Data preparation involves using techniques such as the normalization and standardization to rescale input and output variables prior to training a neural network model.</p> | |||
<p>In this tutorial, you will discover how to improve neural network stability and modeling performance by scaling data.</p> | |||
<p>After completing this tutorial, you will know:</p> | |||
<ul> | |||
<li>Data scaling is a recommended pre-processing step when working with deep learning neural networks.</li> | |||
<li>Data scaling can be achieved by normalizing or standardizing real-valued input and output variables.</li> | |||
<li>How to apply standardization and normalization to improve the performance of a Multilayer Perceptron model on a regression predictive modeling problem.</li> | |||
</ul> | |||
<p><strong>Kick-start your project</strong> with my new book <a href="https://machinelearningmastery.com/better-deep-learning/">Better Deep Learning</a>, including <em>step-by-step tutorials</em> and the <em>Python source code</em> files for all examples.</p> | |||
<p>Let’s get started.</p> | |||
<div id="attachment_6944" style="width: 650px" class="wp-caption aligncenter"><img aria-describedby="caption-attachment-6944" loading="lazy" class="size-full wp-image-6944" src="https://machinelearningmastery.com/wp-content/uploads/2019/02/How-to-Improve-Neural-Network-Stability-and-Modeling-Performance-With-Data-Scaling.jpg" alt="How to Improve Neural Network Stability and Modeling Performance With Data Scaling" width="640" height="249" srcset="https://machinelearningmastery.com/wp-content/uploads/2019/02/How-to-Improve-Neural-Network-Stability-and-Modeling-Performance-With-Data-Scaling.jpg 640w, https://machinelearningmastery.com/wp-content/uploads/2019/02/How-to-Improve-Neural-Network-Stability-and-Modeling-Performance-With-Data-Scaling-300x117.jpg 300w" sizes="(max-width: 640px) 100vw, 640px" /><p id="caption-attachment-6944" class="wp-caption-text">How to Improve Neural Network Stability and Modeling Performance With Data Scaling<br />Photo by <a href="https://www.flickr.com/photos/javiersanp/14202569306/">Javier Sanchez Portero</a>, some rights reserved.</p></div> | |||
<h2>Tutorial Overview</h2> | |||
<p>This tutorial is divided into six parts; they are:</p> | |||
<ol> | |||
<li>The Scale of Your Data Matters</li> | |||
<li>Data Scaling Methods</li> | |||
<li>Regression Predictive Modeling Problem</li> | |||
<li>Multilayer Perceptron With Unscaled Data</li> | |||
<li>Multilayer Perceptron With Scaled Output Variables</li> | |||
<li>Multilayer Perceptron With Scaled Input Variables</li> | |||
</ol> | |||
<h2>The Scale of Your Data Matters</h2> | |||
<p>Deep learning neural network models learn a mapping from input variables to an output variable.</p> | |||
<p>As such, the scale and distribution of the data drawn from the domain may be different for each variable.</p> | |||
<p>Input variables may have different units (e.g. feet, kilometers, and hours) that, in turn, may mean the variables have different scales.</p> | |||
<p>Differences in the scales across input variables may increase the difficulty of the problem being modeled. An example of this is that large input values (e.g. a spread of hundreds or thousands of units) can result in a model that learns large weight values. A model with large weight values is often unstable, meaning that it may suffer from poor performance during learning and sensitivity to input values resulting in higher generalization error.</p> | |||
<blockquote><p>One of the most common forms of pre-processing consists of a simple linear rescaling of the input variables.</p></blockquote> | |||
<p>— Page 298, <a href="https://amzn.to/2S8qdwt">Neural Networks for Pattern Recognition</a>, 1995.</p> | |||
<p>A target variable with a large spread of values, in turn, may result in large error gradient values causing weight values to change dramatically, making the learning process unstable.</p> | |||
<p>Scaling input and output variables is a critical step in using neural network models.</p> | |||
<blockquote><p>In practice it is nearly always advantageous to apply pre-processing transformations to the input data before it is presented to a network. Similarly, the outputs of the network are often post-processed to give the required output values.</p></blockquote> | |||
<p>— Page 296, <a href="https://amzn.to/2S8qdwt">Neural Networks for Pattern Recognition</a>, 1995.</p> | |||
<h3>Scaling Input Variables</h3> | |||
<p>The input variables are those that the network takes on the input or visible layer in order to make a prediction.</p> | |||
<p>A good rule of thumb is that input variables should be small values, probably in the range of 0-1 or standardized with a zero mean and a standard deviation of one.</p> | |||
<p>Whether input variables require scaling depends on the specifics of your problem and of each variable.</p> | |||
<p>You may have a sequence of quantities as inputs, such as prices or temperatures.</p> | |||
<p>If the distribution of the quantity is normal, then it should be standardized, otherwise the data should be normalized. This applies if the range of quantity values is large (10s, 100s, etc.) or small (0.01, 0.0001).</p> | |||
<p>If the quantity values are small (near 0-1) and the distribution is limited (e.g. standard deviation near 1) then perhaps you can get away with no scaling of the data.</p> | |||
<p>Problems can be complex and it may not be clear how to best scale input data.</p> | |||
<p>If in doubt, normalize the input sequence. If you have the resources, explore modeling with the raw data, standardized data, and normalized data and see if there is a beneficial difference in the performance of the resulting model.</p> | |||
<blockquote><p>If the input variables are combined linearly, as in an MLP [Multilayer Perceptron], then it is rarely strictly necessary to standardize the inputs, at least in theory. […] However, there are a variety of practical reasons why standardizing the inputs can make training faster and reduce the chances of getting stuck in local optima.</p></blockquote> | |||
<p>— <a href="ftp://ftp.sas.com/pub/neural/FAQ2.html#A_std">Should I normalize/standardize/rescale the data? Neural Nets FAQ</a></p> | |||
<h3>Scaling Output Variables</h3> | |||
<p>The output variable is the variable predicted by the network.</p> | |||
<p>You must ensure that the scale of your output variable matches the scale of the activation function (transfer function) on the output layer of your network.</p> | |||
<blockquote><p>If your output activation function has a range of [0,1], then obviously you must ensure that the target values lie within that range. But it is generally better to choose an output activation function suited to the distribution of the targets than to force your data to conform to the output activation function.</p></blockquote> | |||
<p>— <a href="ftp://ftp.sas.com/pub/neural/FAQ2.html#A_std">Should I normalize/standardize/rescale the data? Neural Nets FAQ</a></p> | |||
<p>If your problem is a regression problem, then the output will be a real value.</p> | |||
<p>This is best modeled with a linear activation function. If the distribution of the value is normal, then you can standardize the output variable. Otherwise, the output variable can be normalized.</p> | |||
<div class="woo-sc-hr"></div> | |||
<center> | |||
<h3>Want Better Results with Deep Learning?</h3> | |||
<p>Take my free 7-day email crash course now (with sample code).</p> | |||
<p>Click to sign-up and also get a free PDF Ebook version of the course.</p> | |||
<p><a href="https://machinelearningmastery.lpages.co/leadbox/1433e7773f72a2%3A164f8be4f346dc/5764144745676800/" target="_blank" style="background: rgb(255, 206, 10); color: rgb(255, 255, 255); text-decoration: none; font-family: Helvetica, Arial, sans-serif; font-weight: bold; font-size: 16px; line-height: 20px; padding: 10px; display: inline-block; max-width: 300px; border-radius: 5px; text-shadow: rgba(0, 0, 0, 0.25) 0px -1px 1px; box-shadow: rgba(255, 255, 255, 0.5) 0px 1px 3px inset, rgba(0, 0, 0, 0.5) 0px 1px 3px;" rel="noopener noreferrer">Download Your FREE Mini-Course</a></p> | |||
</center> | |||
<div class="woo-sc-hr"></div> | |||
<h2>Data Scaling Methods</h2> | |||
<p>There are two types of scaling of your data that you may want to consider: normalization and standardization.</p> | |||
<p>These can both be achieved using the scikit-learn library.</p> | |||
<h3>Data Normalization</h3> | |||
<p>Normalization is a rescaling of the data from the original range so that all values are within the range of 0 and 1.</p> | |||
<p>Normalization requires that you know or are able to accurately estimate the minimum and maximum observable values. You may be able to estimate these values from your available data.</p> | |||
<p>A value is normalized as follows:</p> | |||
<div id="urvanov-syntax-highlighter-613eee9a17d23443968341" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;"> | |||
<div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><span class="crayon-title"></span> | |||
<div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><div class="crayon-button urvanov-syntax-highlighter-nums-button" title="Toggle Line Numbers"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-plain-button" title="Toggle Plain Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-wrap-button" title="Toggle Line Wrap"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-expand-button" title="Expand Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-copy-button" title="Copy"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-popup-button" title="Open Code In New Window"><div class="urvanov-syntax-highlighter-button-icon"></div></div></div></div> | |||
<div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;"></div> | |||
<div class="urvanov-syntax-highlighter-plain-wrap"><textarea wrap="soft" class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;"> | |||
y = (x - min) / (max - min)</textarea></div> | |||
<div class="urvanov-syntax-highlighter-main" style=""> | |||
<table class="crayon-table"> | |||
<tr class="urvanov-syntax-highlighter-row"> | |||
<td class="crayon-nums " data-settings="show"> | |||
<div class="urvanov-syntax-highlighter-nums-content" style="font-size: 12px !important; line-height: 15px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d23443968341-1">1</div></div> | |||
</td> | |||
<td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d23443968341-1">y = (x - min) / (max - min)</div></div></td> | |||
</tr> | |||
</table> | |||
</div> | |||
</div><p>Where the minimum and maximum values pertain to the value <em>x</em> being normalized.</p> | |||
<p>For example, for a dataset, we could guesstimate the min and max observable values as 30 and -10. We can then normalize any value, like 18.8, as follows:</p> | |||
<div id="urvanov-syntax-highlighter-613eee9a17d28803446066" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;"> | |||
<div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><span class="crayon-title"></span> | |||
<div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><div class="crayon-button urvanov-syntax-highlighter-nums-button" title="Toggle Line Numbers"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-plain-button" title="Toggle Plain Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-wrap-button" title="Toggle Line Wrap"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-expand-button" title="Expand Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-copy-button" title="Copy"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-popup-button" title="Open Code In New Window"><div class="urvanov-syntax-highlighter-button-icon"></div></div></div></div> | |||
<div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;"></div> | |||
<div class="urvanov-syntax-highlighter-plain-wrap"><textarea wrap="soft" class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;"> | |||
y = (x - min) / (max - min) | |||
y = (18.8 - (-10)) / (30 - (-10)) | |||
y = 28.8 / 40 | |||
y = 0.72</textarea></div> | |||
<div class="urvanov-syntax-highlighter-main" style=""> | |||
<table class="crayon-table"> | |||
<tr class="urvanov-syntax-highlighter-row"> | |||
<td class="crayon-nums " data-settings="show"> | |||
<div class="urvanov-syntax-highlighter-nums-content" style="font-size: 12px !important; line-height: 15px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d28803446066-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d28803446066-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d28803446066-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d28803446066-4">4</div></div> | |||
</td> | |||
<td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d28803446066-1">y = (x - min) / (max - min)</div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d28803446066-2">y = (18.8 - (-10)) / (30 - (-10))</div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d28803446066-3">y = 28.8 / 40</div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d28803446066-4">y = 0.72</div></div></td> | |||
</tr> | |||
</table> | |||
</div> | |||
</div><p>You can see that if an <em>x</em> value is provided that is outside the bounds of the minimum and maximum values, the resulting value will not be in the range of 0 and 1. You could check for these observations prior to making predictions and either remove them from the dataset or limit them to the pre-defined maximum or minimum values.</p> | |||
<p>You can normalize your dataset using the scikit-learn object <a href="http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.MinMaxScaler.html">MinMaxScaler</a>.</p> | |||
<p>Good practice usage with the <em>MinMaxScaler</em> and other scaling techniques is as follows:</p> | |||
<ul> | |||
<li><strong>Fit the scaler using available training data</strong>. For normalization, this means the training data will be used to estimate the minimum and maximum observable values. This is done by calling the <em>fit()</em> function.</li> | |||
<li><strong>Apply the scale to training data</strong>. This means you can use the normalized data to train your model. This is done by calling the <em>transform()</em> function.</li> | |||
<li><strong>Apply the scale to data going forward</strong>. This means you can prepare new data in the future on which you want to make predictions.</li> | |||
</ul> | |||
<p>The default scale for the <em>MinMaxScaler</em> is to rescale variables into the range [0,1], although a preferred scale can be specified via the “<em>feature_range</em>” argument and specify a tuple including the min and the max for all variables.</p> | |||
<div id="urvanov-syntax-highlighter-613eee9a17d29766648590" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;"> | |||
<div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><span class="crayon-title"></span> | |||
<div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><div class="crayon-button urvanov-syntax-highlighter-nums-button" title="Toggle Line Numbers"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-plain-button" title="Toggle Plain Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-wrap-button" title="Toggle Line Wrap"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-expand-button" title="Expand Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-copy-button" title="Copy"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-popup-button" title="Open Code In New Window"><div class="urvanov-syntax-highlighter-button-icon"></div></div></div></div> | |||
<div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;"></div> | |||
<div class="urvanov-syntax-highlighter-plain-wrap"><textarea wrap="soft" class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;"> | |||
# create scaler | |||
scaler = MinMaxScaler(feature_range=(-1,1))</textarea></div> | |||
<div class="urvanov-syntax-highlighter-main" style=""> | |||
<table class="crayon-table"> | |||
<tr class="urvanov-syntax-highlighter-row"> | |||
<td class="crayon-nums " data-settings="show"> | |||
<div class="urvanov-syntax-highlighter-nums-content" style="font-size: 12px !important; line-height: 15px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d29766648590-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d29766648590-2">2</div></div> | |||
</td> | |||
<td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d29766648590-1"><span class="crayon-p"># create scaler</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d29766648590-2"><span class="crayon-v">scaler</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">MinMaxScaler</span><span class="crayon-sy">(</span><span class="crayon-v">feature_range</span><span class="crayon-o">=</span><span class="crayon-sy">(</span><span class="crayon-o">-</span><span class="crayon-cn">1</span><span class="crayon-sy">,</span><span class="crayon-cn">1</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div></div></td> | |||
</tr> | |||
</table> | |||
</div> | |||
</div><p>If needed, the transform can be inverted. This is useful for converting predictions back into their original scale for reporting or plotting. This can be done by calling the <em>inverse_transform()</em> function.</p> | |||
<p>The example below provides a general demonstration for using the <em>MinMaxScaler</em> to normalize data.</p> | |||
<div id="urvanov-syntax-highlighter-613eee9a17d2a990976248" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;"> | |||
<div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><span class="crayon-title"></span> | |||
<div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><div class="crayon-button urvanov-syntax-highlighter-nums-button" title="Toggle Line Numbers"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-plain-button" title="Toggle Plain Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-wrap-button" title="Toggle Line Wrap"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-expand-button" title="Expand Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-copy-button" title="Copy"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-popup-button" title="Open Code In New Window"><div class="urvanov-syntax-highlighter-button-icon"></div></div></div></div> | |||
<div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;"></div> | |||
<div class="urvanov-syntax-highlighter-plain-wrap"><textarea wrap="soft" class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;"> | |||
# demonstrate data normalization with sklearn | |||
from sklearn.preprocessing import MinMaxScaler | |||
# load data | |||
data = ... | |||
# create scaler | |||
scaler = MinMaxScaler() | |||
# fit scaler on data | |||
scaler.fit(data) | |||
# apply transform | |||
normalized = scaler.transform(data) | |||
# inverse transform | |||
inverse = scaler.inverse_transform(normalized)</textarea></div> | |||
<div class="urvanov-syntax-highlighter-main" style=""> | |||
<table class="crayon-table"> | |||
<tr class="urvanov-syntax-highlighter-row"> | |||
<td class="crayon-nums " data-settings="show"> | |||
<div class="urvanov-syntax-highlighter-nums-content" style="font-size: 12px !important; line-height: 15px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d2a990976248-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d2a990976248-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d2a990976248-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d2a990976248-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d2a990976248-5">5</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d2a990976248-6">6</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d2a990976248-7">7</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d2a990976248-8">8</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d2a990976248-9">9</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d2a990976248-10">10</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d2a990976248-11">11</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d2a990976248-12">12</div></div> | |||
</td> | |||
<td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d2a990976248-1"><span class="crayon-p"># demonstrate data normalization with sklearn</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d2a990976248-2"><span class="crayon-e">from </span><span class="crayon-v">sklearn</span><span class="crayon-sy">.</span><span class="crayon-e">preprocessing </span><span class="crayon-e">import </span><span class="crayon-i">MinMaxScaler</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d2a990976248-3"><span class="crayon-p"># load data</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d2a990976248-4"><span class="crayon-v">data</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-sy">.</span><span class="crayon-sy">.</span><span class="crayon-sy">.</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d2a990976248-5"><span class="crayon-p"># create scaler</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d2a990976248-6"><span class="crayon-v">scaler</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">MinMaxScaler</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d2a990976248-7"><span class="crayon-p"># fit scaler on data</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d2a990976248-8"><span class="crayon-v">scaler</span><span class="crayon-sy">.</span><span class="crayon-e">fit</span><span class="crayon-sy">(</span><span class="crayon-v">data</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d2a990976248-9"><span class="crayon-p"># apply transform</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d2a990976248-10"><span class="crayon-v">normalized</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">scaler</span><span class="crayon-sy">.</span><span class="crayon-e">transform</span><span class="crayon-sy">(</span><span class="crayon-v">data</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d2a990976248-11"><span class="crayon-p"># inverse transform</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d2a990976248-12"><span class="crayon-v">inverse</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">scaler</span><span class="crayon-sy">.</span><span class="crayon-e">inverse_transform</span><span class="crayon-sy">(</span><span class="crayon-v">normalized</span><span class="crayon-sy">)</span></div></div></td> | |||
</tr> | |||
</table> | |||
</div> | |||
</div><p>You can also perform the fit and transform in a single step using the <em>fit_transform()</em> function; for example:</p> | |||
<div id="urvanov-syntax-highlighter-613eee9a17d2b415457051" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;"> | |||
<div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><span class="crayon-title"></span> | |||
<div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><div class="crayon-button urvanov-syntax-highlighter-nums-button" title="Toggle Line Numbers"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-plain-button" title="Toggle Plain Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-wrap-button" title="Toggle Line Wrap"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-expand-button" title="Expand Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-copy-button" title="Copy"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-popup-button" title="Open Code In New Window"><div class="urvanov-syntax-highlighter-button-icon"></div></div></div></div> | |||
<div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;"></div> | |||
<div class="urvanov-syntax-highlighter-plain-wrap"><textarea wrap="soft" class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;"> | |||
# demonstrate data normalization with sklearn | |||
from sklearn.preprocessing import MinMaxScaler | |||
# load data | |||
data = ... | |||
# create scaler | |||
scaler = MinMaxScaler() | |||
# fit and transform in one step | |||
normalized = scaler.fit_transform(data) | |||
# inverse transform | |||
inverse = scaler.inverse_transform(normalized)</textarea></div> | |||
<div class="urvanov-syntax-highlighter-main" style=""> | |||
<table class="crayon-table"> | |||
<tr class="urvanov-syntax-highlighter-row"> | |||
<td class="crayon-nums " data-settings="show"> | |||
<div class="urvanov-syntax-highlighter-nums-content" style="font-size: 12px !important; line-height: 15px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d2b415457051-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d2b415457051-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d2b415457051-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d2b415457051-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d2b415457051-5">5</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d2b415457051-6">6</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d2b415457051-7">7</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d2b415457051-8">8</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d2b415457051-9">9</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d2b415457051-10">10</div></div> | |||
</td> | |||
<td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d2b415457051-1"><span class="crayon-p"># demonstrate data normalization with sklearn</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d2b415457051-2"><span class="crayon-e">from </span><span class="crayon-v">sklearn</span><span class="crayon-sy">.</span><span class="crayon-e">preprocessing </span><span class="crayon-e">import </span><span class="crayon-i">MinMaxScaler</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d2b415457051-3"><span class="crayon-p"># load data</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d2b415457051-4"><span class="crayon-v">data</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-sy">.</span><span class="crayon-sy">.</span><span class="crayon-sy">.</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d2b415457051-5"><span class="crayon-p"># create scaler</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d2b415457051-6"><span class="crayon-v">scaler</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">MinMaxScaler</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d2b415457051-7"><span class="crayon-p"># fit and transform in one step</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d2b415457051-8"><span class="crayon-v">normalized</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">scaler</span><span class="crayon-sy">.</span><span class="crayon-e">fit_transform</span><span class="crayon-sy">(</span><span class="crayon-v">data</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d2b415457051-9"><span class="crayon-p"># inverse transform</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d2b415457051-10"><span class="crayon-v">inverse</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">scaler</span><span class="crayon-sy">.</span><span class="crayon-e">inverse_transform</span><span class="crayon-sy">(</span><span class="crayon-v">normalized</span><span class="crayon-sy">)</span></div></div></td> | |||
</tr> | |||
</table> | |||
</div> | |||
</div><p></p> | |||
<h3>Data Standardization</h3> | |||
<p>Standardizing a dataset involves rescaling the distribution of values so that the mean of observed values is 0 and the standard deviation is 1. It is sometimes referred to as “<em>whitening</em>.”</p> | |||
<p>This can be thought of as subtracting the mean value or centering the data.</p> | |||
<p>Like normalization, standardization can be useful, and even required in some machine learning algorithms when your data has input values with differing scales.</p> | |||
<p>Standardization assumes that your observations fit a Gaussian distribution (bell curve) with a well behaved mean and standard deviation. You can still standardize your data if this expectation is not met, but you may not get reliable results.</p> | |||
<p>Standardization requires that you know or are able to accurately estimate the mean and standard deviation of observable values. You may be able to estimate these values from your training data.</p> | |||
<p>A value is standardized as follows:</p> | |||
<div id="urvanov-syntax-highlighter-613eee9a17d2c615471602" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;"> | |||
<div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><span class="crayon-title"></span> | |||
<div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><div class="crayon-button urvanov-syntax-highlighter-nums-button" title="Toggle Line Numbers"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-plain-button" title="Toggle Plain Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-wrap-button" title="Toggle Line Wrap"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-expand-button" title="Expand Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-copy-button" title="Copy"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-popup-button" title="Open Code In New Window"><div class="urvanov-syntax-highlighter-button-icon"></div></div></div></div> | |||
<div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;"></div> | |||
<div class="urvanov-syntax-highlighter-plain-wrap"><textarea wrap="soft" class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;"> | |||
y = (x - mean) / standard_deviation</textarea></div> | |||
<div class="urvanov-syntax-highlighter-main" style=""> | |||
<table class="crayon-table"> | |||
<tr class="urvanov-syntax-highlighter-row"> | |||
<td class="crayon-nums " data-settings="show"> | |||
<div class="urvanov-syntax-highlighter-nums-content" style="font-size: 12px !important; line-height: 15px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d2c615471602-1">1</div></div> | |||
</td> | |||
<td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d2c615471602-1">y = (x - mean) / standard_deviation</div></div></td> | |||
</tr> | |||
</table> | |||
</div> | |||
</div><p>Where the <em>mean</em> is calculated as:</p> | |||
<div id="urvanov-syntax-highlighter-613eee9a17d2d121964675" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;"> | |||
<div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><span class="crayon-title"></span> | |||
<div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><div class="crayon-button urvanov-syntax-highlighter-nums-button" title="Toggle Line Numbers"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-plain-button" title="Toggle Plain Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-wrap-button" title="Toggle Line Wrap"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-expand-button" title="Expand Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-copy-button" title="Copy"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-popup-button" title="Open Code In New Window"><div class="urvanov-syntax-highlighter-button-icon"></div></div></div></div> | |||
<div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;"></div> | |||
<div class="urvanov-syntax-highlighter-plain-wrap"><textarea wrap="soft" class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;"> | |||
mean = sum(x) / count(x)</textarea></div> | |||
<div class="urvanov-syntax-highlighter-main" style=""> | |||
<table class="crayon-table"> | |||
<tr class="urvanov-syntax-highlighter-row"> | |||
<td class="crayon-nums " data-settings="show"> | |||
<div class="urvanov-syntax-highlighter-nums-content" style="font-size: 12px !important; line-height: 15px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d2d121964675-1">1</div></div> | |||
</td> | |||
<td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d2d121964675-1">mean = sum(x) / count(x)</div></div></td> | |||
</tr> | |||
</table> | |||
</div> | |||
</div><p>And the <em>standard_deviation</em> is calculated as:</p> | |||
<div id="urvanov-syntax-highlighter-613eee9a17d2e734583720" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;"> | |||
<div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><span class="crayon-title"></span> | |||
<div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><div class="crayon-button urvanov-syntax-highlighter-nums-button" title="Toggle Line Numbers"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-plain-button" title="Toggle Plain Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-wrap-button" title="Toggle Line Wrap"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-expand-button" title="Expand Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-copy-button" title="Copy"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-popup-button" title="Open Code In New Window"><div class="urvanov-syntax-highlighter-button-icon"></div></div></div></div> | |||
<div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;"></div> | |||
<div class="urvanov-syntax-highlighter-plain-wrap"><textarea wrap="soft" class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;"> | |||
standard_deviation = sqrt( sum( (x - mean)^2 ) / count(x))</textarea></div> | |||
<div class="urvanov-syntax-highlighter-main" style=""> | |||
<table class="crayon-table"> | |||
<tr class="urvanov-syntax-highlighter-row"> | |||
<td class="crayon-nums " data-settings="show"> | |||
<div class="urvanov-syntax-highlighter-nums-content" style="font-size: 12px !important; line-height: 15px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d2e734583720-1">1</div></div> | |||
</td> | |||
<td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d2e734583720-1">standard_deviation = sqrt( sum( (x - mean)^2 ) / count(x))</div></div></td> | |||
</tr> | |||
</table> | |||
</div> | |||
</div><p>We can guesstimate a mean of 10 and a standard deviation of about 5. Using these values, we can standardize the first value of 20.7 as follows:</p> | |||
<div id="urvanov-syntax-highlighter-613eee9a17d2f367850592" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;"> | |||
<div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><span class="crayon-title"></span> | |||
<div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><div class="crayon-button urvanov-syntax-highlighter-nums-button" title="Toggle Line Numbers"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-plain-button" title="Toggle Plain Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-wrap-button" title="Toggle Line Wrap"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-expand-button" title="Expand Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-copy-button" title="Copy"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-popup-button" title="Open Code In New Window"><div class="urvanov-syntax-highlighter-button-icon"></div></div></div></div> | |||
<div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;"></div> | |||
<div class="urvanov-syntax-highlighter-plain-wrap"><textarea wrap="soft" class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;"> | |||
y = (x - mean) / standard_deviation | |||
y = (20.7 - 10) / 5 | |||
y = (10.7) / 5 | |||
y = 2.14</textarea></div> | |||
<div class="urvanov-syntax-highlighter-main" style=""> | |||
<table class="crayon-table"> | |||
<tr class="urvanov-syntax-highlighter-row"> | |||
<td class="crayon-nums " data-settings="show"> | |||
<div class="urvanov-syntax-highlighter-nums-content" style="font-size: 12px !important; line-height: 15px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d2f367850592-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d2f367850592-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d2f367850592-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d2f367850592-4">4</div></div> | |||
</td> | |||
<td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d2f367850592-1">y = (x - mean) / standard_deviation</div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d2f367850592-2">y = (20.7 - 10) / 5</div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d2f367850592-3">y = (10.7) / 5</div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d2f367850592-4">y = 2.14</div></div></td> | |||
</tr> | |||
</table> | |||
</div> | |||
</div><p>The mean and standard deviation estimates of a dataset can be more robust to new data than the minimum and maximum.</p> | |||
<p>You can standardize your dataset using the scikit-learn object <a href="http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.StandardScaler.html">StandardScaler</a>.</p> | |||
<div id="urvanov-syntax-highlighter-613eee9a17d30972600390" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;"> | |||
<div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><span class="crayon-title"></span> | |||
<div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><div class="crayon-button urvanov-syntax-highlighter-nums-button" title="Toggle Line Numbers"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-plain-button" title="Toggle Plain Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-wrap-button" title="Toggle Line Wrap"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-expand-button" title="Expand Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-copy-button" title="Copy"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-popup-button" title="Open Code In New Window"><div class="urvanov-syntax-highlighter-button-icon"></div></div></div></div> | |||
<div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;"></div> | |||
<div class="urvanov-syntax-highlighter-plain-wrap"><textarea wrap="soft" class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;"> | |||
# demonstrate data standardization with sklearn | |||
from sklearn.preprocessing import StandardScaler | |||
# load data | |||
data = ... | |||
# create scaler | |||
scaler = StandardScaler() | |||
# fit scaler on data | |||
scaler.fit(data) | |||
# apply transform | |||
standardized = scaler.transform(data) | |||
# inverse transform | |||
inverse = scaler.inverse_transform(standardized)</textarea></div> | |||
<div class="urvanov-syntax-highlighter-main" style=""> | |||
<table class="crayon-table"> | |||
<tr class="urvanov-syntax-highlighter-row"> | |||
<td class="crayon-nums " data-settings="show"> | |||
<div class="urvanov-syntax-highlighter-nums-content" style="font-size: 12px !important; line-height: 15px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d30972600390-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d30972600390-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d30972600390-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d30972600390-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d30972600390-5">5</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d30972600390-6">6</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d30972600390-7">7</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d30972600390-8">8</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d30972600390-9">9</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d30972600390-10">10</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d30972600390-11">11</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d30972600390-12">12</div></div> | |||
</td> | |||
<td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d30972600390-1"><span class="crayon-p"># demonstrate data standardization with sklearn</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d30972600390-2"><span class="crayon-e">from </span><span class="crayon-v">sklearn</span><span class="crayon-sy">.</span><span class="crayon-e">preprocessing </span><span class="crayon-e">import </span><span class="crayon-i">StandardScaler</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d30972600390-3"><span class="crayon-p"># load data</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d30972600390-4"><span class="crayon-v">data</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-sy">.</span><span class="crayon-sy">.</span><span class="crayon-sy">.</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d30972600390-5"><span class="crayon-p"># create scaler</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d30972600390-6"><span class="crayon-v">scaler</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">StandardScaler</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d30972600390-7"><span class="crayon-p"># fit scaler on data</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d30972600390-8"><span class="crayon-v">scaler</span><span class="crayon-sy">.</span><span class="crayon-e">fit</span><span class="crayon-sy">(</span><span class="crayon-v">data</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d30972600390-9"><span class="crayon-p"># apply transform</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d30972600390-10"><span class="crayon-v">standardized</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">scaler</span><span class="crayon-sy">.</span><span class="crayon-e">transform</span><span class="crayon-sy">(</span><span class="crayon-v">data</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d30972600390-11"><span class="crayon-p"># inverse transform</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d30972600390-12"><span class="crayon-v">inverse</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">scaler</span><span class="crayon-sy">.</span><span class="crayon-e">inverse_transform</span><span class="crayon-sy">(</span><span class="crayon-v">standardized</span><span class="crayon-sy">)</span></div></div></td> | |||
</tr> | |||
</table> | |||
</div> | |||
</div><p>You can also perform the fit and transform in a single step using the <em>fit_transform()</em> function; for example:</p> | |||
<div id="urvanov-syntax-highlighter-613eee9a17d31906409623" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;"> | |||
<div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><span class="crayon-title"></span> | |||
<div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><div class="crayon-button urvanov-syntax-highlighter-nums-button" title="Toggle Line Numbers"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-plain-button" title="Toggle Plain Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-wrap-button" title="Toggle Line Wrap"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-expand-button" title="Expand Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-copy-button" title="Copy"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-popup-button" title="Open Code In New Window"><div class="urvanov-syntax-highlighter-button-icon"></div></div></div></div> | |||
<div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;"></div> | |||
<div class="urvanov-syntax-highlighter-plain-wrap"><textarea wrap="soft" class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;"> | |||
# demonstrate data standardization with sklearn | |||
from sklearn.preprocessing import StandardScaler | |||
# load data | |||
data = ... | |||
# create scaler | |||
scaler = StandardScaler() | |||
# fit and transform in one step | |||
standardized = scaler.fit_transform(data) | |||
# inverse transform | |||
inverse = scaler.inverse_transform(standardized)</textarea></div> | |||
<div class="urvanov-syntax-highlighter-main" style=""> | |||
<table class="crayon-table"> | |||
<tr class="urvanov-syntax-highlighter-row"> | |||
<td class="crayon-nums " data-settings="show"> | |||
<div class="urvanov-syntax-highlighter-nums-content" style="font-size: 12px !important; line-height: 15px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d31906409623-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d31906409623-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d31906409623-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d31906409623-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d31906409623-5">5</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d31906409623-6">6</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d31906409623-7">7</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d31906409623-8">8</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d31906409623-9">9</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d31906409623-10">10</div></div> | |||
</td> | |||
<td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d31906409623-1"><span class="crayon-p"># demonstrate data standardization with sklearn</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d31906409623-2"><span class="crayon-e">from </span><span class="crayon-v">sklearn</span><span class="crayon-sy">.</span><span class="crayon-e">preprocessing </span><span class="crayon-e">import </span><span class="crayon-i">StandardScaler</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d31906409623-3"><span class="crayon-p"># load data</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d31906409623-4"><span class="crayon-v">data</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-sy">.</span><span class="crayon-sy">.</span><span class="crayon-sy">.</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d31906409623-5"><span class="crayon-p"># create scaler</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d31906409623-6"><span class="crayon-v">scaler</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">StandardScaler</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d31906409623-7"><span class="crayon-p"># fit and transform in one step</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d31906409623-8"><span class="crayon-v">standardized</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">scaler</span><span class="crayon-sy">.</span><span class="crayon-e">fit_transform</span><span class="crayon-sy">(</span><span class="crayon-v">data</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d31906409623-9"><span class="crayon-p"># inverse transform</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d31906409623-10"><span class="crayon-v">inverse</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">scaler</span><span class="crayon-sy">.</span><span class="crayon-e">inverse_transform</span><span class="crayon-sy">(</span><span class="crayon-v">standardized</span><span class="crayon-sy">)</span></div></div></td> | |||
</tr> | |||
</table> | |||
</div> | |||
</div><p></p> | |||
<h2>Regression Predictive Modeling Problem</h2> | |||
<p>A regression <a href="https://machinelearningmastery.com/gentle-introduction-to-predictive-modeling/">predictive modeling</a> problem involves predicting a real-valued quantity.</p> | |||
<p>We can use a standard regression problem generator provided by the scikit-learn library in the <a href="http://scikit-learn.org/stable/modules/generated/sklearn.datasets.make_regression.html">make_regression() function</a>. This function will generate examples from a simple regression problem with a given number of input variables, statistical noise, and other properties.</p> | |||
<p>We will use this function to define a problem that has 20 input features; 10 of the features will be meaningful and 10 will not be relevant. A total of 1,000 examples will be randomly generated. The <a href="https://machinelearningmastery.com/how-to-generate-random-numbers-in-python/">pseudorandom number generator</a> will be fixed to ensure that we get the same 1,000 examples each time the code is run.</p> | |||
<div id="urvanov-syntax-highlighter-613eee9a17d32688853948" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;"> | |||
<div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><span class="crayon-title"></span> | |||
<div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><div class="crayon-button urvanov-syntax-highlighter-nums-button" title="Toggle Line Numbers"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-plain-button" title="Toggle Plain Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-wrap-button" title="Toggle Line Wrap"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-expand-button" title="Expand Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-copy-button" title="Copy"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-popup-button" title="Open Code In New Window"><div class="urvanov-syntax-highlighter-button-icon"></div></div></div></div> | |||
<div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;"></div> | |||
<div class="urvanov-syntax-highlighter-plain-wrap"><textarea wrap="soft" class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;"> | |||
# generate regression dataset | |||
X, y = make_regression(n_samples=1000, n_features=20, noise=0.1, random_state=1)</textarea></div> | |||
<div class="urvanov-syntax-highlighter-main" style=""> | |||
<table class="crayon-table"> | |||
<tr class="urvanov-syntax-highlighter-row"> | |||
<td class="crayon-nums " data-settings="show"> | |||
<div class="urvanov-syntax-highlighter-nums-content" style="font-size: 12px !important; line-height: 15px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d32688853948-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d32688853948-2">2</div></div> | |||
</td> | |||
<td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d32688853948-1"><span class="crayon-p"># generate regression dataset</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d32688853948-2"><span class="crayon-v">X</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">y</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">make_regression</span><span class="crayon-sy">(</span><span class="crayon-v">n_samples</span><span class="crayon-o">=</span><span class="crayon-cn">1000</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">n_features</span><span class="crayon-o">=</span><span class="crayon-cn">20</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">noise</span><span class="crayon-o">=</span><span class="crayon-cn">0.1</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">random_state</span><span class="crayon-o">=</span><span class="crayon-cn">1</span><span class="crayon-sy">)</span></div></div></td> | |||
</tr> | |||
</table> | |||
</div> | |||
</div><p>Each input variable has a Gaussian distribution, as does the target variable.</p> | |||
<p>We can demonstrate this by creating histograms of some of the input variables and the output variable.</p> | |||
<div id="urvanov-syntax-highlighter-613eee9a17d33659884296" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;"> | |||
<div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><span class="crayon-title"></span> | |||
<div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><div class="crayon-button urvanov-syntax-highlighter-nums-button" title="Toggle Line Numbers"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-plain-button" title="Toggle Plain Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-wrap-button" title="Toggle Line Wrap"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-expand-button" title="Expand Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-copy-button" title="Copy"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-popup-button" title="Open Code In New Window"><div class="urvanov-syntax-highlighter-button-icon"></div></div></div></div> | |||
<div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;"></div> | |||
<div class="urvanov-syntax-highlighter-plain-wrap"><textarea wrap="soft" class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;"> | |||
# regression predictive modeling problem | |||
from sklearn.datasets import make_regression | |||
from matplotlib import pyplot | |||
# generate regression dataset | |||
X, y = make_regression(n_samples=1000, n_features=20, noise=0.1, random_state=1) | |||
# histograms of input variables | |||
pyplot.subplot(211) | |||
pyplot.hist(X[:, 0]) | |||
pyplot.subplot(212) | |||
pyplot.hist(X[:, 1]) | |||
pyplot.show() | |||
# histogram of target variable | |||
pyplot.hist(y) | |||
pyplot.show()</textarea></div> | |||
<div class="urvanov-syntax-highlighter-main" style=""> | |||
<table class="crayon-table"> | |||
<tr class="urvanov-syntax-highlighter-row"> | |||
<td class="crayon-nums " data-settings="show"> | |||
<div class="urvanov-syntax-highlighter-nums-content" style="font-size: 12px !important; line-height: 15px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d33659884296-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d33659884296-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d33659884296-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d33659884296-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d33659884296-5">5</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d33659884296-6">6</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d33659884296-7">7</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d33659884296-8">8</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d33659884296-9">9</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d33659884296-10">10</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d33659884296-11">11</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d33659884296-12">12</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d33659884296-13">13</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d33659884296-14">14</div></div> | |||
</td> | |||
<td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d33659884296-1"><span class="crayon-p"># regression predictive modeling problem</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d33659884296-2"><span class="crayon-e">from </span><span class="crayon-v">sklearn</span><span class="crayon-sy">.</span><span class="crayon-e">datasets </span><span class="crayon-e">import </span><span class="crayon-e">make_regression</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d33659884296-3"><span class="crayon-e">from </span><span class="crayon-e">matplotlib </span><span class="crayon-e">import </span><span class="crayon-i">pyplot</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d33659884296-4"><span class="crayon-p"># generate regression dataset</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d33659884296-5"><span class="crayon-v">X</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">y</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">make_regression</span><span class="crayon-sy">(</span><span class="crayon-v">n_samples</span><span class="crayon-o">=</span><span class="crayon-cn">1000</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">n_features</span><span class="crayon-o">=</span><span class="crayon-cn">20</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">noise</span><span class="crayon-o">=</span><span class="crayon-cn">0.1</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">random_state</span><span class="crayon-o">=</span><span class="crayon-cn">1</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d33659884296-6"><span class="crayon-p"># histograms of input variables</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d33659884296-7"><span class="crayon-v">pyplot</span><span class="crayon-sy">.</span><span class="crayon-e">subplot</span><span class="crayon-sy">(</span><span class="crayon-cn">211</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d33659884296-8"><span class="crayon-v">pyplot</span><span class="crayon-sy">.</span><span class="crayon-e">hist</span><span class="crayon-sy">(</span><span class="crayon-v">X</span><span class="crayon-sy">[</span><span class="crayon-o">:</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">0</span><span class="crayon-sy">]</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d33659884296-9"><span class="crayon-v">pyplot</span><span class="crayon-sy">.</span><span class="crayon-e">subplot</span><span class="crayon-sy">(</span><span class="crayon-cn">212</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d33659884296-10"><span class="crayon-v">pyplot</span><span class="crayon-sy">.</span><span class="crayon-e">hist</span><span class="crayon-sy">(</span><span class="crayon-v">X</span><span class="crayon-sy">[</span><span class="crayon-o">:</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">1</span><span class="crayon-sy">]</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d33659884296-11"><span class="crayon-v">pyplot</span><span class="crayon-sy">.</span><span class="crayon-e">show</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d33659884296-12"><span class="crayon-p"># histogram of target variable</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d33659884296-13"><span class="crayon-v">pyplot</span><span class="crayon-sy">.</span><span class="crayon-e">hist</span><span class="crayon-sy">(</span><span class="crayon-v">y</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d33659884296-14"><span class="crayon-v">pyplot</span><span class="crayon-sy">.</span><span class="crayon-e">show</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div></div></td> | |||
</tr> | |||
</table> | |||
</div> | |||
</div><p>Running the example creates two figures.</p> | |||
<p>The first shows histograms of the first two of the twenty input variables, showing that each has a Gaussian data distribution.</p> | |||
<div id="attachment_6940" style="width: 1290px" class="wp-caption aligncenter"><img aria-describedby="caption-attachment-6940" loading="lazy" class="size-full wp-image-6940" src="https://machinelearningmastery.com/wp-content/uploads/2018/11/Histograms-of-Two-of-the-Twenty-Input-Variables-for-the-Regression-Problem.png" alt="Histograms of Two of the Twenty Input Variables for the Regression Problem" width="1280" height="960" srcset="https://machinelearningmastery.com/wp-content/uploads/2018/11/Histograms-of-Two-of-the-Twenty-Input-Variables-for-the-Regression-Problem.png 1280w, https://machinelearningmastery.com/wp-content/uploads/2018/11/Histograms-of-Two-of-the-Twenty-Input-Variables-for-the-Regression-Problem-300x225.png 300w, https://machinelearningmastery.com/wp-content/uploads/2018/11/Histograms-of-Two-of-the-Twenty-Input-Variables-for-the-Regression-Problem-768x576.png 768w, https://machinelearningmastery.com/wp-content/uploads/2018/11/Histograms-of-Two-of-the-Twenty-Input-Variables-for-the-Regression-Problem-1024x768.png 1024w" sizes="(max-width: 1280px) 100vw, 1280px" /><p id="caption-attachment-6940" class="wp-caption-text">Histograms of Two of the Twenty Input Variables for the Regression Problem</p></div> | |||
<p>The second figure shows a histogram of the target variable, showing a much larger range for the variable as compared to the input variables and, again, a Gaussian data distribution.</p> | |||
<div id="attachment_6941" style="width: 1290px" class="wp-caption aligncenter"><img aria-describedby="caption-attachment-6941" loading="lazy" class="size-full wp-image-6941" src="https://machinelearningmastery.com/wp-content/uploads/2018/11/Histogram-of-the-Target-Variable-for-the-Regression-Problem.png" alt="Histogram of the Target Variable for the Regression Problem" width="1280" height="960" srcset="https://machinelearningmastery.com/wp-content/uploads/2018/11/Histogram-of-the-Target-Variable-for-the-Regression-Problem.png 1280w, https://machinelearningmastery.com/wp-content/uploads/2018/11/Histogram-of-the-Target-Variable-for-the-Regression-Problem-300x225.png 300w, https://machinelearningmastery.com/wp-content/uploads/2018/11/Histogram-of-the-Target-Variable-for-the-Regression-Problem-768x576.png 768w, https://machinelearningmastery.com/wp-content/uploads/2018/11/Histogram-of-the-Target-Variable-for-the-Regression-Problem-1024x768.png 1024w" sizes="(max-width: 1280px) 100vw, 1280px" /><p id="caption-attachment-6941" class="wp-caption-text">Histogram of the Target Variable for the Regression Problem</p></div> | |||
<p>Now that we have a regression problem that we can use as the basis for the investigation, we can develop a model to address it.</p> | |||
<h2>Multilayer Perceptron With Unscaled Data</h2> | |||
<p>We can develop a Multilayer Perceptron (MLP) model for the regression problem.</p> | |||
<p>A model will be demonstrated on the raw data, without any scaling of the input or output variables. We expect that model performance will be generally poor.</p> | |||
<p>The first step is to split the data into train and test sets so that we can fit and evaluate a model. We will generate 1,000 examples from the domain and split the dataset in half, using 500 examples for the train and test datasets.</p> | |||
<div id="urvanov-syntax-highlighter-613eee9a17d34863395121" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;"> | |||
<div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><span class="crayon-title"></span> | |||
<div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><div class="crayon-button urvanov-syntax-highlighter-nums-button" title="Toggle Line Numbers"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-plain-button" title="Toggle Plain Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-wrap-button" title="Toggle Line Wrap"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-expand-button" title="Expand Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-copy-button" title="Copy"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-popup-button" title="Open Code In New Window"><div class="urvanov-syntax-highlighter-button-icon"></div></div></div></div> | |||
<div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;"></div> | |||
<div class="urvanov-syntax-highlighter-plain-wrap"><textarea wrap="soft" class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;"> | |||
# split into train and test | |||
n_train = 500 | |||
trainX, testX = X[:n_train, :], X[n_train:, :] | |||
trainy, testy = y[:n_train], y[n_train:]</textarea></div> | |||
<div class="urvanov-syntax-highlighter-main" style=""> | |||
<table class="crayon-table"> | |||
<tr class="urvanov-syntax-highlighter-row"> | |||
<td class="crayon-nums " data-settings="show"> | |||
<div class="urvanov-syntax-highlighter-nums-content" style="font-size: 12px !important; line-height: 15px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d34863395121-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d34863395121-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d34863395121-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d34863395121-4">4</div></div> | |||
</td> | |||
<td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d34863395121-1"><span class="crayon-p"># split into train and test</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d34863395121-2"><span class="crayon-v">n_train</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-cn">500</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d34863395121-3"><span class="crayon-v">trainX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testX</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">X</span><span class="crayon-sy">[</span><span class="crayon-o">:</span><span class="crayon-v">n_train</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-o">:</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">X</span><span class="crayon-sy">[</span><span class="crayon-v">n_train</span><span class="crayon-o">:</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-o">:</span><span class="crayon-sy">]</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d34863395121-4"><span class="crayon-v">trainy</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testy</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">y</span><span class="crayon-sy">[</span><span class="crayon-o">:</span><span class="crayon-v">n_train</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">y</span><span class="crayon-sy">[</span><span class="crayon-v">n_train</span><span class="crayon-o">:</span><span class="crayon-sy">]</span></div></div></td> | |||
</tr> | |||
</table> | |||
</div> | |||
</div><p>Next, we can define an MLP model. The model will expect 20 inputs in the 20 input variables in the problem.</p> | |||
<p>A single hidden layer will be used with 25 nodes and a rectified linear activation function. The output layer has one node for the single target variable and a linear activation function to predict real values directly.</p> | |||
<div id="urvanov-syntax-highlighter-613eee9a17d35043962436" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;"> | |||
<div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><span class="crayon-title"></span> | |||
<div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><div class="crayon-button urvanov-syntax-highlighter-nums-button" title="Toggle Line Numbers"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-plain-button" title="Toggle Plain Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-wrap-button" title="Toggle Line Wrap"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-expand-button" title="Expand Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-copy-button" title="Copy"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-popup-button" title="Open Code In New Window"><div class="urvanov-syntax-highlighter-button-icon"></div></div></div></div> | |||
<div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;"></div> | |||
<div class="urvanov-syntax-highlighter-plain-wrap"><textarea wrap="soft" class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;"> | |||
# define model | |||
model = Sequential() | |||
model.add(Dense(25, input_dim=20, activation='relu', kernel_initializer='he_uniform')) | |||
model.add(Dense(1, activation='linear'))</textarea></div> | |||
<div class="urvanov-syntax-highlighter-main" style=""> | |||
<table class="crayon-table"> | |||
<tr class="urvanov-syntax-highlighter-row"> | |||
<td class="crayon-nums " data-settings="show"> | |||
<div class="urvanov-syntax-highlighter-nums-content" style="font-size: 12px !important; line-height: 15px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d35043962436-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d35043962436-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d35043962436-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d35043962436-4">4</div></div> | |||
</td> | |||
<td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d35043962436-1"><span class="crayon-p"># define model</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d35043962436-2"><span class="crayon-v">model</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">Sequential</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d35043962436-3"><span class="crayon-v">model</span><span class="crayon-sy">.</span><span class="crayon-e">add</span><span class="crayon-sy">(</span><span class="crayon-e">Dense</span><span class="crayon-sy">(</span><span class="crayon-cn">25</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">input_dim</span><span class="crayon-o">=</span><span class="crayon-cn">20</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">activation</span><span class="crayon-o">=</span><span class="crayon-s">'relu'</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">kernel_initializer</span><span class="crayon-o">=</span><span class="crayon-s">'he_uniform'</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d35043962436-4"><span class="crayon-v">model</span><span class="crayon-sy">.</span><span class="crayon-e">add</span><span class="crayon-sy">(</span><span class="crayon-e">Dense</span><span class="crayon-sy">(</span><span class="crayon-cn">1</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">activation</span><span class="crayon-o">=</span><span class="crayon-s">'linear'</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div></div></td> | |||
</tr> | |||
</table> | |||
</div> | |||
</div><p>The mean squared error loss function will be used to optimize the model and the stochastic gradient descent optimization algorithm will be used with the sensible default configuration of a learning rate of 0.01 and a momentum of 0.9.</p> | |||
<div id="urvanov-syntax-highlighter-613eee9a17d36526581245" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;"> | |||
<div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><span class="crayon-title"></span> | |||
<div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><div class="crayon-button urvanov-syntax-highlighter-nums-button" title="Toggle Line Numbers"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-plain-button" title="Toggle Plain Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-wrap-button" title="Toggle Line Wrap"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-expand-button" title="Expand Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-copy-button" title="Copy"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-popup-button" title="Open Code In New Window"><div class="urvanov-syntax-highlighter-button-icon"></div></div></div></div> | |||
<div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;"></div> | |||
<div class="urvanov-syntax-highlighter-plain-wrap"><textarea wrap="soft" class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;"> | |||
# compile model | |||
model.compile(loss='mean_squared_error', optimizer=SGD(lr=0.01, momentum=0.9))</textarea></div> | |||
<div class="urvanov-syntax-highlighter-main" style=""> | |||
<table class="crayon-table"> | |||
<tr class="urvanov-syntax-highlighter-row"> | |||
<td class="crayon-nums " data-settings="show"> | |||
<div class="urvanov-syntax-highlighter-nums-content" style="font-size: 12px !important; line-height: 15px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d36526581245-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d36526581245-2">2</div></div> | |||
</td> | |||
<td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d36526581245-1"><span class="crayon-p"># compile model</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d36526581245-2"><span class="crayon-v">model</span><span class="crayon-sy">.</span><span class="crayon-e">compile</span><span class="crayon-sy">(</span><span class="crayon-v">loss</span><span class="crayon-o">=</span><span class="crayon-s">'mean_squared_error'</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">optimizer</span><span class="crayon-o">=</span><span class="crayon-e">SGD</span><span class="crayon-sy">(</span><span class="crayon-v">lr</span><span class="crayon-o">=</span><span class="crayon-cn">0.01</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">momentum</span><span class="crayon-o">=</span><span class="crayon-cn">0.9</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div></div></td> | |||
</tr> | |||
</table> | |||
</div> | |||
</div><p>The model will be fit for 100 training epochs and the test set will be used as a validation set, evaluated at the end of each training epoch.</p> | |||
<p>The mean squared error is calculated on the train and test datasets at the end of training to get an idea of how well the model learned the problem.</p> | |||
<div id="urvanov-syntax-highlighter-613eee9a17d37187742241" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;"> | |||
<div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><span class="crayon-title"></span> | |||
<div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><div class="crayon-button urvanov-syntax-highlighter-nums-button" title="Toggle Line Numbers"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-plain-button" title="Toggle Plain Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-wrap-button" title="Toggle Line Wrap"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-expand-button" title="Expand Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-copy-button" title="Copy"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-popup-button" title="Open Code In New Window"><div class="urvanov-syntax-highlighter-button-icon"></div></div></div></div> | |||
<div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;"></div> | |||
<div class="urvanov-syntax-highlighter-plain-wrap"><textarea wrap="soft" class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;"> | |||
# evaluate the model | |||
train_mse = model.evaluate(trainX, trainy, verbose=0) | |||
test_mse = model.evaluate(testX, testy, verbose=0)</textarea></div> | |||
<div class="urvanov-syntax-highlighter-main" style=""> | |||
<table class="crayon-table"> | |||
<tr class="urvanov-syntax-highlighter-row"> | |||
<td class="crayon-nums " data-settings="show"> | |||
<div class="urvanov-syntax-highlighter-nums-content" style="font-size: 12px !important; line-height: 15px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d37187742241-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d37187742241-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d37187742241-3">3</div></div> | |||
</td> | |||
<td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d37187742241-1"><span class="crayon-p"># evaluate the model</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d37187742241-2"><span class="crayon-v">train_mse</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">model</span><span class="crayon-sy">.</span><span class="crayon-e">evaluate</span><span class="crayon-sy">(</span><span class="crayon-v">trainX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">trainy</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">verbose</span><span class="crayon-o">=</span><span class="crayon-cn">0</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d37187742241-3"><span class="crayon-v">test_mse</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">model</span><span class="crayon-sy">.</span><span class="crayon-e">evaluate</span><span class="crayon-sy">(</span><span class="crayon-v">testX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testy</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">verbose</span><span class="crayon-o">=</span><span class="crayon-cn">0</span><span class="crayon-sy">)</span></div></div></td> | |||
</tr> | |||
</table> | |||
</div> | |||
</div><p>Finally, learning curves of mean squared error on the train and test sets at the end of each training epoch are graphed using line plots, providing learning curves to get an idea of the dynamics of the model while learning the problem.</p> | |||
<div id="urvanov-syntax-highlighter-613eee9a17d38267758975" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;"> | |||
<div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><span class="crayon-title"></span> | |||
<div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><div class="crayon-button urvanov-syntax-highlighter-nums-button" title="Toggle Line Numbers"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-plain-button" title="Toggle Plain Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-wrap-button" title="Toggle Line Wrap"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-expand-button" title="Expand Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-copy-button" title="Copy"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-popup-button" title="Open Code In New Window"><div class="urvanov-syntax-highlighter-button-icon"></div></div></div></div> | |||
<div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;"></div> | |||
<div class="urvanov-syntax-highlighter-plain-wrap"><textarea wrap="soft" class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;"> | |||
# plot loss during training | |||
pyplot.title('Mean Squared Error') | |||
pyplot.plot(history.history['loss'], label='train') | |||
pyplot.plot(history.history['val_loss'], label='test') | |||
pyplot.legend() | |||
pyplot.show()</textarea></div> | |||
<div class="urvanov-syntax-highlighter-main" style=""> | |||
<table class="crayon-table"> | |||
<tr class="urvanov-syntax-highlighter-row"> | |||
<td class="crayon-nums " data-settings="show"> | |||
<div class="urvanov-syntax-highlighter-nums-content" style="font-size: 12px !important; line-height: 15px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d38267758975-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d38267758975-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d38267758975-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d38267758975-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d38267758975-5">5</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d38267758975-6">6</div></div> | |||
</td> | |||
<td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d38267758975-1"><span class="crayon-p"># plot loss during training</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d38267758975-2"><span class="crayon-v">pyplot</span><span class="crayon-sy">.</span><span class="crayon-e">title</span><span class="crayon-sy">(</span><span class="crayon-s">'Mean Squared Error'</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d38267758975-3"><span class="crayon-v">pyplot</span><span class="crayon-sy">.</span><span class="crayon-e">plot</span><span class="crayon-sy">(</span><span class="crayon-v">history</span><span class="crayon-sy">.</span><span class="crayon-v">history</span><span class="crayon-sy">[</span><span class="crayon-s">'loss'</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">label</span><span class="crayon-o">=</span><span class="crayon-s">'train'</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d38267758975-4"><span class="crayon-v">pyplot</span><span class="crayon-sy">.</span><span class="crayon-e">plot</span><span class="crayon-sy">(</span><span class="crayon-v">history</span><span class="crayon-sy">.</span><span class="crayon-v">history</span><span class="crayon-sy">[</span><span class="crayon-s">'val_loss'</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">label</span><span class="crayon-o">=</span><span class="crayon-s">'test'</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d38267758975-5"><span class="crayon-v">pyplot</span><span class="crayon-sy">.</span><span class="crayon-e">legend</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d38267758975-6"><span class="crayon-v">pyplot</span><span class="crayon-sy">.</span><span class="crayon-e">show</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div></div></td> | |||
</tr> | |||
</table> | |||
</div> | |||
</div><p>Tying these elements together, the complete example is listed below.</p> | |||
<div id="urvanov-syntax-highlighter-613eee9a17d39344103212" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;"> | |||
<div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><span class="crayon-title"></span> | |||
<div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><div class="crayon-button urvanov-syntax-highlighter-nums-button" title="Toggle Line Numbers"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-plain-button" title="Toggle Plain Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-wrap-button" title="Toggle Line Wrap"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-expand-button" title="Expand Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-copy-button" title="Copy"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-popup-button" title="Open Code In New Window"><div class="urvanov-syntax-highlighter-button-icon"></div></div></div></div> | |||
<div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;"></div> | |||
<div class="urvanov-syntax-highlighter-plain-wrap"><textarea wrap="soft" class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;"> | |||
# mlp with unscaled data for the regression problem | |||
from sklearn.datasets import make_regression | |||
from keras.layers import Dense | |||
from keras.models import Sequential | |||
from keras.optimizers import SGD | |||
from matplotlib import pyplot | |||
# generate regression dataset | |||
X, y = make_regression(n_samples=1000, n_features=20, noise=0.1, random_state=1) | |||
# split into train and test | |||
n_train = 500 | |||
trainX, testX = X[:n_train, :], X[n_train:, :] | |||
trainy, testy = y[:n_train], y[n_train:] | |||
# define model | |||
model = Sequential() | |||
model.add(Dense(25, input_dim=20, activation='relu', kernel_initializer='he_uniform')) | |||
model.add(Dense(1, activation='linear')) | |||
# compile model | |||
model.compile(loss='mean_squared_error', optimizer=SGD(lr=0.01, momentum=0.9)) | |||
# fit model | |||
history = model.fit(trainX, trainy, validation_data=(testX, testy), epochs=100, verbose=0) | |||
# evaluate the model | |||
train_mse = model.evaluate(trainX, trainy, verbose=0) | |||
test_mse = model.evaluate(testX, testy, verbose=0) | |||
print('Train: %.3f, Test: %.3f' % (train_mse, test_mse)) | |||
# plot loss during training | |||
pyplot.title('Mean Squared Error') | |||
pyplot.plot(history.history['loss'], label='train') | |||
pyplot.plot(history.history['val_loss'], label='test') | |||
pyplot.legend() | |||
pyplot.show()</textarea></div> | |||
<div class="urvanov-syntax-highlighter-main" style=""> | |||
<table class="crayon-table"> | |||
<tr class="urvanov-syntax-highlighter-row"> | |||
<td class="crayon-nums " data-settings="show"> | |||
<div class="urvanov-syntax-highlighter-nums-content" style="font-size: 12px !important; line-height: 15px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d39344103212-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d39344103212-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d39344103212-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d39344103212-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d39344103212-5">5</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d39344103212-6">6</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d39344103212-7">7</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d39344103212-8">8</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d39344103212-9">9</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d39344103212-10">10</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d39344103212-11">11</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d39344103212-12">12</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d39344103212-13">13</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d39344103212-14">14</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d39344103212-15">15</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d39344103212-16">16</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d39344103212-17">17</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d39344103212-18">18</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d39344103212-19">19</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d39344103212-20">20</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d39344103212-21">21</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d39344103212-22">22</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d39344103212-23">23</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d39344103212-24">24</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d39344103212-25">25</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d39344103212-26">26</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d39344103212-27">27</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d39344103212-28">28</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d39344103212-29">29</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d39344103212-30">30</div></div> | |||
</td> | |||
<td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d39344103212-1"><span class="crayon-p"># mlp with unscaled data for the regression problem</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d39344103212-2"><span class="crayon-e">from </span><span class="crayon-v">sklearn</span><span class="crayon-sy">.</span><span class="crayon-e">datasets </span><span class="crayon-e">import </span><span class="crayon-e">make_regression</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d39344103212-3"><span class="crayon-e">from </span><span class="crayon-v">keras</span><span class="crayon-sy">.</span><span class="crayon-e">layers </span><span class="crayon-e">import </span><span class="crayon-e">Dense</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d39344103212-4"><span class="crayon-e">from </span><span class="crayon-v">keras</span><span class="crayon-sy">.</span><span class="crayon-e">models </span><span class="crayon-e">import </span><span class="crayon-e">Sequential</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d39344103212-5"><span class="crayon-e">from </span><span class="crayon-v">keras</span><span class="crayon-sy">.</span><span class="crayon-e">optimizers </span><span class="crayon-e">import </span><span class="crayon-e">SGD</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d39344103212-6"><span class="crayon-e">from </span><span class="crayon-e">matplotlib </span><span class="crayon-e">import </span><span class="crayon-i">pyplot</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d39344103212-7"><span class="crayon-p"># generate regression dataset</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d39344103212-8"><span class="crayon-v">X</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">y</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">make_regression</span><span class="crayon-sy">(</span><span class="crayon-v">n_samples</span><span class="crayon-o">=</span><span class="crayon-cn">1000</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">n_features</span><span class="crayon-o">=</span><span class="crayon-cn">20</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">noise</span><span class="crayon-o">=</span><span class="crayon-cn">0.1</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">random_state</span><span class="crayon-o">=</span><span class="crayon-cn">1</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d39344103212-9"><span class="crayon-p"># split into train and test</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d39344103212-10"><span class="crayon-v">n_train</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-cn">500</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d39344103212-11"><span class="crayon-v">trainX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testX</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">X</span><span class="crayon-sy">[</span><span class="crayon-o">:</span><span class="crayon-v">n_train</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-o">:</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">X</span><span class="crayon-sy">[</span><span class="crayon-v">n_train</span><span class="crayon-o">:</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-o">:</span><span class="crayon-sy">]</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d39344103212-12"><span class="crayon-v">trainy</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testy</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">y</span><span class="crayon-sy">[</span><span class="crayon-o">:</span><span class="crayon-v">n_train</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">y</span><span class="crayon-sy">[</span><span class="crayon-v">n_train</span><span class="crayon-o">:</span><span class="crayon-sy">]</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d39344103212-13"><span class="crayon-p"># define model</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d39344103212-14"><span class="crayon-v">model</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">Sequential</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d39344103212-15"><span class="crayon-v">model</span><span class="crayon-sy">.</span><span class="crayon-e">add</span><span class="crayon-sy">(</span><span class="crayon-e">Dense</span><span class="crayon-sy">(</span><span class="crayon-cn">25</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">input_dim</span><span class="crayon-o">=</span><span class="crayon-cn">20</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">activation</span><span class="crayon-o">=</span><span class="crayon-s">'relu'</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">kernel_initializer</span><span class="crayon-o">=</span><span class="crayon-s">'he_uniform'</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d39344103212-16"><span class="crayon-v">model</span><span class="crayon-sy">.</span><span class="crayon-e">add</span><span class="crayon-sy">(</span><span class="crayon-e">Dense</span><span class="crayon-sy">(</span><span class="crayon-cn">1</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">activation</span><span class="crayon-o">=</span><span class="crayon-s">'linear'</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d39344103212-17"><span class="crayon-p"># compile model</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d39344103212-18"><span class="crayon-v">model</span><span class="crayon-sy">.</span><span class="crayon-e">compile</span><span class="crayon-sy">(</span><span class="crayon-v">loss</span><span class="crayon-o">=</span><span class="crayon-s">'mean_squared_error'</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">optimizer</span><span class="crayon-o">=</span><span class="crayon-e">SGD</span><span class="crayon-sy">(</span><span class="crayon-v">lr</span><span class="crayon-o">=</span><span class="crayon-cn">0.01</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">momentum</span><span class="crayon-o">=</span><span class="crayon-cn">0.9</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d39344103212-19"><span class="crayon-p"># fit model</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d39344103212-20"><span class="crayon-v">history</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">model</span><span class="crayon-sy">.</span><span class="crayon-e">fit</span><span class="crayon-sy">(</span><span class="crayon-v">trainX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">trainy</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">validation_data</span><span class="crayon-o">=</span><span class="crayon-sy">(</span><span class="crayon-v">testX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testy</span><span class="crayon-sy">)</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">epochs</span><span class="crayon-o">=</span><span class="crayon-cn">100</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">verbose</span><span class="crayon-o">=</span><span class="crayon-cn">0</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d39344103212-21"><span class="crayon-p"># evaluate the model</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d39344103212-22"><span class="crayon-v">train_mse</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">model</span><span class="crayon-sy">.</span><span class="crayon-e">evaluate</span><span class="crayon-sy">(</span><span class="crayon-v">trainX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">trainy</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">verbose</span><span class="crayon-o">=</span><span class="crayon-cn">0</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d39344103212-23"><span class="crayon-v">test_mse</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">model</span><span class="crayon-sy">.</span><span class="crayon-e">evaluate</span><span class="crayon-sy">(</span><span class="crayon-v">testX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testy</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">verbose</span><span class="crayon-o">=</span><span class="crayon-cn">0</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d39344103212-24"><span class="crayon-e">print</span><span class="crayon-sy">(</span><span class="crayon-s">'Train: %.3f, Test: %.3f'</span><span class="crayon-h"> </span><span class="crayon-o">%</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-v">train_mse</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">test_mse</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d39344103212-25"><span class="crayon-p"># plot loss during training</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d39344103212-26"><span class="crayon-v">pyplot</span><span class="crayon-sy">.</span><span class="crayon-e">title</span><span class="crayon-sy">(</span><span class="crayon-s">'Mean Squared Error'</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d39344103212-27"><span class="crayon-v">pyplot</span><span class="crayon-sy">.</span><span class="crayon-e">plot</span><span class="crayon-sy">(</span><span class="crayon-v">history</span><span class="crayon-sy">.</span><span class="crayon-v">history</span><span class="crayon-sy">[</span><span class="crayon-s">'loss'</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">label</span><span class="crayon-o">=</span><span class="crayon-s">'train'</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d39344103212-28"><span class="crayon-v">pyplot</span><span class="crayon-sy">.</span><span class="crayon-e">plot</span><span class="crayon-sy">(</span><span class="crayon-v">history</span><span class="crayon-sy">.</span><span class="crayon-v">history</span><span class="crayon-sy">[</span><span class="crayon-s">'val_loss'</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">label</span><span class="crayon-o">=</span><span class="crayon-s">'test'</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d39344103212-29"><span class="crayon-v">pyplot</span><span class="crayon-sy">.</span><span class="crayon-e">legend</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d39344103212-30"><span class="crayon-v">pyplot</span><span class="crayon-sy">.</span><span class="crayon-e">show</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div></div></td> | |||
</tr> | |||
</table> | |||
</div> | |||
</div><p>Running the example fits the model and calculates the mean squared error on the train and test sets.</p> | |||
<p>In this case, the model is unable to learn the problem, resulting in predictions of NaN values. The <a href="https://machinelearningmastery.com/exploding-gradients-in-neural-networks/">model weights exploded</a> during training given the very large errors and, in turn, error gradients calculated for weight updates.</p> | |||
<div id="urvanov-syntax-highlighter-613eee9a17d3a430605391" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;"> | |||
<div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><span class="crayon-title"></span> | |||
<div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><div class="crayon-button urvanov-syntax-highlighter-nums-button" title="Toggle Line Numbers"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-plain-button" title="Toggle Plain Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-wrap-button" title="Toggle Line Wrap"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-expand-button" title="Expand Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-copy-button" title="Copy"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-popup-button" title="Open Code In New Window"><div class="urvanov-syntax-highlighter-button-icon"></div></div></div></div> | |||
<div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;"></div> | |||
<div class="urvanov-syntax-highlighter-plain-wrap"><textarea wrap="soft" class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;"> | |||
Train: nan, Test: nan</textarea></div> | |||
<div class="urvanov-syntax-highlighter-main" style=""> | |||
<table class="crayon-table"> | |||
<tr class="urvanov-syntax-highlighter-row"> | |||
<td class="crayon-nums " data-settings="show"> | |||
<div class="urvanov-syntax-highlighter-nums-content" style="font-size: 12px !important; line-height: 15px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3a430605391-1">1</div></div> | |||
</td> | |||
<td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3a430605391-1">Train: nan, Test: nan</div></div></td> | |||
</tr> | |||
</table> | |||
</div> | |||
</div><p>This demonstrates that, at the very least, some data scaling is required for the target variable.</p> | |||
<p>A line plot of training history is created but does not show anything as the model almost immediately results in a NaN mean squared error.</p> | |||
<h2>Multilayer Perceptron With Scaled Output Variables</h2> | |||
<p>The MLP model can be updated to scale the target variable.</p> | |||
<p>Reducing the scale of the target variable will, in turn, reduce the size of the gradient used to update the weights and result in a more stable model and training process.</p> | |||
<p>Given the Gaussian distribution of the target variable, a natural method for rescaling the variable would be to standardize the variable. This requires estimating the mean and standard deviation of the variable and using these estimates to perform the rescaling.</p> | |||
<p>It is best practice is to estimate the mean and standard deviation of the training dataset and use these variables to scale the train and test dataset. This is to avoid any data leakage during the model evaluation process.</p> | |||
<p>The scikit-learn transformers expect input data to be matrices of rows and columns, therefore the 1D arrays for the target variable will have to be reshaped into 2D arrays prior to the transforms.</p> | |||
<div id="urvanov-syntax-highlighter-613eee9a17d3b297839347" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;"> | |||
<div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><span class="crayon-title"></span> | |||
<div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><div class="crayon-button urvanov-syntax-highlighter-nums-button" title="Toggle Line Numbers"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-plain-button" title="Toggle Plain Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-wrap-button" title="Toggle Line Wrap"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-expand-button" title="Expand Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-copy-button" title="Copy"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-popup-button" title="Open Code In New Window"><div class="urvanov-syntax-highlighter-button-icon"></div></div></div></div> | |||
<div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;"></div> | |||
<div class="urvanov-syntax-highlighter-plain-wrap"><textarea wrap="soft" class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;"> | |||
# reshape 1d arrays to 2d arrays | |||
trainy = trainy.reshape(len(trainy), 1) | |||
testy = testy.reshape(len(trainy), 1)</textarea></div> | |||
<div class="urvanov-syntax-highlighter-main" style=""> | |||
<table class="crayon-table"> | |||
<tr class="urvanov-syntax-highlighter-row"> | |||
<td class="crayon-nums " data-settings="show"> | |||
<div class="urvanov-syntax-highlighter-nums-content" style="font-size: 12px !important; line-height: 15px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3b297839347-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3b297839347-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3b297839347-3">3</div></div> | |||
</td> | |||
<td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3b297839347-1"><span class="crayon-p"># reshape 1d arrays to 2d arrays</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3b297839347-2"><span class="crayon-v">trainy</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">trainy</span><span class="crayon-sy">.</span><span class="crayon-e">reshape</span><span class="crayon-sy">(</span><span class="crayon-e">len</span><span class="crayon-sy">(</span><span class="crayon-v">trainy</span><span class="crayon-sy">)</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">1</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3b297839347-3"><span class="crayon-v">testy</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">testy</span><span class="crayon-sy">.</span><span class="crayon-e">reshape</span><span class="crayon-sy">(</span><span class="crayon-e">len</span><span class="crayon-sy">(</span><span class="crayon-v">trainy</span><span class="crayon-sy">)</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">1</span><span class="crayon-sy">)</span></div></div></td> | |||
</tr> | |||
</table> | |||
</div> | |||
</div><p>We can then create and apply the <em>StandardScaler</em> to rescale the target variable.</p> | |||
<div id="urvanov-syntax-highlighter-613eee9a17d3c947284175" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;"> | |||
<div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><span class="crayon-title"></span> | |||
<div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><div class="crayon-button urvanov-syntax-highlighter-nums-button" title="Toggle Line Numbers"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-plain-button" title="Toggle Plain Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-wrap-button" title="Toggle Line Wrap"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-expand-button" title="Expand Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-copy-button" title="Copy"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-popup-button" title="Open Code In New Window"><div class="urvanov-syntax-highlighter-button-icon"></div></div></div></div> | |||
<div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;"></div> | |||
<div class="urvanov-syntax-highlighter-plain-wrap"><textarea wrap="soft" class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;"> | |||
# created scaler | |||
scaler = StandardScaler() | |||
# fit scaler on training dataset | |||
scaler.fit(trainy) | |||
# transform training dataset | |||
trainy = scaler.transform(trainy) | |||
# transform test dataset | |||
testy = scaler.transform(testy)</textarea></div> | |||
<div class="urvanov-syntax-highlighter-main" style=""> | |||
<table class="crayon-table"> | |||
<tr class="urvanov-syntax-highlighter-row"> | |||
<td class="crayon-nums " data-settings="show"> | |||
<div class="urvanov-syntax-highlighter-nums-content" style="font-size: 12px !important; line-height: 15px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3c947284175-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3c947284175-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3c947284175-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3c947284175-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3c947284175-5">5</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3c947284175-6">6</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3c947284175-7">7</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3c947284175-8">8</div></div> | |||
</td> | |||
<td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3c947284175-1"><span class="crayon-p"># created scaler</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3c947284175-2"><span class="crayon-v">scaler</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">StandardScaler</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3c947284175-3"><span class="crayon-p"># fit scaler on training dataset</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3c947284175-4"><span class="crayon-v">scaler</span><span class="crayon-sy">.</span><span class="crayon-e">fit</span><span class="crayon-sy">(</span><span class="crayon-v">trainy</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3c947284175-5"><span class="crayon-p"># transform training dataset</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3c947284175-6"><span class="crayon-v">trainy</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">scaler</span><span class="crayon-sy">.</span><span class="crayon-e">transform</span><span class="crayon-sy">(</span><span class="crayon-v">trainy</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3c947284175-7"><span class="crayon-p"># transform test dataset</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3c947284175-8"><span class="crayon-v">testy</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">scaler</span><span class="crayon-sy">.</span><span class="crayon-e">transform</span><span class="crayon-sy">(</span><span class="crayon-v">testy</span><span class="crayon-sy">)</span></div></div></td> | |||
</tr> | |||
</table> | |||
</div> | |||
</div><p>Rescaling the target variable means that estimating the performance of the model and plotting the learning curves will calculate an MSE in squared units of the scaled variable rather than squared units of the original scale. This can make interpreting the error within the context of the domain challenging.</p> | |||
<p>In practice, it may be helpful to estimate the performance of the model by first inverting the transform on the test dataset target variable and on the model predictions and estimating model performance using the root mean squared error on the unscaled data. This is left as an exercise to the reader.</p> | |||
<p>The complete example of standardizing the target variable for the MLP on the regression problem is listed below.</p> | |||
<div id="urvanov-syntax-highlighter-613eee9a17d3d947248931" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;"> | |||
<div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><span class="crayon-title"></span> | |||
<div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><div class="crayon-button urvanov-syntax-highlighter-nums-button" title="Toggle Line Numbers"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-plain-button" title="Toggle Plain Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-wrap-button" title="Toggle Line Wrap"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-expand-button" title="Expand Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-copy-button" title="Copy"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-popup-button" title="Open Code In New Window"><div class="urvanov-syntax-highlighter-button-icon"></div></div></div></div> | |||
<div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;"></div> | |||
<div class="urvanov-syntax-highlighter-plain-wrap"><textarea wrap="soft" class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;"> | |||
# mlp with scaled outputs on the regression problem | |||
from sklearn.datasets import make_regression | |||
from sklearn.preprocessing import StandardScaler | |||
from keras.layers import Dense | |||
from keras.models import Sequential | |||
from keras.optimizers import SGD | |||
from matplotlib import pyplot | |||
# generate regression dataset | |||
X, y = make_regression(n_samples=1000, n_features=20, noise=0.1, random_state=1) | |||
# split into train and test | |||
n_train = 500 | |||
trainX, testX = X[:n_train, :], X[n_train:, :] | |||
trainy, testy = y[:n_train], y[n_train:] | |||
# reshape 1d arrays to 2d arrays | |||
trainy = trainy.reshape(len(trainy), 1) | |||
testy = testy.reshape(len(trainy), 1) | |||
# created scaler | |||
scaler = StandardScaler() | |||
# fit scaler on training dataset | |||
scaler.fit(trainy) | |||
# transform training dataset | |||
trainy = scaler.transform(trainy) | |||
# transform test dataset | |||
testy = scaler.transform(testy) | |||
# define model | |||
model = Sequential() | |||
model.add(Dense(25, input_dim=20, activation='relu', kernel_initializer='he_uniform')) | |||
model.add(Dense(1, activation='linear')) | |||
# compile model | |||
model.compile(loss='mean_squared_error', optimizer=SGD(lr=0.01, momentum=0.9)) | |||
# fit model | |||
history = model.fit(trainX, trainy, validation_data=(testX, testy), epochs=100, verbose=0) | |||
# evaluate the model | |||
train_mse = model.evaluate(trainX, trainy, verbose=0) | |||
test_mse = model.evaluate(testX, testy, verbose=0) | |||
print('Train: %.3f, Test: %.3f' % (train_mse, test_mse)) | |||
# plot loss during training | |||
pyplot.title('Loss / Mean Squared Error') | |||
pyplot.plot(history.history['loss'], label='train') | |||
pyplot.plot(history.history['val_loss'], label='test') | |||
pyplot.legend() | |||
pyplot.show()</textarea></div> | |||
<div class="urvanov-syntax-highlighter-main" style=""> | |||
<table class="crayon-table"> | |||
<tr class="urvanov-syntax-highlighter-row"> | |||
<td class="crayon-nums " data-settings="show"> | |||
<div class="urvanov-syntax-highlighter-nums-content" style="font-size: 12px !important; line-height: 15px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-5">5</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-6">6</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-7">7</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-8">8</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-9">9</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-10">10</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-11">11</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-12">12</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-13">13</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-14">14</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-15">15</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-16">16</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-17">17</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-18">18</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-19">19</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-20">20</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-21">21</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-22">22</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-23">23</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-24">24</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-25">25</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-26">26</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-27">27</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-28">28</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-29">29</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-30">30</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-31">31</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-32">32</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-33">33</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-34">34</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-35">35</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-36">36</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-37">37</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-38">38</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-39">39</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-40">40</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-41">41</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3d947248931-42">42</div></div> | |||
</td> | |||
<td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-1"><span class="crayon-p"># mlp with scaled outputs on the regression problem</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-2"><span class="crayon-e">from </span><span class="crayon-v">sklearn</span><span class="crayon-sy">.</span><span class="crayon-e">datasets </span><span class="crayon-e">import </span><span class="crayon-e">make_regression</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-3"><span class="crayon-e">from </span><span class="crayon-v">sklearn</span><span class="crayon-sy">.</span><span class="crayon-e">preprocessing </span><span class="crayon-e">import </span><span class="crayon-e">StandardScaler</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-4"><span class="crayon-e">from </span><span class="crayon-v">keras</span><span class="crayon-sy">.</span><span class="crayon-e">layers </span><span class="crayon-e">import </span><span class="crayon-e">Dense</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-5"><span class="crayon-e">from </span><span class="crayon-v">keras</span><span class="crayon-sy">.</span><span class="crayon-e">models </span><span class="crayon-e">import </span><span class="crayon-e">Sequential</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-6"><span class="crayon-e">from </span><span class="crayon-v">keras</span><span class="crayon-sy">.</span><span class="crayon-e">optimizers </span><span class="crayon-e">import </span><span class="crayon-e">SGD</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-7"><span class="crayon-e">from </span><span class="crayon-e">matplotlib </span><span class="crayon-e">import </span><span class="crayon-i">pyplot</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-8"><span class="crayon-p"># generate regression dataset</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-9"><span class="crayon-v">X</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">y</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">make_regression</span><span class="crayon-sy">(</span><span class="crayon-v">n_samples</span><span class="crayon-o">=</span><span class="crayon-cn">1000</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">n_features</span><span class="crayon-o">=</span><span class="crayon-cn">20</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">noise</span><span class="crayon-o">=</span><span class="crayon-cn">0.1</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">random_state</span><span class="crayon-o">=</span><span class="crayon-cn">1</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-10"><span class="crayon-p"># split into train and test</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-11"><span class="crayon-v">n_train</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-cn">500</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-12"><span class="crayon-v">trainX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testX</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">X</span><span class="crayon-sy">[</span><span class="crayon-o">:</span><span class="crayon-v">n_train</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-o">:</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">X</span><span class="crayon-sy">[</span><span class="crayon-v">n_train</span><span class="crayon-o">:</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-o">:</span><span class="crayon-sy">]</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-13"><span class="crayon-v">trainy</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testy</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">y</span><span class="crayon-sy">[</span><span class="crayon-o">:</span><span class="crayon-v">n_train</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">y</span><span class="crayon-sy">[</span><span class="crayon-v">n_train</span><span class="crayon-o">:</span><span class="crayon-sy">]</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-14"><span class="crayon-p"># reshape 1d arrays to 2d arrays</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-15"><span class="crayon-v">trainy</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">trainy</span><span class="crayon-sy">.</span><span class="crayon-e">reshape</span><span class="crayon-sy">(</span><span class="crayon-e">len</span><span class="crayon-sy">(</span><span class="crayon-v">trainy</span><span class="crayon-sy">)</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">1</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-16"><span class="crayon-v">testy</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">testy</span><span class="crayon-sy">.</span><span class="crayon-e">reshape</span><span class="crayon-sy">(</span><span class="crayon-e">len</span><span class="crayon-sy">(</span><span class="crayon-v">trainy</span><span class="crayon-sy">)</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">1</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-17"><span class="crayon-p"># created scaler</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-18"><span class="crayon-v">scaler</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">StandardScaler</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-19"><span class="crayon-p"># fit scaler on training dataset</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-20"><span class="crayon-v">scaler</span><span class="crayon-sy">.</span><span class="crayon-e">fit</span><span class="crayon-sy">(</span><span class="crayon-v">trainy</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-21"><span class="crayon-p"># transform training dataset</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-22"><span class="crayon-v">trainy</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">scaler</span><span class="crayon-sy">.</span><span class="crayon-e">transform</span><span class="crayon-sy">(</span><span class="crayon-v">trainy</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-23"><span class="crayon-p"># transform test dataset</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-24"><span class="crayon-v">testy</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">scaler</span><span class="crayon-sy">.</span><span class="crayon-e">transform</span><span class="crayon-sy">(</span><span class="crayon-v">testy</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-25"><span class="crayon-p"># define model</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-26"><span class="crayon-v">model</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">Sequential</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-27"><span class="crayon-v">model</span><span class="crayon-sy">.</span><span class="crayon-e">add</span><span class="crayon-sy">(</span><span class="crayon-e">Dense</span><span class="crayon-sy">(</span><span class="crayon-cn">25</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">input_dim</span><span class="crayon-o">=</span><span class="crayon-cn">20</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">activation</span><span class="crayon-o">=</span><span class="crayon-s">'relu'</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">kernel_initializer</span><span class="crayon-o">=</span><span class="crayon-s">'he_uniform'</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-28"><span class="crayon-v">model</span><span class="crayon-sy">.</span><span class="crayon-e">add</span><span class="crayon-sy">(</span><span class="crayon-e">Dense</span><span class="crayon-sy">(</span><span class="crayon-cn">1</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">activation</span><span class="crayon-o">=</span><span class="crayon-s">'linear'</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-29"><span class="crayon-p"># compile model</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-30"><span class="crayon-v">model</span><span class="crayon-sy">.</span><span class="crayon-e">compile</span><span class="crayon-sy">(</span><span class="crayon-v">loss</span><span class="crayon-o">=</span><span class="crayon-s">'mean_squared_error'</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">optimizer</span><span class="crayon-o">=</span><span class="crayon-e">SGD</span><span class="crayon-sy">(</span><span class="crayon-v">lr</span><span class="crayon-o">=</span><span class="crayon-cn">0.01</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">momentum</span><span class="crayon-o">=</span><span class="crayon-cn">0.9</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-31"><span class="crayon-p"># fit model</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-32"><span class="crayon-v">history</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">model</span><span class="crayon-sy">.</span><span class="crayon-e">fit</span><span class="crayon-sy">(</span><span class="crayon-v">trainX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">trainy</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">validation_data</span><span class="crayon-o">=</span><span class="crayon-sy">(</span><span class="crayon-v">testX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testy</span><span class="crayon-sy">)</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">epochs</span><span class="crayon-o">=</span><span class="crayon-cn">100</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">verbose</span><span class="crayon-o">=</span><span class="crayon-cn">0</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-33"><span class="crayon-p"># evaluate the model</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-34"><span class="crayon-v">train_mse</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">model</span><span class="crayon-sy">.</span><span class="crayon-e">evaluate</span><span class="crayon-sy">(</span><span class="crayon-v">trainX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">trainy</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">verbose</span><span class="crayon-o">=</span><span class="crayon-cn">0</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-35"><span class="crayon-v">test_mse</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">model</span><span class="crayon-sy">.</span><span class="crayon-e">evaluate</span><span class="crayon-sy">(</span><span class="crayon-v">testX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testy</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">verbose</span><span class="crayon-o">=</span><span class="crayon-cn">0</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-36"><span class="crayon-e">print</span><span class="crayon-sy">(</span><span class="crayon-s">'Train: %.3f, Test: %.3f'</span><span class="crayon-h"> </span><span class="crayon-o">%</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-v">train_mse</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">test_mse</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-37"><span class="crayon-p"># plot loss during training</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-38"><span class="crayon-v">pyplot</span><span class="crayon-sy">.</span><span class="crayon-e">title</span><span class="crayon-sy">(</span><span class="crayon-s">'Loss / Mean Squared Error'</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-39"><span class="crayon-v">pyplot</span><span class="crayon-sy">.</span><span class="crayon-e">plot</span><span class="crayon-sy">(</span><span class="crayon-v">history</span><span class="crayon-sy">.</span><span class="crayon-v">history</span><span class="crayon-sy">[</span><span class="crayon-s">'loss'</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">label</span><span class="crayon-o">=</span><span class="crayon-s">'train'</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-40"><span class="crayon-v">pyplot</span><span class="crayon-sy">.</span><span class="crayon-e">plot</span><span class="crayon-sy">(</span><span class="crayon-v">history</span><span class="crayon-sy">.</span><span class="crayon-v">history</span><span class="crayon-sy">[</span><span class="crayon-s">'val_loss'</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">label</span><span class="crayon-o">=</span><span class="crayon-s">'test'</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-41"><span class="crayon-v">pyplot</span><span class="crayon-sy">.</span><span class="crayon-e">legend</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3d947248931-42"><span class="crayon-v">pyplot</span><span class="crayon-sy">.</span><span class="crayon-e">show</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div></div></td> | |||
</tr> | |||
</table> | |||
</div> | |||
</div><p>Running the example fits the model and calculates the mean squared error on the train and test sets.</p> | |||
<p><strong>Note</strong>: Your <a href="https://machinelearningmastery.com/different-results-each-time-in-machine-learning/">results may vary</a> given the stochastic nature of the algorithm or evaluation procedure, or differences in numerical precision. Consider running the example a few times and compare the average outcome.</p> | |||
<p>In this case, the model does appear to learn the problem and achieves near-zero mean squared error, at least to three decimal places.</p> | |||
<div id="urvanov-syntax-highlighter-613eee9a17d3e323242747" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;"> | |||
<div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><span class="crayon-title"></span> | |||
<div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><div class="crayon-button urvanov-syntax-highlighter-nums-button" title="Toggle Line Numbers"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-plain-button" title="Toggle Plain Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-wrap-button" title="Toggle Line Wrap"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-expand-button" title="Expand Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-copy-button" title="Copy"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-popup-button" title="Open Code In New Window"><div class="urvanov-syntax-highlighter-button-icon"></div></div></div></div> | |||
<div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;"></div> | |||
<div class="urvanov-syntax-highlighter-plain-wrap"><textarea wrap="soft" class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;"> | |||
Train: 0.003, Test: 0.007</textarea></div> | |||
<div class="urvanov-syntax-highlighter-main" style=""> | |||
<table class="crayon-table"> | |||
<tr class="urvanov-syntax-highlighter-row"> | |||
<td class="crayon-nums " data-settings="show"> | |||
<div class="urvanov-syntax-highlighter-nums-content" style="font-size: 12px !important; line-height: 15px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3e323242747-1">1</div></div> | |||
</td> | |||
<td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3e323242747-1">Train: 0.003, Test: 0.007</div></div></td> | |||
</tr> | |||
</table> | |||
</div> | |||
</div><p>A line plot of the mean squared error on the train (blue) and test (orange) dataset over each training epoch is created.</p> | |||
<p>In this case, we can see that the model rapidly learns to effectively map inputs to outputs for the regression problem and achieves good performance on both datasets over the course of the run, neither overfitting or underfitting the training dataset.</p> | |||
<div id="attachment_6942" style="width: 1290px" class="wp-caption aligncenter"><img aria-describedby="caption-attachment-6942" loading="lazy" class="size-full wp-image-6942" src="https://machinelearningmastery.com/wp-content/uploads/2018/11/Line-Plot-of-Mean-Squared-Error-on-the-Train-a-Test-Datasets-For-Each-Training-Epoch.png" alt="Line Plot of Mean Squared Error on the Train a Test Datasets for Each Training Epoch" width="1280" height="960" srcset="https://machinelearningmastery.com/wp-content/uploads/2018/11/Line-Plot-of-Mean-Squared-Error-on-the-Train-a-Test-Datasets-For-Each-Training-Epoch.png 1280w, https://machinelearningmastery.com/wp-content/uploads/2018/11/Line-Plot-of-Mean-Squared-Error-on-the-Train-a-Test-Datasets-For-Each-Training-Epoch-300x225.png 300w, https://machinelearningmastery.com/wp-content/uploads/2018/11/Line-Plot-of-Mean-Squared-Error-on-the-Train-a-Test-Datasets-For-Each-Training-Epoch-768x576.png 768w, https://machinelearningmastery.com/wp-content/uploads/2018/11/Line-Plot-of-Mean-Squared-Error-on-the-Train-a-Test-Datasets-For-Each-Training-Epoch-1024x768.png 1024w" sizes="(max-width: 1280px) 100vw, 1280px" /><p id="caption-attachment-6942" class="wp-caption-text">Line Plot of Mean Squared Error on the Train a Test Datasets for Each Training Epoch</p></div> | |||
<p>It may be interesting to repeat this experiment and normalize the target variable instead and compare results.</p> | |||
<h2>Multilayer Perceptron With Scaled Input Variables</h2> | |||
<p>We have seen that data scaling can stabilize the training process when fitting a model for regression with a target variable that has a wide spread.</p> | |||
<p>It is also possible to improve the stability and performance of the model by scaling the input variables.</p> | |||
<p>In this section, we will design an experiment to compare the performance of different scaling methods for the input variables.</p> | |||
<p>The input variables also have a Gaussian data distribution, like the target variable, therefore we would expect that standardizing the data would be the best approach. This is not always the case.</p> | |||
<p>We can compare the performance of the unscaled input variables to models fit with either standardized and normalized input variables.</p> | |||
<p>The first step is to define a function to create the same 1,000 data samples, split them into train and test sets, and apply the data scaling methods specified via input arguments. The <em>get_dataset()</em> function below implements this, requiring the scaler to be provided for the input and target variables and returns the train and test datasets split into input and output components ready to train and evaluate a model.</p> | |||
<div id="urvanov-syntax-highlighter-613eee9a17d3f691029655" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;"> | |||
<div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><span class="crayon-title"></span> | |||
<div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><div class="crayon-button urvanov-syntax-highlighter-nums-button" title="Toggle Line Numbers"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-plain-button" title="Toggle Plain Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-wrap-button" title="Toggle Line Wrap"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-expand-button" title="Expand Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-copy-button" title="Copy"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-popup-button" title="Open Code In New Window"><div class="urvanov-syntax-highlighter-button-icon"></div></div></div></div> | |||
<div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;"></div> | |||
<div class="urvanov-syntax-highlighter-plain-wrap"><textarea wrap="soft" class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;"> | |||
# prepare dataset with input and output scalers, can be none | |||
def get_dataset(input_scaler, output_scaler): | |||
# generate dataset | |||
X, y = make_regression(n_samples=1000, n_features=20, noise=0.1, random_state=1) | |||
# split into train and test | |||
n_train = 500 | |||
trainX, testX = X[:n_train, :], X[n_train:, :] | |||
trainy, testy = y[:n_train], y[n_train:] | |||
# scale inputs | |||
if input_scaler is not None: | |||
# fit scaler | |||
input_scaler.fit(trainX) | |||
# transform training dataset | |||
trainX = input_scaler.transform(trainX) | |||
# transform test dataset | |||
testX = input_scaler.transform(testX) | |||
if output_scaler is not None: | |||
# reshape 1d arrays to 2d arrays | |||
trainy = trainy.reshape(len(trainy), 1) | |||
testy = testy.reshape(len(trainy), 1) | |||
# fit scaler on training dataset | |||
output_scaler.fit(trainy) | |||
# transform training dataset | |||
trainy = output_scaler.transform(trainy) | |||
# transform test dataset | |||
testy = output_scaler.transform(testy) | |||
return trainX, trainy, testX, testy</textarea></div> | |||
<div class="urvanov-syntax-highlighter-main" style=""> | |||
<table class="crayon-table"> | |||
<tr class="urvanov-syntax-highlighter-row"> | |||
<td class="crayon-nums " data-settings="show"> | |||
<div class="urvanov-syntax-highlighter-nums-content" style="font-size: 12px !important; line-height: 15px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3f691029655-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3f691029655-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3f691029655-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3f691029655-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3f691029655-5">5</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3f691029655-6">6</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3f691029655-7">7</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3f691029655-8">8</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3f691029655-9">9</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3f691029655-10">10</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3f691029655-11">11</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3f691029655-12">12</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3f691029655-13">13</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3f691029655-14">14</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3f691029655-15">15</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3f691029655-16">16</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3f691029655-17">17</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3f691029655-18">18</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3f691029655-19">19</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3f691029655-20">20</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3f691029655-21">21</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3f691029655-22">22</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3f691029655-23">23</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3f691029655-24">24</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3f691029655-25">25</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d3f691029655-26">26</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d3f691029655-27">27</div></div> | |||
</td> | |||
<td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3f691029655-1"><span class="crayon-p"># prepare dataset with input and output scalers, can be none</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3f691029655-2"><span class="crayon-e">def </span><span class="crayon-e">get_dataset</span><span class="crayon-sy">(</span><span class="crayon-v">input_scaler</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">output_scaler</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3f691029655-3"><span class="crayon-h"> </span><span class="crayon-p"># generate dataset</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3f691029655-4"><span class="crayon-h"> </span><span class="crayon-v">X</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">y</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">make_regression</span><span class="crayon-sy">(</span><span class="crayon-v">n_samples</span><span class="crayon-o">=</span><span class="crayon-cn">1000</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">n_features</span><span class="crayon-o">=</span><span class="crayon-cn">20</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">noise</span><span class="crayon-o">=</span><span class="crayon-cn">0.1</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">random_state</span><span class="crayon-o">=</span><span class="crayon-cn">1</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3f691029655-5"><span class="crayon-h"> </span><span class="crayon-p"># split into train and test</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3f691029655-6"><span class="crayon-h"> </span><span class="crayon-v">n_train</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-cn">500</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3f691029655-7"><span class="crayon-h"> </span><span class="crayon-v">trainX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testX</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">X</span><span class="crayon-sy">[</span><span class="crayon-o">:</span><span class="crayon-v">n_train</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-o">:</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">X</span><span class="crayon-sy">[</span><span class="crayon-v">n_train</span><span class="crayon-o">:</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-o">:</span><span class="crayon-sy">]</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3f691029655-8"><span class="crayon-h"> </span><span class="crayon-v">trainy</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testy</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">y</span><span class="crayon-sy">[</span><span class="crayon-o">:</span><span class="crayon-v">n_train</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">y</span><span class="crayon-sy">[</span><span class="crayon-v">n_train</span><span class="crayon-o">:</span><span class="crayon-sy">]</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3f691029655-9"><span class="crayon-h"> </span><span class="crayon-p"># scale inputs</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3f691029655-10"><span class="crayon-h"> </span><span class="crayon-st">if</span><span class="crayon-h"> </span><span class="crayon-e">input_scaler </span><span class="crayon-st">is</span><span class="crayon-h"> </span><span class="crayon-st">not</span><span class="crayon-h"> </span><span class="crayon-v">None</span><span class="crayon-o">:</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3f691029655-11"><span class="crayon-h"> </span><span class="crayon-p"># fit scaler</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3f691029655-12"><span class="crayon-h"> </span><span class="crayon-v">input_scaler</span><span class="crayon-sy">.</span><span class="crayon-e">fit</span><span class="crayon-sy">(</span><span class="crayon-v">trainX</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3f691029655-13"><span class="crayon-h"> </span><span class="crayon-p"># transform training dataset</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3f691029655-14"><span class="crayon-h"> </span><span class="crayon-v">trainX</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">input_scaler</span><span class="crayon-sy">.</span><span class="crayon-e">transform</span><span class="crayon-sy">(</span><span class="crayon-v">trainX</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3f691029655-15"><span class="crayon-h"> </span><span class="crayon-p"># transform test dataset</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3f691029655-16"><span class="crayon-h"> </span><span class="crayon-v">testX</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">input_scaler</span><span class="crayon-sy">.</span><span class="crayon-e">transform</span><span class="crayon-sy">(</span><span class="crayon-v">testX</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3f691029655-17"><span class="crayon-h"> </span><span class="crayon-st">if</span><span class="crayon-h"> </span><span class="crayon-e">output_scaler </span><span class="crayon-st">is</span><span class="crayon-h"> </span><span class="crayon-st">not</span><span class="crayon-h"> </span><span class="crayon-v">None</span><span class="crayon-o">:</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3f691029655-18"><span class="crayon-h"> </span><span class="crayon-p"># reshape 1d arrays to 2d arrays</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3f691029655-19"><span class="crayon-h"> </span><span class="crayon-v">trainy</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">trainy</span><span class="crayon-sy">.</span><span class="crayon-e">reshape</span><span class="crayon-sy">(</span><span class="crayon-e">len</span><span class="crayon-sy">(</span><span class="crayon-v">trainy</span><span class="crayon-sy">)</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">1</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3f691029655-20"><span class="crayon-h"> </span><span class="crayon-v">testy</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">testy</span><span class="crayon-sy">.</span><span class="crayon-e">reshape</span><span class="crayon-sy">(</span><span class="crayon-e">len</span><span class="crayon-sy">(</span><span class="crayon-v">trainy</span><span class="crayon-sy">)</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">1</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3f691029655-21"><span class="crayon-h"> </span><span class="crayon-p"># fit scaler on training dataset</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3f691029655-22"><span class="crayon-h"> </span><span class="crayon-v">output_scaler</span><span class="crayon-sy">.</span><span class="crayon-e">fit</span><span class="crayon-sy">(</span><span class="crayon-v">trainy</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3f691029655-23"><span class="crayon-h"> </span><span class="crayon-p"># transform training dataset</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3f691029655-24"><span class="crayon-h"> </span><span class="crayon-v">trainy</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">output_scaler</span><span class="crayon-sy">.</span><span class="crayon-e">transform</span><span class="crayon-sy">(</span><span class="crayon-v">trainy</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3f691029655-25"><span class="crayon-h"> </span><span class="crayon-p"># transform test dataset</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d3f691029655-26"><span class="crayon-h"> </span><span class="crayon-v">testy</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">output_scaler</span><span class="crayon-sy">.</span><span class="crayon-e">transform</span><span class="crayon-sy">(</span><span class="crayon-v">testy</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d3f691029655-27"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-v">trainX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">trainy</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testy</span></div></div></td> | |||
</tr> | |||
</table> | |||
</div> | |||
</div><p>Next, we can define a function to fit an MLP model on a given dataset and return the mean squared error for the fit model on the test dataset.</p> | |||
<p>The <em>evaluate_model()</em> function below implements this behavior.</p> | |||
<div id="urvanov-syntax-highlighter-613eee9a17d40678787949" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;"> | |||
<div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><span class="crayon-title"></span> | |||
<div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><div class="crayon-button urvanov-syntax-highlighter-nums-button" title="Toggle Line Numbers"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-plain-button" title="Toggle Plain Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-wrap-button" title="Toggle Line Wrap"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-expand-button" title="Expand Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-copy-button" title="Copy"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-popup-button" title="Open Code In New Window"><div class="urvanov-syntax-highlighter-button-icon"></div></div></div></div> | |||
<div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;"></div> | |||
<div class="urvanov-syntax-highlighter-plain-wrap"><textarea wrap="soft" class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;"> | |||
# fit and evaluate mse of model on test set | |||
def evaluate_model(trainX, trainy, testX, testy): | |||
# define model | |||
model = Sequential() | |||
model.add(Dense(25, input_dim=20, activation='relu', kernel_initializer='he_uniform')) | |||
model.add(Dense(1, activation='linear')) | |||
# compile model | |||
model.compile(loss='mean_squared_error', optimizer=SGD(lr=0.01, momentum=0.9)) | |||
# fit model | |||
model.fit(trainX, trainy, epochs=100, verbose=0) | |||
# evaluate the model | |||
test_mse = model.evaluate(testX, testy, verbose=0) | |||
return test_mse</textarea></div> | |||
<div class="urvanov-syntax-highlighter-main" style=""> | |||
<table class="crayon-table"> | |||
<tr class="urvanov-syntax-highlighter-row"> | |||
<td class="crayon-nums " data-settings="show"> | |||
<div class="urvanov-syntax-highlighter-nums-content" style="font-size: 12px !important; line-height: 15px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d40678787949-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d40678787949-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d40678787949-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d40678787949-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d40678787949-5">5</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d40678787949-6">6</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d40678787949-7">7</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d40678787949-8">8</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d40678787949-9">9</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d40678787949-10">10</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d40678787949-11">11</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d40678787949-12">12</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d40678787949-13">13</div></div> | |||
</td> | |||
<td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d40678787949-1"><span class="crayon-p"># fit and evaluate mse of model on test set</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d40678787949-2"><span class="crayon-e">def </span><span class="crayon-e">evaluate_model</span><span class="crayon-sy">(</span><span class="crayon-v">trainX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">trainy</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testy</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d40678787949-3"><span class="crayon-h"> </span><span class="crayon-p"># define model</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d40678787949-4"><span class="crayon-h"> </span><span class="crayon-v">model</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">Sequential</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d40678787949-5"><span class="crayon-h"> </span><span class="crayon-v">model</span><span class="crayon-sy">.</span><span class="crayon-e">add</span><span class="crayon-sy">(</span><span class="crayon-e">Dense</span><span class="crayon-sy">(</span><span class="crayon-cn">25</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">input_dim</span><span class="crayon-o">=</span><span class="crayon-cn">20</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">activation</span><span class="crayon-o">=</span><span class="crayon-s">'relu'</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">kernel_initializer</span><span class="crayon-o">=</span><span class="crayon-s">'he_uniform'</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d40678787949-6"><span class="crayon-h"> </span><span class="crayon-v">model</span><span class="crayon-sy">.</span><span class="crayon-e">add</span><span class="crayon-sy">(</span><span class="crayon-e">Dense</span><span class="crayon-sy">(</span><span class="crayon-cn">1</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">activation</span><span class="crayon-o">=</span><span class="crayon-s">'linear'</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d40678787949-7"><span class="crayon-h"> </span><span class="crayon-p"># compile model</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d40678787949-8"><span class="crayon-h"> </span><span class="crayon-v">model</span><span class="crayon-sy">.</span><span class="crayon-e">compile</span><span class="crayon-sy">(</span><span class="crayon-v">loss</span><span class="crayon-o">=</span><span class="crayon-s">'mean_squared_error'</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">optimizer</span><span class="crayon-o">=</span><span class="crayon-e">SGD</span><span class="crayon-sy">(</span><span class="crayon-v">lr</span><span class="crayon-o">=</span><span class="crayon-cn">0.01</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">momentum</span><span class="crayon-o">=</span><span class="crayon-cn">0.9</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d40678787949-9"><span class="crayon-h"> </span><span class="crayon-p"># fit model</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d40678787949-10"><span class="crayon-h"> </span><span class="crayon-v">model</span><span class="crayon-sy">.</span><span class="crayon-e">fit</span><span class="crayon-sy">(</span><span class="crayon-v">trainX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">trainy</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">epochs</span><span class="crayon-o">=</span><span class="crayon-cn">100</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">verbose</span><span class="crayon-o">=</span><span class="crayon-cn">0</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d40678787949-11"><span class="crayon-h"> </span><span class="crayon-p"># evaluate the model</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d40678787949-12"><span class="crayon-h"> </span><span class="crayon-v">test_mse</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">model</span><span class="crayon-sy">.</span><span class="crayon-e">evaluate</span><span class="crayon-sy">(</span><span class="crayon-v">testX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testy</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">verbose</span><span class="crayon-o">=</span><span class="crayon-cn">0</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d40678787949-13"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-v">test_mse</span></div></div></td> | |||
</tr> | |||
</table> | |||
</div> | |||
</div><p>Neural networks are trained using a stochastic learning algorithm. This means that the same model fit on the same data may result in a different performance.</p> | |||
<p>We can address this in our experiment by repeating the evaluation of each model configuration, in this case a choice of data scaling, multiple times and report performance as the mean of the error scores across all of the runs. We will repeat each run 30 times to ensure the mean is statistically robust.</p> | |||
<p>The <em>repeated_evaluation()</em> function below implements this, taking the scaler for input and output variables as arguments, evaluating a model 30 times with those scalers, printing error scores along the way, and returning a list of the calculated error scores from each run.</p> | |||
<div id="urvanov-syntax-highlighter-613eee9a17d41454902264" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;"> | |||
<div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><span class="crayon-title"></span> | |||
<div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><div class="crayon-button urvanov-syntax-highlighter-nums-button" title="Toggle Line Numbers"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-plain-button" title="Toggle Plain Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-wrap-button" title="Toggle Line Wrap"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-expand-button" title="Expand Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-copy-button" title="Copy"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-popup-button" title="Open Code In New Window"><div class="urvanov-syntax-highlighter-button-icon"></div></div></div></div> | |||
<div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;"></div> | |||
<div class="urvanov-syntax-highlighter-plain-wrap"><textarea wrap="soft" class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;"> | |||
# evaluate model multiple times with given input and output scalers | |||
def repeated_evaluation(input_scaler, output_scaler, n_repeats=30): | |||
# get dataset | |||
trainX, trainy, testX, testy = get_dataset(input_scaler, output_scaler) | |||
# repeated evaluation of model | |||
results = list() | |||
for _ in range(n_repeats): | |||
test_mse = evaluate_model(trainX, trainy, testX, testy) | |||
print('>%.3f' % test_mse) | |||
results.append(test_mse) | |||
return results</textarea></div> | |||
<div class="urvanov-syntax-highlighter-main" style=""> | |||
<table class="crayon-table"> | |||
<tr class="urvanov-syntax-highlighter-row"> | |||
<td class="crayon-nums " data-settings="show"> | |||
<div class="urvanov-syntax-highlighter-nums-content" style="font-size: 12px !important; line-height: 15px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d41454902264-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d41454902264-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d41454902264-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d41454902264-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d41454902264-5">5</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d41454902264-6">6</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d41454902264-7">7</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d41454902264-8">8</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d41454902264-9">9</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d41454902264-10">10</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d41454902264-11">11</div></div> | |||
</td> | |||
<td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d41454902264-1"><span class="crayon-p"># evaluate model multiple times with given input and output scalers</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d41454902264-2"><span class="crayon-e">def </span><span class="crayon-e">repeated_evaluation</span><span class="crayon-sy">(</span><span class="crayon-v">input_scaler</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">output_scaler</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">n_repeats</span><span class="crayon-o">=</span><span class="crayon-cn">30</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d41454902264-3"><span class="crayon-h"> </span><span class="crayon-p"># get dataset</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d41454902264-4"><span class="crayon-h"> </span><span class="crayon-v">trainX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">trainy</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testy</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">get_dataset</span><span class="crayon-sy">(</span><span class="crayon-v">input_scaler</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">output_scaler</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d41454902264-5"><span class="crayon-h"> </span><span class="crayon-p"># repeated evaluation of model</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d41454902264-6"><span class="crayon-h"> </span><span class="crayon-v">results</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">list</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d41454902264-7"><span class="crayon-h"> </span><span class="crayon-st">for</span><span class="crayon-h"> </span><span class="crayon-i">_</span><span class="crayon-h"> </span><span class="crayon-st">in</span><span class="crayon-h"> </span><span class="crayon-e">range</span><span class="crayon-sy">(</span><span class="crayon-v">n_repeats</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d41454902264-8"><span class="crayon-h"> </span><span class="crayon-v">test_mse</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">evaluate_model</span><span class="crayon-sy">(</span><span class="crayon-v">trainX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">trainy</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testy</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d41454902264-9"><span class="crayon-h"> </span><span class="crayon-e">print</span><span class="crayon-sy">(</span><span class="crayon-s">'>%.3f'</span><span class="crayon-h"> </span><span class="crayon-o">%</span><span class="crayon-h"> </span><span class="crayon-v">test_mse</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d41454902264-10"><span class="crayon-h"> </span><span class="crayon-v">results</span><span class="crayon-sy">.</span><span class="crayon-e">append</span><span class="crayon-sy">(</span><span class="crayon-v">test_mse</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d41454902264-11"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-v">results</span></div></div></td> | |||
</tr> | |||
</table> | |||
</div> | |||
</div><p>Finally, we can run the experiment and evaluate the same model on the same dataset three different ways:</p> | |||
<ul> | |||
<li>No scaling of inputs, standardized outputs.</li> | |||
<li>Normalized inputs, standardized outputs.</li> | |||
<li>Standardized inputs, standardized outputs.</li> | |||
</ul> | |||
<p>The mean and standard deviation of the error for each configuration is reported, then box and whisker plots are created to summarize the error scores for each configuration.</p> | |||
<div id="urvanov-syntax-highlighter-613eee9a17d51819446063" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;"> | |||
<div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><span class="crayon-title"></span> | |||
<div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><div class="crayon-button urvanov-syntax-highlighter-nums-button" title="Toggle Line Numbers"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-plain-button" title="Toggle Plain Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-wrap-button" title="Toggle Line Wrap"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-expand-button" title="Expand Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-copy-button" title="Copy"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-popup-button" title="Open Code In New Window"><div class="urvanov-syntax-highlighter-button-icon"></div></div></div></div> | |||
<div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;"></div> | |||
<div class="urvanov-syntax-highlighter-plain-wrap"><textarea wrap="soft" class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;"> | |||
# unscaled inputs | |||
results_unscaled_inputs = repeated_evaluation(None, StandardScaler()) | |||
# normalized inputs | |||
results_normalized_inputs = repeated_evaluation(MinMaxScaler(), StandardScaler()) | |||
# standardized inputs | |||
results_standardized_inputs = repeated_evaluation(StandardScaler(), StandardScaler()) | |||
# summarize results | |||
print('Unscaled: %.3f (%.3f)' % (mean(results_unscaled_inputs), std(results_unscaled_inputs))) | |||
print('Normalized: %.3f (%.3f)' % (mean(results_normalized_inputs), std(results_normalized_inputs))) | |||
print('Standardized: %.3f (%.3f)' % (mean(results_standardized_inputs), std(results_standardized_inputs))) | |||
# plot results | |||
results = [results_unscaled_inputs, results_normalized_inputs, results_standardized_inputs] | |||
labels = ['unscaled', 'normalized', 'standardized'] | |||
pyplot.boxplot(results, labels=labels) | |||
pyplot.show()</textarea></div> | |||
<div class="urvanov-syntax-highlighter-main" style=""> | |||
<table class="crayon-table"> | |||
<tr class="urvanov-syntax-highlighter-row"> | |||
<td class="crayon-nums " data-settings="show"> | |||
<div class="urvanov-syntax-highlighter-nums-content" style="font-size: 12px !important; line-height: 15px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d51819446063-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d51819446063-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d51819446063-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d51819446063-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d51819446063-5">5</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d51819446063-6">6</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d51819446063-7">7</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d51819446063-8">8</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d51819446063-9">9</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d51819446063-10">10</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d51819446063-11">11</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d51819446063-12">12</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d51819446063-13">13</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d51819446063-14">14</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d51819446063-15">15</div></div> | |||
</td> | |||
<td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d51819446063-1"><span class="crayon-p"># unscaled inputs</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d51819446063-2"><span class="crayon-v">results_unscaled_inputs</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">repeated_evaluation</span><span class="crayon-sy">(</span><span class="crayon-v">None</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-e">StandardScaler</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d51819446063-3"><span class="crayon-p"># normalized inputs</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d51819446063-4"><span class="crayon-v">results_normalized_inputs</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">repeated_evaluation</span><span class="crayon-sy">(</span><span class="crayon-e">MinMaxScaler</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-e">StandardScaler</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d51819446063-5"><span class="crayon-p"># standardized inputs</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d51819446063-6"><span class="crayon-v">results_standardized_inputs</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">repeated_evaluation</span><span class="crayon-sy">(</span><span class="crayon-e">StandardScaler</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-e">StandardScaler</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d51819446063-7"><span class="crayon-p"># summarize results</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d51819446063-8"><span class="crayon-e">print</span><span class="crayon-sy">(</span><span class="crayon-s">'Unscaled: %.3f (%.3f)'</span><span class="crayon-h"> </span><span class="crayon-o">%</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-e">mean</span><span class="crayon-sy">(</span><span class="crayon-v">results_unscaled_inputs</span><span class="crayon-sy">)</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-e">std</span><span class="crayon-sy">(</span><span class="crayon-v">results_unscaled_inputs</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d51819446063-9"><span class="crayon-e">print</span><span class="crayon-sy">(</span><span class="crayon-s">'Normalized: %.3f (%.3f)'</span><span class="crayon-h"> </span><span class="crayon-o">%</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-e">mean</span><span class="crayon-sy">(</span><span class="crayon-v">results_normalized_inputs</span><span class="crayon-sy">)</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-e">std</span><span class="crayon-sy">(</span><span class="crayon-v">results_normalized_inputs</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d51819446063-10"><span class="crayon-e">print</span><span class="crayon-sy">(</span><span class="crayon-s">'Standardized: %.3f (%.3f)'</span><span class="crayon-h"> </span><span class="crayon-o">%</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-e">mean</span><span class="crayon-sy">(</span><span class="crayon-v">results_standardized_inputs</span><span class="crayon-sy">)</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-e">std</span><span class="crayon-sy">(</span><span class="crayon-v">results_standardized_inputs</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d51819446063-11"><span class="crayon-p"># plot results</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d51819446063-12"><span class="crayon-v">results</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-sy">[</span><span class="crayon-v">results_unscaled_inputs</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">results_normalized_inputs</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">results_standardized_inputs</span><span class="crayon-sy">]</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d51819446063-13"><span class="crayon-v">labels</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-sy">[</span><span class="crayon-s">'unscaled'</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-s">'normalized'</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-s">'standardized'</span><span class="crayon-sy">]</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d51819446063-14"><span class="crayon-v">pyplot</span><span class="crayon-sy">.</span><span class="crayon-e">boxplot</span><span class="crayon-sy">(</span><span class="crayon-v">results</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">labels</span><span class="crayon-o">=</span><span class="crayon-v">labels</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d51819446063-15"><span class="crayon-v">pyplot</span><span class="crayon-sy">.</span><span class="crayon-e">show</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div></div></td> | |||
</tr> | |||
</table> | |||
</div> | |||
</div><p>Tying these elements together, the complete example is listed below.</p> | |||
<div id="urvanov-syntax-highlighter-613eee9a17d52691236049" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;"> | |||
<div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><span class="crayon-title"></span> | |||
<div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><div class="crayon-button urvanov-syntax-highlighter-nums-button" title="Toggle Line Numbers"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-plain-button" title="Toggle Plain Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-wrap-button" title="Toggle Line Wrap"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-expand-button" title="Expand Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-copy-button" title="Copy"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-popup-button" title="Open Code In New Window"><div class="urvanov-syntax-highlighter-button-icon"></div></div></div></div> | |||
<div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;"></div> | |||
<div class="urvanov-syntax-highlighter-plain-wrap"><textarea wrap="soft" class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;"> | |||
# compare scaling methods for mlp inputs on regression problem | |||
from sklearn.datasets import make_regression | |||
from sklearn.preprocessing import StandardScaler | |||
from sklearn.preprocessing import MinMaxScaler | |||
from keras.layers import Dense | |||
from keras.models import Sequential | |||
from keras.optimizers import SGD | |||
from matplotlib import pyplot | |||
from numpy import mean | |||
from numpy import std | |||
# prepare dataset with input and output scalers, can be none | |||
def get_dataset(input_scaler, output_scaler): | |||
# generate dataset | |||
X, y = make_regression(n_samples=1000, n_features=20, noise=0.1, random_state=1) | |||
# split into train and test | |||
n_train = 500 | |||
trainX, testX = X[:n_train, :], X[n_train:, :] | |||
trainy, testy = y[:n_train], y[n_train:] | |||
# scale inputs | |||
if input_scaler is not None: | |||
# fit scaler | |||
input_scaler.fit(trainX) | |||
# transform training dataset | |||
trainX = input_scaler.transform(trainX) | |||
# transform test dataset | |||
testX = input_scaler.transform(testX) | |||
if output_scaler is not None: | |||
# reshape 1d arrays to 2d arrays | |||
trainy = trainy.reshape(len(trainy), 1) | |||
testy = testy.reshape(len(trainy), 1) | |||
# fit scaler on training dataset | |||
output_scaler.fit(trainy) | |||
# transform training dataset | |||
trainy = output_scaler.transform(trainy) | |||
# transform test dataset | |||
testy = output_scaler.transform(testy) | |||
return trainX, trainy, testX, testy | |||
# fit and evaluate mse of model on test set | |||
def evaluate_model(trainX, trainy, testX, testy): | |||
# define model | |||
model = Sequential() | |||
model.add(Dense(25, input_dim=20, activation='relu', kernel_initializer='he_uniform')) | |||
model.add(Dense(1, activation='linear')) | |||
# compile model | |||
model.compile(loss='mean_squared_error', optimizer=SGD(lr=0.01, momentum=0.9)) | |||
# fit model | |||
model.fit(trainX, trainy, epochs=100, verbose=0) | |||
# evaluate the model | |||
test_mse = model.evaluate(testX, testy, verbose=0) | |||
return test_mse | |||
# evaluate model multiple times with given input and output scalers | |||
def repeated_evaluation(input_scaler, output_scaler, n_repeats=30): | |||
# get dataset | |||
trainX, trainy, testX, testy = get_dataset(input_scaler, output_scaler) | |||
# repeated evaluation of model | |||
results = list() | |||
for _ in range(n_repeats): | |||
test_mse = evaluate_model(trainX, trainy, testX, testy) | |||
print('>%.3f' % test_mse) | |||
results.append(test_mse) | |||
return results | |||
# unscaled inputs | |||
results_unscaled_inputs = repeated_evaluation(None, StandardScaler()) | |||
# normalized inputs | |||
results_normalized_inputs = repeated_evaluation(MinMaxScaler(), StandardScaler()) | |||
# standardized inputs | |||
results_standardized_inputs = repeated_evaluation(StandardScaler(), StandardScaler()) | |||
# summarize results | |||
print('Unscaled: %.3f (%.3f)' % (mean(results_unscaled_inputs), std(results_unscaled_inputs))) | |||
print('Normalized: %.3f (%.3f)' % (mean(results_normalized_inputs), std(results_normalized_inputs))) | |||
print('Standardized: %.3f (%.3f)' % (mean(results_standardized_inputs), std(results_standardized_inputs))) | |||
# plot results | |||
results = [results_unscaled_inputs, results_normalized_inputs, results_standardized_inputs] | |||
labels = ['unscaled', 'normalized', 'standardized'] | |||
pyplot.boxplot(results, labels=labels) | |||
pyplot.show()</textarea></div> | |||
<div class="urvanov-syntax-highlighter-main" style=""> | |||
<table class="crayon-table"> | |||
<tr class="urvanov-syntax-highlighter-row"> | |||
<td class="crayon-nums " data-settings="show"> | |||
<div class="urvanov-syntax-highlighter-nums-content" style="font-size: 12px !important; line-height: 15px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-5">5</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-6">6</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-7">7</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-8">8</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-9">9</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-10">10</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-11">11</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-12">12</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-13">13</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-14">14</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-15">15</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-16">16</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-17">17</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-18">18</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-19">19</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-20">20</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-21">21</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-22">22</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-23">23</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-24">24</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-25">25</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-26">26</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-27">27</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-28">28</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-29">29</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-30">30</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-31">31</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-32">32</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-33">33</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-34">34</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-35">35</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-36">36</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-37">37</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-38">38</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-39">39</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-40">40</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-41">41</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-42">42</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-43">43</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-44">44</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-45">45</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-46">46</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-47">47</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-48">48</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-49">49</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-50">50</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-51">51</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-52">52</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-53">53</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-54">54</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-55">55</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-56">56</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-57">57</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-58">58</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-59">59</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-60">60</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-61">61</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-62">62</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-63">63</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-64">64</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-65">65</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-66">66</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-67">67</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-68">68</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-69">69</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-70">70</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-71">71</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-72">72</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-73">73</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-74">74</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-75">75</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-76">76</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-77">77</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-78">78</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-79">79</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d52691236049-80">80</div></div> | |||
</td> | |||
<td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-1"><span class="crayon-p"># compare scaling methods for mlp inputs on regression problem</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-2"><span class="crayon-e">from </span><span class="crayon-v">sklearn</span><span class="crayon-sy">.</span><span class="crayon-e">datasets </span><span class="crayon-e">import </span><span class="crayon-e">make_regression</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-3"><span class="crayon-e">from </span><span class="crayon-v">sklearn</span><span class="crayon-sy">.</span><span class="crayon-e">preprocessing </span><span class="crayon-e">import </span><span class="crayon-e">StandardScaler</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-4"><span class="crayon-e">from </span><span class="crayon-v">sklearn</span><span class="crayon-sy">.</span><span class="crayon-e">preprocessing </span><span class="crayon-e">import </span><span class="crayon-e">MinMaxScaler</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-5"><span class="crayon-e">from </span><span class="crayon-v">keras</span><span class="crayon-sy">.</span><span class="crayon-e">layers </span><span class="crayon-e">import </span><span class="crayon-e">Dense</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-6"><span class="crayon-e">from </span><span class="crayon-v">keras</span><span class="crayon-sy">.</span><span class="crayon-e">models </span><span class="crayon-e">import </span><span class="crayon-e">Sequential</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-7"><span class="crayon-e">from </span><span class="crayon-v">keras</span><span class="crayon-sy">.</span><span class="crayon-e">optimizers </span><span class="crayon-e">import </span><span class="crayon-e">SGD</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-8"><span class="crayon-e">from </span><span class="crayon-e">matplotlib </span><span class="crayon-e">import </span><span class="crayon-e">pyplot</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-9"><span class="crayon-e">from </span><span class="crayon-e">numpy </span><span class="crayon-e">import </span><span class="crayon-e">mean</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-10"><span class="crayon-e">from </span><span class="crayon-e">numpy </span><span class="crayon-e">import </span><span class="crayon-i">std</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-11"> </div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-12"><span class="crayon-p"># prepare dataset with input and output scalers, can be none</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-13"><span class="crayon-e">def </span><span class="crayon-e">get_dataset</span><span class="crayon-sy">(</span><span class="crayon-v">input_scaler</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">output_scaler</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-14"><span class="crayon-h"> </span><span class="crayon-p"># generate dataset</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-15"><span class="crayon-h"> </span><span class="crayon-v">X</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">y</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">make_regression</span><span class="crayon-sy">(</span><span class="crayon-v">n_samples</span><span class="crayon-o">=</span><span class="crayon-cn">1000</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">n_features</span><span class="crayon-o">=</span><span class="crayon-cn">20</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">noise</span><span class="crayon-o">=</span><span class="crayon-cn">0.1</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">random_state</span><span class="crayon-o">=</span><span class="crayon-cn">1</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-16"><span class="crayon-h"> </span><span class="crayon-p"># split into train and test</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-17"><span class="crayon-h"> </span><span class="crayon-v">n_train</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-cn">500</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-18"><span class="crayon-h"> </span><span class="crayon-v">trainX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testX</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">X</span><span class="crayon-sy">[</span><span class="crayon-o">:</span><span class="crayon-v">n_train</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-o">:</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">X</span><span class="crayon-sy">[</span><span class="crayon-v">n_train</span><span class="crayon-o">:</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-o">:</span><span class="crayon-sy">]</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-19"><span class="crayon-h"> </span><span class="crayon-v">trainy</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testy</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">y</span><span class="crayon-sy">[</span><span class="crayon-o">:</span><span class="crayon-v">n_train</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">y</span><span class="crayon-sy">[</span><span class="crayon-v">n_train</span><span class="crayon-o">:</span><span class="crayon-sy">]</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-20"><span class="crayon-h"> </span><span class="crayon-p"># scale inputs</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-21"><span class="crayon-h"> </span><span class="crayon-st">if</span><span class="crayon-h"> </span><span class="crayon-e">input_scaler </span><span class="crayon-st">is</span><span class="crayon-h"> </span><span class="crayon-st">not</span><span class="crayon-h"> </span><span class="crayon-v">None</span><span class="crayon-o">:</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-22"><span class="crayon-h"> </span><span class="crayon-p"># fit scaler</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-23"><span class="crayon-h"> </span><span class="crayon-v">input_scaler</span><span class="crayon-sy">.</span><span class="crayon-e">fit</span><span class="crayon-sy">(</span><span class="crayon-v">trainX</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-24"><span class="crayon-h"> </span><span class="crayon-p"># transform training dataset</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-25"><span class="crayon-h"> </span><span class="crayon-v">trainX</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">input_scaler</span><span class="crayon-sy">.</span><span class="crayon-e">transform</span><span class="crayon-sy">(</span><span class="crayon-v">trainX</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-26"><span class="crayon-h"> </span><span class="crayon-p"># transform test dataset</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-27"><span class="crayon-h"> </span><span class="crayon-v">testX</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">input_scaler</span><span class="crayon-sy">.</span><span class="crayon-e">transform</span><span class="crayon-sy">(</span><span class="crayon-v">testX</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-28"><span class="crayon-h"> </span><span class="crayon-st">if</span><span class="crayon-h"> </span><span class="crayon-e">output_scaler </span><span class="crayon-st">is</span><span class="crayon-h"> </span><span class="crayon-st">not</span><span class="crayon-h"> </span><span class="crayon-v">None</span><span class="crayon-o">:</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-29"><span class="crayon-h"> </span><span class="crayon-p"># reshape 1d arrays to 2d arrays</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-30"><span class="crayon-h"> </span><span class="crayon-v">trainy</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">trainy</span><span class="crayon-sy">.</span><span class="crayon-e">reshape</span><span class="crayon-sy">(</span><span class="crayon-e">len</span><span class="crayon-sy">(</span><span class="crayon-v">trainy</span><span class="crayon-sy">)</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">1</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-31"><span class="crayon-h"> </span><span class="crayon-v">testy</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">testy</span><span class="crayon-sy">.</span><span class="crayon-e">reshape</span><span class="crayon-sy">(</span><span class="crayon-e">len</span><span class="crayon-sy">(</span><span class="crayon-v">trainy</span><span class="crayon-sy">)</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">1</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-32"><span class="crayon-h"> </span><span class="crayon-p"># fit scaler on training dataset</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-33"><span class="crayon-h"> </span><span class="crayon-v">output_scaler</span><span class="crayon-sy">.</span><span class="crayon-e">fit</span><span class="crayon-sy">(</span><span class="crayon-v">trainy</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-34"><span class="crayon-h"> </span><span class="crayon-p"># transform training dataset</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-35"><span class="crayon-h"> </span><span class="crayon-v">trainy</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">output_scaler</span><span class="crayon-sy">.</span><span class="crayon-e">transform</span><span class="crayon-sy">(</span><span class="crayon-v">trainy</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-36"><span class="crayon-h"> </span><span class="crayon-p"># transform test dataset</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-37"><span class="crayon-h"> </span><span class="crayon-v">testy</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">output_scaler</span><span class="crayon-sy">.</span><span class="crayon-e">transform</span><span class="crayon-sy">(</span><span class="crayon-v">testy</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-38"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-v">trainX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">trainy</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-i">testy</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-39"> </div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-40"><span class="crayon-p"># fit and evaluate mse of model on test set</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-41"><span class="crayon-e">def </span><span class="crayon-e">evaluate_model</span><span class="crayon-sy">(</span><span class="crayon-v">trainX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">trainy</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testy</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-42"><span class="crayon-h"> </span><span class="crayon-p"># define model</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-43"><span class="crayon-h"> </span><span class="crayon-v">model</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">Sequential</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-44"><span class="crayon-h"> </span><span class="crayon-v">model</span><span class="crayon-sy">.</span><span class="crayon-e">add</span><span class="crayon-sy">(</span><span class="crayon-e">Dense</span><span class="crayon-sy">(</span><span class="crayon-cn">25</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">input_dim</span><span class="crayon-o">=</span><span class="crayon-cn">20</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">activation</span><span class="crayon-o">=</span><span class="crayon-s">'relu'</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">kernel_initializer</span><span class="crayon-o">=</span><span class="crayon-s">'he_uniform'</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-45"><span class="crayon-h"> </span><span class="crayon-v">model</span><span class="crayon-sy">.</span><span class="crayon-e">add</span><span class="crayon-sy">(</span><span class="crayon-e">Dense</span><span class="crayon-sy">(</span><span class="crayon-cn">1</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">activation</span><span class="crayon-o">=</span><span class="crayon-s">'linear'</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-46"><span class="crayon-h"> </span><span class="crayon-p"># compile model</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-47"><span class="crayon-h"> </span><span class="crayon-v">model</span><span class="crayon-sy">.</span><span class="crayon-e">compile</span><span class="crayon-sy">(</span><span class="crayon-v">loss</span><span class="crayon-o">=</span><span class="crayon-s">'mean_squared_error'</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">optimizer</span><span class="crayon-o">=</span><span class="crayon-e">SGD</span><span class="crayon-sy">(</span><span class="crayon-v">lr</span><span class="crayon-o">=</span><span class="crayon-cn">0.01</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">momentum</span><span class="crayon-o">=</span><span class="crayon-cn">0.9</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-48"><span class="crayon-h"> </span><span class="crayon-p"># fit model</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-49"><span class="crayon-h"> </span><span class="crayon-v">model</span><span class="crayon-sy">.</span><span class="crayon-e">fit</span><span class="crayon-sy">(</span><span class="crayon-v">trainX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">trainy</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">epochs</span><span class="crayon-o">=</span><span class="crayon-cn">100</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">verbose</span><span class="crayon-o">=</span><span class="crayon-cn">0</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-50"><span class="crayon-h"> </span><span class="crayon-p"># evaluate the model</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-51"><span class="crayon-h"> </span><span class="crayon-v">test_mse</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">model</span><span class="crayon-sy">.</span><span class="crayon-e">evaluate</span><span class="crayon-sy">(</span><span class="crayon-v">testX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testy</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">verbose</span><span class="crayon-o">=</span><span class="crayon-cn">0</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-52"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-v">test</span><span class="crayon-sy">_</span>mse</div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-53"> </div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-54"><span class="crayon-p"># evaluate model multiple times with given input and output scalers</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-55"><span class="crayon-e">def </span><span class="crayon-e">repeated_evaluation</span><span class="crayon-sy">(</span><span class="crayon-v">input_scaler</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">output_scaler</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">n_repeats</span><span class="crayon-o">=</span><span class="crayon-cn">30</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-56"><span class="crayon-h"> </span><span class="crayon-p"># get dataset</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-57"><span class="crayon-h"> </span><span class="crayon-v">trainX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">trainy</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testy</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">get_dataset</span><span class="crayon-sy">(</span><span class="crayon-v">input_scaler</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">output_scaler</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-58"><span class="crayon-h"> </span><span class="crayon-p"># repeated evaluation of model</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-59"><span class="crayon-h"> </span><span class="crayon-v">results</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">list</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-60"><span class="crayon-h"> </span><span class="crayon-st">for</span><span class="crayon-h"> </span><span class="crayon-i">_</span><span class="crayon-h"> </span><span class="crayon-st">in</span><span class="crayon-h"> </span><span class="crayon-e">range</span><span class="crayon-sy">(</span><span class="crayon-v">n_repeats</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-61"><span class="crayon-h"> </span><span class="crayon-v">test_mse</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">evaluate_model</span><span class="crayon-sy">(</span><span class="crayon-v">trainX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">trainy</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testX</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">testy</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-62"><span class="crayon-h"> </span><span class="crayon-e">print</span><span class="crayon-sy">(</span><span class="crayon-s">'>%.3f'</span><span class="crayon-h"> </span><span class="crayon-o">%</span><span class="crayon-h"> </span><span class="crayon-v">test_mse</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-63"><span class="crayon-h"> </span><span class="crayon-v">results</span><span class="crayon-sy">.</span><span class="crayon-e">append</span><span class="crayon-sy">(</span><span class="crayon-v">test_mse</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-64"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-i">results</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-65"> </div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-66"><span class="crayon-p"># unscaled inputs</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-67"><span class="crayon-v">results_unscaled_inputs</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">repeated_evaluation</span><span class="crayon-sy">(</span><span class="crayon-v">None</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-e">StandardScaler</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-68"><span class="crayon-p"># normalized inputs</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-69"><span class="crayon-v">results_normalized_inputs</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">repeated_evaluation</span><span class="crayon-sy">(</span><span class="crayon-e">MinMaxScaler</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-e">StandardScaler</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-70"><span class="crayon-p"># standardized inputs</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-71"><span class="crayon-v">results_standardized_inputs</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">repeated_evaluation</span><span class="crayon-sy">(</span><span class="crayon-e">StandardScaler</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-e">StandardScaler</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-72"><span class="crayon-p"># summarize results</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-73"><span class="crayon-e">print</span><span class="crayon-sy">(</span><span class="crayon-s">'Unscaled: %.3f (%.3f)'</span><span class="crayon-h"> </span><span class="crayon-o">%</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-e">mean</span><span class="crayon-sy">(</span><span class="crayon-v">results_unscaled_inputs</span><span class="crayon-sy">)</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-e">std</span><span class="crayon-sy">(</span><span class="crayon-v">results_unscaled_inputs</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-74"><span class="crayon-e">print</span><span class="crayon-sy">(</span><span class="crayon-s">'Normalized: %.3f (%.3f)'</span><span class="crayon-h"> </span><span class="crayon-o">%</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-e">mean</span><span class="crayon-sy">(</span><span class="crayon-v">results_normalized_inputs</span><span class="crayon-sy">)</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-e">std</span><span class="crayon-sy">(</span><span class="crayon-v">results_normalized_inputs</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-75"><span class="crayon-e">print</span><span class="crayon-sy">(</span><span class="crayon-s">'Standardized: %.3f (%.3f)'</span><span class="crayon-h"> </span><span class="crayon-o">%</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-e">mean</span><span class="crayon-sy">(</span><span class="crayon-v">results_standardized_inputs</span><span class="crayon-sy">)</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-e">std</span><span class="crayon-sy">(</span><span class="crayon-v">results_standardized_inputs</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-76"><span class="crayon-p"># plot results</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-77"><span class="crayon-v">results</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-sy">[</span><span class="crayon-v">results_unscaled_inputs</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">results_normalized_inputs</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">results_standardized_inputs</span><span class="crayon-sy">]</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-78"><span class="crayon-v">labels</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-sy">[</span><span class="crayon-s">'unscaled'</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-s">'normalized'</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-s">'standardized'</span><span class="crayon-sy">]</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-79"><span class="crayon-v">pyplot</span><span class="crayon-sy">.</span><span class="crayon-e">boxplot</span><span class="crayon-sy">(</span><span class="crayon-v">results</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">labels</span><span class="crayon-o">=</span><span class="crayon-v">labels</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d52691236049-80"><span class="crayon-v">pyplot</span><span class="crayon-sy">.</span><span class="crayon-e">show</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div></div></td> | |||
</tr> | |||
</table> | |||
</div> | |||
</div><p>Running the example prints the mean squared error for each model run along the way.</p> | |||
<p>After each of the three configurations have been evaluated 30 times each, the mean errors for each are reported.</p> | |||
<p><strong>Note</strong>: Your <a href="https://machinelearningmastery.com/different-results-each-time-in-machine-learning/">results may vary</a> given the stochastic nature of the algorithm or evaluation procedure, or differences in numerical precision. Consider running the example a few times and compare the average outcome.</p> | |||
<p>In this case, we can see that as we expected, scaling the input variables does result in a model with better performance. Unexpectedly, better performance is seen using normalized inputs instead of standardized inputs. This may be related to the choice of the rectified linear activation function in the first hidden layer.</p> | |||
<div id="urvanov-syntax-highlighter-613eee9a17d55153126715" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-mac print-yes notranslate" data-settings=" minimize scroll-mouseover disable-anim" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;"> | |||
<div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><span class="crayon-title"></span> | |||
<div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;"><div class="crayon-button urvanov-syntax-highlighter-nums-button" title="Toggle Line Numbers"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-plain-button" title="Toggle Plain Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-wrap-button" title="Toggle Line Wrap"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-expand-button" title="Expand Code"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-copy-button" title="Copy"><div class="urvanov-syntax-highlighter-button-icon"></div></div><div class="crayon-button urvanov-syntax-highlighter-popup-button" title="Open Code In New Window"><div class="urvanov-syntax-highlighter-button-icon"></div></div></div></div> | |||
<div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;"></div> | |||
<div class="urvanov-syntax-highlighter-plain-wrap"><textarea wrap="soft" class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;"> | |||
... | |||
>0.010 | |||
>0.012 | |||
>0.005 | |||
>0.008 | |||
>0.008 | |||
Unscaled: 0.007 (0.004) | |||
Normalized: 0.001 (0.000) | |||
Standardized: 0.008 (0.004)</textarea></div> | |||
<div class="urvanov-syntax-highlighter-main" style=""> | |||
<table class="crayon-table"> | |||
<tr class="urvanov-syntax-highlighter-row"> | |||
<td class="crayon-nums " data-settings="show"> | |||
<div class="urvanov-syntax-highlighter-nums-content" style="font-size: 12px !important; line-height: 15px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d55153126715-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d55153126715-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d55153126715-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d55153126715-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d55153126715-5">5</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d55153126715-6">6</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d55153126715-7">7</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-613eee9a17d55153126715-8">8</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-613eee9a17d55153126715-9">9</div></div> | |||
</td> | |||
<td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d55153126715-1">...</div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d55153126715-2">>0.010</div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d55153126715-3">>0.012</div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d55153126715-4">>0.005</div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d55153126715-5">>0.008</div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d55153126715-6">>0.008</div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d55153126715-7">Unscaled: 0.007 (0.004)</div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-613eee9a17d55153126715-8">Normalized: 0.001 (0.000)</div><div class="crayon-line" id="urvanov-syntax-highlighter-613eee9a17d55153126715-9">Standardized: 0.008 (0.004)</div></div></td> | |||
</tr> | |||
</table> | |||
</div> | |||
</div><p>A figure with three box and whisker plots is created summarizing the spread of error scores for each configuration.</p> | |||
<p>The plots show that there was little difference between the distributions of error scores for the unscaled and standardized input variables, and that the normalized input variables result in better performance and more stable or a tighter distribution of error scores.</p> | |||
<p>These results highlight that it is important to actually experiment and confirm the results of data scaling methods rather than assuming that a given data preparation scheme will work best based on the observed distribution of the data.</p> | |||
<div id="attachment_6943" style="width: 1290px" class="wp-caption aligncenter"><img aria-describedby="caption-attachment-6943" loading="lazy" class="size-full wp-image-6943" src="https://machinelearningmastery.com/wp-content/uploads/2018/11/Box-and-Whisker-Plots-of-Mean-Squared-Error-With-Unscaled-Normalized-and-Standardized-Input-Variables-for-the-Regression-Problem.png" alt="Box and Whisker Plots of Mean Squared Error With Unscaled, Normalized and Standardized Input Variables for the Regression Problem" width="1280" height="960" srcset="https://machinelearningmastery.com/wp-content/uploads/2018/11/Box-and-Whisker-Plots-of-Mean-Squared-Error-With-Unscaled-Normalized-and-Standardized-Input-Variables-for-the-Regression-Problem.png 1280w, https://machinelearningmastery.com/wp-content/uploads/2018/11/Box-and-Whisker-Plots-of-Mean-Squared-Error-With-Unscaled-Normalized-and-Standardized-Input-Variables-for-the-Regression-Problem-300x225.png 300w, https://machinelearningmastery.com/wp-content/uploads/2018/11/Box-and-Whisker-Plots-of-Mean-Squared-Error-With-Unscaled-Normalized-and-Standardized-Input-Variables-for-the-Regression-Problem-768x576.png 768w, https://machinelearningmastery.com/wp-content/uploads/2018/11/Box-and-Whisker-Plots-of-Mean-Squared-Error-With-Unscaled-Normalized-and-Standardized-Input-Variables-for-the-Regression-Problem-1024x768.png 1024w" sizes="(max-width: 1280px) 100vw, 1280px" /><p id="caption-attachment-6943" class="wp-caption-text">Box and Whisker Plots of Mean Squared Error With Unscaled, Normalized and Standardized Input Variables for the Regression Problem</p></div> | |||
<h2>Extensions</h2> | |||
<p>This section lists some ideas for extending the tutorial that you may wish to explore.</p> | |||
<ul> | |||
<li><strong>Normalize Target Variable</strong>. Update the example and normalize instead of standardize the target variable and compare results.</li> | |||
<li><strong>Compared Scaling for Target Variable</strong>. Update the example to compare standardizing and normalizing the target variable using repeated experiments and compare the results.</li> | |||
<li><strong>Other Scales</strong>. Update the example to evaluate other min/max scales when normalizing and compare performance, e.g. [-1, 1] and [0.0, 0.5].</li> | |||
</ul> | |||
<p>If you explore any of these extensions, I’d love to know.</p> | |||
<h2>Further Reading</h2> | |||
<p>This section provides more resources on the topic if you are looking to go deeper.</p> | |||
<h3>Posts</h3> | |||
<ul> | |||
<li><a href="https://machinelearningmastery.com/how-to-scale-data-for-long-short-term-memory-networks-in-python/">How to Scale Data for Long Short-Term Memory Networks in Python</a></li> | |||
<li><a href="https://machinelearningmastery.com/scale-machine-learning-data-scratch-python/">How to Scale Machine Learning Data From Scratch With Python</a></li> | |||
<li><a href="https://machinelearningmastery.com/normalize-standardize-time-series-data-python/">How to Normalize and Standardize Time Series Data in Python</a></li> | |||
<li><a href="https://machinelearningmastery.com/prepare-data-machine-learning-python-scikit-learn/">How to Prepare Your Data for Machine Learning in Python with Scikit-Learn</a></li> | |||
</ul> | |||
<h3>Books</h3> | |||
<ul> | |||
<li>Section 8.2 Input normalization and encoding, <a href="https://amzn.to/2S8qdwt">Neural Networks for Pattern Recognition</a>, 1995.</li> | |||
</ul> | |||
<h3>API</h3> | |||
<ul> | |||
<li><a href="http://scikit-learn.org/stable/modules/generated/sklearn.datasets.make_regression.html">sklearn.datasets.make_regression API</a></li> | |||
<li><a href="http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.MinMaxScaler.html">sklearn.preprocessing.MinMaxScaler API</a></li> | |||
<li><a href="http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.StandardScaler.html">sklearn.preprocessing.StandardScaler API</a></li> | |||
</ul> | |||
<h3>Articles</h3> | |||
<ul> | |||
<li><a href="ftp://ftp.sas.com/pub/neural/FAQ2.html#A_std">Should I normalize/standardize/rescale the data? Neural Nets FAQ</a></li> | |||
</ul> | |||
<h2>Summary</h2> | |||
<p>In this tutorial, you discovered how to improve neural network stability and modeling performance by scaling data.</p> | |||
<p>Specifically, you learned:</p> | |||
<ul> | |||
<li>Data scaling is a recommended pre-processing step when working with deep learning neural networks.</li> | |||
<li>Data scaling can be achieved by normalizing or standardizing real-valued input and output variables.</li> | |||
<li>How to apply standardization and normalization to improve the performance of a Multilayer Perceptron model on a regression predictive modeling problem.</li> | |||
</ul> | |||
<p>Do you have any questions?<br /> | |||
Ask your questions in the comments below and I will do my best to answer.</p> | |||
<div class="widget_text awac-wrapper" id="custom_html-66"><div class="widget_text awac widget custom_html-66"><div class="textwidget custom-html-widget"><div style="text-align: center;"> | |||
<div class="woo-sc-hr"></div> | |||
<h2>Develop Better Deep Learning Models Today!</h2> | |||
<a href="/better-deep-learning/" rel="nofollow"><img width="220" height="311" style="border: 0;" src="/wp-content/uploads/2018/12/Cover-220.png" alt="Better Deep Learning" align="left" /></a> | |||
<h4>Train Faster, Reduce Overftting, and Ensembles</h4> | |||
<p>...with just a few lines of python code</p> | |||
<p>Discover how in my new Ebook:<br> | |||
<a href="/better-deep-learning/" rel="nofollow">Better Deep Learning</a></p> | |||
<p>It provides <strong>self-study tutorials</strong> on topics like:<br> <em>weight decay</em>, <em>batch normalization</em>, <em>dropout</em>, <em>model stacking</em> and much more...</p> | |||
<h4>Bring better deep learning to your projects!</h4> | |||
<p>Skip the Academics. Just Results.</p> | |||
<a href="/better-deep-learning/" class="woo-sc-button red" ><span class="woo-">See What's Inside</span></a> | |||
<div class="woo-sc-hr"></div> | |||
</div></div></div></div><div class="simplesocialbuttons simplesocial-simple-icons simplesocialbuttons_inline simplesocialbuttons-align-left post-6939 post simplesocialbuttons-inline-no-animation"> | |||
<button class="ssb_tweet-icon" data-href="https://twitter.com/share?text=How+to+use+Data+Scaling+Improve+Deep+Learning+Model+Stability+and+Performance&url=https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/" rel="nofollow" onclick="if (!window.__cfRLUnblockHandlers) return false; javascript:window.open(this.dataset.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;" data-cf-modified-f3886dae12b0536ad361ea93-=""> | |||
<span class="icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 72 72"><path fill="none" d="M0 0h72v72H0z"/><path class="icon" fill="#fff" d="M68.812 15.14c-2.348 1.04-4.87 1.744-7.52 2.06 2.704-1.62 4.78-4.186 5.757-7.243-2.53 1.5-5.33 2.592-8.314 3.176C56.35 10.59 52.948 9 49.182 9c-7.23 0-13.092 5.86-13.092 13.093 0 1.026.118 2.02.338 2.98C25.543 24.527 15.9 19.318 9.44 11.396c-1.125 1.936-1.77 4.184-1.77 6.58 0 4.543 2.312 8.552 5.824 10.9-2.146-.07-4.165-.658-5.93-1.64-.002.056-.002.11-.002.163 0 6.345 4.513 11.638 10.504 12.84-1.1.298-2.256.457-3.45.457-.845 0-1.666-.078-2.464-.23 1.667 5.2 6.5 8.985 12.23 9.09-4.482 3.51-10.13 5.605-16.26 5.605-1.055 0-2.096-.06-3.122-.184 5.794 3.717 12.676 5.882 20.067 5.882 24.083 0 37.25-19.95 37.25-37.25 0-.565-.013-1.133-.038-1.693 2.558-1.847 4.778-4.15 6.532-6.774z"/></svg></span><i class="simplesocialtxt">Tweet </i></button> | |||
<button class="ssb_fbshare-icon" target="_blank" data-href="https://www.facebook.com/sharer/sharer.php?u=https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/" onclick="if (!window.__cfRLUnblockHandlers) return false; javascript:window.open(this.dataset.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;" data-cf-modified-f3886dae12b0536ad361ea93-=""> | |||
<span class="icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" class="_1pbq" color="#ffffff"><path fill="#ffffff" fill-rule="evenodd" class="icon" d="M8 14H3.667C2.733 13.9 2 13.167 2 12.233V3.667A1.65 1.65 0 0 1 3.667 2h8.666A1.65 1.65 0 0 1 14 3.667v8.566c0 .934-.733 1.667-1.667 1.767H10v-3.967h1.3l.7-2.066h-2V6.933c0-.466.167-.9.867-.9H12v-1.8c.033 0-.933-.266-1.533-.266-1.267 0-2.434.7-2.467 2.133v1.867H6v2.066h2V14z"></path></svg></span> | |||
<span class="simplesocialtxt">Share </span> </button> | |||
<button class="ssb_linkedin-icon" data-href="https://www.linkedin.com/cws/share?url=https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/" onclick="if (!window.__cfRLUnblockHandlers) return false; javascript:window.open(this.dataset.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;" data-cf-modified-f3886dae12b0536ad361ea93-=""> | |||
<span class="icon"> <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="15px" height="14.1px" viewBox="-301.4 387.5 15 14.1" enable-background="new -301.4 387.5 15 14.1" xml:space="preserve"> <g id="XMLID_398_"> <path id="XMLID_399_" fill="#FFFFFF" d="M-296.2,401.6c0-3.2,0-6.3,0-9.5h0.1c1,0,2,0,2.9,0c0.1,0,0.1,0,0.1,0.1c0,0.4,0,0.8,0,1.2 c0.1-0.1,0.2-0.3,0.3-0.4c0.5-0.7,1.2-1,2.1-1.1c0.8-0.1,1.5,0,2.2,0.3c0.7,0.4,1.2,0.8,1.5,1.4c0.4,0.8,0.6,1.7,0.6,2.5 c0,1.8,0,3.6,0,5.4v0.1c-1.1,0-2.1,0-3.2,0c0-0.1,0-0.1,0-0.2c0-1.6,0-3.2,0-4.8c0-0.4,0-0.8-0.2-1.2c-0.2-0.7-0.8-1-1.6-1 c-0.8,0.1-1.3,0.5-1.6,1.2c-0.1,0.2-0.1,0.5-0.1,0.8c0,1.7,0,3.4,0,5.1c0,0.2,0,0.2-0.2,0.2c-1,0-1.9,0-2.9,0 C-296.1,401.6-296.2,401.6-296.2,401.6z"/> <path id="XMLID_400_" fill="#FFFFFF" d="M-298,401.6L-298,401.6c-1.1,0-2.1,0-3,0c-0.1,0-0.1,0-0.1-0.1c0-3.1,0-6.1,0-9.2 c0-0.1,0-0.1,0.1-0.1c1,0,2,0,2.9,0h0.1C-298,395.3-298,398.5-298,401.6z"/> <path id="XMLID_401_" fill="#FFFFFF" d="M-299.6,390.9c-0.7-0.1-1.2-0.3-1.6-0.8c-0.5-0.8-0.2-2.1,1-2.4c0.6-0.2,1.2-0.1,1.8,0.2 c0.5,0.4,0.7,0.9,0.6,1.5c-0.1,0.7-0.5,1.1-1.1,1.3C-299.1,390.8-299.4,390.8-299.6,390.9L-299.6,390.9z"/> </g> </svg> </span> | |||
<span class="simplesocialtxt">Share</span> </button> | |||
</div> | |||
</section><!-- /.entry --> | |||
<div class="fix"></div> | |||
<aside id="post-author"> | |||
<div class="profile-image"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=160&d=mm&r=g 2x' class='avatar avatar-80 photo' height='80' width='80' loading='lazy'/></div> | |||
<div class="profile-content"> | |||
<h4>About Jason Brownlee</h4> | |||
Jason Brownlee, PhD is a machine learning specialist who teaches developers how to get results with modern machine learning methods via hands-on tutorials. <div class="profile-link"> | |||
<a href="https://machinelearningmastery.com/author/jasonb/"> | |||
View all posts by Jason Brownlee <span class="meta-nav">→</span> </a> | |||
</div><!--#profile-link--> | |||
</div> | |||
<div class="fix"></div> | |||
</aside> | |||
<div class="post-utility"></div> | |||
</article><!-- /.post --> | |||
<div class="post-entries"> | |||
<div class="nav-prev fl"><a href="https://machinelearningmastery.com/greedy-layer-wise-pretraining-tutorial/" rel="prev"><i class="fa fa-angle-left"></i> How to Use Greedy Layer-Wise Pretraining in Deep Learning Neural Networks</a></div> | |||
<div class="nav-next fr"><a href="https://machinelearningmastery.com/how-to-avoid-exploding-gradients-in-neural-networks-with-gradient-clipping/" rel="next">How to Avoid Exploding Gradients With Gradient Clipping <i class="fa fa-angle-right"></i></a></div> | |||
<div class="fix"></div> | |||
</div> | |||
<div id="comments"> <h3 id="comments-title">125 Responses to <em>How to use Data Scaling Improve Deep Learning Model Stability and Performance</em></h3> | |||
<ol class="commentlist"> | |||
<li id="comment-468125" class="comment even thread-even depth-1"> | |||
<div id="li-comment-468125" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/5f4a8c800d8bc7448b290942ea3679f6?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/5f4a8c800d8bc7448b290942ea3679f6?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Wonbin</span> | |||
<span class="date">February 13, 2019 at 6:03 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-468125" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Thank you for this helpful post for beginners!</p> | |||
<p>Could you please provide more details about the steps of “using the root mean squared error on the unscaled data” to interpret the performance in a specific domain?</p> | |||
<p>Would it be like this??<br /> | |||
———————————————————–<br /> | |||
1. Finalize the model (based on the performance being calculated from the scaled output variable)<br /> | |||
2. Make predictions on test set<br /> | |||
3. Invert the predictions (to convert them back into their original scale)<br /> | |||
4. Calculate the metrics (e.g. RMSE, MAPE)<br /> | |||
———————————————————–</p> | |||
<p>Waiting for your reply! Cheers mate!</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-468125' data-commentid="468125" data-postid="6939" data-belowelement="comment-468125" data-respondelement="respond" data-replyto="Reply to Wonbin" aria-label='Reply to Wonbin'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-468241" class="comment byuser comment-author-jasonb bypostauthor odd alt depth-2"> | |||
<div id="li-comment-468241" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">February 14, 2019 at 8:39 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-468241" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Correct.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-468241' data-commentid="468241" data-postid="6939" data-belowelement="comment-468241" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-483399" class="comment even depth-2"> | |||
<div id="li-comment-483399" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/ec47f14bc90cbce4c89a445adfd2b7a8?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/ec47f14bc90cbce4c89a445adfd2b7a8?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">ajebulon</span> | |||
<span class="date">April 30, 2019 at 2:44 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-483399" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Really nice article! I got Some quick questions,</p> | |||
<p>If I have multiple input columns, each has different value range, might be [0, 1000] or even a one-hot-encoded data, should all be scaled with same method, or it can be processed differently? </p> | |||
<p>For example:<br /> | |||
– input A is normalized to [0, 1],<br /> | |||
– input B is normalized to [-1, 1],<br /> | |||
– input C is standardized,<br /> | |||
– one-hot-encoded data is not scaled</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-483399' data-commentid="483399" data-postid="6939" data-belowelement="comment-483399" data-respondelement="respond" data-replyto="Reply to ajebulon" aria-label='Reply to ajebulon'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-483508" class="comment byuser comment-author-jasonb bypostauthor odd alt depth-3"> | |||
<div id="li-comment-483508" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">May 1, 2019 at 6:58 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-483508" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Yes, typically it is a good idea to scale all columns to have the same range. Perhaps start with [0,1] and compare others to see if they result in an improvement.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-483508' data-commentid="483508" data-postid="6939" data-belowelement="comment-483508" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-469330" class="comment even thread-odd thread-alt depth-1"> | |||
<div id="li-comment-469330" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/fda5f1787e764f8a4c0b7ba80e7be001?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/fda5f1787e764f8a4c0b7ba80e7be001?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">mk123qwe</span> | |||
<span class="date">February 19, 2019 at 5:38 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-469330" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>we want standardized inputs, no scaling of outputs,but outputs value is not in (0,1).Are the predictions inaccurate?</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-469330' data-commentid="469330" data-postid="6939" data-belowelement="comment-469330" data-respondelement="respond" data-replyto="Reply to mk123qwe" aria-label='Reply to mk123qwe'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-469398" class="comment byuser comment-author-jasonb bypostauthor odd alt depth-2"> | |||
<div id="li-comment-469398" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">February 20, 2019 at 7:51 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-469398" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>I don’t follow, are what predictions accurate?</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-469398' data-commentid="469398" data-postid="6939" data-belowelement="comment-469398" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-471145" class="comment even thread-even depth-1"> | |||
<div id="li-comment-471145" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/716e4d0c9a6ea39edb7c9e05126eb3a0?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/716e4d0c9a6ea39edb7c9e05126eb3a0?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">yingxiao kong</span> | |||
<span class="date">February 28, 2019 at 8:17 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-471145" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi Jason,</p> | |||
<p>Your experiment is very helpful for me to understand the difference between different methods, actually I have also done similar things. I always standardized the input data. I have compared the results between standardized and standardized targets. The plots shows that with standardized targets, the network seems to work better. However, here I have a question: suppose the standard deviation of my target is 300, then I think the MSE will be strongly decreased after you fixed the standard deviation to 1. So shall we multiply the original std to the MSE in order to get the MSE in the original target value space?</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-471145' data-commentid="471145" data-postid="6939" data-belowelement="comment-471145" data-respondelement="respond" data-replyto="Reply to yingxiao kong" aria-label='Reply to yingxiao kong'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-471186" class="comment byuser comment-author-jasonb bypostauthor odd alt depth-2"> | |||
<div id="li-comment-471186" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">February 28, 2019 at 2:32 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-471186" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>You can invert the standardization, by adding the mean and multiplying by the stdev.</p> | |||
<p>I also have an example here using the sklaern:<br /> | |||
<a href="https://machinelearningmastery.com/machine-learning-data-transforms-for-time-series-forecasting/" rel="nofollow ugc">https://machinelearningmastery.com/machine-learning-data-transforms-for-time-series-forecasting/</a></p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-471186' data-commentid="471186" data-postid="6939" data-belowelement="comment-471186" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-473491" class="comment even thread-odd thread-alt depth-1"> | |||
<div id="li-comment-473491" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/0243aa151ffd1cb2f529425448cfd9c2?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/0243aa151ffd1cb2f529425448cfd9c2?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Beato</span> | |||
<span class="date">March 11, 2019 at 3:51 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-473491" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi Jason,</p> | |||
<p>My data includes categorical and continued data. Could I transform the categorical data with 1,2,3…into standardized data and put them into the neural network models to make classification? Or do I need to transformr the categorical data with with one-hot coding(0,1)? I have been confused about it. Thanks</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-473491' data-commentid="473491" data-postid="6939" data-belowelement="comment-473491" data-respondelement="respond" data-replyto="Reply to Beato" aria-label='Reply to Beato'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-473523" class="comment byuser comment-author-jasonb bypostauthor odd alt depth-2"> | |||
<div id="li-comment-473523" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">March 11, 2019 at 6:53 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-473523" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Yes, perhaps try it and compare results?</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-473523' data-commentid="473523" data-postid="6939" data-belowelement="comment-473523" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-474594" class="comment even thread-even depth-1"> | |||
<div id="li-comment-474594" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/49db595c12abdfab10272020470cfc8d?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/49db595c12abdfab10272020470cfc8d?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Bart</span> | |||
<span class="date">March 16, 2019 at 5:23 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-474594" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi Jason, I have a specific Question regarding the normalization (min-max scaling) of the output value. Usually you are supposed to use normalization only on the training data set and then apply those stats to the validation and test set. Otherwise you would feed the model at training time certain information about the world it shouldn’t have access to. (The Elements of Statistical Learning: Data Mining, Inference, and Prediction p.247)</p> | |||
<p>But for instance, my output value is a single percentage value ranging [0, 100%] and I am using the ReLU activation function in my output layer. I know for sure that in the “real world” regarding my problem statement, that I will get samples ranging form 60 – 100%. But my training sample size is to small and does not contain enough data points including all possible output values. So here comes my question: Should I stay with my initial statement (normalization only on training data set) or should I apply the maximum possible value of 100% to max()-value of the normalization step? The latter would contradict the literature. Best Regards Bart</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-474594' data-commentid="474594" data-postid="6939" data-belowelement="comment-474594" data-respondelement="respond" data-replyto="Reply to Bart" aria-label='Reply to Bart'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-474629" class="comment byuser comment-author-jasonb bypostauthor odd alt depth-2"> | |||
<div id="li-comment-474629" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">March 16, 2019 at 8:02 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-474629" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Correct.</p> | |||
<p>I would recommend a sigmoid activation in the output.</p> | |||
<p>I would then recommend interpreting the 0-1 scale as 60-100 prior to model evaluation.</p> | |||
<p>Does that help?</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-474629' data-commentid="474629" data-postid="6939" data-belowelement="comment-474629" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-474728" class="comment even depth-3"> | |||
<div id="li-comment-474728" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/49db595c12abdfab10272020470cfc8d?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/49db595c12abdfab10272020470cfc8d?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Bart</span> | |||
<span class="date">March 17, 2019 at 1:37 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-474728" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>I’m not quite sure what you mean by your second recommendation. How would I achieve that?</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-474728' data-commentid="474728" data-postid="6939" data-belowelement="comment-474728" data-respondelement="respond" data-replyto="Reply to Bart" aria-label='Reply to Bart'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-474757" class="comment byuser comment-author-jasonb bypostauthor odd alt depth-4"> | |||
<div id="li-comment-474757" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">March 17, 2019 at 6:24 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-474757" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>You can project the scale of 0-1 to anything you want, such as 60-100.</p> | |||
<p>First rescale to a number between 0 and 40 (value * 40) then add the min value (+ 60)</p> | |||
<p>result = value * 40 + 60</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-474757' data-commentid="474757" data-postid="6939" data-belowelement="comment-474757" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-476186" class="comment even thread-odd thread-alt depth-1"> | |||
<div id="li-comment-476186" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/0884dd13d296d711d439bc0b6287cda4?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/0884dd13d296d711d439bc0b6287cda4?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Mike</span> | |||
<span class="date">March 25, 2019 at 1:04 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-476186" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Dear Jason, thank you for the great article.</p> | |||
<p>I am wondering if there is any advantage using StadardScaler or MinMaxScaler over scaling manually. I could calculate the mean, std or min, max of my training data and apply them with the corresponding formula for standard or minmax scaling.</p> | |||
<p>Would this approach produce the same results as the StadardScaler or MinMaxScaler or are the sklearn scalers special?</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-476186' data-commentid="476186" data-postid="6939" data-belowelement="comment-476186" data-respondelement="respond" data-replyto="Reply to Mike" aria-label='Reply to Mike'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-476218" class="comment byuser comment-author-jasonb bypostauthor odd alt depth-2"> | |||
<div id="li-comment-476218" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">March 25, 2019 at 6:46 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-476218" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Yes, it is reliable bug free code all wrapped up in a single class – making it harder to introduce new bugs.</p> | |||
<p>Same results as manual, if you coded the manual scaling correctly.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-476218' data-commentid="476218" data-postid="6939" data-belowelement="comment-476218" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-484599" class="comment even thread-even depth-1"> | |||
<div id="li-comment-484599" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/d2d0dadc3e4cd520f14f70264094f8f1?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/d2d0dadc3e4cd520f14f70264094f8f1?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Magnus</span> | |||
<span class="date">May 9, 2019 at 8:32 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-484599" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Dear Jason,</p> | |||
<p>I have a few questions from section “Data normalization”. You mention that we should estimate the max and min values, and use that to normalize the training set to e.g. [-1,1]. But what if the max and min values are in the validation or test set? Then I might get values e.g. [-1.2, 1.3] in the validation set. Do you consider this to be incorrect or not? </p> | |||
<p>Another approach is then to make sure that the min and max values for all parameters are contained in the training set. What are your thoughts on this? Is this the way to do it? Or should we use the max and min values for all data combined (training, validation and test sets) when normalizing the training set? </p> | |||
<p>For the moment I use the MinMaxScaler and fit_transform on the training set and then apply that scaler on the validation and test set using transform. But I realise that some of my max values are in the validation set. I suppose this is also related to network saturation.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-484599' data-commentid="484599" data-postid="6939" data-belowelement="comment-484599" data-respondelement="respond" data-replyto="Reply to Magnus" aria-label='Reply to Magnus'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-484663" class="comment byuser comment-author-jasonb bypostauthor odd alt depth-2"> | |||
<div id="li-comment-484663" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">May 10, 2019 at 8:16 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-484663" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Perhaps estimate the min/max using domain knowledge. If new data exceeded the limits, snap to known limits, or not – test and see how the model is impacted.</p> | |||
<p>Regardless, the training set must be representative of the problem.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-484663' data-commentid="484663" data-postid="6939" data-belowelement="comment-484663" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-487085" class="comment even thread-odd thread-alt depth-1"> | |||
<div id="li-comment-487085" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/bce58c4b2459db4a5d3793f7ecfded68?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/bce58c4b2459db4a5d3793f7ecfded68?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">youssef</span> | |||
<span class="date">May 30, 2019 at 9:33 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-487085" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hello Jason, I am a huge fan of your work! Thank you so much for your insightful tutorials. You are a life saver! I have a small question if i may: </p> | |||
<p>I am trying to fit spectrograms in a cnn in order to do some classification tasks. Unfortunately each spectrogram is around (3000,300) array. Is there a way to reduce the dimensionality without losing so much information?</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-487085' data-commentid="487085" data-postid="6939" data-belowelement="comment-487085" data-respondelement="respond" data-replyto="Reply to youssef" aria-label='Reply to youssef'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-487103" class="comment byuser comment-author-jasonb bypostauthor odd alt depth-2"> | |||
<div id="li-comment-487103" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">May 30, 2019 at 2:50 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-487103" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Ouch, perhaps start with simple downsampling and see what effect that has?</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-487103' data-commentid="487103" data-postid="6939" data-belowelement="comment-487103" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-491581" class="comment even thread-even depth-1"> | |||
<div id="li-comment-491581" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/bfc82179cc284608a467f9bd6338e8a3?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/bfc82179cc284608a467f9bd6338e8a3?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Muktamani</span> | |||
<span class="date">July 4, 2019 at 8:54 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-491581" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi Jason,<br /> | |||
It was always good and informative to go through your blogs and your interaction with comments by different people all across the globe.<br /> | |||
I have question regarding the scaling techniques.</p> | |||
<p>As you explained about scaling :<br /> | |||
Case1:<br /> | |||
# created scaler<br /> | |||
scaler = StandardScaler()<br /> | |||
# fit scaler on training dataset<br /> | |||
scaler.fit(trainy)<br /> | |||
# transform training dataset<br /> | |||
trainy = scaler.transform(trainy)<br /> | |||
# transform test dataset<br /> | |||
testy = scaler.transform(testy)</p> | |||
<p>in this case mean and standard deviation for all train and test remain same. </p> | |||
<p>What i approached is:<br /> | |||
case2<br /> | |||
# created scaler<br /> | |||
scaler_train = StandardScaler()<br /> | |||
# fit scaler on training dataset<br /> | |||
scaler_train.fit(trainy)<br /> | |||
# transform training dataset<br /> | |||
trainy = scaler_train.transform(trainy)</p> | |||
<p># created scaler<br /> | |||
scaler_test = StandardScaler()<br /> | |||
# fit scaler on training dataset<br /> | |||
scaler_test.fit(trainy)<br /> | |||
# transform test dataset<br /> | |||
testy = scaler_test.transform(testy)</p> | |||
<p>Here the mean and standard deviation in train data and test data are different.so model may find the test data completely unknown and new .rather in first case where mean and standard deviation is same on train and test data that may leads to providing the known test data to model (known in term of same mean and standard deviation treatment).</p> | |||
<p>Jason,can you guide me if my logics is good to go with case2 or shall i consider case1 .<br /> | |||
or if logic is wrong you can also say that and explain.<br /> | |||
(Also i applied Same for min-max scaling i.e normalization, if i choose this then)<br /> | |||
Again thanks Jason for such a nice work !</p> | |||
<p>Happy Learning !!</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-491581' data-commentid="491581" data-postid="6939" data-belowelement="comment-491581" data-respondelement="respond" data-replyto="Reply to Muktamani" aria-label='Reply to Muktamani'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-491669" class="comment byuser comment-author-jasonb bypostauthor odd alt depth-2"> | |||
<div id="li-comment-491669" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">July 5, 2019 at 8:06 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-491669" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>I recommend fitting the scaler on the training dataset once, then apply it to transform the training dataset and test set.</p> | |||
<p>If you fit the scaler using the test dataset, you will have data leakage and possibly an invalid estimate of model performance.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-491669' data-commentid="491669" data-postid="6939" data-belowelement="comment-491669" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-491776" class="comment even thread-odd thread-alt depth-1"> | |||
<div id="li-comment-491776" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/0a3cc60781e8ac093426c5c4d8d4acd7?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/0a3cc60781e8ac093426c5c4d8d4acd7?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">ICHaLiL</span> | |||
<span class="date">July 6, 2019 at 12:13 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-491776" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi Jason,</p> | |||
<p>I’m working on sequence2sequence problem. Input’s max and min points are around 500-300, however output’s are 200-0. If I want to normalize them, should I use different scalers? For example:</p> | |||
<p>scx = MinMaxScaler(feature_range = (0, 1))<br /> | |||
scy = MinMaxScaler(feature_range = (0, 1))</p> | |||
<p> trainx = scx.fit_transform(trainx)<br /> | |||
trainy = scy.fit_transform(trainy)</p> | |||
<p>or should I scale them with same scale like below?</p> | |||
<p>sc = MinMaxScaler(feature_range = (0, 1))</p> | |||
<p> trainx = sc.fit_transform(trainx)<br /> | |||
trainy = sc.fit_transform(trainy)</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-491776' data-commentid="491776" data-postid="6939" data-belowelement="comment-491776" data-respondelement="respond" data-replyto="Reply to ICHaLiL" aria-label='Reply to ICHaLiL'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-491833" class="comment byuser comment-author-jasonb bypostauthor odd alt depth-2"> | |||
<div id="li-comment-491833" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">July 6, 2019 at 8:40 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-491833" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Yes, use a separate transform for inputs and outputs is a good idea. Otherwise have them all as separate columns in the same matrix and use one scaler, but the column order for transform/inverse_transform will always have to be consistent.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-491833' data-commentid="491833" data-postid="6939" data-belowelement="comment-491833" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-492643" class="comment even thread-even depth-1"> | |||
<div id="li-comment-492643" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/1a0f3d8c68c551f8c48172336f96e584?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/1a0f3d8c68c551f8c48172336f96e584?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Brent</span> | |||
<span class="date">July 12, 2019 at 6:55 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-492643" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi Jason, </p> | |||
<p>Confused about one aspect, I have a small NN with 8 independent variables and one dichotomous dependent variable. I have standardized the input variables (the output variable was left untouched). I have both trained and created the final model with the same standardized data. However, the question is, if I want to create a user interface to receive manual inputs, those will no longer be in the standardized format, so what is the best way to proceed?</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-492643' data-commentid="492643" data-postid="6939" data-belowelement="comment-492643" data-respondelement="respond" data-replyto="Reply to Brent" aria-label='Reply to Brent'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-492702" class="comment byuser comment-author-jasonb bypostauthor odd alt depth-2"> | |||
<div id="li-comment-492702" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">July 13, 2019 at 6:53 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-492702" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>You must maintain the objects used to prepare the data, or the coefficients used by those objects (mean and stdev) so that you can prepare new data in an identically way to the way data was prepared during training.</p> | |||
<p>Does that help</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-492702' data-commentid="492702" data-postid="6939" data-belowelement="comment-492702" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-492925" class="comment even depth-3"> | |||
<div id="li-comment-492925" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/1a0f3d8c68c551f8c48172336f96e584?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/1a0f3d8c68c551f8c48172336f96e584?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Brent</span> | |||
<span class="date">July 15, 2019 at 10:58 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-492925" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Thank you, that makes perfect sense.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-492925' data-commentid="492925" data-postid="6939" data-belowelement="comment-492925" data-respondelement="respond" data-replyto="Reply to Brent" aria-label='Reply to Brent'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-493673" class="comment odd alt thread-odd thread-alt depth-1"> | |||
<div id="li-comment-493673" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/c59c98d4d3a9300927a6776535a99ce1?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/c59c98d4d3a9300927a6776535a99ce1?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">cgv</span> | |||
<span class="date">July 21, 2019 at 11:27 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-493673" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi Jason,</p> | |||
<p>I have built an ANN model and scaled my inputs and outputs before feeding to the network. I measure the performance of the model by r2_score. My output variable is height. My r2_score when the output variable is in metres is .98, but when my output variable is in centi-metres , my r2_score is .91. I have scaled my output too before feeding to the network, why is there a difference in r2_score even because the output variable is scaled before feeding to the network.</p> | |||
<p>Thanks in advance</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-493673' data-commentid="493673" data-postid="6939" data-belowelement="comment-493673" data-respondelement="respond" data-replyto="Reply to cgv" aria-label='Reply to cgv'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-493717" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-493717" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">July 22, 2019 at 8:27 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-493717" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Good question, this is why it is important to test different scaling approaches in order to discover what works best for a given dataset and model combination.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-493717' data-commentid="493717" data-postid="6939" data-belowelement="comment-493717" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-498877" class="comment odd alt depth-2"> | |||
<div id="li-comment-498877" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/f8d373791418b8a89df9877140f5e930?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/f8d373791418b8a89df9877140f5e930?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">madhuri</span> | |||
<span class="date">August 29, 2019 at 3:44 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-498877" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi Jason,<br /> | |||
I am working on sequence to data prediction problem wherein i am performing normalization on input and output both.<br /> | |||
Once model is trained then to get the actual output in real-time, I have to perform the de-normalization and when I will perform the denorm then error will increase by the same factor I have used for normalization.<br /> | |||
Lets consider, norm predicted output is 0.1 and error of the model is 0.01 .<br /> | |||
denorm predicted output become 0.1*100 = 10 and after de-normalizing the error will be 0.01*100= 1<br /> | |||
So, what will be solution to this eliminate this kind of problem in regression. </p> | |||
<p>Thanks</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-498877' data-commentid="498877" data-postid="6939" data-belowelement="comment-498877" data-respondelement="respond" data-replyto="Reply to madhuri" aria-label='Reply to madhuri'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-498916" class="comment byuser comment-author-jasonb bypostauthor even depth-3"> | |||
<div id="li-comment-498916" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">August 29, 2019 at 6:16 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-498916" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>What problem exactly?</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-498916' data-commentid="498916" data-postid="6939" data-belowelement="comment-498916" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-498975" class="comment odd alt depth-4"> | |||
<div id="li-comment-498975" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/f8d373791418b8a89df9877140f5e930?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/f8d373791418b8a89df9877140f5e930?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">madhuri</span> | |||
<span class="date">August 29, 2019 at 8:38 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-498975" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>The problem is after de-normalization of the output, the error difference between actual and predicted output is scaled up by the normalization factor (max-min) So, I want to know what can be done to make the error difference same for both de-normized as well as normalized output.</p> | |||
<p>Thanks</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-498975' data-commentid="498975" data-postid="6939" data-belowelement="comment-498975" data-respondelement="respond" data-replyto="Reply to madhuri" aria-label='Reply to madhuri'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-499030" class="comment byuser comment-author-jasonb bypostauthor even depth-5"> | |||
<div id="li-comment-499030" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">August 30, 2019 at 6:18 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-499030" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>I don’t understand, sorry.</p> | |||
<div class="reply"> | |||
</div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-504531" class="comment odd alt thread-even depth-1"> | |||
<div id="li-comment-504531" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/55e911fe89990c37d91f85e294e0b5b1?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/55e911fe89990c37d91f85e294e0b5b1?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">joshBorrison</span> | |||
<span class="date">October 7, 2019 at 5:08 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-504531" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi Jason,</p> | |||
<p>Do I have to use only one normalization formula for all inputs? </p> | |||
<p>For example: I have 5 inputs [inp1, inp2, inp3, inp4, inp5] where I can estimate max and min only for [inp1, inp2]. So can I use</p> | |||
<p>y = (x – min) / (max – min)</p> | |||
<p>for [inp1, inp2] and</p> | |||
<p>y = x/(1+x)</p> | |||
<p>for [inp3, inp4, inp5]?</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-504531' data-commentid="504531" data-postid="6939" data-belowelement="comment-504531" data-respondelement="respond" data-replyto="Reply to joshBorrison" aria-label='Reply to joshBorrison'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-504621" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-504621" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">October 8, 2019 at 7:53 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-504621" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Yes, it is applied to each input separately – assuming they have different units.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-504621' data-commentid="504621" data-postid="6939" data-belowelement="comment-504621" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-510336" class="comment odd alt thread-odd thread-alt depth-1"> | |||
<div id="li-comment-510336" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/29eba53be478753830b44a7192949154?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/29eba53be478753830b44a7192949154?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">shiva</span> | |||
<span class="date">November 13, 2019 at 4:17 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-510336" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi Jason</p> | |||
<p>what if I scale the word vectors(glove) for exposing to LSTM?</p> | |||
<p>would it affect the accuracy of results or it maintains the semantic relations of words?</p> | |||
<p>Thank you a lot.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-510336' data-commentid="510336" data-postid="6939" data-belowelement="comment-510336" data-respondelement="respond" data-replyto="Reply to shiva" aria-label='Reply to shiva'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-510377" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-510377" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">November 13, 2019 at 5:53 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-510377" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>I don’t think so. Try it and see?</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-510377' data-commentid="510377" data-postid="6939" data-belowelement="comment-510377" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-510516" class="comment odd alt thread-even depth-1"> | |||
<div id="li-comment-510516" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/6893f70f68043ccb6fa48815e9962258?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/6893f70f68043ccb6fa48815e9962258?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Murilo Souza</span> | |||
<span class="date">November 14, 2019 at 12:35 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-510516" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hello, i was trying to normalize/inverse transoformation in my data, but i got one error that i think its due to the resize i did in my input data. Here’s my code:</p> | |||
<p>import numpy as np<br /> | |||
import tensorflow as tf<br /> | |||
from tensorflow import keras<br /> | |||
import pandas as pd<br /> | |||
import time as time<br /> | |||
import matplotlib.pyplot as plt<br /> | |||
import pydot<br /> | |||
import csv as csv<br /> | |||
import keras.backend as K<br /> | |||
from sklearn.preprocessing import MinMaxScaler</p> | |||
<p># Downloading data<br /> | |||
!wget <a href="https://raw.githubusercontent.com/sibyjackgrove/CNN-on-Wind-Power-Data/master/MISO_power_data_classification_labels.csv" rel="nofollow ugc">https://raw.githubusercontent.com/sibyjackgrove/CNN-on-Wind-Power-Data/master/MISO_power_data_classification_labels.csv</a><br /> | |||
!wget <a href="https://raw.githubusercontent.com/sibyjackgrove/CNN-on-Wind-Power-Data/master/MISO_power_data_input.csv" rel="nofollow ugc">https://raw.githubusercontent.com/sibyjackgrove/CNN-on-Wind-Power-Data/master/MISO_power_data_input.csv</a></p> | |||
<p># Trying normalization<br /> | |||
batch_size = 1<br /> | |||
valid_size = max(1,np.int(0.2*batch_size))<br /> | |||
df_input = pd.read_csv(‘./MISO_power_data_input.csv’,usecols =[‘Wind_MWh’,’Actual_Load_MWh’], chunksize=24*(batch_size+valid_size),nrows = 24*(batch_size+valid_size),iterator=True)<br /> | |||
df_target = pd.read_csv(‘./MISO_power_data_classification_labels.csv’,usecols =[‘Mean Wind Power’,’Standard Deviation’,’WindShare’],chunksize =batch_size+valid_size,nrows = batch_size+valid_size, iterator=True)<br /> | |||
for chunk, chunk2 in zip(df_input,df_target):<br /> | |||
InputX = chunk.values<br /> | |||
InputX = np.resize(InputX,(batch_size+valid_size,24,2,1))<br /> | |||
print(InputX)<br /> | |||
InputX.astype(‘float32’, copy=False)<br /> | |||
InputY = chunk2.values<br /> | |||
InputY.astype(‘float32’, copy=False)<br /> | |||
print(InputY)</p> | |||
<p># create scaler<br /> | |||
scaler = MinMaxScaler() # Define limits for normalize data<br /> | |||
normalized_input = scaler.fit_transform(InputX) # Normalize input data<br /> | |||
normalized_output = scaler.fit_transform(InputY) # Normalize output data<br /> | |||
print(normalized_input)<br /> | |||
print(normalized_output)<br /> | |||
inverse_output = scaler.inverse_transform(normalized_output) # Inverse transformation of output data<br /> | |||
print(inverse_output)</p> | |||
<p>The error:</p> | |||
<p>“ValueError: Found array with dim 4. MinMaxScaler expected <= 2."</p> | |||
<p>Do you have any idea how can i fix this? I really didn't wish to change the resize command at the moment.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-510516' data-commentid="510516" data-postid="6939" data-belowelement="comment-510516" data-respondelement="respond" data-replyto="Reply to Murilo Souza" aria-label='Reply to Murilo Souza'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-510565" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-510565" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">November 14, 2019 at 8:04 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-510565" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Perhaps this will help:<br /> | |||
<a href="https://machinelearningmastery.com/machine-learning-data-transforms-for-time-series-forecasting/" rel="nofollow ugc">https://machinelearningmastery.com/machine-learning-data-transforms-for-time-series-forecasting/</a></p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-510565' data-commentid="510565" data-postid="6939" data-belowelement="comment-510565" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-510656" class="comment odd alt depth-3"> | |||
<div id="li-comment-510656" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/6893f70f68043ccb6fa48815e9962258?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/6893f70f68043ccb6fa48815e9962258?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Murilo Souza</span> | |||
<span class="date">November 15, 2019 at 12:44 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-510656" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Is there anyway i can do the inverse transform inside the model itself? Because, for example, my MSE reported at the end of each epoch would be in the “wrong” scale.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-510656' data-commentid="510656" data-postid="6939" data-belowelement="comment-510656" data-respondelement="respond" data-replyto="Reply to Murilo Souza" aria-label='Reply to Murilo Souza'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-510694" class="comment byuser comment-author-jasonb bypostauthor even depth-4"> | |||
<div id="li-comment-510694" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">November 15, 2019 at 7:53 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-510694" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Yes, you could wrap the model in a sklearn pipeline.</p> | |||
<p>Or wrap the model in your own wrapper class.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-510694' data-commentid="510694" data-postid="6939" data-belowelement="comment-510694" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-607456" class="comment odd alt depth-5"> | |||
<div id="li-comment-607456" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/9ebba87d298788ebb9631a55f842a0d0?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/9ebba87d298788ebb9631a55f842a0d0?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Mariana Costa</span> | |||
<span class="date">April 29, 2021 at 11:11 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-607456" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Does it improve the net to do this?</p> | |||
<div class="reply"> | |||
</div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-607501" class="comment byuser comment-author-jasonb bypostauthor even depth-5"> | |||
<div id="li-comment-607501" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">April 30, 2021 at 6:06 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-607501" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>No, but it may be helpful when coding.</p> | |||
<div class="reply"> | |||
</div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-510545" class="comment odd alt thread-odd thread-alt depth-1"> | |||
<div id="li-comment-510545" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/9eaa036e1e8a4d28f1f88518e1991d30?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/9eaa036e1e8a4d28f1f88518e1991d30?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Jules Damji</span> | |||
<span class="date">November 14, 2019 at 6:55 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-510545" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hey Jason,</p> | |||
<p>I love this tutorial. I was wondering if I can get your permission to use this tutorial, convert all its experimentation and tracking using MLflow, and include it in my tutorials I teach at conferences.</p> | |||
<p>It’s a fitting example of how you can use MLFlow to track different experiments and visually compare the outcomes.</p> | |||
<p>All the credit will be given to you as the source and inspiration. You can see some of the examples here: <a href="https://github.com/dmatrix/spark-saturday/tree/master/tutorials/mlflow/src/python" rel="nofollow ugc">https://github.com/dmatrix/spark-saturday/tree/master/tutorials/mlflow/src/python</a>.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-510545' data-commentid="510545" data-postid="6939" data-belowelement="comment-510545" data-respondelement="respond" data-replyto="Reply to Jules Damji" aria-label='Reply to Jules Damji'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-510572" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-510572" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">November 14, 2019 at 8:07 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-510572" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Thanks!</p> | |||
<p>No problem as long as you clearly cite and link to the post.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-510572' data-commentid="510572" data-postid="6939" data-belowelement="comment-510572" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-510615" class="comment odd alt thread-even depth-1"> | |||
<div id="li-comment-510615" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/9eaa036e1e8a4d28f1f88518e1991d30?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/9eaa036e1e8a4d28f1f88518e1991d30?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">jules Damji</span> | |||
<span class="date">November 14, 2019 at 2:45 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-510615" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Thanks, I will certainly put the original link and plug your book too, along with your site and an excellent resource of tutorials and examples to learn from.</p> | |||
<p>Cheers<br /> | |||
Jules</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-510615' data-commentid="510615" data-postid="6939" data-belowelement="comment-510615" data-respondelement="respond" data-replyto="Reply to jules Damji" aria-label='Reply to jules Damji'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-510683" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-510683" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">November 15, 2019 at 7:42 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-510683" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Thanks Jules.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-510683' data-commentid="510683" data-postid="6939" data-belowelement="comment-510683" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-512747" class="comment odd alt thread-odd thread-alt depth-1"> | |||
<div id="li-comment-512747" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/47703d72c09f5d8df5925defa68b82b3?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/47703d72c09f5d8df5925defa68b82b3?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Hanser</span> | |||
<span class="date">November 28, 2019 at 8:13 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-512747" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Amazing content Jason! I was wondering if it is possible to apply different scalers to different inputs given based on their original characteristics? I am asking you that because as you mentioned in the tutorial “Differences in the scales across input variables may increase the difficulty of the problem being modeled” Therefore, if I use standard scaler in one input and normal scaler in another it could be bad for gradient descend.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-512747' data-commentid="512747" data-postid="6939" data-belowelement="comment-512747" data-respondelement="respond" data-replyto="Reply to Hanser" aria-label='Reply to Hanser'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-512749" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-512749" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">November 28, 2019 at 8:16 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-512749" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Thanks!</p> | |||
<p>Yes, perhaps try it and compare the results to using one type of scaling for all inputs.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-512749' data-commentid="512749" data-postid="6939" data-belowelement="comment-512749" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-514349" class="comment odd alt thread-even depth-1"> | |||
<div id="li-comment-514349" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/06742781eee91a4b33cd5636540afa46?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/06742781eee91a4b33cd5636540afa46?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Riyaz Pasha</span> | |||
<span class="date">December 9, 2019 at 9:26 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-514349" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi Jason,<br /> | |||
I am solving the Regression problem and my accuracy after normalizing the target variable is 92% but I have the doubt about scaling the target variable. So can you elaborate about scaling the Target variable?</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-514349' data-commentid="514349" data-postid="6939" data-belowelement="comment-514349" data-respondelement="respond" data-replyto="Reply to Riyaz Pasha" aria-label='Reply to Riyaz Pasha'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-514403" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-514403" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">December 10, 2019 at 7:30 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-514403" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>You cannot calculate accuracy for regression. You must calculate error.</p> | |||
<p>More details here:<br /> | |||
<a href="https://machinelearningmastery.com/faq/single-faq/how-do-i-calculate-accuracy-for-regression" rel="nofollow ugc">https://machinelearningmastery.com/faq/single-faq/how-do-i-calculate-accuracy-for-regression</a></p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-514403' data-commentid="514403" data-postid="6939" data-belowelement="comment-514403" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-516662" class="comment odd alt thread-odd thread-alt depth-1"> | |||
<div id="li-comment-516662" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/392a28317334d0cdf35969ebce45afd6?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/392a28317334d0cdf35969ebce45afd6?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">FAIZ</span> | |||
<span class="date">December 30, 2019 at 7:34 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-516662" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi Jason Sir!<br /> | |||
My data range is variable, e.g. -1500000, 0.0003456, 2387900,23,50,-45,-0.034, what should i do? i want to use MLP, 1D-CNN and SAE.<br /> | |||
THANKS</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-516662' data-commentid="516662" data-postid="6939" data-belowelement="comment-516662" data-respondelement="respond" data-replyto="Reply to FAIZ" aria-label='Reply to FAIZ'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-516700" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-516700" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">December 31, 2019 at 7:31 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-516700" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Perhaps try normalizing the data first?</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-516700' data-commentid="516700" data-postid="6939" data-belowelement="comment-516700" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-516827" class="comment odd alt depth-3"> | |||
<div id="li-comment-516827" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/392a28317334d0cdf35969ebce45afd6?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/392a28317334d0cdf35969ebce45afd6?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Faiz</span> | |||
<span class="date">January 2, 2020 at 12:28 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-516827" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>i tried different type of normalization but got data type errors, i used “MinMaxScaler ” and also (X-min(X))/ (max(X)-min(X)), but it can’t process. I want to know about the tf.compat.v1.keras.utils.normalize() command, what it actually do? thanks</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-516827' data-commentid="516827" data-postid="6939" data-belowelement="comment-516827" data-respondelement="respond" data-replyto="Reply to Faiz" aria-label='Reply to Faiz'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-516854" class="comment byuser comment-author-jasonb bypostauthor even depth-4"> | |||
<div id="li-comment-516854" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">January 2, 2020 at 6:42 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-516854" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>I don’t have a tutorial on that, perhaps check the source code?</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-516854' data-commentid="516854" data-postid="6939" data-belowelement="comment-516854" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-519745" class="comment odd alt thread-even depth-1"> | |||
<div id="li-comment-519745" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/13394f97aa5b4cb680aa557b82cfe3b7?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/13394f97aa5b4cb680aa557b82cfe3b7?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">BNB</span> | |||
<span class="date">January 30, 2020 at 3:32 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-519745" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi Jason</p> | |||
<p>I have a question about the normalization of data. Samples from the population may be added to the dataset over time, and the attribute values for these new objects may then lie outside those you have seen so far. One possibility to handle new minimum and maximum values is to periodically renormalize the data after including the new values. Is there any normalization approach without renormalization?</p> | |||
<p>Thanks,</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-519745' data-commentid="519745" data-postid="6939" data-belowelement="comment-519745" data-respondelement="respond" data-replyto="Reply to BNB" aria-label='Reply to BNB'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-519769" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-519769" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">January 30, 2020 at 6:56 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-519769" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Yes, re-normalizing is one approach.</p> | |||
<p>Clipping values to historical limits is another.</p> | |||
<p>Perhaps try a few methods and see what makes sense for your project?</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-519769' data-commentid="519769" data-postid="6939" data-belowelement="comment-519769" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-522214" class="comment odd alt thread-odd thread-alt depth-1"> | |||
<div id="li-comment-522214" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a032a57b03c559f83d394587e12353bf?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a032a57b03c559f83d394587e12353bf?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://None.' rel='external nofollow ugc' class='url'>Tajik</a></span> | |||
<span class="date">February 19, 2020 at 1:15 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-522214" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi Jason</p> | |||
<p>Should we use “standard_deviation = sqrt( sum( (x – mean)**2 ) / count(x))” instead of “standard_deviation = sqrt( sum( (x – mean)^2 ) / count(x))”?</p> | |||
<p>Does “^” sign represent square root in Python and is it fine not to subtract count (x) by 1 (in order to make it std of sample distribution, unless we have 100% observation of a population)?</p> | |||
<p>Thank you</p> | |||
<p>Best<br /> | |||
Tajik</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-522214' data-commentid="522214" data-postid="6939" data-belowelement="comment-522214" data-respondelement="respond" data-replyto="Reply to Tajik" aria-label='Reply to Tajik'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-522269" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-522269" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">February 19, 2020 at 8:06 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-522269" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>^ means superscript (e.g. exponent) in latex and excel.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-522269' data-commentid="522269" data-postid="6939" data-belowelement="comment-522269" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-522703" class="comment odd alt thread-even depth-1"> | |||
<div id="li-comment-522703" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/2c010c2a8cb9a42b1e486859a52ff1c8?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/2c010c2a8cb9a42b1e486859a52ff1c8?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Peter</span> | |||
<span class="date">February 22, 2020 at 6:18 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-522703" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi Jason,</p> | |||
<p>Very helpful post as always! I am slightly confused regarding the use of the scaler object though. In my scenario…</p> | |||
<p>If I have a set of data that I split into a training set and validation set, I then scale the data as follows:</p> | |||
<p>scaler = MinMaxScaler()<br /> | |||
scaledTrain = scaler.fit_transform(trainingSet)<br /> | |||
scaledValid = scaler.transform(validationSet)</p> | |||
<p>I then use this data to train a deep learning model.</p> | |||
<p>My question is, should I use the same scaler object, which was created using the training set, to scale my new, unseen test data before using that test set for predicting my model’s performance? Or should I create a new, separate scaler object using the test data?</p> | |||
<p>Thanks in advance</p> | |||
<p> Michael</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-522703' data-commentid="522703" data-postid="6939" data-belowelement="comment-522703" data-respondelement="respond" data-replyto="Reply to Peter" aria-label='Reply to Peter'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-522738" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-522738" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">February 22, 2020 at 6:40 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-522738" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Yes.</p> | |||
<p>Any data given to your model MUST be prepared in the same way. You are defining the expectations for the model based on how the training set looks.</p> | |||
<p>Use the same scaler object – it knows – from being fit on the training dataset – how to transform data in the way your model expects.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-522738' data-commentid="522738" data-postid="6939" data-belowelement="comment-522738" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-522746" class="comment odd alt depth-3"> | |||
<div id="li-comment-522746" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/2c010c2a8cb9a42b1e486859a52ff1c8?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/2c010c2a8cb9a42b1e486859a52ff1c8?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Peter</span> | |||
<span class="date">February 22, 2020 at 7:24 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-522746" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Awesome! Thanks so much for the quick response and clearing that up for me.</p> | |||
<p>Very best wishes.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-522746' data-commentid="522746" data-postid="6939" data-belowelement="comment-522746" data-respondelement="respond" data-replyto="Reply to Peter" aria-label='Reply to Peter'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-522840" class="comment byuser comment-author-jasonb bypostauthor even depth-4"> | |||
<div id="li-comment-522840" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">February 23, 2020 at 7:19 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-522840" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>You’re welcome.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-522840' data-commentid="522840" data-postid="6939" data-belowelement="comment-522840" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-525070" class="comment odd alt thread-odd thread-alt depth-1"> | |||
<div id="li-comment-525070" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/f9d8a0f5294efbd30ff2bce83feec60e?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/f9d8a0f5294efbd30ff2bce83feec60e?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Mike</span> | |||
<span class="date">March 10, 2020 at 2:21 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-525070" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi Jason,</p> | |||
<p>Thank you for the tutorial. A question about the conclusion: I find it surprising that standardization did not yield better performance compared to the model with unscaled inputs. Shouldn’t standardization provide better convergence properties when training neural networks? It’s also surprising that min-max scaling worked so well. If all of your inputs are positive (i.e between [0, 1] in this case), doesn’t that mean ALL of your weight updates at each step will be the same sign, which leads to inefficient learning?</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-525070' data-commentid="525070" data-postid="6939" data-belowelement="comment-525070" data-respondelement="respond" data-replyto="Reply to Mike" aria-label='Reply to Mike'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-525143" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-525143" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">March 11, 2020 at 5:16 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-525143" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Not always. It really depends on the problem and the model.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-525143' data-commentid="525143" data-postid="6939" data-belowelement="comment-525143" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-534865" class="comment odd alt thread-even depth-1"> | |||
<div id="li-comment-534865" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/b667b30b21ddb5dd97602fdd1511b7ac?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/b667b30b21ddb5dd97602fdd1511b7ac?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Zeynep newby</span> | |||
<span class="date">May 15, 2020 at 8:59 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-534865" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi Jason,</p> | |||
<p>I am an absolute beginner into neural networks and I appreciate your helpful website. In the lecture, I learned that when normalizing a training set, one should use the same mean and standard deviation from training for the test set. But I see in your codes that you’re normalizing training and test sets individually. Is that for a specific reason?</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-534865' data-commentid="534865" data-postid="6939" data-belowelement="comment-534865" data-respondelement="respond" data-replyto="Reply to Zeynep newby" aria-label='Reply to Zeynep newby'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-534911" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-534911" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">May 16, 2020 at 6:09 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-534911" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>The example correctly fits the transform on the training set then applies the transform to train and test sets.</p> | |||
<p>If we don’t do it this way, it will result in data leakage and in turn an optimistic estimate of model performance.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-534911' data-commentid="534911" data-postid="6939" data-belowelement="comment-534911" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-534866" class="comment odd alt thread-odd thread-alt depth-1"> | |||
<div id="li-comment-534866" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/b667b30b21ddb5dd97602fdd1511b7ac?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/b667b30b21ddb5dd97602fdd1511b7ac?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Zeynep newby</span> | |||
<span class="date">May 15, 2020 at 9:04 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-534866" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi again,</p> | |||
<p>since I saw another comment having the same question like me, I noticed that you acutally have done exactly the same thing as I expected. Since I am not familiar with the syntax yet, I got it wrong. Thanks very much!</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-534866' data-commentid="534866" data-postid="6939" data-belowelement="comment-534866" data-respondelement="respond" data-replyto="Reply to Zeynep newby" aria-label='Reply to Zeynep newby'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-534912" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-534912" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">May 16, 2020 at 6:10 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-534912" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>No problem!</p> | |||
<p>Ask questions anyway, even if you’re not sure. The tutorials are really just the starting point in a conversation.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-534912' data-commentid="534912" data-postid="6939" data-belowelement="comment-534912" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-535093" class="comment odd alt thread-even depth-1"> | |||
<div id="li-comment-535093" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/019c11b37908413095fb30111b8fa83b?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/019c11b37908413095fb30111b8fa83b?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Isaac</span> | |||
<span class="date">May 17, 2020 at 3:25 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-535093" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hai Jaison, I am a beginner in ML and I am having an issue with normalizing..<br /> | |||
I am developing a multivariate regression model with three inputs and three outputs.<br /> | |||
The three inputs are in the range of [700 1500] , [700-1500] and [700 1500]<br /> | |||
The three outputs are in the range of [-0.5 0.5] , [-0.5 0.5] and [700 1500]<br /> | |||
I have normalized everything in the range of [-1 1].<br /> | |||
The loss at the end of 1000 epoch is in the order of 1e-4, but still, I am not satisfied with the fit of the model. Since the loss function is based on normalized target variables and normalized prediction, its value id very less from the first epoch itself. </p> | |||
<p>Is there a way to bring the cost further down?</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-535093' data-commentid="535093" data-postid="6939" data-belowelement="comment-535093" data-respondelement="respond" data-replyto="Reply to Isaac" aria-label='Reply to Isaac'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-535157" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-535157" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">May 18, 2020 at 6:08 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-535157" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Yes, the suggestions here will help you improve your model:<br /> | |||
<a href="https://machinelearningmastery.com/start-here/#better" rel="nofollow ugc">https://machinelearningmastery.com/start-here/#better</a></p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-535157' data-commentid="535157" data-postid="6939" data-belowelement="comment-535157" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-538831" class="comment odd alt thread-odd thread-alt depth-1"> | |||
<div id="li-comment-538831" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/ff1ca8052a3c188d4610d96d1b1d0e2b?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/ff1ca8052a3c188d4610d96d1b1d0e2b?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Victor Yu</span> | |||
<span class="date">June 9, 2020 at 11:43 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-538831" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi Jason,</p> | |||
<p>I wonder how you apply scaling to batch data? Say we batch load from tfrecords, for each batch we fit a scaler? If so, then the final scaler is on the last batch, which will be used for test data? Also in batch data, if the batch is small, then it seems the scaler is volatile, especially for MaxMin. Would like to hear your thoughts since in a lot of practices it’s nearly impossible to load huge data into driver to do scaling.</p> | |||
<p>Thanks!</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-538831' data-commentid="538831" data-postid="6939" data-belowelement="comment-538831" data-respondelement="respond" data-replyto="Reply to Victor Yu" aria-label='Reply to Victor Yu'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-538889" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-538889" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">June 10, 2020 at 6:16 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-538889" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Scaling is fit on the training set, then applied to all data, e.g. train, test, val.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-538889' data-commentid="538889" data-postid="6939" data-belowelement="comment-538889" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-538935" class="comment odd alt depth-3"> | |||
<div id="li-comment-538935" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/ff1ca8052a3c188d4610d96d1b1d0e2b?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/ff1ca8052a3c188d4610d96d1b1d0e2b?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Victor Yu</span> | |||
<span class="date">June 10, 2020 at 12:10 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-538935" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>The entire training set? What if the entire training set is too big to load in the memory? Even doing batch training, you still do scaling on the entire training set first then do batch training? That seems pretty inefficient</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-538935' data-commentid="538935" data-postid="6939" data-belowelement="comment-538935" data-respondelement="respond" data-replyto="Reply to Victor Yu" aria-label='Reply to Victor Yu'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-538949" class="comment byuser comment-author-jasonb bypostauthor even depth-4"> | |||
<div id="li-comment-538949" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">June 10, 2020 at 1:25 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-538949" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>You can use a generator to load the data step by step, only keep in memory what you can/need.</p> | |||
<p>More suggestions here:<br /> | |||
<a href="https://machinelearningmastery.com/faq/single-faq/how-to-i-work-with-a-very-large-dataset" rel="nofollow ugc">https://machinelearningmastery.com/faq/single-faq/how-to-i-work-with-a-very-large-dataset</a></p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-538949' data-commentid="538949" data-postid="6939" data-belowelement="comment-538949" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-539080" class="comment odd alt depth-5"> | |||
<div id="li-comment-539080" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/ff1ca8052a3c188d4610d96d1b1d0e2b?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/ff1ca8052a3c188d4610d96d1b1d0e2b?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Victor Yu</span> | |||
<span class="date">June 11, 2020 at 10:53 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-539080" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Yes, that’s my question. When doing batch training, do you fit (or re-fit) a scaler on each batch? If so, it seems the final scaler that will be used for scoring is fit on the final batch. Do you see any issue with that especially when batch is small? Thanks</p> | |||
<div class="reply"> | |||
</div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-539088" class="comment byuser comment-author-jasonb bypostauthor even depth-5"> | |||
<div id="li-comment-539088" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">June 11, 2020 at 1:31 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-539088" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>You could, this is what batch norm does.</p> | |||
<p>Or you can estimate the coefficients used in scaling up front from a sample of training data. Or some other way you prefer.</p> | |||
<div class="reply"> | |||
</div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-540105" class="comment odd alt thread-even depth-1"> | |||
<div id="li-comment-540105" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/019dc51ce56a4d8653eca7fafb8d6777?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/019dc51ce56a4d8653eca7fafb8d6777?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Najeh</span> | |||
<span class="date">June 19, 2020 at 6:32 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-540105" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi Jason,</p> | |||
<p>In deep learning as machine learning, data should be transformed into a tabular format? if yes or no why?</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-540105' data-commentid="540105" data-postid="6939" data-belowelement="comment-540105" data-respondelement="respond" data-replyto="Reply to Najeh" aria-label='Reply to Najeh'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-540131" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-540131" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">June 19, 2020 at 1:08 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-540131" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Input data must be vectors or matrices of numbers, this covers tabular data, images, audio, text, and so on.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-540131' data-commentid="540131" data-postid="6939" data-belowelement="comment-540131" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-540919" class="comment odd alt thread-odd thread-alt depth-1"> | |||
<div id="li-comment-540919" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/89fde3b7ab677bddd5b849b15143f92c?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/89fde3b7ab677bddd5b849b15143f92c?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Julie</span> | |||
<span class="date">June 24, 2020 at 10:29 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-540919" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hello Jason,</p> | |||
<p>I used your method (i did standardized my outputs and normalized my inputs with MinMaxScaler()) but i keep having the same issue : when i train my neural network with 3200 and validate with 800 everything alright, i have R2 = 99% but when i increase the training / validation set, R2 decreases which is weird, it should be even higher ? Do you think it has something to do with the scaling of the data ?<br /> | |||
Thank you !</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-540919' data-commentid="540919" data-postid="6939" data-belowelement="comment-540919" data-respondelement="respond" data-replyto="Reply to Julie" aria-label='Reply to Julie'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-540974" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-540974" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">June 25, 2020 at 6:17 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-540974" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>It might be interesting to perform a sensitivity analysis on model performance vs train or test set size to understand the relationship.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-540974' data-commentid="540974" data-postid="6939" data-belowelement="comment-540974" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-598424" class="comment odd alt depth-3"> | |||
<div id="li-comment-598424" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/6d7de1da9f2b4c1eb0e8b60cf3258437?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/6d7de1da9f2b4c1eb0e8b60cf3258437?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Munaf</span> | |||
<span class="date">February 23, 2021 at 4:30 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-598424" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Sir how can I normalize real-time data and scale them between -150 to 150? The data are coming every 5 min interval.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-598424' data-commentid="598424" data-postid="6939" data-belowelement="comment-598424" data-respondelement="respond" data-replyto="Reply to Munaf" aria-label='Reply to Munaf'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-598446" class="comment byuser comment-author-jasonb bypostauthor even depth-4"> | |||
<div id="li-comment-598446" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">February 23, 2021 at 6:25 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-598446" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Perhaps use the minmaxscaler if you’re having trouble:<br /> | |||
<a href="https://machinelearningmastery.com/standardscaler-and-minmaxscaler-transforms-in-python/" rel="nofollow ugc">https://machinelearningmastery.com/standardscaler-and-minmaxscaler-transforms-in-python/</a></p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-598446' data-commentid="598446" data-postid="6939" data-belowelement="comment-598446" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-542748" class="comment odd alt thread-even depth-1"> | |||
<div id="li-comment-542748" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/db93172ca2e4548c6328be19ab0761e1?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/db93172ca2e4548c6328be19ab0761e1?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">David</span> | |||
<span class="date">July 4, 2020 at 1:29 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-542748" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi sir, </p> | |||
<p>I have a NN with 6 input variables and one output , I employed minmaxscaler for inputs as well as outputs . My approach was applying the scaler to my whole dataset then splitting it into training and testing dataset, as I dont know the know-hows so is my approach wrong .<br /> | |||
Currently the problem I am facing is my actual outputs are positive values but after unscaling the NN predictions I am getting negative values. I tried changing the feature range, still NN predicted negative values , so how can i solve this?</p> | |||
<p>Y1=Y1.reshape(-1, 1)<br /> | |||
Y2=Y2.reshape(-1, 1)<br /> | |||
TY1=TY1.reshape(-1, 1)<br /> | |||
TY2=TY2.reshape(-1, 1)<br /> | |||
scaler1 = MinMaxScaler(feature_range=(0, 1))<br /> | |||
rescaledX= scaler1.fit_transform(X)<br /> | |||
rescaledTX=scaler1.fit_transform(TX)<br /> | |||
scaler2 = MinMaxScaler(feature_range=(0, 2))<br /> | |||
rescaledY1 = scaler2.fit_transform(Y1)</p> | |||
<p>scaler3 = MinMaxScaler(feature_range=(0, 2))</p> | |||
<p>rescaledY2 = scaler3.fit_transform(Y2)</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-542748' data-commentid="542748" data-postid="6939" data-belowelement="comment-542748" data-respondelement="respond" data-replyto="Reply to David" aria-label='Reply to David'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-542829" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-542829" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">July 5, 2020 at 6:54 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-542829" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>First, perhaps confirm that there is no bug in your code.</p> | |||
<p>Second, it is possible for the model to predict values that get mapped to a value out of bounds. You could use a n if-statement to snap them to the required range or use a model that forces predictions to the required range.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-542829' data-commentid="542829" data-postid="6939" data-belowelement="comment-542829" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-546517" class="comment odd alt thread-odd thread-alt depth-1"> | |||
<div id="li-comment-546517" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/d3ca990d016251679e2fe27de19d6af2?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/d3ca990d016251679e2fe27de19d6af2?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">TAMER A. FARRAG</span> | |||
<span class="date">July 30, 2020 at 9:42 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-546517" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Thanks a lot,<br /> | |||
My question is: </p> | |||
<p>I finish training my model and I use normalized data for inputs and outputs.<br /> | |||
my problem now is when I need to use this model I do the following:<br /> | |||
1- I load the model<br /> | |||
2- normalize the inputs<br /> | |||
3- use model to get the outputs (predicted data)</p> | |||
<p>how to denormalized the output of the model ??? I don’t have the MinMaxScaler for the output ??</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-546517' data-commentid="546517" data-postid="6939" data-belowelement="comment-546517" data-respondelement="respond" data-replyto="Reply to TAMER A. FARRAG" aria-label='Reply to TAMER A. FARRAG'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-546534" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-546534" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">July 30, 2020 at 1:45 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-546534" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>You can call inverse_transform() on the scaler object for the predictions to get the data back to the original scale.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-546534' data-commentid="546534" data-postid="6939" data-belowelement="comment-546534" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-546563" class="comment odd alt depth-3"> | |||
<div id="li-comment-546563" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/d3ca990d016251679e2fe27de19d6af2?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/d3ca990d016251679e2fe27de19d6af2?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">TAMER A. FARRAG</span> | |||
<span class="date">July 30, 2020 at 7:21 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-546563" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Thanks for fast replay,</p> | |||
<p>I think my question is not clear to you. </p> | |||
<p>imagine than I finish the training phase and save the trained model named “model1”.</p> | |||
<p>I send the “model1” file to a friend and he tries to use it, he will normalize the inputs and get the outputs. In this case, he doesn’t have the scaler object to recover the original values using inverse_transform().</p> | |||
<p>my problem is similar to: <a href="https://stackoverflow.com/questions/37595891/how-to-recover-original-values-after-a-model-predict-in-keras" rel="nofollow ugc">https://stackoverflow.com/questions/37595891/how-to-recover-original-values-after-a-model-predict-in-keras</a><br /> | |||
but the answer don’t use the scaler object. It depends on manual normalization and normalization process</p> | |||
<p>Thanks for your time</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-546563' data-commentid="546563" data-postid="6939" data-belowelement="comment-546563" data-respondelement="respond" data-replyto="Reply to TAMER A. FARRAG" aria-label='Reply to TAMER A. FARRAG'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-546618" class="comment byuser comment-author-jasonb bypostauthor even depth-4"> | |||
<div id="li-comment-546618" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">July 31, 2020 at 6:15 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-546618" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Save the scaler object as well:<br /> | |||
<a href="https://machinelearningmastery.com/how-to-save-and-load-models-and-data-preparation-in-scikit-learn-for-later-use/" rel="nofollow ugc">https://machinelearningmastery.com/how-to-save-and-load-models-and-data-preparation-in-scikit-learn-for-later-use/</a></p> | |||
<p>You are developing a “modeling pipeline”, not just a predictive model.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-546618' data-commentid="546618" data-postid="6939" data-belowelement="comment-546618" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-549367" class="comment odd alt thread-even depth-1"> | |||
<div id="li-comment-549367" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/694ca2a3a45788c5d034de77503eff26?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/694ca2a3a45788c5d034de77503eff26?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Mel</span> | |||
<span class="date">August 15, 2020 at 9:09 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-549367" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi Jason,</p> | |||
<p>Do you know of any textbooks or journal articles that address the input scaling issue as you’ve described it here, in addition to the Bishop textbook? I’m struggling so far in vain to find discussions of this type of scaling, when different raw input variables have much different ranges. Instead I’m finding plenty of mentions in tutorials and blog posts (of which yours is one of the clearest), and papers describing the problems of scale (size) variance in neural networks designed for image recognition.</p> | |||
<p>Thanks!</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-549367' data-commentid="549367" data-postid="6939" data-belowelement="comment-549367" data-respondelement="respond" data-replyto="Reply to Mel" aria-label='Reply to Mel'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-549393" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-549393" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">August 15, 2020 at 1:26 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-549393" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Not really, practical issues are not often discussed in textbooks/papers.</p> | |||
<p>Maybe “neural smithing”? Maybe Bishops later book?</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-549393' data-commentid="549393" data-postid="6939" data-belowelement="comment-549393" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-565174" class="comment odd alt thread-odd thread-alt depth-1"> | |||
<div id="li-comment-565174" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/be0a9890d7813eeca82228a828048fa0?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/be0a9890d7813eeca82228a828048fa0?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Munisha Bansal</span> | |||
<span class="date">September 29, 2020 at 5:44 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-565174" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi Jason, </p> | |||
<p>Thank you very much for the article. I wanted to understand the following scenario</p> | |||
<p>I have mix of categorical and numerical inputs. I can normalize/standardize the numerical inputs and the output numerical variable.<br /> | |||
But in the categorical variables I have high number of categories ~3000. So I use label encoder (not one hot coding) and then I use embedding layers. How can I achieve scaling in this case.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-565174' data-commentid="565174" data-postid="6939" data-belowelement="comment-565174" data-respondelement="respond" data-replyto="Reply to Munisha Bansal" aria-label='Reply to Munisha Bansal'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-565223" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-565223" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">September 30, 2020 at 6:24 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-565223" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>You can separate the columns and scale them independently, then aggregate the results.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-565223' data-commentid="565223" data-postid="6939" data-belowelement="comment-565223" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-569739" class="comment odd alt thread-even depth-1"> | |||
<div id="li-comment-569739" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/3c1d6c60d6feecafba80283ef8eed1d4?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/3c1d6c60d6feecafba80283ef8eed1d4?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Hamed</span> | |||
<span class="date">October 22, 2020 at 1:58 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-569739" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi Jason, </p> | |||
<p>I really enjoyed reading your article. My CNN regression network has binary image as input which the background is black, and foreground is white. The ground truth associated with each input is an image with color range from 0 to 255 which is normalized between 0 and 1.</p> | |||
<p>The network can almost detect edges and background but in foreground all the predicted values are almost same. Do you have any idea what is the solution?</p> | |||
<p>I appreciate in advance.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-569739' data-commentid="569739" data-postid="6939" data-belowelement="comment-569739" data-respondelement="respond" data-replyto="Reply to Hamed" aria-label='Reply to Hamed'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-569783" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-569783" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">October 22, 2020 at 6:45 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-569783" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Thanks.</p> | |||
<p>Perhaps these tips will help you improve the performance of your model:<br /> | |||
<a href="https://machinelearningmastery.com/start-here/#better" rel="nofollow ugc">https://machinelearningmastery.com/start-here/#better</a></p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-569783' data-commentid="569783" data-postid="6939" data-belowelement="comment-569783" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-573097" class="comment odd alt thread-odd thread-alt depth-1"> | |||
<div id="li-comment-573097" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/893fb72ba4861e6b85b47d6575d13777?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/893fb72ba4861e6b85b47d6575d13777?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">walid</span> | |||
<span class="date">November 5, 2020 at 11:50 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-573097" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi jason, how are you?</p> | |||
<p>i have data with input X (matrix with real values) and output y (matrix real values).<br /> | |||
i tried to normalize X and y :</p> | |||
<p>scaler1 = Normalizer()<br /> | |||
X = scaler1.fit_transform(X)<br /> | |||
scaler2 = Normalizer()<br /> | |||
y = scaler2.fit_transform(y)</p> | |||
<p>i get a good result with the transform normalizer as shown by: <a href="https://ibb.co/bQYCkvK" rel="nofollow ugc">https://ibb.co/bQYCkvK</a></p> | |||
<p>at the end i tried to get the predicted values: yhat = model.predict(X_test)</p> | |||
<p>the problem here yhat is not the original data, it’s a transformed data and there is no inverse for normalizer.</p> | |||
<p>I tried to use the minmaxScalar an order to do the inverse operation (invyhat = scaler2.inverse_transform(yhat)) but i get a big numbers compared to the y_test values that i want.</p> | |||
<p>I tried to normalize just X, i get a worst result compared to the first one.</p> | |||
<p>could you please help me.</p> | |||
<p>example of X values : 1006.808362,13.335140,104.536458 …..<br /> | |||
289.197205,257.489613,106.245104,566.941857…..<br /> | |||
.</p> | |||
<p>example of y values: 0.50000, 250.0000<br /> | |||
0.879200,436.000000<br /> | |||
.<br /> | |||
.</p> | |||
<p>this is my code:</p> | |||
<p>X = dataset[:,0:20]<br /> | |||
y = dataset[:,20:22]</p> | |||
<p>scaler1 = Normalizer()<br /> | |||
X = scaler1.fit_transform(X)<br /> | |||
scaler2 = Normalizer()<br /> | |||
y = scaler2.fit_transform(y)</p> | |||
<p>X_train = X[90000:,:]<br /> | |||
X_test= X[:90000,:]<br /> | |||
y_train =y[90000:,:]<br /> | |||
y_test=y[:90000,:]</p> | |||
<p>print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)<br /> | |||
# define the keras model<br /> | |||
model = Sequential()<br /> | |||
#input layer<br /> | |||
model.add(Dense(20, input_dim=20,activation=’relu’,kernel_initializer=’normal’))<br /> | |||
#hidden layer<br /> | |||
model.add(Dense(7272,activation=’relu’,kernel_initializer=’normal’))<br /> | |||
model.add(Dropout(0.8))<br /> | |||
#output layer<br /> | |||
model.add(Dense(2, activation=’linear’))<br /> | |||
opt =Adadelta(lr=0.01)<br /> | |||
# compile the keras model<br /> | |||
model.compile(loss=’mean_squared_error’, optimizer=opt, metrics=[‘mse’])<br /> | |||
# fit the keras model on the dataset<br /> | |||
history=model.fit(X_train, y_train, validation_data=(X_test, y_test),epochs=20,verbose=0)<br /> | |||
# evaluate the model<br /> | |||
_, train_mse = model.evaluate(X_train, y_train, verbose=0)<br /> | |||
_, test_mse = model.evaluate(X_test, y_test, verbose=0)<br /> | |||
print(‘Train: %.3f, Test: %.3f’ % (train_mse, test_mse))<br /> | |||
yhat = model.predict(X_test)<br /> | |||
#plot loss during training<br /> | |||
pyplot.title(‘Loss / Mean Squared Error’)<br /> | |||
pyplot.plot(history.history[‘loss’], label=’train’)<br /> | |||
pyplot.plot(history.history[‘val_loss’], label=’test’)<br /> | |||
pyplot.legend()<br /> | |||
pyplot.show()</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-573097' data-commentid="573097" data-postid="6939" data-belowelement="comment-573097" data-respondelement="respond" data-replyto="Reply to walid" aria-label='Reply to walid'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-573152" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-573152" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">November 6, 2020 at 5:57 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-573152" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Sorry to hear that you’re having trouble, perhaps some of these tips will help:<br /> | |||
<a href="https://machinelearningmastery.com/faq/single-faq/can-you-read-review-or-debug-my-code" rel="nofollow ugc">https://machinelearningmastery.com/faq/single-faq/can-you-read-review-or-debug-my-code</a></p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-573152' data-commentid="573152" data-postid="6939" data-belowelement="comment-573152" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-575881" class="comment odd alt thread-even depth-1"> | |||
<div id="li-comment-575881" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/6616000cf7519211f351670ba2f44684?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/6616000cf7519211f351670ba2f44684?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Carlos</span> | |||
<span class="date">November 17, 2020 at 9:18 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-575881" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi Jason, first thanks for the wonderful article. I have a little doubt. By normalizing my data and then dividing it into training and testing, all samples will be normalized. But in the case of a real application, where I have an input given by the user, do I need to put it together with all the data and normalize it so that it has the same pattern as the other data? What would be the best alternative?</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-575881' data-commentid="575881" data-postid="6939" data-belowelement="comment-575881" data-respondelement="respond" data-replyto="Reply to Carlos" aria-label='Reply to Carlos'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-575928" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-575928" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">November 17, 2020 at 12:56 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-575928" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Good question.</p> | |||
<p>No, save the scaler object or coefficients used for scaling along with the model and use them to prepare new data in the future. More here:<br /> | |||
<a href="https://machinelearningmastery.com/how-to-save-and-load-models-and-data-preparation-in-scikit-learn-for-later-use/" rel="nofollow ugc">https://machinelearningmastery.com/how-to-save-and-load-models-and-data-preparation-in-scikit-learn-for-later-use/</a></p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-575928' data-commentid="575928" data-postid="6939" data-belowelement="comment-575928" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-580301" class="comment odd alt thread-odd thread-alt depth-1"> | |||
<div id="li-comment-580301" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/02c42cbeb380c2b5c7be98988003ca06?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/02c42cbeb380c2b5c7be98988003ca06?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://www.iqvia.com' rel='external nofollow ugc' class='url'>Chris</a></span> | |||
<span class="date">December 3, 2020 at 2:41 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-580301" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi Jason, what is the best way to scale NANs when you need the model to generate them? I am creating a synthetic dataset where NANs are critical part. In one case we have people with no corresponding values for a field (truly missing) and in another case we have missing values but want to replicate the fact that values are missing. I tried filling the missing values with the negative sys.max value, but the model tends to spread values between the real data negative limit and the max limit, instead of treating the max value as an outlier. In another case, it seems to ignore that value and always generates values with the real data range, resulting in no generated NANs. I enjoyed your book and look forward to your response.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-580301' data-commentid="580301" data-postid="6939" data-belowelement="comment-580301" data-respondelement="respond" data-replyto="Reply to Chris" aria-label='Reply to Chris'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-580371" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-580371" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">December 3, 2020 at 8:21 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-580371" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>You cannot scale a NaN, you must replace it with a value, called imputation.</p> | |||
<p>If you want to mark missing values with a special value, mark and then scale, or remove the rows from the scale process, and impute after scale. The latter sounds better to me.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-580371' data-commentid="580371" data-postid="6939" data-belowelement="comment-580371" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-591809" class="comment odd alt thread-even depth-1"> | |||
<div id="li-comment-591809" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/13b5071fa396d975a63419f7285e76e3?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/13b5071fa396d975a63419f7285e76e3?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Luke Mao</span> | |||
<span class="date">January 6, 2021 at 4:13 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-591809" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Thanks Jason for the blog post.<br /> | |||
One question:<br /> | |||
is it necessary to apply feature scaling for linear regression models as well as MLP’s?</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-591809' data-commentid="591809" data-postid="6939" data-belowelement="comment-591809" data-respondelement="respond" data-replyto="Reply to Luke Mao" aria-label='Reply to Luke Mao'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-591844" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-591844" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">January 6, 2021 at 6:32 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-591844" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Yes, it is a good idea to scale input data prior to modeling for models that use a weighted sum of input, like neural nets and regression models.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-591844' data-commentid="591844" data-postid="6939" data-belowelement="comment-591844" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-591999" class="comment odd alt depth-3"> | |||
<div id="li-comment-591999" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/13b5071fa396d975a63419f7285e76e3?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/13b5071fa396d975a63419f7285e76e3?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Lu Mao</span> | |||
<span class="date">January 6, 2021 at 9:00 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-591999" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Thanks Jason. May I ask a follow up question, what is your view on if it is wrong to only scale the input, not scale the output?.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-591999' data-commentid="591999" data-postid="6939" data-belowelement="comment-591999" data-respondelement="respond" data-replyto="Reply to Lu Mao" aria-label='Reply to Lu Mao'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-592055" class="comment byuser comment-author-jasonb bypostauthor even depth-4"> | |||
<div id="li-comment-592055" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">January 7, 2021 at 6:16 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-592055" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>It depends on the data and model. </p> | |||
<p>Do whatever results in the best performance for your prediction problem.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-592055' data-commentid="592055" data-postid="6939" data-belowelement="comment-592055" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-594313" class="comment odd alt thread-odd thread-alt depth-1"> | |||
<div id="li-comment-594313" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/e42eb5aaa2a16c0da0eb985043120cc1?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/e42eb5aaa2a16c0da0eb985043120cc1?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Nisarg Patel</span> | |||
<span class="date">January 25, 2021 at 1:23 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-594313" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>sir, i have a 1 problem</p> | |||
<p>When normalizing a dataset, the resulting data will have a minimum value of 0 and a<br /> | |||
maximum value of 1. However, the dataset we work with in data mining is typically a<br /> | |||
sample of a population. Therefore, the minimum and maximum for each of the attributes<br /> | |||
in the population are unknown.<br /> | |||
Samples from the population may be added to the dataset over time, and the attribute<br /> | |||
values for these new objects may then lie outside those you have seen so far. One<br /> | |||
possibility to handle new minimum and maximum values is to periodically renormalize<br /> | |||
the data after including the new values. Your task is to think of a normalization scheme<br /> | |||
that does not require you to renormalize all of the data. Your normalization approach has<br /> | |||
to fulfill all of the following requirements:<br /> | |||
– all values (old and new) have to lie in the range between 0 and 1<br /> | |||
– no transformation or renormalization of the old values is allowed<br /> | |||
Describe your normalization approach.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-594313' data-commentid="594313" data-postid="6939" data-belowelement="comment-594313" data-respondelement="respond" data-replyto="Reply to Nisarg Patel" aria-label='Reply to Nisarg Patel'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-594316" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-594316" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">January 25, 2021 at 1:31 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-594316" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Perhaps you can use domain knowledge to estimate a broader min and max range prior to scaling.</p> | |||
<p>Perhaps you can clip values to a pre-defined range prior to scaling.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-594316' data-commentid="594316" data-postid="6939" data-belowelement="comment-594316" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-600398" class="comment odd alt thread-even depth-1"> | |||
<div id="li-comment-600398" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/c81ade4d31cc6f6be6d5015e33a80777?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/c81ade4d31cc6f6be6d5015e33a80777?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">J</span> | |||
<span class="date">March 10, 2021 at 5:43 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-600398" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi Jason!<br /> | |||
Thank you so much for this great post 🙂</p> | |||
<p>I have one question I hope you could help with:<br /> | |||
Why do we need to conduct 30 model runs in particular? I do understand the idea, but i mean why 30 exactly?</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-600398' data-commentid="600398" data-postid="6939" data-belowelement="comment-600398" data-respondelement="respond" data-replyto="Reply to J" aria-label='Reply to J'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-600404" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-600404" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">March 10, 2021 at 6:28 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-600404" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>30 is often used to create a large enough sample that we can use statistical methods and that the estimated stats like mean and stev are not too noisy.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-600404' data-commentid="600404" data-postid="6939" data-belowelement="comment-600404" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-601009" class="comment odd alt thread-odd thread-alt depth-1"> | |||
<div id="li-comment-601009" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/7e738f045a3bc45ac8cf4d42d8618ebe?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/7e738f045a3bc45ac8cf4d42d8618ebe?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Maha</span> | |||
<span class="date">March 16, 2021 at 7:14 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-601009" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Thanks Jason<br /> | |||
I have some confused questions<br /> | |||
If the scaling to input data done on the all data set or done to each sample of the data set seperately?</p> | |||
<p>the scalling is done after dividing data to training and test, yes?</p> | |||
<p>If I done normalizations manual to inputs and output, so I should save the max and min values to normalization inputs and denormalization outputs in future prediction?</p> | |||
<p>If I have the outputs containing two differerent range of variables , is same normalization is effective or I should do further things,for example two different normalization?</p> | |||
<p>Thanks in advance</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-601009' data-commentid="601009" data-postid="6939" data-belowelement="comment-601009" data-respondelement="respond" data-replyto="Reply to Maha" aria-label='Reply to Maha'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-601015" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-601015" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">March 16, 2021 at 7:59 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-601015" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Data scaling, and all data pre-processing should be fit on the training set and applied to the training set, validation set and test sets in order to avoid data leakage. You can learn more about this here:<br /> | |||
<a href="https://machinelearningmastery.com/data-preparation-without-data-leakage/" rel="nofollow ugc">https://machinelearningmastery.com/data-preparation-without-data-leakage/</a></p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-601015' data-commentid="601015" data-postid="6939" data-belowelement="comment-601015" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-601019" class="comment odd alt depth-3"> | |||
<div id="li-comment-601019" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/7e738f045a3bc45ac8cf4d42d8618ebe?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/7e738f045a3bc45ac8cf4d42d8618ebe?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Maha</span> | |||
<span class="date">March 16, 2021 at 9:20 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-601019" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Many thanks for that, I hd read your mentioned article and understood to avoid data leakage. as I should split data first and got the scale from the trainning set .</p> | |||
<p>but i had another Q:<br /> | |||
my data set , for example contain four vectors [ x1 x2 x3 x4 ], where for example each had 100 values ., x1= [value1……………………..value100], x2=[value1…….value100],……<br /> | |||
then my traing data may be 400Xnumberof sumples.</p> | |||
<p>but the range of values to these is varying , x1 , x2 and x3 had values in range [ -04], forexample [ – 4.7338e-04 to – 1.33-04 ] and the x4 has values in range of [-02], forexample[ -1.33e-02 to 3.66e-02 ]</p> | |||
<p>the same the output has values some in range [-0.0698 to 0.06211] and other in range [-3.1556 to 3.15556]</p> | |||
<p>sorry for long discription , but , what suitable scaling you recommend me to do, if normalization(max, min ) to input and outs can be suitable , or I had to do any other prepation </p> | |||
<p>many thanks to you</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-601019' data-commentid="601019" data-postid="6939" data-belowelement="comment-601019" data-respondelement="respond" data-replyto="Reply to Maha" aria-label='Reply to Maha'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-601133" class="comment byuser comment-author-jasonb bypostauthor even depth-4"> | |||
<div id="li-comment-601133" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">March 17, 2021 at 5:55 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-601133" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>I recommend starting with normalization. Perhaps try standardization if the variables look like they have a gaussian probability distribution.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-601133' data-commentid="601133" data-postid="6939" data-belowelement="comment-601133" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-603221" class="comment odd alt thread-even depth-1"> | |||
<div id="li-comment-603221" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/5c8b1fab861a9605f3124497cc3b3113?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/5c8b1fab861a9605f3124497cc3b3113?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Maha</span> | |||
<span class="date">April 3, 2021 at 3:27 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-603221" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>If normalization and standarization is done of the whole data or each row of the samples , for example , in standardization , we got the mean of the whole data set and substract from each element in data set , or we treat each row in the data set separately and got its mean ,..?</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-603221' data-commentid="603221" data-postid="6939" data-belowelement="comment-603221" data-respondelement="respond" data-replyto="Reply to Maha" aria-label='Reply to Maha'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-603243" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-603243" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">April 3, 2021 at 5:35 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-603243" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>No, data preparation is typically fit on the training set and applied to the train and test dataset.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-603243' data-commentid="603243" data-postid="6939" data-belowelement="comment-603243" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-607743" class="comment odd alt thread-odd thread-alt depth-1"> | |||
<div id="li-comment-607743" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/76a3b3b0611d93f19dabacbb58a53773?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/76a3b3b0611d93f19dabacbb58a53773?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Carlos</span> | |||
<span class="date">May 2, 2021 at 11:12 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-607743" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi Jason,<br /> | |||
I have a question.. I hope you have time to answer it…</p> | |||
<p>If I scale/normalize the input data… The output label (calculated) will be generated “scalated/normalized” also..correct…<br /> | |||
and in order to calculate the output error the expected label should be scalated also..<br /> | |||
Correct??<br /> | |||
In other words.. I should scalate both..data and labels??</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-607743' data-commentid="607743" data-postid="6939" data-belowelement="comment-607743" data-respondelement="respond" data-replyto="Reply to Carlos" aria-label='Reply to Carlos'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-607817" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-607817" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">May 3, 2021 at 4:55 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-607817" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Scaling input is a good idea, depending on the data and choice of model. </p> | |||
<p>If the target is numeric (e.g. regression), then scaling the target is a good idea, depending on the data and choice of model. </p> | |||
<p>If the target was sealed, then the scaling must be inverted on the prediction and the test data before calculating an error metic.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-607817' data-commentid="607817" data-postid="6939" data-belowelement="comment-607817" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-608235" class="comment odd alt thread-even depth-1"> | |||
<div id="li-comment-608235" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/aa14c08ab4ab406e94848ea6539cd2cb?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/aa14c08ab4ab406e94848ea6539cd2cb?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Israel</span> | |||
<span class="date">May 6, 2021 at 10:03 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-608235" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi Jason,</p> | |||
<p>I’m new to deep learning. I tried to implement a CNN regression model with multiple impute image chips of 31 channels(Raster image/TIFF format), and a numeric target variable. But the result I got is quite weird cos its giving me 100% accuracy (r2_score). I also noticed that during training, the loss/val loss output values were all zeros and the training was pretty fast considering feeding over 5000 images into the network. so I feel the network isn’t training anything passé.</p> | |||
<p>I want to ask if this could be as a result of data scaling? My image chips pixel values are in decimals (float) between 0 and 1 (all the image chips are less than 1), while my target variable are a continuous variable between 0 and 160 (integer). </p> | |||
<p>Do you think i need to perform some sort of normalization or standardization of my data?</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-608235' data-commentid="608235" data-postid="6939" data-belowelement="comment-608235" data-respondelement="respond" data-replyto="Reply to Israel" aria-label='Reply to Israel'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-608282" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-608282" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">May 7, 2021 at 6:26 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-608282" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Perhaps try scaling the data and see if it makes a difference.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-608282' data-commentid="608282" data-postid="6939" data-belowelement="comment-608282" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-608515" class="comment odd alt thread-odd thread-alt depth-1"> | |||
<div id="li-comment-608515" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/38fad5c19b9f971dbd2b25b324bda3f8?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/38fad5c19b9f971dbd2b25b324bda3f8?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='https://acehl.org/' rel='external nofollow ugc' class='url'>JG</a></span> | |||
<span class="date">May 9, 2021 at 6:00 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-608515" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi Jason,</p> | |||
<p>Great Tutorial! Thank you very much.<br /> | |||
very clear explanation of scaling inputs and output necessity !</p> | |||
<p>I am introducing your tutorial to a friend of mine who is very interested in following you.</p> | |||
<p>regards</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-608515' data-commentid="608515" data-postid="6939" data-belowelement="comment-608515" data-respondelement="respond" data-replyto="Reply to JG" aria-label='Reply to JG'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-608599" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-608599" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">May 10, 2021 at 6:18 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-608599" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Thanks!</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-608599' data-commentid="608599" data-postid="6939" data-belowelement="comment-608599" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-616693" class="comment odd alt depth-2"> | |||
<div id="li-comment-616693" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/e46bacdd9def9b26d935ea5f69c08d42?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/e46bacdd9def9b26d935ea5f69c08d42?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">Phil</span> | |||
<span class="date">July 16, 2021 at 4:00 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-616693" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>Hi Jason,</p> | |||
<p>Im currently training an MLP and I have 9 metric features and 3 binary coded to 0/1.<br /> | |||
So I have decided only to standardize the 9 metric feautes and leave the binary features untouched.<br /> | |||
Is this approch okay or should standardize the binary features as well – so they have an mean neat to zero and sd of 1</p> | |||
<p>Cheers</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-616693' data-commentid="616693" data-postid="6939" data-belowelement="comment-616693" data-respondelement="respond" data-replyto="Reply to Phil" aria-label='Reply to Phil'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-616721" class="comment byuser comment-author-jasonb bypostauthor even depth-3"> | |||
<div id="li-comment-616721" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">July 16, 2021 at 5:29 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-616721" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>It sounds strange to me that you would standardize binary features. Often they would be excluded from any scaling operation.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-616721' data-commentid="616721" data-postid="6939" data-belowelement="comment-616721" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
<li id="comment-619500" class="comment odd alt thread-even depth-1"> | |||
<div id="li-comment-619500" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/64dea7ac4d545d5f96926180a6c7ed54?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/64dea7ac4d545d5f96926180a6c7ed54?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name">voloddia</span> | |||
<span class="date">August 2, 2021 at 1:01 pm</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-619500" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>“You must ensure that the scale of your output variable matches the scale of the activation function (transfer function) on the output layer of your network.” </p> | |||
<p>I don’t understand this point.<br /> | |||
First, the output layer often has no activation function, or in other words, identity activation function which has arbitrary scale.<br /> | |||
Second, normalization and standardization are only linear transformations.<br /> | |||
Therefore, is it true that normalization/standardization of output is almost always unnecessary? If not, why?</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-619500' data-commentid="619500" data-postid="6939" data-belowelement="comment-619500" data-respondelement="respond" data-replyto="Reply to voloddia" aria-label='Reply to voloddia'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
<ul class="children"> | |||
<li id="comment-619602" class="comment byuser comment-author-jasonb bypostauthor even depth-2"> | |||
<div id="li-comment-619602" class="comment-container"> | |||
<div class="avatar"><img alt='' src='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=40&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=80&d=mm&r=g 2x' class='avatar avatar-40 photo' height='40' width='40' loading='lazy'/></div> | |||
<div class="comment-head"> | |||
<span class="name"><a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a></span> | |||
<span class="date">August 3, 2021 at 4:49 am</span> | |||
<span class="perma"><a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-619602" title="Direct link to this comment">#</a></span> | |||
<span class="edit"></span> | |||
</div><!-- /.comment-head --> | |||
<div class="comment-entry"> | |||
<p>This was critical in the olden days of sigmoid and tanh. These days, normalizing or standardizing is sufficient.</p> | |||
<p>It’s critical because large inputs cause large weights which leads to an unstable network, in general.</p> | |||
<div class="reply"> | |||
<a rel='nofollow' class='comment-reply-link' href='#comment-619602' data-commentid="619602" data-postid="6939" data-belowelement="comment-619602" data-respondelement="respond" data-replyto="Reply to Jason Brownlee" aria-label='Reply to Jason Brownlee'>Reply</a> </div><!-- /.reply --> | |||
</div><!-- /comment-entry --> | |||
</div><!-- /.comment-container --> | |||
</li><!-- #comment-## --> | |||
</ul><!-- .children --> | |||
</li><!-- #comment-## --> | |||
</ol> | |||
</div> <div id="respond" class="comment-respond"> | |||
<h3 id="reply-title" class="comment-reply-title">Leave a Reply <small><a rel="nofollow" id="cancel-comment-reply-link" href="/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#respond" style="display:none;">Click here to cancel reply.</a></small></h3><form action="https://machinelearningmastery.com/wp-comments-post.php?wpe-comment-post=mlmastery" method="post" id="commentform" class="comment-form"><p class="comment-form-comment"><label class="hide" for="comment">Comment</label> <textarea tabindex="4" id="comment" name="comment" cols="50" rows="10" maxlength="65525" required="required"></textarea></p><p class="comment-form-author"><input id="author" name="author" type="text" class="txt" tabindex="1" value="" size="30" aria-required='true' /><label for="author">Name <span class="required">(required)</span></label> </p> | |||
<p class="comment-form-email"><input id="email" name="email" type="text" class="txt" tabindex="2" value="" size="30" aria-required='true' /><label for="email">Email (will not be published) <span class="required">(required)</span></label> </p> | |||
<p class="comment-form-url"><input id="url" name="url" type="text" class="txt" tabindex="3" value="" size="30" /><label for="url">Website</label></p> | |||
<p class="form-submit"><input name="submit" type="submit" id="submit" class="submit" value="Submit Comment" /> <input type='hidden' name='comment_post_ID' value='6939' id='comment_post_ID' /> | |||
<input type='hidden' name='comment_parent' id='comment_parent' value='0' /> | |||
</p><p style="display: none;"><input type="hidden" id="akismet_comment_nonce" name="akismet_comment_nonce" value="2ca2f8d589" /></p><input type="hidden" id="ak_js" name="ak_js" value="32"/><textarea name="ak_hp_textarea" cols="45" rows="8" maxlength="100" style="display: none !important;"></textarea></form> </div><!-- #respond --> | |||
</section><!-- /#main --> | |||
<aside id="sidebar"> | |||
<div id="woo_blogauthorinfo-2" class="widget widget_woo_blogauthorinfo"><span class="left"><img alt='' src='https://secure.gravatar.com/avatar/1d75d46040c28497f0dee5d8e100db37?s=88&d=mm&r=g' srcset='https://secure.gravatar.com/avatar/1d75d46040c28497f0dee5d8e100db37?s=176&d=mm&r=g 2x' class='avatar avatar-88 photo' height='88' width='88' loading='lazy'/></span> | |||
<p><strong>Welcome!</strong><br> | |||
I'm <em>Jason Brownlee</em> PhD <br> | |||
and I <strong>help developers</strong> get results with <strong>machine learning</strong>.<br> | |||
<a href="/about">Read more</a></p> | |||
<div class="fix"></div> | |||
</div><div id="custom_html-89" class="widget_text widget widget_custom_html"><div class="textwidget custom-html-widget"><h4>Never miss a tutorial:</h4> | |||
<br> | |||
<div style="text-align: left;"> | |||
<a href="https://www.linkedin.com/company/machine-learning-mastery/"><img width="30" height="30" src="/wp-content/uploads/2019/12/small_icon_blue_linkedin3.png" alt="LinkedIn"></a> | |||
| |||
<a href="https://twitter.com/TeachTheMachine"><img width="30" height="30" src="/wp-content/uploads/2019/12/small_icon_blue_twitter3.png" alt="Twitter"></a> | |||
| |||
<a href="https://www.facebook.com/MachineLearningMastery/"><img width="30" height="30" src="/wp-content/uploads/2019/12/small_icon_blue_facebook3.png" alt="Facebook"></a> | |||
| |||
<a href="/newsletter/"><img width="30" height="30" src="/wp-content/uploads/2019/12/small_icon_blue_email3.png" alt="Email Newsletter"></a> | |||
| |||
<a href="https://machinelearningmastery.com/rss-feed/"><img width="30" height="30" src="/wp-content/uploads/2019/12/small_icon_blue_rss3.png" alt="RSS Feed"></a> | |||
</div></div></div><div id="custom_html-50" class="widget_text widget widget_custom_html"><div class="textwidget custom-html-widget"><h4>Picked for you:</h4> | |||
<br> | |||
<div class="display-posts-listing image-left"><div class="listing-item"><a class="image" href="https://machinelearningmastery.com/learning-curves-for-diagnosing-machine-learning-model-performance/"><img width="150" height="150" src="https://machinelearningmastery.com/wp-content/uploads/2018/12/Example-of-Train-and-Validation-Learning-Curves-Showing-a-Training-Dataset-the-May-be-too-Small-Relative-to-the-Validation-Dataset-150x150.png" class="attachment-thumbnail size-thumbnail wp-post-image" alt="Example of Train and Validation Learning Curves Showing a Training Dataset That May Be too Small Relative to the Validation Dataset" loading="lazy" /></a> <a class="title" href="https://machinelearningmastery.com/learning-curves-for-diagnosing-machine-learning-model-performance/" rel="nofollow">How to use Learning Curves to Diagnose Machine Learning Model Performance</a></div><div class="listing-item"><a class="image" href="https://machinelearningmastery.com/stacking-ensemble-for-deep-learning-neural-networks/"><img width="150" height="150" src="https://machinelearningmastery.com/wp-content/uploads/2018/10/Visualization-of-Stacked-Generalization-Ensemble-of-Neural-Network-Models-150x150.png" class="attachment-thumbnail size-thumbnail wp-post-image" alt="Visualization of Stacked Generalization Ensemble of Neural Network Models" loading="lazy" /></a> <a class="title" href="https://machinelearningmastery.com/stacking-ensemble-for-deep-learning-neural-networks/" rel="nofollow">Stacking Ensemble for Deep Learning Neural Networks in Python</a></div><div class="listing-item"><a class="image" href="https://machinelearningmastery.com/improve-deep-learning-performance/"><img width="150" height="150" src="https://machinelearningmastery.com/wp-content/uploads/2016/09/How-To-Improve-Deep-Learning-Performance-150x150.jpg" class="attachment-thumbnail size-thumbnail wp-post-image" alt="How To Improve Deep Learning Performance" loading="lazy" /></a> <a class="title" href="https://machinelearningmastery.com/improve-deep-learning-performance/" rel="nofollow">How To Improve Deep Learning Performance</a></div><div class="listing-item"><a class="image" href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/"><img width="150" height="150" src="https://machinelearningmastery.com/wp-content/uploads/2018/11/Box-and-Whisker-Plots-of-Mean-Squared-Error-With-Unscaled-Normalized-and-Standardized-Input-Variables-for-the-Regression-Problem-150x150.png" class="attachment-thumbnail size-thumbnail wp-post-image" alt="Box and Whisker Plots of Mean Squared Error With Unscaled, Normalized and Standardized Input Variables for the Regression Problem" loading="lazy" /></a> <a class="title" href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/" rel="nofollow">How to use Data Scaling Improve Deep Learning Model Stability and Performance</a></div><div class="listing-item"><a class="image" href="https://machinelearningmastery.com/adam-optimization-algorithm-for-deep-learning/"><img width="150" height="150" src="https://machinelearningmastery.com/wp-content/uploads/2017/05/Comparison-of-Adam-to-Other-Optimization-Algorithms-Training-a-Multilayer-Perceptron-150x150.png" class="attachment-thumbnail size-thumbnail wp-post-image" alt="Comparison of Adam to Other Optimization Algorithms Training a Multilayer Perceptron" loading="lazy" /></a> <a class="title" href="https://machinelearningmastery.com/adam-optimization-algorithm-for-deep-learning/" rel="nofollow">Gentle Introduction to the Adam Optimization Algorithm for Deep Learning</a></div></div></div></div><div id="custom_html-13" class="widget_text widget widget_custom_html"><div class="textwidget custom-html-widget"><div style="text-align: center;"> | |||
<p><h4>Loving the Tutorials?</h4></p> | |||
<p>The <a href="/better-deep-learning/" rel="nofollow">Better Deep Learning</a> EBook is <br>where you'll find the <strong><i>Really Good</i></strong> stuff.</p> | |||
<a href="/better-deep-learning/" class="woo-sc-button red" ><span class="woo-">>> See What's Inside</span></a> | |||
</div></div></div></aside><!-- /#sidebar --> | |||
</div><!-- /#main-sidebar-container --> | |||
</div><!-- /#content --> | |||
<footer id="footer" class="col-full"> | |||
<div id="copyright" class="col-left"> | |||
<p>© 2021 Machine Learning Mastery Pty. Ltd. All Rights Reserved.<br /> | |||
<a href="https://www.linkedin.com/company/machine-learning-mastery/">LinkedIn</a> | | |||
<a href="https://twitter.com/TeachTheMachine">Twitter</a> | | |||
<a href="https://www.facebook.com/MachineLearningMastery/">Facebook</a> | | |||
<a href="/newsletter/">Newsletter</a> | | |||
<a href="/rss-feed/">RSS</a></p> </div> | |||
<div id="credit" class="col-right"> | |||
<p><a href="/privacy/">Privacy</a> | | |||
<a href="/disclaimer/">Disclaimer</a> | | |||
<a href="/terms-of-service/">Terms</a> | | |||
<a href="/contact/">Contact</a> | | |||
<a href="/sitemap/">Sitemap</a> | | |||
<a href="/site-search/">Search</a></p> </div> | |||
</footer> | |||
</div><!-- /#inner-wrapper --> | |||
</div><!-- /#wrapper --> | |||
<div class="fix"></div><!--/.fix--> | |||
<!-- Drip --> | |||
<script data-cfasync="false" type="text/javascript"> | |||
var _dcq = _dcq || []; | |||
var _dcs = _dcs || {}; | |||
_dcs.account = '9556588'; | |||
(function() { | |||
var dc = document.createElement('script'); | |||
dc.type = 'text/javascript'; dc.async = true; | |||
dc.src = '//tag.getdrip.com/9556588.js'; | |||
var s = document.getElementsByTagName('script')[0]; | |||
s.parentNode.insertBefore(dc, s); | |||
})(); | |||
</script> | |||
<!-- end Drip --> | |||
<script type="f3886dae12b0536ad361ea93-text/javascript" id='rocket-browser-checker-js-after'> | |||
"use strict";var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||!1,descriptor.configurable=!0,"value"in descriptor&&(descriptor.writable=!0),Object.defineProperty(target,descriptor.key,descriptor)}}return function(Constructor,protoProps,staticProps){return protoProps&&defineProperties(Constructor.prototype,protoProps),staticProps&&defineProperties(Constructor,staticProps),Constructor}}();function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor))throw new TypeError("Cannot call a class as a function")}var RocketBrowserCompatibilityChecker=function(){function RocketBrowserCompatibilityChecker(options){_classCallCheck(this,RocketBrowserCompatibilityChecker),this.passiveSupported=!1,this._checkPassiveOption(this),this.options=!!this.passiveSupported&&options}return _createClass(RocketBrowserCompatibilityChecker,[{key:"_checkPassiveOption",value:function(self){try{var options={get passive(){return!(self.passiveSupported=!0)}};window.addEventListener("test",null,options),window.removeEventListener("test",null,options)}catch(err){self.passiveSupported=!1}}},{key:"initRequestIdleCallback",value:function(){!1 in window&&(window.requestIdleCallback=function(cb){var start=Date.now();return setTimeout(function(){cb({didTimeout:!1,timeRemaining:function(){return Math.max(0,50-(Date.now()-start))}})},1)}),!1 in window&&(window.cancelIdleCallback=function(id){return clearTimeout(id)})}},{key:"isDataSaverModeOn",value:function(){return"connection"in navigator&&!0===navigator.connection.saveData}},{key:"supportsLinkPrefetch",value:function(){var elem=document.createElement("link");return elem.relList&&elem.relList.supports&&elem.relList.supports("prefetch")&&window.IntersectionObserver&&"isIntersecting"in IntersectionObserverEntry.prototype}},{key:"isSlowConnection",value:function(){return"connection"in navigator&&"effectiveType"in navigator.connection&&("2g"===navigator.connection.effectiveType||"slow-2g"===navigator.connection.effectiveType)}}]),RocketBrowserCompatibilityChecker}(); | |||
</script> | |||
<script type="f3886dae12b0536ad361ea93-text/javascript" id='rocket-delay-js-js-after'> | |||
(function() { | |||
"use strict";var e=function(){function n(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(e,t,r){return t&&n(e.prototype,t),r&&n(e,r),e}}();function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var t=function(){function r(e,t){n(this,r),this.attrName="data-rocketlazyloadscript",this.browser=t,this.options=this.browser.options,this.triggerEvents=e,this.userEventListener=this.triggerListener.bind(this)}return e(r,[{key:"init",value:function(){this._addEventListener(this)}},{key:"reset",value:function(){this._removeEventListener(this)}},{key:"_addEventListener",value:function(t){this.triggerEvents.forEach(function(e){return window.addEventListener(e,t.userEventListener,t.options)})}},{key:"_removeEventListener",value:function(t){this.triggerEvents.forEach(function(e){return window.removeEventListener(e,t.userEventListener,t.options)})}},{key:"_loadScriptSrc",value:function(){var r=this,e=document.querySelectorAll("script["+this.attrName+"]");0!==e.length&&Array.prototype.slice.call(e).forEach(function(e){var t=e.getAttribute(r.attrName);e.setAttribute("src",t),e.removeAttribute(r.attrName)}),this.reset()}},{key:"triggerListener",value:function(){this._loadScriptSrc(),this._removeEventListener(this)}}],[{key:"run",value:function(){RocketBrowserCompatibilityChecker&&new r(["keydown","mouseover","touchmove","touchstart","wheel"],new RocketBrowserCompatibilityChecker({passive:!0})).init()}}]),r}();t.run(); | |||
}()); | |||
</script> | |||
<script type="f3886dae12b0536ad361ea93-text/javascript" id='rocket-preload-links-js-extra'> | |||
/* <![CDATA[ */ | |||
var RocketPreloadLinksConfig = {"excludeUris":"\/register\/machine-learning-university\/|\/courses\/\/lessons\/holding-back-goals\/|\/courses\/course-get-started\/|\/courses\/course-get-started\/lessons\/holding-back-goals\/|\/courses\/course-get-started\/lessons\/why-machine-learning-does-not-have-to-be-so-hard\/|\/courses\/course-get-started\/lessons\/how-to-think-about-machine-learning\/|\/courses\/course-get-started\/lessons\/find-your-machine-learning-tribe\/|\/courses\/step-by-step-process\/|\/courses\/probability-for-machine-learning\/|\/account\/|\/register\/|\/courses\/|\/machine-learning-mastery-university-registration\/|\/register\/|\/newsletter\/|\/(.+\/)?feed\/?.+\/?|\/(?:.+\/)?embed\/|\/(index\\.php\/)?wp\\-json(\/.*|$)|\/wp-admin\/|\/logout\/|\/login\/","usesTrailingSlash":"1","imageExt":"jpg|jpeg|gif|png|tiff|bmp|webp|avif","fileExt":"jpg|jpeg|gif|png|tiff|bmp|webp|avif|php|pdf|html|htm","siteUrl":"https:\/\/machinelearningmastery.com","onHoverDelay":"100","rateThrottle":"3"}; | |||
/* ]]> */ | |||
</script> | |||
<script type="f3886dae12b0536ad361ea93-text/javascript" id='rocket-preload-links-js-after'> | |||
(function() { | |||
"use strict";var r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},e=function(){function i(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}return function(e,t,n){return t&&i(e.prototype,t),n&&i(e,n),e}}();function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var t=function(){function n(e,t){i(this,n),this.browser=e,this.config=t,this.options=this.browser.options,this.prefetched=new Set,this.eventTime=null,this.threshold=1111,this.numOnHover=0}return e(n,[{key:"init",value:function(){!this.browser.supportsLinkPrefetch()||this.browser.isDataSaverModeOn()||this.browser.isSlowConnection()||(this.regex={excludeUris:RegExp(this.config.excludeUris,"i"),images:RegExp(".("+this.config.imageExt+")$","i"),fileExt:RegExp(".("+this.config.fileExt+")$","i")},this._initListeners(this))}},{key:"_initListeners",value:function(e){-1<this.config.onHoverDelay&&document.addEventListener("mouseover",e.listener.bind(e),e.listenerOptions),document.addEventListener("mousedown",e.listener.bind(e),e.listenerOptions),document.addEventListener("touchstart",e.listener.bind(e),e.listenerOptions)}},{key:"listener",value:function(e){var t=e.target.closest("a"),n=this._prepareUrl(t);if(null!==n)switch(e.type){case"mousedown":case"touchstart":this._addPrefetchLink(n);break;case"mouseover":this._earlyPrefetch(t,n,"mouseout")}}},{key:"_earlyPrefetch",value:function(t,e,n){var i=this,r=setTimeout(function(){if(r=null,0===i.numOnHover)setTimeout(function(){return i.numOnHover=0},1e3);else if(i.numOnHover>i.config.rateThrottle)return;i.numOnHover++,i._addPrefetchLink(e)},this.config.onHoverDelay);t.addEventListener(n,function e(){t.removeEventListener(n,e,{passive:!0}),null!==r&&(clearTimeout(r),r=null)},{passive:!0})}},{key:"_addPrefetchLink",value:function(i){return this.prefetched.add(i.href),new Promise(function(e,t){var n=document.createElement("link");n.rel="prefetch",n.href=i.href,n.onload=e,n.onerror=t,document.head.appendChild(n)}).catch(function(){})}},{key:"_prepareUrl",value:function(e){if(null===e||"object"!==(void 0===e?"undefined":r(e))||!1 in e||-1===["http:","https:"].indexOf(e.protocol))return null;var t=e.href.substring(0,this.config.siteUrl.length),n=this._getPathname(e.href,t),i={original:e.href,protocol:e.protocol,origin:t,pathname:n,href:t+n};return this._isLinkOk(i)?i:null}},{key:"_getPathname",value:function(e,t){var n=t?e.substring(this.config.siteUrl.length):e;return n.startsWith("/")||(n="/"+n),this._shouldAddTrailingSlash(n)?n+"/":n}},{key:"_shouldAddTrailingSlash",value:function(e){return this.config.usesTrailingSlash&&!e.endsWith("/")&&!this.regex.fileExt.test(e)}},{key:"_isLinkOk",value:function(e){return null!==e&&"object"===(void 0===e?"undefined":r(e))&&(!this.prefetched.has(e.href)&&e.origin===this.config.siteUrl&&-1===e.href.indexOf("?")&&-1===e.href.indexOf("#")&&!this.regex.excludeUris.test(e.href)&&!this.regex.images.test(e.href))}}],[{key:"run",value:function(){"undefined"!=typeof RocketPreloadLinksConfig&&new n(new RocketBrowserCompatibilityChecker({capture:!0,passive:!0}),RocketPreloadLinksConfig).init()}}]),n}();t.run(); | |||
}()); | |||
</script> | |||
<script type="f3886dae12b0536ad361ea93-text/javascript">window.lazyLoadOptions={elements_selector:"iframe[data-lazy-src]",data_src:"lazy-src",data_srcset:"lazy-srcset",data_sizes:"lazy-sizes",class_loading:"lazyloading",class_loaded:"lazyloaded",threshold:300,callback_loaded:function(element){if(element.tagName==="IFRAME"&&element.dataset.rocketLazyload=="fitvidscompatible"){if(element.classList.contains("lazyloaded")){if(typeof window.jQuery!="undefined"){if(jQuery.fn.fitVids){jQuery(element).parent().fitVids()}}}}}};window.addEventListener('LazyLoad::Initialized',function(e){var lazyLoadInstance=e.detail.instance;if(window.MutationObserver){var observer=new MutationObserver(function(mutations){var image_count=0;var iframe_count=0;var rocketlazy_count=0;mutations.forEach(function(mutation){for(i=0;i<mutation.addedNodes.length;i++){if(typeof mutation.addedNodes[i].getElementsByTagName!=='function'){continue} | |||
if(typeof mutation.addedNodes[i].getElementsByClassName!=='function'){continue} | |||
images=mutation.addedNodes[i].getElementsByTagName('img');is_image=mutation.addedNodes[i].tagName=="IMG";iframes=mutation.addedNodes[i].getElementsByTagName('iframe');is_iframe=mutation.addedNodes[i].tagName=="IFRAME";rocket_lazy=mutation.addedNodes[i].getElementsByClassName('rocket-lazyload');image_count+=images.length;iframe_count+=iframes.length;rocketlazy_count+=rocket_lazy.length;if(is_image){image_count+=1} | |||
if(is_iframe){iframe_count+=1}}});if(image_count>0||iframe_count>0||rocketlazy_count>0){lazyLoadInstance.update()}});var b=document.getElementsByTagName("body")[0];var config={childList:!0,subtree:!0};observer.observe(b,config)}},!1)</script><script data-no-minify="1" async src="https://machinelearningmastery.com/wp-content/plugins/wp-rocket/assets/js/lazyload/16.1/lazyload.min.js" type="f3886dae12b0536ad361ea93-text/javascript"></script><script src="https://machinelearningmastery.com/wp-content/cache/min/1/faa2bb2b045a56fea4ed2ea21d2cc719.js" data-minify="1" defer type="f3886dae12b0536ad361ea93-text/javascript"></script><script src="/cdn-cgi/scripts/7d0fa10a/cloudflare-static/rocket-loader.min.js" data-cf-settings="f3886dae12b0536ad361ea93-|49" defer=""></script></body> | |||
</html> | |||
<!-- This website is like a Rocket, isn't it? Performance optimized by WP Rocket. Learn more: https://wp-rocket.me --> | |||
Revision as of 12:50, 13 September 2021
<!DOCTYPE html> <html lang="en-US"> <head> <meta charset="UTF-8" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <link rel="pingback" href="https://machinelearningmastery.com/xmlrpc.php" /> <meta name='robots' content='index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1' />
<meta content="initial-scale=1.0, maximum-scale=1.0, user-scalable=yes" name="viewport"/>
<title>How to use Data Scaling Improve Deep Learning Model Stability and Performance</title><link rel="stylesheet" href="https://machinelearningmastery.com/wp-content/cache/min/1/7ab4e5ffb1c7994e88de46542752e00b.css" media="all" data-minify="1" /> <link rel="canonical" href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/" /> <meta property="og:locale" content="en_US" /> <meta property="og:type" content="article" /> <meta property="og:title" content="How to use Data Scaling Improve Deep Learning Model Stability and Performance" /> <meta property="og:description" content="Deep learning neural networks learn how to map inputs to outputs from examples in a training dataset. The weights of […]" /> <meta property="og:url" content="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/" /> <meta property="og:site_name" content="Machine Learning Mastery" /> <meta property="article:publisher" content="https://www.facebook.com/MachineLearningMastery/" /> <meta property="article:author" content="https://www.facebook.com/MachineLearningMastery/" /> <meta property="article:published_time" content="2019-02-03T18:00:34+00:00" /> <meta property="article:modified_time" content="2020-08-25T00:18:53+00:00" /> <meta property="og:image" content="https://machinelearningmastery.com/wp-content/uploads/2018/11/Box-and-Whisker-Plots-of-Mean-Squared-Error-With-Unscaled-Normalized-and-Standardized-Input-Variables-for-the-Regression-Problem.png" /> <meta property="og:image:width" content="1280" /> <meta property="og:image:height" content="960" /> <meta name="twitter:label1" content="Written by" /> <meta name="twitter:data1" content="Jason Brownlee" /> <meta name="twitter:label2" content="Est. reading time" /> <meta name="twitter:data2" content="26 minutes" /> <script type="application/ld+json" class="yoast-schema-graph">{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://machinelearningmastery.com/#organization","name":"Machine Learning Mastery","url":"https://machinelearningmastery.com/","sameAs":["https://www.facebook.com/MachineLearningMastery/","https://www.linkedin.com/company/machine-learning-mastery","https://twitter.com/TeachTheMachine"],"logo":{"@type":"ImageObject","@id":"https://machinelearningmastery.com/#logo","inLanguage":"en-US","url":"https://machinelearningmastery.com/wp-content/uploads/2016/09/cropped-icon.png","contentUrl":"https://machinelearningmastery.com/wp-content/uploads/2016/09/cropped-icon.png","width":512,"height":512,"caption":"Machine Learning Mastery"},"image":{"@id":"https://machinelearningmastery.com/#logo"}},{"@type":"WebSite","@id":"https://machinelearningmastery.com/#website","url":"https://machinelearningmastery.com/","name":"Machine Learning Mastery","description":"Making developers awesome at machine learning","publisher":{"@id":"https://machinelearningmastery.com/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://machinelearningmastery.com/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#primaryimage","inLanguage":"en-US","url":"https://machinelearningmastery.com/wp-content/uploads/2018/11/Box-and-Whisker-Plots-of-Mean-Squared-Error-With-Unscaled-Normalized-and-Standardized-Input-Variables-for-the-Regression-Problem.png","contentUrl":"https://machinelearningmastery.com/wp-content/uploads/2018/11/Box-and-Whisker-Plots-of-Mean-Squared-Error-With-Unscaled-Normalized-and-Standardized-Input-Variables-for-the-Regression-Problem.png","width":1280,"height":960,"caption":"Box and Whisker Plots of Mean Squared Error With Unscaled, Normalized and Standardized Input Variables for the Regression Problem"},{"@type":"WebPage","@id":"https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#webpage","url":"https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/","name":"How to use Data Scaling Improve Deep Learning Model Stability and Performance","isPartOf":{"@id":"https://machinelearningmastery.com/#website"},"primaryImageOfPage":{"@id":"https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#primaryimage"},"datePublished":"2019-02-03T18:00:34+00:00","dateModified":"2020-08-25T00:18:53+00:00","breadcrumb":{"@id":"https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/"]}]},{"@type":"BreadcrumbList","@id":"https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://machinelearningmastery.com/"},{"@type":"ListItem","position":2,"name":"Blog","item":"https://machinelearningmastery.com/blog/"},{"@type":"ListItem","position":3,"name":"How to use Data Scaling Improve Deep Learning Model Stability and Performance"}]},{"@type":"Article","@id":"https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#article","isPartOf":{"@id":"https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#webpage"},"author":{"@id":"https://machinelearningmastery.com/#/schema/person/e2d0ff4828d406a3b47e5a3c9a0591e8"},"headline":"How to use Data Scaling Improve Deep Learning Model Stability and Performance","datePublished":"2019-02-03T18:00:34+00:00","dateModified":"2020-08-25T00:18:53+00:00","mainEntityOfPage":{"@id":"https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#webpage"},"wordCount":3566,"commentCount":125,"publisher":{"@id":"https://machinelearningmastery.com/#organization"},"image":{"@id":"https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#primaryimage"},"thumbnailUrl":"https://machinelearningmastery.com/wp-content/uploads/2018/11/Box-and-Whisker-Plots-of-Mean-Squared-Error-With-Unscaled-Normalized-and-Standardized-Input-Variables-for-the-Regression-Problem.png","articleSection":["Deep Learning Performance"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#respond"]}]},{"@type":"Person","@id":"https://machinelearningmastery.com/#/schema/person/e2d0ff4828d406a3b47e5a3c9a0591e8","name":"Jason Brownlee","image":{"@type":"ImageObject","@id":"https://machinelearningmastery.com/#personlogo","inLanguage":"en-US","url":"https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=96&d=mm&r=g","contentUrl":"https://secure.gravatar.com/avatar/a0942b56b07831ac15d4a168a750e34a?s=96&d=mm&r=g","caption":"Jason Brownlee"},"description":"Jason Brownlee, PhD is a machine learning specialist who teaches developers how to get results with modern machine learning methods via hands-on tutorials.","sameAs":["http://MachineLearningMastery.com","https://www.facebook.com/MachineLearningMastery/","https://www.linkedin.com/company/machine-learning-mastery","https://twitter.com/teachthemachine"]}]}</script>
<link rel='dns-prefetch' href='//cdn.jsdelivr.net' />
<link rel='dns-prefetch' href='//ads.adthrive.com' />
<link rel='dns-prefetch' href='//www.google-analytics.com' />
<link rel='dns-prefetch' href='//loadeu.exelator.com' />
<link rel='dns-prefetch' href='//sync.crwdcntrl.net' />
<link rel='dns-prefetch' href='//gdpr-wrapper.privacymanager.io' />
<link rel='dns-prefetch' href='//securepubads.g.doubleclick.net' />
<link rel='dns-prefetch' href='//gdpr.privacymanager.io' />
<link rel='dns-prefetch' href='//sb.scorecardresearch.com' />
<link rel='dns-prefetch' href='//confiant-integrations.global.ssl.fastly.net' />
<link rel="alternate" type="application/rss+xml" title="Machine Learning Mastery » Feed" href="https://feeds.feedburner.com/MachineLearningMastery" /> <link rel="alternate" type="application/rss+xml" title="Machine Learning Mastery » Comments Feed" href="https://machinelearningmastery.com/comments/feed/" /> <link rel="alternate" type="application/rss+xml" title="Machine Learning Mastery » How to use Data Scaling Improve Deep Learning Model Stability and Performance Comments Feed" href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/feed/" /> <style type="text/css"> img.wp-smiley, img.emoji { display: inline !important; border: none !important; box-shadow: none !important; height: 1em !important; width: 1em !important; margin: 0 .07em !important; vertical-align: -0.1em !important; background: none !important; padding: 0 !important; } </style>
<script type="f3886dae12b0536ad361ea93-text/javascript" src='https://machinelearningmastery.com/wp-includes/js/jquery/jquery.min.js?ver=3.6.0' id='jquery-core-js' defer></script>
<script type="f3886dae12b0536ad361ea93-text/javascript" id='ssb-front-js-js-extra'> /* <![CDATA[ */ var SSB = {"ajax_url":"https:\/\/machinelearningmastery.com\/wp-admin\/admin-ajax.php","fb_share_nonce":"0f0d514a34"}; /* ]]> */ </script>
<link rel="https://api.w.org/" href="https://machinelearningmastery.com/wp-json/" /><link rel="alternate" type="application/json" href="https://machinelearningmastery.com/wp-json/wp/v2/posts/6939" /><link rel="EditURI" type="application/rsd+xml" title="RSD" href="https://machinelearningmastery.com/xmlrpc.php?rsd" />
<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="https://machinelearningmastery.com/wp-includes/wlwmanifest.xml" />
<link rel='shortlink' href='https://machinelearningmastery.com/?p=6939' />
<style type="text/css">
.mpcs-classroom .nav-back i,
.mpcs-classroom .navbar-section a.btn,
.mpcs-classroom .navbar-section a,
.mpcs-classroom .navbar-section button {
color: rgba(255, 255, 255) !important;
}
.mpcs-classroom .navbar-section .dropdown .menu a {
color: rgba(44, 54, 55) !important;
}
.mpcs-classroom .mpcs-progress-ring {
background-color: rgba(29, 166, 154) !important;
}
.mpcs-classroom .mpcs-course-filter .dropdown .btn span,
.mpcs-classroom .mpcs-course-filter .dropdown .btn i,
.mpcs-classroom .mpcs-course-filter .input-group .input-group-btn,
.mpcs-classroom .mpcs-course-filter .input-group .mpcs-search,
.mpcs-classroom .mpcs-course-filter .input-group input[type=text],
.mpcs-classroom .mpcs-course-filter .dropdown a,
.mpcs-classroom .pagination,
.mpcs-classroom .pagination i,
.mpcs-classroom .pagination a {
color: rgba(44, 54, 55) !important;
border-color: rgba(44, 54, 55) !important;
}
/* body.mpcs-classroom a{
color: rgba();
} */
#mpcs-navbar,
#mpcs-navbar button#previous_lesson_link,
#mpcs-navbar button#previous_lesson_link:hover {
background: rgba(44, 54, 55);
}
.course-progress .user-progress,
.btn-green,
#mpcs-navbar button:not(#previous_lesson_link){
background: rgba(29, 166, 154, 0.9);
}
.btn-green:hover,
#mpcs-navbar button:not(#previous_lesson_link):focus,
#mpcs-navbar button:not(#previous_lesson_link):hover{
background: rgba(29, 166, 154);
}
.btn-green{border: rgba(29, 166, 154)}
.course-progress .progress-text,
.mpcs-lesson i.mpcs-circle-regular {
color: rgba(29, 166, 154)
}
#mpcs-main #bookmark, .mpcs-lesson.current{background: rgba(29, 166, 154, 0.3)}
.mpcs-instructor .tile-subtitle{
color: rgba(29, 166, 154, 1)
}
</style>
<style media="screen">
.simplesocialbuttons.simplesocialbuttons_inline .ssb-fb-like { margin: ; } /*inline margin*/
.simplesocialbuttons.simplesocialbuttons_inline.simplesocial-simple-icons button{
margin: ;
}
/*margin-digbar*/
</style>
<meta property="og:title" content="How to use Data Scaling Improve Deep Learning Model Stability and Performance - Machine Learning Mastery" /> <meta property="og:description" content="Deep learning neural networks learn how to map inputs to outputs from examples in a training dataset.
The weights of the model are initialized to small random values and updated via an optimization algorithm in response to estimates of error on the training dataset.
Given the use of small weights in the model and the use of error between predictions and expected" /> <meta property="og:url" content="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/" /> <meta property="og:site_name" content="Machine Learning Mastery" /> <meta property="og:image" content="https://machinelearningmastery.com/wp-content/uploads/2018/11/Box-and-Whisker-Plots-of-Mean-Squared-Error-With-Unscaled-Normalized-and-Standardized-Input-Variables-for-the-Regression-Problem.png" /> <meta name="twitter:card" content="summary_large_image" /> <meta name="twitter:description" content="Deep learning neural networks learn how to map inputs to outputs from examples in a training dataset.
The weights of the model are initialized to small random values and updated via an optimization algorithm in response to estimates of error on the training dataset.
Given the use of small weights in the model and the use of error between predictions and expected" /> <meta name="twitter:title" content="How to use Data Scaling Improve Deep Learning Model Stability and Performance - Machine Learning Mastery" /> <meta property="twitter:image" content="https://machinelearningmastery.com/wp-content/uploads/2018/11/Box-and-Whisker-Plots-of-Mean-Squared-Error-With-Unscaled-Normalized-and-Standardized-Input-Variables-for-the-Regression-Problem.png" />
<style id="wplmi-inline-css" type="text/css"> span.wplmi-user-avatar { width: 16px;display: inline-block !important;flex-shrink: 0; } img.wplmi-elementor-avatar { border-radius: 100%;margin-right: 3px; }
</style>
<link rel="preload" as="font" href="https://machinelearningmastery.com/wp-content/themes/canvas-new/includes/fonts/fontawesome-webfont.woff2?v=4.5.0" crossorigin> <style type="text/css">
- logo .site-title, #logo .site-description { display:none; }
body {background-repeat:no-repeat;background-position:top left;background-attachment:scroll;border-top:0px solid #000000;}
- header {background-repeat:no-repeat;background-position:left top;margin-top:0px;margin-bottom:0px;padding-top:10px;padding-bottom:10px;border:0px solid ;}
- logo .site-title a {font:bold 40px/1em "Helvetica Neue", Helvetica, sans-serif;color:#222222;}
- logo .site-description {font:normal 13px/1em "Helvetica Neue", Helvetica, sans-serif;color:#999999;}
body, p { font:normal 14px/1.5em "Helvetica Neue", Helvetica, sans-serif;color:#555555; } h1 { font:bold 28px/1.2em "Helvetica Neue", Helvetica, sans-serif;color:#222222; }h2 { font:bold 24px/1.2em "Helvetica Neue", Helvetica, sans-serif;color:#222222; }h3 { font:bold 20px/1.2em "Helvetica Neue", Helvetica, sans-serif;color:#222222; }h4 { font:bold 16px/1.2em "Helvetica Neue", Helvetica, sans-serif;color:#222222; }h5 { font:bold 14px/1.2em "Helvetica Neue", Helvetica, sans-serif;color:#222222; }h6 { font:bold 12px/1.2em "Helvetica Neue", Helvetica, sans-serif;color:#222222; } .page-title, .post .title, .page .title {font:bold 28px/1.1em "Helvetica Neue", Helvetica, sans-serif;color:#222222;} .post .title a:link, .post .title a:visited, .page .title a:link, .page .title a:visited {color:#222222} .post-meta { font:normal 12px/1.5em "Helvetica Neue", Helvetica, sans-serif;color:#999999; } .entry, .entry p{ font:normal 15px/1.5em "Helvetica Neue", Helvetica, sans-serif;color:#555555; } .post-more {font:normal 13px/1.5em "Helvetica Neue", Helvetica, sans-serif;color:;border-top:0px solid #e6e6e6;border-bottom:0px solid #e6e6e6;}
- post-author, #connect {border-top:1px solid #e6e6e6;border-bottom:1px solid #e6e6e6;border-left:1px solid #e6e6e6;border-right:1px solid #e6e6e6;border-radius:5px;-moz-border-radius:5px;-webkit-border-radius:5px;background-color:#fafafa}
.nav-entries a, .woo-pagination { font:normal 13px/1em "Helvetica Neue", Helvetica, sans-serif;color:#888; } .woo-pagination a, .woo-pagination a:hover {color:#888!important} .widget h3 {font:bold 14px/1.2em "Helvetica Neue", Helvetica, sans-serif;color:#555555;border-bottom:1px solid #e6e6e6;} .widget_recent_comments li, #twitter li { border-color: #e6e6e6;} .widget p, .widget .textwidget { font:normal 13px/1.5em "Helvetica Neue", Helvetica, sans-serif;color:#555555; } .widget {font:normal 13px/1.5em "Helvetica Neue", Helvetica, sans-serif;color:#555555;border-radius:0px;-moz-border-radius:0px;-webkit-border-radius:0px;}
- tabs .inside li a, .widget_woodojo_tabs .tabbable .tab-pane li a { font:bold 12px/1.5em "Helvetica Neue", Helvetica, sans-serif;color:#555555; }
- tabs .inside li span.meta, .widget_woodojo_tabs .tabbable .tab-pane li span.meta { font:300 11px/1.5em "Helvetica Neue", Helvetica, sans-serif;color:#999999; }
- tabs ul.wooTabs li a, .widget_woodojo_tabs .tabbable .nav-tabs li a { font:300 11px/2em "Helvetica Neue", Helvetica, sans-serif;color:#999999; }
@media only screen and (min-width:768px) { ul.nav li a, #navigation ul.rss a, #navigation ul.cart a.cart-contents, #navigation .cart-contents #navigation ul.rss, #navigation ul.nav-search, #navigation ul.nav-search a { font:bold 15px/1.2em "Helvetica Neue", Helvetica, sans-serif;color:#ffffff; } #navigation ul.rss li a:before, #navigation ul.nav-search a.search-contents:before { color:#ffffff;}
- navigation ul.nav > li a:hover, #navigation ul.nav > li:hover a, #navigation ul.nav li ul li a, #navigation ul.cart > li:hover > a, #navigation ul.cart > li > ul > div, #navigation ul.cart > li > ul > div p, #navigation ul.cart > li > ul span, #navigation ul.cart .cart_list a, #navigation ul.nav li.current_page_item a, #navigation ul.nav li.current_page_parent a, #navigation ul.nav li.current-menu-ancestor a, #navigation ul.nav li.current-cat a, #navigation ul.nav li.current-menu-item a { color:#eeeeee!important; }
- navigation ul.nav > li a:hover, #navigation ul.nav > li:hover, #navigation ul.nav li ul, #navigation ul.cart li:hover a.cart-contents, #navigation ul.nav-search li:hover a.search-contents, #navigation ul.nav-search a.search-contents + ul, #navigation ul.cart a.cart-contents + ul, #navigation ul.nav li.current_page_item a, #navigation ul.nav li.current_page_parent a, #navigation ul.nav li.current-menu-ancestor a, #navigation ul.nav li.current-cat a, #navigation ul.nav li.current-menu-item a{background-color:#84abc7!important}
- navigation ul.nav li ul, #navigation ul.cart > li > ul > div { border: 0px solid #dbdbdb; }
- navigation ul.nav > li:hover > ul { left: 0; }
- navigation ul.nav > li { border-right: 0px solid #dbdbdb; }#navigation ul.nav > li:hover > ul { left: 0; }
- navigation { box-shadow: none; -moz-box-shadow: none; -webkit-box-shadow: none; }#navigation ul li:first-child, #navigation ul li:first-child a { border-radius:0px 0 0 0px; -moz-border-radius:0px 0 0 0px; -webkit-border-radius:0px 0 0 0px; }
- navigation {background:#84abc7;border-top:0px solid #dbdbdb;border-bottom:0px solid #dbdbdb;border-left:0px solid #dbdbdb;border-right:0px solid #dbdbdb;border-radius:0px; -moz-border-radius:0px; -webkit-border-radius:0px;}
- top ul.nav li a { font:normal 12px/1.6em "Helvetica Neue", Helvetica, sans-serif;color:#ddd; }
}
- footer, #footer p { font:normal 13px/1.4em "Helvetica Neue", Helvetica, sans-serif;color:#999999; }
- footer {border-top:1px solid #dbdbdb;border-bottom:0px solid ;border-left:0px solid ;border-right:0px solid ;border-radius:0px; -moz-border-radius:0px; -webkit-border-radius:0px;}
.magazine #loopedSlider .content h2.title a { font:bold 24px/1em Arial, sans-serif;color:#ffffff; } .wooslider-theme-magazine .slide-title a { font:bold 24px/1em Arial, sans-serif;color:#ffffff; } .magazine #loopedSlider .content .excerpt p { font:300 13px/1.5em Arial, sans-serif;color:#cccccc; } .wooslider-theme-magazine .slide-content p, .wooslider-theme-magazine .slide-excerpt p { font:300 13px/1.5em Arial, sans-serif;color:#cccccc; } .magazine .block .post .title a {font:bold 18px/1.2em Helvetica Neue, Helvetica, sans-serif;color:#222222; }
- loopedSlider.business-slider .content h2 { font:bold 24px/1em Arial, sans-serif;color:#ffffff; }
- loopedSlider.business-slider .content h2.title a { font:bold 24px/1em Arial, sans-serif;color:#ffffff; }
.wooslider-theme-business .has-featured-image .slide-title { font:bold 24px/1em Arial, sans-serif;color:#ffffff; } .wooslider-theme-business .has-featured-image .slide-title a { font:bold 24px/1em Arial, sans-serif;color:#ffffff; }
- wrapper #loopedSlider.business-slider .content p { font:300 13px/1.5em Arial, sans-serif;color:#cccccc; }
.wooslider-theme-business .has-featured-image .slide-content p { font:300 13px/1.5em Arial, sans-serif;color:#cccccc; } .wooslider-theme-business .has-featured-image .slide-excerpt p { font:300 13px/1.5em Arial, sans-serif;color:#cccccc; } .archive_header { font:bold 18px/1em Arial, sans-serif;color:#222222; } .archive_header {border-bottom:1px solid #e6e6e6;} .archive_header .catrss { display:none; } </style>
<link rel="shortcut icon" href="https://machinelearningmastery.com/wp-content/uploads/2019/09/icon-16x16.png"/> <style type="text/css">
- logo img {
max-width: 100%; height: auto;
} </style>
<meta name="generator" content="Canvas 5.9.21" />
<meta name="generator" content="WooFramework 6.2.9" />
<link rel="icon" href="https://machinelearningmastery.com/wp-content/uploads/2016/09/cropped-icon-32x32.png" sizes="32x32" /> <link rel="icon" href="https://machinelearningmastery.com/wp-content/uploads/2016/09/cropped-icon-192x192.png" sizes="192x192" /> <link rel="apple-touch-icon" href="https://machinelearningmastery.com/wp-content/uploads/2016/09/cropped-icon-180x180.png" /> <meta name="msapplication-TileImage" content="https://machinelearningmastery.com/wp-content/uploads/2016/09/cropped-icon-270x270.png" /> <style type="text/css" id="wp-custom-css"> .display-posts-listing.image-left .listing-item { overflow: hidden; margin-bottom: 30px; width: 100%; }
.display-posts-listing.image-left .image { float: left; margin: 0 10px 0 0; }
.display-posts-listing.image-left .attachment-thumbnail { height: auto; width: auto; max-width: 50px; max-height: 50px; border-radius: 50%; }
.display-posts-listing.image-left .title { display: block; }
.display-posts-listing.image-left .excerpt-dash { display: none; }
.display-posts-listing.image-left { margin: 0 0 40px 0; } </style> <noscript><style id="rocket-lazyload-nojs-css">.rll-youtube-player, [data-lazy-src]{display:none !important;}</style></noscript> <script type="f3886dae12b0536ad361ea93-text/javascript"> !function(f,b,e,v,n,t,s) {if(f.fbq)return;n=f.fbq=function(){n.callMethod? n.callMethod.apply(n,arguments):n.queue.push(arguments)}; if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0'; n.queue=[];t=b.createElement(e);t.async=!0; t.src=v;s=b.getElementsByTagName(e)[0]; s.parentNode.insertBefore(t,s)}(window, document,'script', 'https://machinelearningmastery.com/wp-content/cache/busting/facebook-tracking/fbpix-events-en_US-2.9.5.js'); fbq('init', '834324500844861'); fbq('track', 'PageView'); </script> <noscript><img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=834324500844861&ev=PageView&noscript=1" /></noscript> </head> <body class="post-template-default single single-post postid-6939 single-format-standard chrome alt-style-default two-col-left width-960 two-col-left-960">
<a href="/super-bundle/?utm_campaign=Machine%20Learning%20Mastery%20Super%20Bundle&utm_source=website&utm_medium=banner">Click to get the 20-book Super Bundle! (Save $250)</a>
<header id="header" class="col-full">
<a href="https://machinelearningmastery.com/" title="Making developers awesome at machine learning"><img width="480" height="80" src="https://machinelearningmastery.com/wp-content/uploads/2019/09/Header_smaller_text_better-1.png" alt="Machine Learning Mastery" /></a> <a href="https://machinelearningmastery.com/">Machine Learning Mastery</a> Making developers awesome at machine learning
</header> <nav id="navigation" class="col-full" role="navigation">
<section class="menus">
<a href="https://machinelearningmastery.com" class="nav-home">Home</a>
Main Menu
</section>
<a href="#top" class="nav-close">Return to Content</a>
</nav>
<footer id="footer" class="col-full">
© 2021 Machine Learning Mastery Pty. Ltd. All Rights Reserved.
<a href="https://www.linkedin.com/company/machine-learning-mastery/">LinkedIn</a> |
<a href="https://twitter.com/TeachTheMachine">Twitter</a> |
<a href="https://www.facebook.com/MachineLearningMastery/">Facebook</a> |
<a href="/newsletter/">Newsletter</a> |
<a href="/rss-feed/">RSS</a>
<a href="/privacy/">Privacy</a> | <a href="/disclaimer/">Disclaimer</a> | <a href="/terms-of-service/">Terms</a> | <a href="/contact/">Contact</a> | <a href="/sitemap/">Sitemap</a> | <a href="/site-search/">Search</a>
</footer>
<script data-cfasync="false" type="text/javascript">
var _dcq = _dcq || [];
var _dcs = _dcs || {};
_dcs.account = '9556588';
(function() {
var dc = document.createElement('script');
dc.type = 'text/javascript'; dc.async = true;
dc.src = '//tag.getdrip.com/9556588.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(dc, s);
})();
</script>
<script type="f3886dae12b0536ad361ea93-text/javascript" id='rocket-browser-checker-js-after'> "use strict";var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||!1,descriptor.configurable=!0,"value"in descriptor&&(descriptor.writable=!0),Object.defineProperty(target,descriptor.key,descriptor)}}return function(Constructor,protoProps,staticProps){return protoProps&&defineProperties(Constructor.prototype,protoProps),staticProps&&defineProperties(Constructor,staticProps),Constructor}}();function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor))throw new TypeError("Cannot call a class as a function")}var RocketBrowserCompatibilityChecker=function(){function RocketBrowserCompatibilityChecker(options){_classCallCheck(this,RocketBrowserCompatibilityChecker),this.passiveSupported=!1,this._checkPassiveOption(this),this.options=!!this.passiveSupported&&options}return _createClass(RocketBrowserCompatibilityChecker,[{key:"_checkPassiveOption",value:function(self){try{var options={get passive(){return!(self.passiveSupported=!0)}};window.addEventListener("test",null,options),window.removeEventListener("test",null,options)}catch(err){self.passiveSupported=!1}}},{key:"initRequestIdleCallback",value:function(){!1 in window&&(window.requestIdleCallback=function(cb){var start=Date.now();return setTimeout(function(){cb({didTimeout:!1,timeRemaining:function(){return Math.max(0,50-(Date.now()-start))}})},1)}),!1 in window&&(window.cancelIdleCallback=function(id){return clearTimeout(id)})}},{key:"isDataSaverModeOn",value:function(){return"connection"in navigator&&!0===navigator.connection.saveData}},{key:"supportsLinkPrefetch",value:function(){var elem=document.createElement("link");return elem.relList&&elem.relList.supports&&elem.relList.supports("prefetch")&&window.IntersectionObserver&&"isIntersecting"in IntersectionObserverEntry.prototype}},{key:"isSlowConnection",value:function(){return"connection"in navigator&&"effectiveType"in navigator.connection&&("2g"===navigator.connection.effectiveType||"slow-2g"===navigator.connection.effectiveType)}}]),RocketBrowserCompatibilityChecker}(); </script> <script type="f3886dae12b0536ad361ea93-text/javascript" id='rocket-delay-js-js-after'> (function() { "use strict";var e=function(){function n(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(e,t,r){return t&&n(e.prototype,t),r&&n(e,r),e}}();function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var t=function(){function r(e,t){n(this,r),this.attrName="data-rocketlazyloadscript",this.browser=t,this.options=this.browser.options,this.triggerEvents=e,this.userEventListener=this.triggerListener.bind(this)}return e(r,[{key:"init",value:function(){this._addEventListener(this)}},{key:"reset",value:function(){this._removeEventListener(this)}},{key:"_addEventListener",value:function(t){this.triggerEvents.forEach(function(e){return window.addEventListener(e,t.userEventListener,t.options)})}},{key:"_removeEventListener",value:function(t){this.triggerEvents.forEach(function(e){return window.removeEventListener(e,t.userEventListener,t.options)})}},{key:"_loadScriptSrc",value:function(){var r=this,e=document.querySelectorAll("script["+this.attrName+"]");0!==e.length&&Array.prototype.slice.call(e).forEach(function(e){var t=e.getAttribute(r.attrName);e.setAttribute("src",t),e.removeAttribute(r.attrName)}),this.reset()}},{key:"triggerListener",value:function(){this._loadScriptSrc(),this._removeEventListener(this)}}],[{key:"run",value:function(){RocketBrowserCompatibilityChecker&&new r(["keydown","mouseover","touchmove","touchstart","wheel"],new RocketBrowserCompatibilityChecker({passive:!0})).init()}}]),r}();t.run(); }()); </script> <script type="f3886dae12b0536ad361ea93-text/javascript" id='rocket-preload-links-js-extra'> /* <![CDATA[ */ var RocketPreloadLinksConfig = {"excludeUris":"\/register\/machine-learning-university\/|\/courses\/\/lessons\/holding-back-goals\/|\/courses\/course-get-started\/|\/courses\/course-get-started\/lessons\/holding-back-goals\/|\/courses\/course-get-started\/lessons\/why-machine-learning-does-not-have-to-be-so-hard\/|\/courses\/course-get-started\/lessons\/how-to-think-about-machine-learning\/|\/courses\/course-get-started\/lessons\/find-your-machine-learning-tribe\/|\/courses\/step-by-step-process\/|\/courses\/probability-for-machine-learning\/|\/account\/|\/register\/|\/courses\/|\/machine-learning-mastery-university-registration\/|\/register\/|\/newsletter\/|\/(.+\/)?feed\/?.+\/?|\/(?:.+\/)?embed\/|\/(index\\.php\/)?wp\\-json(\/.*|$)|\/wp-admin\/|\/logout\/|\/login\/","usesTrailingSlash":"1","imageExt":"jpg|jpeg|gif|png|tiff|bmp|webp|avif","fileExt":"jpg|jpeg|gif|png|tiff|bmp|webp|avif|php|pdf|html|htm","siteUrl":"https:\/\/machinelearningmastery.com","onHoverDelay":"100","rateThrottle":"3"}; /* ]]> */ </script> <script type="f3886dae12b0536ad361ea93-text/javascript" id='rocket-preload-links-js-after'> (function() { "use strict";var r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},e=function(){function i(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}return function(e,t,n){return t&&i(e.prototype,t),n&&i(e,n),e}}();function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var t=function(){function n(e,t){i(this,n),this.browser=e,this.config=t,this.options=this.browser.options,this.prefetched=new Set,this.eventTime=null,this.threshold=1111,this.numOnHover=0}return e(n,[{key:"init",value:function(){!this.browser.supportsLinkPrefetch()||this.browser.isDataSaverModeOn()||this.browser.isSlowConnection()||(this.regex={excludeUris:RegExp(this.config.excludeUris,"i"),images:RegExp(".("+this.config.imageExt+")$","i"),fileExt:RegExp(".("+this.config.fileExt+")$","i")},this._initListeners(this))}},{key:"_initListeners",value:function(e){-1<this.config.onHoverDelay&&document.addEventListener("mouseover",e.listener.bind(e),e.listenerOptions),document.addEventListener("mousedown",e.listener.bind(e),e.listenerOptions),document.addEventListener("touchstart",e.listener.bind(e),e.listenerOptions)}},{key:"listener",value:function(e){var t=e.target.closest("a"),n=this._prepareUrl(t);if(null!==n)switch(e.type){case"mousedown":case"touchstart":this._addPrefetchLink(n);break;case"mouseover":this._earlyPrefetch(t,n,"mouseout")}}},{key:"_earlyPrefetch",value:function(t,e,n){var i=this,r=setTimeout(function(){if(r=null,0===i.numOnHover)setTimeout(function(){return i.numOnHover=0},1e3);else if(i.numOnHover>i.config.rateThrottle)return;i.numOnHover++,i._addPrefetchLink(e)},this.config.onHoverDelay);t.addEventListener(n,function e(){t.removeEventListener(n,e,{passive:!0}),null!==r&&(clearTimeout(r),r=null)},{passive:!0})}},{key:"_addPrefetchLink",value:function(i){return this.prefetched.add(i.href),new Promise(function(e,t){var n=document.createElement("link");n.rel="prefetch",n.href=i.href,n.onload=e,n.onerror=t,document.head.appendChild(n)}).catch(function(){})}},{key:"_prepareUrl",value:function(e){if(null===e||"object"!==(void 0===e?"undefined":r(e))||!1 in e||-1===["http:","https:"].indexOf(e.protocol))return null;var t=e.href.substring(0,this.config.siteUrl.length),n=this._getPathname(e.href,t),i={original:e.href,protocol:e.protocol,origin:t,pathname:n,href:t+n};return this._isLinkOk(i)?i:null}},{key:"_getPathname",value:function(e,t){var n=t?e.substring(this.config.siteUrl.length):e;return n.startsWith("/")||(n="/"+n),this._shouldAddTrailingSlash(n)?n+"/":n}},{key:"_shouldAddTrailingSlash",value:function(e){return this.config.usesTrailingSlash&&!e.endsWith("/")&&!this.regex.fileExt.test(e)}},{key:"_isLinkOk",value:function(e){return null!==e&&"object"===(void 0===e?"undefined":r(e))&&(!this.prefetched.has(e.href)&&e.origin===this.config.siteUrl&&-1===e.href.indexOf("?")&&-1===e.href.indexOf("#")&&!this.regex.excludeUris.test(e.href)&&!this.regex.images.test(e.href))}}],[{key:"run",value:function(){"undefined"!=typeof RocketPreloadLinksConfig&&new n(new RocketBrowserCompatibilityChecker({capture:!0,passive:!0}),RocketPreloadLinksConfig).init()}}]),n}();t.run(); }()); </script>
<script type="f3886dae12b0536ad361ea93-text/javascript">window.lazyLoadOptions={elements_selector:"iframe[data-lazy-src]",data_src:"lazy-src",data_srcset:"lazy-srcset",data_sizes:"lazy-sizes",class_loading:"lazyloading",class_loaded:"lazyloaded",threshold:300,callback_loaded:function(element){if(element.tagName==="IFRAME"&&element.dataset.rocketLazyload=="fitvidscompatible"){if(element.classList.contains("lazyloaded")){if(typeof window.jQuery!="undefined"){if(jQuery.fn.fitVids){jQuery(element).parent().fitVids()}}}}}};window.addEventListener('LazyLoad::Initialized',function(e){var lazyLoadInstance=e.detail.instance;if(window.MutationObserver){var observer=new MutationObserver(function(mutations){var image_count=0;var iframe_count=0;var rocketlazy_count=0;mutations.forEach(function(mutation){for(i=0;i<mutation.addedNodes.length;i++){if(typeof mutation.addedNodes[i].getElementsByTagName!=='function'){continue} if(typeof mutation.addedNodes[i].getElementsByClassName!=='function'){continue} images=mutation.addedNodes[i].getElementsByTagName('img');is_image=mutation.addedNodes[i].tagName=="IMG";iframes=mutation.addedNodes[i].getElementsByTagName('iframe');is_iframe=mutation.addedNodes[i].tagName=="IFRAME";rocket_lazy=mutation.addedNodes[i].getElementsByClassName('rocket-lazyload');image_count+=images.length;iframe_count+=iframes.length;rocketlazy_count+=rocket_lazy.length;if(is_image){image_count+=1} if(is_iframe){iframe_count+=1}}});if(image_count>0||iframe_count>0||rocketlazy_count>0){lazyLoadInstance.update()}});var b=document.getElementsByTagName("body")[0];var config={childList:!0,subtree:!0};observer.observe(b,config)}},!1)</script><script data-no-minify="1" async src="https://machinelearningmastery.com/wp-content/plugins/wp-rocket/assets/js/lazyload/16.1/lazyload.min.js" type="f3886dae12b0536ad361ea93-text/javascript"></script><script src="https://machinelearningmastery.com/wp-content/cache/min/1/faa2bb2b045a56fea4ed2ea21d2cc719.js" data-minify="1" defer type="f3886dae12b0536ad361ea93-text/javascript"></script><script src="/cdn-cgi/scripts/7d0fa10a/cloudflare-static/rocket-loader.min.js" data-cf-settings="f3886dae12b0536ad361ea93-|49" defer=""></script></body> </html>
Wonbin February 13, 2019 at 6:03 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-468125" title="Direct link to this comment">#</a>
Thank you for this helpful post for beginners!
Could you please provide more details about the steps of “using the root mean squared error on the unscaled data” to interpret the performance in a specific domain?
Would it be like this??
———————————————————–
1. Finalize the model (based on the performance being calculated from the scaled output variable)
2. Make predictions on test set
3. Invert the predictions (to convert them back into their original scale)
4. Calculate the metrics (e.g. RMSE, MAPE)
———————————————————–
Waiting for your reply! Cheers mate!
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> February 14, 2019 at 8:39 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-468241" title="Direct link to this comment">#</a>
Correct.
ajebulon April 30, 2019 at 2:44 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-483399" title="Direct link to this comment">#</a>
Really nice article! I got Some quick questions,
If I have multiple input columns, each has different value range, might be [0, 1000] or even a one-hot-encoded data, should all be scaled with same method, or it can be processed differently?
For example:
– input A is normalized to [0, 1],
– input B is normalized to [-1, 1],
– input C is standardized,
– one-hot-encoded data is not scaled
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> May 1, 2019 at 6:58 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-483508" title="Direct link to this comment">#</a>
Yes, typically it is a good idea to scale all columns to have the same range. Perhaps start with [0,1] and compare others to see if they result in an improvement.
mk123qwe February 19, 2019 at 5:38 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-469330" title="Direct link to this comment">#</a>
we want standardized inputs, no scaling of outputs,but outputs value is not in (0,1).Are the predictions inaccurate?
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> February 20, 2019 at 7:51 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-469398" title="Direct link to this comment">#</a>
I don’t follow, are what predictions accurate?
yingxiao kong February 28, 2019 at 8:17 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-471145" title="Direct link to this comment">#</a>
Hi Jason,
Your experiment is very helpful for me to understand the difference between different methods, actually I have also done similar things. I always standardized the input data. I have compared the results between standardized and standardized targets. The plots shows that with standardized targets, the network seems to work better. However, here I have a question: suppose the standard deviation of my target is 300, then I think the MSE will be strongly decreased after you fixed the standard deviation to 1. So shall we multiply the original std to the MSE in order to get the MSE in the original target value space?
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> February 28, 2019 at 2:32 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-471186" title="Direct link to this comment">#</a>
You can invert the standardization, by adding the mean and multiplying by the stdev.
I also have an example here using the sklaern:
<a href="https://machinelearningmastery.com/machine-learning-data-transforms-for-time-series-forecasting/" rel="nofollow ugc">https://machinelearningmastery.com/machine-learning-data-transforms-for-time-series-forecasting/</a>
Beato March 11, 2019 at 3:51 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-473491" title="Direct link to this comment">#</a>
Hi Jason,
My data includes categorical and continued data. Could I transform the categorical data with 1,2,3…into standardized data and put them into the neural network models to make classification? Or do I need to transformr the categorical data with with one-hot coding(0,1)? I have been confused about it. Thanks
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> March 11, 2019 at 6:53 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-473523" title="Direct link to this comment">#</a>
Yes, perhaps try it and compare results?
Bart March 16, 2019 at 5:23 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-474594" title="Direct link to this comment">#</a>
Hi Jason, I have a specific Question regarding the normalization (min-max scaling) of the output value. Usually you are supposed to use normalization only on the training data set and then apply those stats to the validation and test set. Otherwise you would feed the model at training time certain information about the world it shouldn’t have access to. (The Elements of Statistical Learning: Data Mining, Inference, and Prediction p.247)
But for instance, my output value is a single percentage value ranging [0, 100%] and I am using the ReLU activation function in my output layer. I know for sure that in the “real world” regarding my problem statement, that I will get samples ranging form 60 – 100%. But my training sample size is to small and does not contain enough data points including all possible output values. So here comes my question: Should I stay with my initial statement (normalization only on training data set) or should I apply the maximum possible value of 100% to max()-value of the normalization step? The latter would contradict the literature. Best Regards Bart
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> March 16, 2019 at 8:02 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-474629" title="Direct link to this comment">#</a>
Correct.
I would recommend a sigmoid activation in the output.
I would then recommend interpreting the 0-1 scale as 60-100 prior to model evaluation.
Does that help?
Bart March 17, 2019 at 1:37 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-474728" title="Direct link to this comment">#</a>
I’m not quite sure what you mean by your second recommendation. How would I achieve that?
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> March 17, 2019 at 6:24 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-474757" title="Direct link to this comment">#</a>
You can project the scale of 0-1 to anything you want, such as 60-100.
First rescale to a number between 0 and 40 (value * 40) then add the min value (+ 60)
result = value * 40 + 60
Mike March 25, 2019 at 1:04 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-476186" title="Direct link to this comment">#</a>
Dear Jason, thank you for the great article.
I am wondering if there is any advantage using StadardScaler or MinMaxScaler over scaling manually. I could calculate the mean, std or min, max of my training data and apply them with the corresponding formula for standard or minmax scaling.
Would this approach produce the same results as the StadardScaler or MinMaxScaler or are the sklearn scalers special?
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> March 25, 2019 at 6:46 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-476218" title="Direct link to this comment">#</a>
Yes, it is reliable bug free code all wrapped up in a single class – making it harder to introduce new bugs.
Same results as manual, if you coded the manual scaling correctly.
Magnus May 9, 2019 at 8:32 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-484599" title="Direct link to this comment">#</a>
Dear Jason,
I have a few questions from section “Data normalization”. You mention that we should estimate the max and min values, and use that to normalize the training set to e.g. [-1,1]. But what if the max and min values are in the validation or test set? Then I might get values e.g. [-1.2, 1.3] in the validation set. Do you consider this to be incorrect or not?
Another approach is then to make sure that the min and max values for all parameters are contained in the training set. What are your thoughts on this? Is this the way to do it? Or should we use the max and min values for all data combined (training, validation and test sets) when normalizing the training set?
For the moment I use the MinMaxScaler and fit_transform on the training set and then apply that scaler on the validation and test set using transform. But I realise that some of my max values are in the validation set. I suppose this is also related to network saturation.
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> May 10, 2019 at 8:16 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-484663" title="Direct link to this comment">#</a>
Perhaps estimate the min/max using domain knowledge. If new data exceeded the limits, snap to known limits, or not – test and see how the model is impacted.
Regardless, the training set must be representative of the problem.
youssef May 30, 2019 at 9:33 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-487085" title="Direct link to this comment">#</a>
Hello Jason, I am a huge fan of your work! Thank you so much for your insightful tutorials. You are a life saver! I have a small question if i may:
I am trying to fit spectrograms in a cnn in order to do some classification tasks. Unfortunately each spectrogram is around (3000,300) array. Is there a way to reduce the dimensionality without losing so much information?
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> May 30, 2019 at 2:50 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-487103" title="Direct link to this comment">#</a>
Ouch, perhaps start with simple downsampling and see what effect that has?
Muktamani July 4, 2019 at 8:54 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-491581" title="Direct link to this comment">#</a>
Hi Jason,
It was always good and informative to go through your blogs and your interaction with comments by different people all across the globe.
I have question regarding the scaling techniques.
As you explained about scaling :
Case1:
- created scaler
scaler = StandardScaler()- fit scaler on training dataset
scaler.fit(trainy)- transform training dataset
trainy = scaler.transform(trainy)- transform test dataset
testy = scaler.transform(testy)in this case mean and standard deviation for all train and test remain same.
What i approached is:
case2
- created scaler
scaler_train = StandardScaler()- fit scaler on training dataset
scaler_train.fit(trainy)- transform training dataset
trainy = scaler_train.transform(trainy)# created scaler
scaler_test = StandardScaler()
- fit scaler on training dataset
scaler_test.fit(trainy)- transform test dataset
testy = scaler_test.transform(testy)Here the mean and standard deviation in train data and test data are different.so model may find the test data completely unknown and new .rather in first case where mean and standard deviation is same on train and test data that may leads to providing the known test data to model (known in term of same mean and standard deviation treatment).
Jason,can you guide me if my logics is good to go with case2 or shall i consider case1 .
or if logic is wrong you can also say that and explain.
(Also i applied Same for min-max scaling i.e normalization, if i choose this then)
Again thanks Jason for such a nice work !
Happy Learning !!
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> July 5, 2019 at 8:06 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-491669" title="Direct link to this comment">#</a>
I recommend fitting the scaler on the training dataset once, then apply it to transform the training dataset and test set.
If you fit the scaler using the test dataset, you will have data leakage and possibly an invalid estimate of model performance.
ICHaLiL July 6, 2019 at 12:13 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-491776" title="Direct link to this comment">#</a>
Hi Jason,
I’m working on sequence2sequence problem. Input’s max and min points are around 500-300, however output’s are 200-0. If I want to normalize them, should I use different scalers? For example:
scx = MinMaxScaler(feature_range = (0, 1))
scy = MinMaxScaler(feature_range = (0, 1))
trainx = scx.fit_transform(trainx)
trainy = scy.fit_transform(trainy)
or should I scale them with same scale like below?
sc = MinMaxScaler(feature_range = (0, 1))
trainx = sc.fit_transform(trainx)
trainy = sc.fit_transform(trainy)
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> July 6, 2019 at 8:40 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-491833" title="Direct link to this comment">#</a>
Yes, use a separate transform for inputs and outputs is a good idea. Otherwise have them all as separate columns in the same matrix and use one scaler, but the column order for transform/inverse_transform will always have to be consistent.
Brent July 12, 2019 at 6:55 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-492643" title="Direct link to this comment">#</a>
Hi Jason,
Confused about one aspect, I have a small NN with 8 independent variables and one dichotomous dependent variable. I have standardized the input variables (the output variable was left untouched). I have both trained and created the final model with the same standardized data. However, the question is, if I want to create a user interface to receive manual inputs, those will no longer be in the standardized format, so what is the best way to proceed?
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> July 13, 2019 at 6:53 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-492702" title="Direct link to this comment">#</a>
You must maintain the objects used to prepare the data, or the coefficients used by those objects (mean and stdev) so that you can prepare new data in an identically way to the way data was prepared during training.
Does that help
Brent July 15, 2019 at 10:58 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-492925" title="Direct link to this comment">#</a>
Thank you, that makes perfect sense.
cgv July 21, 2019 at 11:27 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-493673" title="Direct link to this comment">#</a>
Hi Jason,
I have built an ANN model and scaled my inputs and outputs before feeding to the network. I measure the performance of the model by r2_score. My output variable is height. My r2_score when the output variable is in metres is .98, but when my output variable is in centi-metres , my r2_score is .91. I have scaled my output too before feeding to the network, why is there a difference in r2_score even because the output variable is scaled before feeding to the network.
Thanks in advance
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> July 22, 2019 at 8:27 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-493717" title="Direct link to this comment">#</a>
Good question, this is why it is important to test different scaling approaches in order to discover what works best for a given dataset and model combination.
madhuri August 29, 2019 at 3:44 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-498877" title="Direct link to this comment">#</a>
Hi Jason,
I am working on sequence to data prediction problem wherein i am performing normalization on input and output both.
Once model is trained then to get the actual output in real-time, I have to perform the de-normalization and when I will perform the denorm then error will increase by the same factor I have used for normalization.
Lets consider, norm predicted output is 0.1 and error of the model is 0.01 .
denorm predicted output become 0.1*100 = 10 and after de-normalizing the error will be 0.01*100= 1
So, what will be solution to this eliminate this kind of problem in regression.
Thanks
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> August 29, 2019 at 6:16 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-498916" title="Direct link to this comment">#</a>
What problem exactly?
madhuri August 29, 2019 at 8:38 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-498975" title="Direct link to this comment">#</a>
The problem is after de-normalization of the output, the error difference between actual and predicted output is scaled up by the normalization factor (max-min) So, I want to know what can be done to make the error difference same for both de-normized as well as normalized output.
Thanks
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> August 30, 2019 at 6:18 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-499030" title="Direct link to this comment">#</a>
I don’t understand, sorry.
joshBorrison October 7, 2019 at 5:08 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-504531" title="Direct link to this comment">#</a>
Hi Jason,
Do I have to use only one normalization formula for all inputs?
For example: I have 5 inputs [inp1, inp2, inp3, inp4, inp5] where I can estimate max and min only for [inp1, inp2]. So can I use
y = (x – min) / (max – min)
for [inp1, inp2] and
y = x/(1+x)
for [inp3, inp4, inp5]?
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> October 8, 2019 at 7:53 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-504621" title="Direct link to this comment">#</a>
Yes, it is applied to each input separately – assuming they have different units.
shiva November 13, 2019 at 4:17 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-510336" title="Direct link to this comment">#</a>
Hi Jason
what if I scale the word vectors(glove) for exposing to LSTM?
would it affect the accuracy of results or it maintains the semantic relations of words?
Thank you a lot.
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> November 13, 2019 at 5:53 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-510377" title="Direct link to this comment">#</a>
I don’t think so. Try it and see?
Murilo Souza November 14, 2019 at 12:35 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-510516" title="Direct link to this comment">#</a>
Hello, i was trying to normalize/inverse transoformation in my data, but i got one error that i think its due to the resize i did in my input data. Here’s my code:
import numpy as np
import tensorflow as tf
from tensorflow import keras
import pandas as pd
import time as time
import matplotlib.pyplot as plt
import pydot
import csv as csv
import keras.backend as K
from sklearn.preprocessing import MinMaxScaler
# Downloading data
!wget <a href="https://raw.githubusercontent.com/sibyjackgrove/CNN-on-Wind-Power-Data/master/MISO_power_data_classification_labels.csv" rel="nofollow ugc">https://raw.githubusercontent.com/sibyjackgrove/CNN-on-Wind-Power-Data/master/MISO_power_data_classification_labels.csv</a>
!wget <a href="https://raw.githubusercontent.com/sibyjackgrove/CNN-on-Wind-Power-Data/master/MISO_power_data_input.csv" rel="nofollow ugc">https://raw.githubusercontent.com/sibyjackgrove/CNN-on-Wind-Power-Data/master/MISO_power_data_input.csv</a>
# Trying normalization
batch_size = 1
valid_size = max(1,np.int(0.2*batch_size))
df_input = pd.read_csv(‘./MISO_power_data_input.csv’,usecols =[‘Wind_MWh’,’Actual_Load_MWh’], chunksize=24*(batch_size+valid_size),nrows = 24*(batch_size+valid_size),iterator=True)
df_target = pd.read_csv(‘./MISO_power_data_classification_labels.csv’,usecols =[‘Mean Wind Power’,’Standard Deviation’,’WindShare’],chunksize =batch_size+valid_size,nrows = batch_size+valid_size, iterator=True)
for chunk, chunk2 in zip(df_input,df_target):
InputX = chunk.values
InputX = np.resize(InputX,(batch_size+valid_size,24,2,1))
print(InputX)
InputX.astype(‘float32’, copy=False)
InputY = chunk2.values
InputY.astype(‘float32’, copy=False)
print(InputY)
# create scaler
scaler = MinMaxScaler() # Define limits for normalize data
normalized_input = scaler.fit_transform(InputX) # Normalize input data
normalized_output = scaler.fit_transform(InputY) # Normalize output data
print(normalized_input)
print(normalized_output)
inverse_output = scaler.inverse_transform(normalized_output) # Inverse transformation of output data
print(inverse_output)
The error:
“ValueError: Found array with dim 4. MinMaxScaler expected <= 2."
Do you have any idea how can i fix this? I really didn't wish to change the resize command at the moment.
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> November 14, 2019 at 8:04 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-510565" title="Direct link to this comment">#</a>
Perhaps this will help:
<a href="https://machinelearningmastery.com/machine-learning-data-transforms-for-time-series-forecasting/" rel="nofollow ugc">https://machinelearningmastery.com/machine-learning-data-transforms-for-time-series-forecasting/</a>
Murilo Souza November 15, 2019 at 12:44 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-510656" title="Direct link to this comment">#</a>
Is there anyway i can do the inverse transform inside the model itself? Because, for example, my MSE reported at the end of each epoch would be in the “wrong” scale.
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> November 15, 2019 at 7:53 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-510694" title="Direct link to this comment">#</a>
Yes, you could wrap the model in a sklearn pipeline.
Or wrap the model in your own wrapper class.
Mariana Costa April 29, 2021 at 11:11 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-607456" title="Direct link to this comment">#</a>
Does it improve the net to do this?
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> April 30, 2021 at 6:06 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-607501" title="Direct link to this comment">#</a>
No, but it may be helpful when coding.
Jules Damji November 14, 2019 at 6:55 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-510545" title="Direct link to this comment">#</a>
Hey Jason,
I love this tutorial. I was wondering if I can get your permission to use this tutorial, convert all its experimentation and tracking using MLflow, and include it in my tutorials I teach at conferences.
It’s a fitting example of how you can use MLFlow to track different experiments and visually compare the outcomes.
All the credit will be given to you as the source and inspiration. You can see some of the examples here: <a href="https://github.com/dmatrix/spark-saturday/tree/master/tutorials/mlflow/src/python" rel="nofollow ugc">https://github.com/dmatrix/spark-saturday/tree/master/tutorials/mlflow/src/python</a>.
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> November 14, 2019 at 8:07 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-510572" title="Direct link to this comment">#</a>
Thanks!
No problem as long as you clearly cite and link to the post.
jules Damji November 14, 2019 at 2:45 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-510615" title="Direct link to this comment">#</a>
Thanks, I will certainly put the original link and plug your book too, along with your site and an excellent resource of tutorials and examples to learn from.
Cheers
Jules
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> November 15, 2019 at 7:42 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-510683" title="Direct link to this comment">#</a>
Thanks Jules.
Hanser November 28, 2019 at 8:13 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-512747" title="Direct link to this comment">#</a>
Amazing content Jason! I was wondering if it is possible to apply different scalers to different inputs given based on their original characteristics? I am asking you that because as you mentioned in the tutorial “Differences in the scales across input variables may increase the difficulty of the problem being modeled” Therefore, if I use standard scaler in one input and normal scaler in another it could be bad for gradient descend.
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> November 28, 2019 at 8:16 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-512749" title="Direct link to this comment">#</a>
Thanks!
Yes, perhaps try it and compare the results to using one type of scaling for all inputs.
Riyaz Pasha December 9, 2019 at 9:26 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-514349" title="Direct link to this comment">#</a>
Hi Jason,
I am solving the Regression problem and my accuracy after normalizing the target variable is 92% but I have the doubt about scaling the target variable. So can you elaborate about scaling the Target variable?
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> December 10, 2019 at 7:30 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-514403" title="Direct link to this comment">#</a>
You cannot calculate accuracy for regression. You must calculate error.
More details here:
<a href="https://machinelearningmastery.com/faq/single-faq/how-do-i-calculate-accuracy-for-regression" rel="nofollow ugc">https://machinelearningmastery.com/faq/single-faq/how-do-i-calculate-accuracy-for-regression</a>
FAIZ December 30, 2019 at 7:34 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-516662" title="Direct link to this comment">#</a>
Hi Jason Sir!
My data range is variable, e.g. -1500000, 0.0003456, 2387900,23,50,-45,-0.034, what should i do? i want to use MLP, 1D-CNN and SAE.
THANKS
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> December 31, 2019 at 7:31 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-516700" title="Direct link to this comment">#</a>
Perhaps try normalizing the data first?
Faiz January 2, 2020 at 12:28 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-516827" title="Direct link to this comment">#</a>
i tried different type of normalization but got data type errors, i used “MinMaxScaler ” and also (X-min(X))/ (max(X)-min(X)), but it can’t process. I want to know about the tf.compat.v1.keras.utils.normalize() command, what it actually do? thanks
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> January 2, 2020 at 6:42 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-516854" title="Direct link to this comment">#</a>
I don’t have a tutorial on that, perhaps check the source code?
BNB January 30, 2020 at 3:32 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-519745" title="Direct link to this comment">#</a>
Hi Jason
I have a question about the normalization of data. Samples from the population may be added to the dataset over time, and the attribute values for these new objects may then lie outside those you have seen so far. One possibility to handle new minimum and maximum values is to periodically renormalize the data after including the new values. Is there any normalization approach without renormalization?
Thanks,
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> January 30, 2020 at 6:56 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-519769" title="Direct link to this comment">#</a>
Yes, re-normalizing is one approach.
Clipping values to historical limits is another.
Perhaps try a few methods and see what makes sense for your project?
<a href='http://None.' rel='external nofollow ugc' class='url'>Tajik</a> February 19, 2020 at 1:15 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-522214" title="Direct link to this comment">#</a>
Hi Jason
Should we use “standard_deviation = sqrt( sum( (x – mean)**2 ) / count(x))” instead of “standard_deviation = sqrt( sum( (x – mean)^2 ) / count(x))”?
Does “^” sign represent square root in Python and is it fine not to subtract count (x) by 1 (in order to make it std of sample distribution, unless we have 100% observation of a population)?
Thank you
Best
Tajik
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> February 19, 2020 at 8:06 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-522269" title="Direct link to this comment">#</a>
^ means superscript (e.g. exponent) in latex and excel.
Peter February 22, 2020 at 6:18 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-522703" title="Direct link to this comment">#</a>
Hi Jason,
Very helpful post as always! I am slightly confused regarding the use of the scaler object though. In my scenario…
If I have a set of data that I split into a training set and validation set, I then scale the data as follows:
scaler = MinMaxScaler()
scaledTrain = scaler.fit_transform(trainingSet)
scaledValid = scaler.transform(validationSet)
I then use this data to train a deep learning model.
My question is, should I use the same scaler object, which was created using the training set, to scale my new, unseen test data before using that test set for predicting my model’s performance? Or should I create a new, separate scaler object using the test data?
Thanks in advance
Michael
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> February 22, 2020 at 6:40 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-522738" title="Direct link to this comment">#</a>
Yes.
Any data given to your model MUST be prepared in the same way. You are defining the expectations for the model based on how the training set looks.
Use the same scaler object – it knows – from being fit on the training dataset – how to transform data in the way your model expects.
Peter February 22, 2020 at 7:24 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-522746" title="Direct link to this comment">#</a>
Awesome! Thanks so much for the quick response and clearing that up for me.
Very best wishes.
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> February 23, 2020 at 7:19 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-522840" title="Direct link to this comment">#</a>
You’re welcome.
Mike March 10, 2020 at 2:21 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-525070" title="Direct link to this comment">#</a>
Hi Jason,
Thank you for the tutorial. A question about the conclusion: I find it surprising that standardization did not yield better performance compared to the model with unscaled inputs. Shouldn’t standardization provide better convergence properties when training neural networks? It’s also surprising that min-max scaling worked so well. If all of your inputs are positive (i.e between [0, 1] in this case), doesn’t that mean ALL of your weight updates at each step will be the same sign, which leads to inefficient learning?
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> March 11, 2020 at 5:16 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-525143" title="Direct link to this comment">#</a>
Not always. It really depends on the problem and the model.
Zeynep newby May 15, 2020 at 8:59 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-534865" title="Direct link to this comment">#</a>
Hi Jason,
I am an absolute beginner into neural networks and I appreciate your helpful website. In the lecture, I learned that when normalizing a training set, one should use the same mean and standard deviation from training for the test set. But I see in your codes that you’re normalizing training and test sets individually. Is that for a specific reason?
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> May 16, 2020 at 6:09 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-534911" title="Direct link to this comment">#</a>
The example correctly fits the transform on the training set then applies the transform to train and test sets.
If we don’t do it this way, it will result in data leakage and in turn an optimistic estimate of model performance.
Zeynep newby May 15, 2020 at 9:04 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-534866" title="Direct link to this comment">#</a>
Hi again,
since I saw another comment having the same question like me, I noticed that you acutally have done exactly the same thing as I expected. Since I am not familiar with the syntax yet, I got it wrong. Thanks very much!
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> May 16, 2020 at 6:10 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-534912" title="Direct link to this comment">#</a>
No problem!
Ask questions anyway, even if you’re not sure. The tutorials are really just the starting point in a conversation.
Isaac May 17, 2020 at 3:25 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-535093" title="Direct link to this comment">#</a>
Hai Jaison, I am a beginner in ML and I am having an issue with normalizing..
I am developing a multivariate regression model with three inputs and three outputs.
The three inputs are in the range of [700 1500] , [700-1500] and [700 1500]
The three outputs are in the range of [-0.5 0.5] , [-0.5 0.5] and [700 1500]
I have normalized everything in the range of [-1 1].
The loss at the end of 1000 epoch is in the order of 1e-4, but still, I am not satisfied with the fit of the model. Since the loss function is based on normalized target variables and normalized prediction, its value id very less from the first epoch itself.
Is there a way to bring the cost further down?
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> May 18, 2020 at 6:08 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-535157" title="Direct link to this comment">#</a>
Yes, the suggestions here will help you improve your model:
<a href="https://machinelearningmastery.com/start-here/#better" rel="nofollow ugc">https://machinelearningmastery.com/start-here/#better</a>
Victor Yu June 9, 2020 at 11:43 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-538831" title="Direct link to this comment">#</a>
Hi Jason,
I wonder how you apply scaling to batch data? Say we batch load from tfrecords, for each batch we fit a scaler? If so, then the final scaler is on the last batch, which will be used for test data? Also in batch data, if the batch is small, then it seems the scaler is volatile, especially for MaxMin. Would like to hear your thoughts since in a lot of practices it’s nearly impossible to load huge data into driver to do scaling.
Thanks!
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> June 10, 2020 at 6:16 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-538889" title="Direct link to this comment">#</a>
Scaling is fit on the training set, then applied to all data, e.g. train, test, val.
Victor Yu June 10, 2020 at 12:10 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-538935" title="Direct link to this comment">#</a>
The entire training set? What if the entire training set is too big to load in the memory? Even doing batch training, you still do scaling on the entire training set first then do batch training? That seems pretty inefficient
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> June 10, 2020 at 1:25 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-538949" title="Direct link to this comment">#</a>
You can use a generator to load the data step by step, only keep in memory what you can/need.
More suggestions here:
<a href="https://machinelearningmastery.com/faq/single-faq/how-to-i-work-with-a-very-large-dataset" rel="nofollow ugc">https://machinelearningmastery.com/faq/single-faq/how-to-i-work-with-a-very-large-dataset</a>
Victor Yu June 11, 2020 at 10:53 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-539080" title="Direct link to this comment">#</a>
Yes, that’s my question. When doing batch training, do you fit (or re-fit) a scaler on each batch? If so, it seems the final scaler that will be used for scoring is fit on the final batch. Do you see any issue with that especially when batch is small? Thanks
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> June 11, 2020 at 1:31 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-539088" title="Direct link to this comment">#</a>
You could, this is what batch norm does.
Or you can estimate the coefficients used in scaling up front from a sample of training data. Or some other way you prefer.
Najeh June 19, 2020 at 6:32 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-540105" title="Direct link to this comment">#</a>
Hi Jason,
In deep learning as machine learning, data should be transformed into a tabular format? if yes or no why?
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> June 19, 2020 at 1:08 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-540131" title="Direct link to this comment">#</a>
Input data must be vectors or matrices of numbers, this covers tabular data, images, audio, text, and so on.
Julie June 24, 2020 at 10:29 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-540919" title="Direct link to this comment">#</a>
Hello Jason,
I used your method (i did standardized my outputs and normalized my inputs with MinMaxScaler()) but i keep having the same issue : when i train my neural network with 3200 and validate with 800 everything alright, i have R2 = 99% but when i increase the training / validation set, R2 decreases which is weird, it should be even higher ? Do you think it has something to do with the scaling of the data ?
Thank you !
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> June 25, 2020 at 6:17 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-540974" title="Direct link to this comment">#</a>
It might be interesting to perform a sensitivity analysis on model performance vs train or test set size to understand the relationship.
Munaf February 23, 2021 at 4:30 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-598424" title="Direct link to this comment">#</a>
Sir how can I normalize real-time data and scale them between -150 to 150? The data are coming every 5 min interval.
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> February 23, 2021 at 6:25 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-598446" title="Direct link to this comment">#</a>
Perhaps use the minmaxscaler if you’re having trouble:
<a href="https://machinelearningmastery.com/standardscaler-and-minmaxscaler-transforms-in-python/" rel="nofollow ugc">https://machinelearningmastery.com/standardscaler-and-minmaxscaler-transforms-in-python/</a>
David July 4, 2020 at 1:29 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-542748" title="Direct link to this comment">#</a>
Hi sir,
I have a NN with 6 input variables and one output , I employed minmaxscaler for inputs as well as outputs . My approach was applying the scaler to my whole dataset then splitting it into training and testing dataset, as I dont know the know-hows so is my approach wrong .
Currently the problem I am facing is my actual outputs are positive values but after unscaling the NN predictions I am getting negative values. I tried changing the feature range, still NN predicted negative values , so how can i solve this?
Y1=Y1.reshape(-1, 1)
Y2=Y2.reshape(-1, 1)
TY1=TY1.reshape(-1, 1)
TY2=TY2.reshape(-1, 1)
scaler1 = MinMaxScaler(feature_range=(0, 1))
rescaledX= scaler1.fit_transform(X)
rescaledTX=scaler1.fit_transform(TX)
scaler2 = MinMaxScaler(feature_range=(0, 2))
rescaledY1 = scaler2.fit_transform(Y1)
scaler3 = MinMaxScaler(feature_range=(0, 2))
rescaledY2 = scaler3.fit_transform(Y2)
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> July 5, 2020 at 6:54 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-542829" title="Direct link to this comment">#</a>
First, perhaps confirm that there is no bug in your code.
Second, it is possible for the model to predict values that get mapped to a value out of bounds. You could use a n if-statement to snap them to the required range or use a model that forces predictions to the required range.
TAMER A. FARRAG July 30, 2020 at 9:42 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-546517" title="Direct link to this comment">#</a>
Thanks a lot,
My question is:
I finish training my model and I use normalized data for inputs and outputs.
my problem now is when I need to use this model I do the following:
1- I load the model
2- normalize the inputs
3- use model to get the outputs (predicted data)
how to denormalized the output of the model ??? I don’t have the MinMaxScaler for the output ??
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> July 30, 2020 at 1:45 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-546534" title="Direct link to this comment">#</a>
You can call inverse_transform() on the scaler object for the predictions to get the data back to the original scale.
TAMER A. FARRAG July 30, 2020 at 7:21 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-546563" title="Direct link to this comment">#</a>
Thanks for fast replay,
I think my question is not clear to you.
imagine than I finish the training phase and save the trained model named “model1”.
I send the “model1” file to a friend and he tries to use it, he will normalize the inputs and get the outputs. In this case, he doesn’t have the scaler object to recover the original values using inverse_transform().
my problem is similar to: <a href="https://stackoverflow.com/questions/37595891/how-to-recover-original-values-after-a-model-predict-in-keras" rel="nofollow ugc">https://stackoverflow.com/questions/37595891/how-to-recover-original-values-after-a-model-predict-in-keras</a>
but the answer don’t use the scaler object. It depends on manual normalization and normalization process
Thanks for your time
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> July 31, 2020 at 6:15 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-546618" title="Direct link to this comment">#</a>
Save the scaler object as well:
<a href="https://machinelearningmastery.com/how-to-save-and-load-models-and-data-preparation-in-scikit-learn-for-later-use/" rel="nofollow ugc">https://machinelearningmastery.com/how-to-save-and-load-models-and-data-preparation-in-scikit-learn-for-later-use/</a>
You are developing a “modeling pipeline”, not just a predictive model.
Mel August 15, 2020 at 9:09 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-549367" title="Direct link to this comment">#</a>
Hi Jason,
Do you know of any textbooks or journal articles that address the input scaling issue as you’ve described it here, in addition to the Bishop textbook? I’m struggling so far in vain to find discussions of this type of scaling, when different raw input variables have much different ranges. Instead I’m finding plenty of mentions in tutorials and blog posts (of which yours is one of the clearest), and papers describing the problems of scale (size) variance in neural networks designed for image recognition.
Thanks!
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> August 15, 2020 at 1:26 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-549393" title="Direct link to this comment">#</a>
Not really, practical issues are not often discussed in textbooks/papers.
Maybe “neural smithing”? Maybe Bishops later book?
Munisha Bansal September 29, 2020 at 5:44 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-565174" title="Direct link to this comment">#</a>
Hi Jason,
Thank you very much for the article. I wanted to understand the following scenario
I have mix of categorical and numerical inputs. I can normalize/standardize the numerical inputs and the output numerical variable.
But in the categorical variables I have high number of categories ~3000. So I use label encoder (not one hot coding) and then I use embedding layers. How can I achieve scaling in this case.
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> September 30, 2020 at 6:24 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-565223" title="Direct link to this comment">#</a>
You can separate the columns and scale them independently, then aggregate the results.
Hamed October 22, 2020 at 1:58 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-569739" title="Direct link to this comment">#</a>
Hi Jason,
I really enjoyed reading your article. My CNN regression network has binary image as input which the background is black, and foreground is white. The ground truth associated with each input is an image with color range from 0 to 255 which is normalized between 0 and 1.
The network can almost detect edges and background but in foreground all the predicted values are almost same. Do you have any idea what is the solution?
I appreciate in advance.
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> October 22, 2020 at 6:45 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-569783" title="Direct link to this comment">#</a>
Thanks.
Perhaps these tips will help you improve the performance of your model:
<a href="https://machinelearningmastery.com/start-here/#better" rel="nofollow ugc">https://machinelearningmastery.com/start-here/#better</a>
walid November 5, 2020 at 11:50 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-573097" title="Direct link to this comment">#</a>
Hi jason, how are you?
i have data with input X (matrix with real values) and output y (matrix real values).
i tried to normalize X and y :
scaler1 = Normalizer()
X = scaler1.fit_transform(X)
scaler2 = Normalizer()
y = scaler2.fit_transform(y)
i get a good result with the transform normalizer as shown by: <a href="https://ibb.co/bQYCkvK" rel="nofollow ugc">https://ibb.co/bQYCkvK</a>
at the end i tried to get the predicted values: yhat = model.predict(X_test)
the problem here yhat is not the original data, it’s a transformed data and there is no inverse for normalizer.
I tried to use the minmaxScalar an order to do the inverse operation (invyhat = scaler2.inverse_transform(yhat)) but i get a big numbers compared to the y_test values that i want.
I tried to normalize just X, i get a worst result compared to the first one.
could you please help me.
example of X values : 1006.808362,13.335140,104.536458 …..
289.197205,257.489613,106.245104,566.941857…..
.
example of y values: 0.50000, 250.0000
0.879200,436.000000
.
.
this is my code:
X = dataset[:,0:20]
y = dataset[:,20:22]
scaler1 = Normalizer()
X = scaler1.fit_transform(X)
scaler2 = Normalizer()
y = scaler2.fit_transform(y)
X_train = X[90000:,:]
X_test= X[:90000,:]
y_train =y[90000:,:]
y_test=y[:90000,:]
print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)
- define the keras model
model = Sequential()- input layer
model.add(Dense(20, input_dim=20,activation=’relu’,kernel_initializer=’normal’))- hidden layer
model.add(Dense(7272,activation=’relu’,kernel_initializer=’normal’))model.add(Dropout(0.8))
- output layer
model.add(Dense(2, activation=’linear’))opt =Adadelta(lr=0.01)
- compile the keras model
model.compile(loss=’mean_squared_error’, optimizer=opt, metrics=[‘mse’])- fit the keras model on the dataset
history=model.fit(X_train, y_train, validation_data=(X_test, y_test),epochs=20,verbose=0)- evaluate the model
_, train_mse = model.evaluate(X_train, y_train, verbose=0)_, test_mse = model.evaluate(X_test, y_test, verbose=0)
print(‘Train: %.3f, Test: %.3f’ % (train_mse, test_mse))
yhat = model.predict(X_test)
- plot loss during training
pyplot.title(‘Loss / Mean Squared Error’)pyplot.plot(history.history[‘loss’], label=’train’)
pyplot.plot(history.history[‘val_loss’], label=’test’)
pyplot.legend()
pyplot.show()
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> November 6, 2020 at 5:57 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-573152" title="Direct link to this comment">#</a>
Sorry to hear that you’re having trouble, perhaps some of these tips will help:
<a href="https://machinelearningmastery.com/faq/single-faq/can-you-read-review-or-debug-my-code" rel="nofollow ugc">https://machinelearningmastery.com/faq/single-faq/can-you-read-review-or-debug-my-code</a>
Carlos November 17, 2020 at 9:18 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-575881" title="Direct link to this comment">#</a>
Hi Jason, first thanks for the wonderful article. I have a little doubt. By normalizing my data and then dividing it into training and testing, all samples will be normalized. But in the case of a real application, where I have an input given by the user, do I need to put it together with all the data and normalize it so that it has the same pattern as the other data? What would be the best alternative?
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> November 17, 2020 at 12:56 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-575928" title="Direct link to this comment">#</a>
Good question.
No, save the scaler object or coefficients used for scaling along with the model and use them to prepare new data in the future. More here:
<a href="https://machinelearningmastery.com/how-to-save-and-load-models-and-data-preparation-in-scikit-learn-for-later-use/" rel="nofollow ugc">https://machinelearningmastery.com/how-to-save-and-load-models-and-data-preparation-in-scikit-learn-for-later-use/</a>
<a href='http://www.iqvia.com' rel='external nofollow ugc' class='url'>Chris</a> December 3, 2020 at 2:41 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-580301" title="Direct link to this comment">#</a>
Hi Jason, what is the best way to scale NANs when you need the model to generate them? I am creating a synthetic dataset where NANs are critical part. In one case we have people with no corresponding values for a field (truly missing) and in another case we have missing values but want to replicate the fact that values are missing. I tried filling the missing values with the negative sys.max value, but the model tends to spread values between the real data negative limit and the max limit, instead of treating the max value as an outlier. In another case, it seems to ignore that value and always generates values with the real data range, resulting in no generated NANs. I enjoyed your book and look forward to your response.
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> December 3, 2020 at 8:21 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-580371" title="Direct link to this comment">#</a>
You cannot scale a NaN, you must replace it with a value, called imputation.
If you want to mark missing values with a special value, mark and then scale, or remove the rows from the scale process, and impute after scale. The latter sounds better to me.
Luke Mao January 6, 2021 at 4:13 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-591809" title="Direct link to this comment">#</a>
Thanks Jason for the blog post.
One question:
is it necessary to apply feature scaling for linear regression models as well as MLP’s?
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> January 6, 2021 at 6:32 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-591844" title="Direct link to this comment">#</a>
Yes, it is a good idea to scale input data prior to modeling for models that use a weighted sum of input, like neural nets and regression models.
Lu Mao January 6, 2021 at 9:00 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-591999" title="Direct link to this comment">#</a>
Thanks Jason. May I ask a follow up question, what is your view on if it is wrong to only scale the input, not scale the output?.
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> January 7, 2021 at 6:16 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-592055" title="Direct link to this comment">#</a>
It depends on the data and model.
Do whatever results in the best performance for your prediction problem.
Nisarg Patel January 25, 2021 at 1:23 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-594313" title="Direct link to this comment">#</a>
sir, i have a 1 problem
When normalizing a dataset, the resulting data will have a minimum value of 0 and a
maximum value of 1. However, the dataset we work with in data mining is typically a
sample of a population. Therefore, the minimum and maximum for each of the attributes
in the population are unknown.
Samples from the population may be added to the dataset over time, and the attribute
values for these new objects may then lie outside those you have seen so far. One
possibility to handle new minimum and maximum values is to periodically renormalize
the data after including the new values. Your task is to think of a normalization scheme
that does not require you to renormalize all of the data. Your normalization approach has
to fulfill all of the following requirements:
– all values (old and new) have to lie in the range between 0 and 1
– no transformation or renormalization of the old values is allowed
Describe your normalization approach.
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> January 25, 2021 at 1:31 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-594316" title="Direct link to this comment">#</a>
Perhaps you can use domain knowledge to estimate a broader min and max range prior to scaling.
Perhaps you can clip values to a pre-defined range prior to scaling.
J March 10, 2021 at 5:43 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-600398" title="Direct link to this comment">#</a>
Hi Jason!
Thank you so much for this great post 🙂
I have one question I hope you could help with:
Why do we need to conduct 30 model runs in particular? I do understand the idea, but i mean why 30 exactly?
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> March 10, 2021 at 6:28 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-600404" title="Direct link to this comment">#</a>
30 is often used to create a large enough sample that we can use statistical methods and that the estimated stats like mean and stev are not too noisy.
Maha March 16, 2021 at 7:14 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-601009" title="Direct link to this comment">#</a>
Thanks Jason
I have some confused questions
If the scaling to input data done on the all data set or done to each sample of the data set seperately?
the scalling is done after dividing data to training and test, yes?
If I done normalizations manual to inputs and output, so I should save the max and min values to normalization inputs and denormalization outputs in future prediction?
If I have the outputs containing two differerent range of variables , is same normalization is effective or I should do further things,for example two different normalization?
Thanks in advance
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> March 16, 2021 at 7:59 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-601015" title="Direct link to this comment">#</a>
Data scaling, and all data pre-processing should be fit on the training set and applied to the training set, validation set and test sets in order to avoid data leakage. You can learn more about this here:
<a href="https://machinelearningmastery.com/data-preparation-without-data-leakage/" rel="nofollow ugc">https://machinelearningmastery.com/data-preparation-without-data-leakage/</a>
Maha March 16, 2021 at 9:20 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-601019" title="Direct link to this comment">#</a>
Many thanks for that, I hd read your mentioned article and understood to avoid data leakage. as I should split data first and got the scale from the trainning set .
but i had another Q:
my data set , for example contain four vectors [ x1 x2 x3 x4 ], where for example each had 100 values ., x1= [value1……………………..value100], x2=[value1…….value100],……
then my traing data may be 400Xnumberof sumples.
but the range of values to these is varying , x1 , x2 and x3 had values in range [ -04], forexample [ – 4.7338e-04 to – 1.33-04 ] and the x4 has values in range of [-02], forexample[ -1.33e-02 to 3.66e-02 ]
the same the output has values some in range [-0.0698 to 0.06211] and other in range [-3.1556 to 3.15556]
sorry for long discription , but , what suitable scaling you recommend me to do, if normalization(max, min ) to input and outs can be suitable , or I had to do any other prepation
many thanks to you
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> March 17, 2021 at 5:55 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-601133" title="Direct link to this comment">#</a>
I recommend starting with normalization. Perhaps try standardization if the variables look like they have a gaussian probability distribution.
Maha April 3, 2021 at 3:27 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-603221" title="Direct link to this comment">#</a>
If normalization and standarization is done of the whole data or each row of the samples , for example , in standardization , we got the mean of the whole data set and substract from each element in data set , or we treat each row in the data set separately and got its mean ,..?
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> April 3, 2021 at 5:35 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-603243" title="Direct link to this comment">#</a>
No, data preparation is typically fit on the training set and applied to the train and test dataset.
Carlos May 2, 2021 at 11:12 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-607743" title="Direct link to this comment">#</a>
Hi Jason,
I have a question.. I hope you have time to answer it…
If I scale/normalize the input data… The output label (calculated) will be generated “scalated/normalized” also..correct…
and in order to calculate the output error the expected label should be scalated also..
Correct??
In other words.. I should scalate both..data and labels??
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> May 3, 2021 at 4:55 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-607817" title="Direct link to this comment">#</a>
Scaling input is a good idea, depending on the data and choice of model.
If the target is numeric (e.g. regression), then scaling the target is a good idea, depending on the data and choice of model.
If the target was sealed, then the scaling must be inverted on the prediction and the test data before calculating an error metic.
Israel May 6, 2021 at 10:03 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-608235" title="Direct link to this comment">#</a>
Hi Jason,
I’m new to deep learning. I tried to implement a CNN regression model with multiple impute image chips of 31 channels(Raster image/TIFF format), and a numeric target variable. But the result I got is quite weird cos its giving me 100% accuracy (r2_score). I also noticed that during training, the loss/val loss output values were all zeros and the training was pretty fast considering feeding over 5000 images into the network. so I feel the network isn’t training anything passé.
I want to ask if this could be as a result of data scaling? My image chips pixel values are in decimals (float) between 0 and 1 (all the image chips are less than 1), while my target variable are a continuous variable between 0 and 160 (integer).
Do you think i need to perform some sort of normalization or standardization of my data?
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> May 7, 2021 at 6:26 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-608282" title="Direct link to this comment">#</a>
Perhaps try scaling the data and see if it makes a difference.
<a href='https://acehl.org/' rel='external nofollow ugc' class='url'>JG</a> May 9, 2021 at 6:00 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-608515" title="Direct link to this comment">#</a>
Hi Jason,
Great Tutorial! Thank you very much.
very clear explanation of scaling inputs and output necessity !
I am introducing your tutorial to a friend of mine who is very interested in following you.
regards
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> May 10, 2021 at 6:18 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-608599" title="Direct link to this comment">#</a>
Thanks!
Phil July 16, 2021 at 4:00 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-616693" title="Direct link to this comment">#</a>
Hi Jason,
Im currently training an MLP and I have 9 metric features and 3 binary coded to 0/1.
So I have decided only to standardize the 9 metric feautes and leave the binary features untouched.
Is this approch okay or should standardize the binary features as well – so they have an mean neat to zero and sd of 1
Cheers
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> July 16, 2021 at 5:29 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-616721" title="Direct link to this comment">#</a>
It sounds strange to me that you would standardize binary features. Often they would be excluded from any scaling operation.
voloddia August 2, 2021 at 1:01 pm <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-619500" title="Direct link to this comment">#</a>
“You must ensure that the scale of your output variable matches the scale of the activation function (transfer function) on the output layer of your network.”
I don’t understand this point.
First, the output layer often has no activation function, or in other words, identity activation function which has arbitrary scale.
Second, normalization and standardization are only linear transformations.
Therefore, is it true that normalization/standardization of output is almost always unnecessary? If not, why?
<a href='http://MachineLearningMastery.com' rel='external nofollow ugc' class='url'>Jason Brownlee</a> August 3, 2021 at 4:49 am <a href="https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-performance-with-data-scaling/#comment-619602" title="Direct link to this comment">#</a>
This was critical in the olden days of sigmoid and tanh. These days, normalizing or standardizing is sufficient.
It’s critical because large inputs cause large weights which leads to an unstable network, in general.