{"id":3887,"date":"2020-09-20T19:00:13","date_gmt":"2020-09-20T19:00:13","guid":{"rendered":"https:\/\/www.aiproblog.com\/index.php\/2020\/09\/20\/multi-core-machine-learning-in-python-with-scikit-learn\/"},"modified":"2020-09-20T19:00:13","modified_gmt":"2020-09-20T19:00:13","slug":"multi-core-machine-learning-in-python-with-scikit-learn","status":"publish","type":"post","link":"https:\/\/www.aiproblog.com\/index.php\/2020\/09\/20\/multi-core-machine-learning-in-python-with-scikit-learn\/","title":{"rendered":"Multi-Core Machine Learning in Python With Scikit-Learn"},"content":{"rendered":"<p>Author: Jason Brownlee<\/p>\n<div>\n<p>Many computationally expensive tasks for machine learning can be made parallel by splitting the work across <strong>multiple CPU cores<\/strong>, referred to as multi-core processing.<\/p>\n<p>Common machine learning tasks that can be made parallel include training models like ensembles of decision trees, evaluating models using resampling procedures like k-fold cross-validation, and tuning model hyperparameters, such as grid and random search.<\/p>\n<p>Using multiple cores for common machine learning tasks can dramatically decrease the execution time as a factor of the number of cores available on your system. A common laptop and desktop computer may have 2, 4, or 8 cores. Larger server systems may have 32, 64, or more cores available, allowing machine learning tasks that take hours to be completed in minutes.<\/p>\n<p>In this tutorial, you will discover how to configure scikit-learn for multi-core machine learning.<\/p>\n<p>After completing this tutorial, you will know:<\/p>\n<ul>\n<li>How to train machine learning models using multiple cores.<\/li>\n<li>How to make the evaluation of machine learning models parallel.<\/li>\n<li>How to use multiple cores to tune machine learning model hyperparameters.<\/li>\n<\/ul>\n<p>Let&rsquo;s get started.<\/p>\n<div id=\"attachment_10958\" style=\"width: 810px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" aria-describedby=\"caption-attachment-10958\" loading=\"lazy\" class=\"size-full wp-image-10958\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2020\/09\/Multi-Core-Machine-Learning-in-Python-With-Scikit-Learn.jpg\" alt=\"Multi-Core Machine Learning in Python With Scikit-Learn\" width=\"800\" height=\"450\" srcset=\"http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/09\/Multi-Core-Machine-Learning-in-Python-With-Scikit-Learn.jpg 800w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/09\/Multi-Core-Machine-Learning-in-Python-With-Scikit-Learn-300x169.jpg 300w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/09\/Multi-Core-Machine-Learning-in-Python-With-Scikit-Learn-768x432.jpg 768w\" sizes=\"(max-width: 800px) 100vw, 800px\"><\/p>\n<p id=\"caption-attachment-10958\" class=\"wp-caption-text\">Multi-Core Machine Learning in Python With Scikit-Learn<br \/>Photo by <a href=\"https:\/\/www.flickr.com\/photos\/erix-pix\/16030189175\/\">ER Bauer<\/a>, some rights reserved.<\/p>\n<\/div>\n<h2>Tutorial Overview<\/h2>\n<p>This tutorial is divided into five parts; they are:<\/p>\n<ol>\n<li>Multi-Core Scikit-Learn<\/li>\n<li>Multi-Core Model Training<\/li>\n<li>Multi-Core Model Evaluation<\/li>\n<li>Multi-Core Hyperparameter Tuning<\/li>\n<li>Recommendations<\/li>\n<\/ol>\n<h2>Multi-Core Scikit-Learn<\/h2>\n<p>Machine learning can be computationally expensive.<\/p>\n<p>There are three main centers of this computational cost; they are:<\/p>\n<ul>\n<li>Training machine learning models.<\/li>\n<li>Evaluating machine learning models.<\/li>\n<li>Hyperparameter tuning machine learning models.<\/li>\n<\/ul>\n<p>Worse, these concerns compound.<\/p>\n<p>For example, evaluating machine learning models using a resampling technique like <a href=\"https:\/\/machinelearningmastery.com\/cross-validation-for-imbalanced-classification\/\">k-fold cross-validation<\/a> requires that the training process is repeated multiple times.<\/p>\n<ul>\n<li>Evaluation Requires Repeated Training<\/li>\n<\/ul>\n<p>Tuning model hyperparameters compounds this further as it requires the evaluation procedure repeated for each combination of hyperparameters tested.<\/p>\n<ul>\n<li>Tuning Requires Repeated Evaluation<\/li>\n<\/ul>\n<p>Most, if not all, modern computers have multi-core CPUs. This includes your workstation, your laptop, as well as larger servers.<\/p>\n<p>You can configure your machine learning models to harness multiple cores of your computer, dramatically speeding up computationally expensive operations.<\/p>\n<p>The scikit-learn Python machine learning library provides this capability via the <a href=\"https:\/\/scikit-learn.org\/stable\/glossary.html#term-n-jobs\">n_jobs argument<\/a> on key machine learning tasks, such as model training, model evaluation, and hyperparameter tuning.<\/p>\n<p>This configuration argument allows you to specify the number of cores to use for the task. The default is None, which will use a single core. You can also specify a number of cores as an integer, such as 1 or 2. Finally, you can specify -1, in which case the task will use all of the cores available on your system.<\/p>\n<ul>\n<li><strong>n_jobs<\/strong>: Specify the number of cores to use for key machine learning tasks.<\/li>\n<\/ul>\n<p>Common values are:<\/p>\n<ul>\n<li><strong>n_jobs=None<\/strong>: Use a single core or the default configured by your backend library.<\/li>\n<li><strong>n_jobs=4<\/strong>: Use the specified number of cores, in this case 4.<\/li>\n<li><strong>n_jobs=-1<\/strong>: Use all available cores.<\/li>\n<\/ul>\n<p><strong>What is a core?<\/strong><\/p>\n<p>A CPU may have <a href=\"https:\/\/en.wikipedia.org\/wiki\/Multi-core_processor\">multiple physical CPU cores<\/a>, which is essentially like having multiple CPUs. Each core may also have <a href=\"https:\/\/en.wikipedia.org\/wiki\/Hyper-threading\">hyper-threading<\/a>, a technology that under many circumstances allows you to double the number of cores.<\/p>\n<p>For example, my workstation has four physical cores, which are doubled to eight cores due to hyper-threading. Therefore, I can experiment with 1-8 cores or specify -1 to use all cores on my workstation.<\/p>\n<p>Now that we are familiar with the scikit-learn library&rsquo;s capability to support multi-core parallel processing for machine learning, let&rsquo;s work through some examples.<\/p>\n<p>You will get different timings for all of the examples in this tutorial; share your results in the comments. You may also need to change the number of cores to match the number of cores on your system.<\/p>\n<p><strong>Note<\/strong>: Yes, I am aware of the <a href=\"https:\/\/docs.python.org\/3\/library\/timeit.html\">timeit<\/a> API, but chose against it for this tutorial. We are not profiling the code examples per se; instead, I want you to focus on how and when to use the multi-core capabilities of scikit-learn and that they offer real benefits. I wanted the code examples to be clean and simple to read, even for beginners. I set it as an extension to update all examples to use the timeit API and get more accurate timings. Share your results in the comments.<\/p>\n<h2>Multi-Core Model Training<\/h2>\n<p>Many machine learning algorithms support multi-core training via an n_jobs argument when the model is defined.<\/p>\n<p>This affects not just the training of the model, but also the use of the model when making predictions.<\/p>\n<p>A popular example is the ensemble of decision trees, such as bagged decision trees, random forest, and gradient boosting.<\/p>\n<p>In this section we will explore accelerating the training of a <a href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.ensemble.RandomForestClassifier.html\">RandomForestClassifier<\/a> model using multiple cores. We will use a synthetic classification task for our experiments.<\/p>\n<p>In this case, we will define a random forest model with 500 trees and use a single core to train the model.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# define the model\r\nmodel = RandomForestClassifier(n_estimators=500, n_jobs=1)<\/pre>\n<p>We can record the time before and after the call to the <em>train()<\/em> function using the <em>time()<\/em> function. We can then subtract the start time from the end time and report the execution time in the number of seconds.<\/p>\n<p>The complete example of evaluating the execution time of training a random forest model with a single core is listed below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\"># example of timing the training of a random forest model on one core\r\nfrom time import time\r\nfrom sklearn.datasets import make_classification\r\nfrom sklearn.ensemble import RandomForestClassifier\r\n# define dataset\r\nX, y = make_classification(n_samples=10000, n_features=20, n_informative=15, n_redundant=5, random_state=3)\r\n# define the model\r\nmodel = RandomForestClassifier(n_estimators=500, n_jobs=1)\r\n# record current time\r\nstart = time()\r\n# fit the model\r\nmodel.fit(X, y)\r\n# record current time\r\nend = time()\r\n# report execution time\r\nresult = end - start\r\nprint('%.3f seconds' % result)<\/pre>\n<p>Running the example reports the time taken to train the model with a single core.<\/p>\n<p>In this case, we can see that it takes about 10 seconds.<\/p>\n<p>How long does it take on your system? Share your results in the comments below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">10.702 seconds<\/pre>\n<p>We can now change the example to use all of the physical cores on the system, in this case, four.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# define the model\r\nmodel = RandomForestClassifier(n_estimators=500, n_jobs=4)<\/pre>\n<p>The complete example of multi-core training of the model with four cores is listed below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\"># example of timing the training of a random forest model on 4 cores\r\nfrom time import time\r\nfrom sklearn.datasets import make_classification\r\nfrom sklearn.ensemble import RandomForestClassifier\r\n# define dataset\r\nX, y = make_classification(n_samples=10000, n_features=20, n_informative=15, n_redundant=5, random_state=3)\r\n# define the model\r\nmodel = RandomForestClassifier(n_estimators=500, n_jobs=4)\r\n# record current time\r\nstart = time()\r\n# fit the model\r\nmodel.fit(X, y)\r\n# record current time\r\nend = time()\r\n# report execution time\r\nresult = end - start\r\nprint('%.3f seconds' % result)<\/pre>\n<p>Running the example reports the time taken to train the model with a single core.<\/p>\n<p>In this case, we can see that the speed of execution more than halved to about 3.151 seconds.<\/p>\n<p><strong>How long does it take on your system?<\/strong> Share your results in the comments below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">3.151 seconds<\/pre>\n<p>We can now change the number of cores to eight to account for the hyper-threading supported by the four physical cores.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# define the model\r\nmodel = RandomForestClassifier(n_estimators=500, n_jobs=8)<\/pre>\n<p>We can achieve the same effect by setting <em>n_jobs<\/em> to -1 to automatically use all cores; for example:<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# define the model\r\nmodel = RandomForestClassifier(n_estimators=500, n_jobs=-1)<\/pre>\n<p>We will stick to manually specifying the number of cores for now.<\/p>\n<p>The complete example of multi-core training of the model with eight cores is listed below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\"># example of timing the training of a random forest model on 8 cores\r\nfrom time import time\r\nfrom sklearn.datasets import make_classification\r\nfrom sklearn.ensemble import RandomForestClassifier\r\n# define dataset\r\nX, y = make_classification(n_samples=10000, n_features=20, n_informative=15, n_redundant=5, random_state=3)\r\n# define the model\r\nmodel = RandomForestClassifier(n_estimators=500, n_jobs=8)\r\n# record current time\r\nstart = time()\r\n# fit the model\r\nmodel.fit(X, y)\r\n# record current time\r\nend = time()\r\n# report execution time\r\nresult = end - start\r\nprint('%.3f seconds' % result)<\/pre>\n<p>Running the example reports the time taken to train the model with a single core.<\/p>\n<p>In this case, we can see that we got another drop in execution speed from about 3.151 to about 2.521 by using all cores.<\/p>\n<p>How long does it take on your system? Share your results in the comments below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">2.521 seconds<\/pre>\n<p>We can make the relationship between the number of cores used during training and execution speed more concrete by comparing all values between one and eight and plotting the result.<\/p>\n<p>The complete example is listed below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\"># example of comparing number of cores used during training to execution speed\r\nfrom time import time\r\nfrom sklearn.datasets import make_classification\r\nfrom sklearn.ensemble import RandomForestClassifier\r\nfrom matplotlib import pyplot\r\n# define dataset\r\nX, y = make_classification(n_samples=10000, n_features=20, n_informative=15, n_redundant=5, random_state=3)\r\nresults = list()\r\n# compare timing for number of cores\r\nn_cores = [1, 2, 3, 4, 5, 6, 7, 8]\r\nfor n in n_cores:\r\n\t# capture current time\r\n\tstart = time()\r\n\t# define the model\r\n\tmodel = RandomForestClassifier(n_estimators=500, n_jobs=n)\r\n\t# fit the model\r\n\tmodel.fit(X, y)\r\n\t# capture current time\r\n\tend = time()\r\n\t# store execution time\r\n\tresult = end - start\r\n\tprint('&gt;cores=%d: %.3f seconds' % (n, result))\r\n\tresults.append(result)\r\npyplot.plot(n_cores, results)\r\npyplot.show()<\/pre>\n<p>Running the example first reports the execution speed for each number of cores used during training.<\/p>\n<p>We can see a steady decrease in execution speed from one to eight cores, although the dramatic benefits stop after four physical cores.<\/p>\n<p>How long does it take on your system? Share your results in the comments below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">&gt;cores=1: 10.798 seconds\r\n&gt;cores=2: 5.743 seconds\r\n&gt;cores=3: 3.964 seconds\r\n&gt;cores=4: 3.158 seconds\r\n&gt;cores=5: 2.868 seconds\r\n&gt;cores=6: 2.631 seconds\r\n&gt;cores=7: 2.528 seconds\r\n&gt;cores=8: 2.440 seconds<\/pre>\n<p>A plot is also created to show the relationship between the number of cores used during training and the execution speed, showing that we continue to see a benefit all the way to eight cores.<\/p>\n<div id=\"attachment_10956\" style=\"width: 1290px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" aria-describedby=\"caption-attachment-10956\" loading=\"lazy\" class=\"size-full wp-image-10956\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2020\/05\/Line-Plot-of-Number-of-Cores-Used-During-Training-vs-Execution-Speed.png\" alt=\"Line Plot of Number of Cores Used During Training vs. Execution Speed\" width=\"1280\" height=\"960\" srcset=\"http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/05\/Line-Plot-of-Number-of-Cores-Used-During-Training-vs-Execution-Speed.png 1280w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/05\/Line-Plot-of-Number-of-Cores-Used-During-Training-vs-Execution-Speed-300x225.png 300w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/05\/Line-Plot-of-Number-of-Cores-Used-During-Training-vs-Execution-Speed-1024x768.png 1024w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/05\/Line-Plot-of-Number-of-Cores-Used-During-Training-vs-Execution-Speed-768x576.png 768w\" sizes=\"(max-width: 1280px) 100vw, 1280px\"><\/p>\n<p id=\"caption-attachment-10956\" class=\"wp-caption-text\">Line Plot of Number of Cores Used During Training vs. Execution Speed<\/p>\n<\/div>\n<p>Now that we are familiar with the benefit of multi-core training of machine learning models, let&rsquo;s look at multi-core model evaluation.<\/p>\n<h2>Multi-Core Model Evaluation<\/h2>\n<p>The gold standard for model evaluation is <a href=\"https:\/\/machinelearningmastery.com\/k-fold-cross-validation\/\">k-fold cross-validation<\/a>.<\/p>\n<p>This is a resampling procedure that requires that the model is trained and evaluated <em>k<\/em> times on different partitioned subsets of the dataset. The result is an estimate of the performance of a model when making predictions on data not used during training that can be used to compare and select a good or best model for a dataset.<\/p>\n<p>In addition, it is also a good practice to repeat this evaluation process multiple times, referred to as repeated k-fold cross-validation.<\/p>\n<p>The evaluation procedure can be configured to use multiple cores, where each model training and evaluation happens on a separate core. This can be done by setting the <em>n_jobs<\/em> argument on the call to <a href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.model_selection.cross_val_score.html\">cross_val_score() function<\/a>; for example:<\/p>\n<p>We can explore the effect of multiple cores on model evaluation.<\/p>\n<p>First, let&rsquo;s evaluate the model using a single core.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# evaluate the model\r\nn_scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=1)<\/pre>\n<p>We will evaluate the random forest model and use a single core in the training of the model (for now).<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# define the model\r\nmodel = RandomForestClassifier(n_estimators=100, n_jobs=1)<\/pre>\n<p>The complete example is listed below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\"># example of evaluating a model using a single core\r\nfrom time import time\r\nfrom sklearn.datasets import make_classification\r\nfrom sklearn.model_selection import cross_val_score\r\nfrom sklearn.model_selection import RepeatedStratifiedKFold\r\nfrom sklearn.ensemble import RandomForestClassifier\r\n# define dataset\r\nX, y = make_classification(n_samples=1000, n_features=20, n_informative=15, n_redundant=5, random_state=3)\r\n# define the model\r\nmodel = RandomForestClassifier(n_estimators=100, n_jobs=1)\r\n# define the evaluation procedure\r\ncv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)\r\n# record current time\r\nstart = time()\r\n# evaluate the model\r\nn_scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=1)\r\n# record current time\r\nend = time()\r\n# report execution time\r\nresult = end - start\r\nprint('%.3f seconds' % result)<\/pre>\n<p>Running the example evaluates the model using 10-fold cross-validation with three repeats.<\/p>\n<p>In this case, we see that the evaluation of the model took about 6.412 seconds.<\/p>\n<p>How long does it take on your system? Share your results in the comments below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">6.412 seconds<\/pre>\n<p>We can update the example to use all eight cores of the system and expect a large speedup.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# evaluate the model\r\nn_scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=8)<\/pre>\n<p>The complete example is listed below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\"># example of evaluating a model using 8 cores\r\nfrom time import time\r\nfrom sklearn.datasets import make_classification\r\nfrom sklearn.model_selection import cross_val_score\r\nfrom sklearn.model_selection import RepeatedStratifiedKFold\r\nfrom sklearn.ensemble import RandomForestClassifier\r\n# define dataset\r\nX, y = make_classification(n_samples=1000, n_features=20, n_informative=15, n_redundant=5, random_state=3)\r\n# define the model\r\nmodel = RandomForestClassifier(n_estimators=100, n_jobs=1)\r\n# define the evaluation procedure\r\ncv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)\r\n# record current time\r\nstart = time()\r\n# evaluate the model\r\nn_scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=8)\r\n# record current time\r\nend = time()\r\n# report execution time\r\nresult = end - start\r\nprint('%.3f seconds' % result)<\/pre>\n<p>Running the example evaluates the model using multiple cores.<\/p>\n<p>In this case, we can see the execution timing dropped from 6.412 seconds to about 2.371 seconds, giving a welcome speedup.<\/p>\n<p>How long does it take on your system? Share your results in the comments below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">2.371 seconds<\/pre>\n<p>As we did in the previous section, we can time the execution speed for each number of cores from one to eight to get an idea of the relationship.<\/p>\n<p>The complete example is listed below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\"># compare execution speed for model evaluation vs number of cpu cores\r\nfrom time import time\r\nfrom sklearn.datasets import make_classification\r\nfrom sklearn.model_selection import cross_val_score\r\nfrom sklearn.model_selection import RepeatedStratifiedKFold\r\nfrom sklearn.ensemble import RandomForestClassifier\r\nfrom matplotlib import pyplot\r\n# define dataset\r\nX, y = make_classification(n_samples=1000, n_features=20, n_informative=15, n_redundant=5, random_state=3)\r\nresults = list()\r\n# compare timing for number of cores\r\nn_cores = [1, 2, 3, 4, 5, 6, 7, 8]\r\nfor n in n_cores:\r\n\t# define the model\r\n\tmodel = RandomForestClassifier(n_estimators=100, n_jobs=1)\r\n\t# define the evaluation procedure\r\n\tcv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)\r\n\t# record the current time\r\n\tstart = time()\r\n\t# evaluate the model\r\n\tn_scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=n)\r\n\t# record the current time\r\n\tend = time()\r\n\t# store execution time\r\n\tresult = end - start\r\n\tprint('&gt;cores=%d: %.3f seconds' % (n, result))\r\n\tresults.append(result)\r\npyplot.plot(n_cores, results)\r\npyplot.show()<\/pre>\n<p>Running the example first reports the execution time in seconds for each number of cores for evaluating the model.<\/p>\n<p>We can see that there is not a dramatic improvement above four physical cores.<\/p>\n<p>We can also see a difference here when training with eight cores from the previous experiment. In this case, evaluating performance took 1.492 seconds whereas the standalone case took about 2.371 seconds.<\/p>\n<p>This highlights the limitation of the evaluation methodology we are using where we are reporting the performance of a single run rather than repeated runs. There is some spin-up time required to load classes into memory and perform any JIT optimization.<\/p>\n<p>Regardless of the accuracy of our flimsy profiling, we do see the familiar speedup of model evaluation with the increase of cores used during the process.<\/p>\n<p>How long does it take on your system? Share your results in the comments below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">&gt;cores=1: 6.339 seconds\r\n&gt;cores=2: 3.765 seconds\r\n&gt;cores=3: 2.404 seconds\r\n&gt;cores=4: 1.826 seconds\r\n&gt;cores=5: 1.806 seconds\r\n&gt;cores=6: 1.686 seconds\r\n&gt;cores=7: 1.587 seconds\r\n&gt;cores=8: 1.492 seconds<\/pre>\n<p>A plot of the relationship between the number of cores and the execution speed is also created.<\/p>\n<div id=\"attachment_10957\" style=\"width: 1290px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" aria-describedby=\"caption-attachment-10957\" loading=\"lazy\" class=\"size-full wp-image-10957\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2020\/09\/Line-Plot-of-Number-of-Cores-Used-During-Evaluation-vs-Execution-Speed.png\" alt=\"Line Plot of Number of Cores Used During Evaluation vs. Execution Speed\" width=\"1280\" height=\"960\" srcset=\"http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/09\/Line-Plot-of-Number-of-Cores-Used-During-Evaluation-vs-Execution-Speed.png 1280w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/09\/Line-Plot-of-Number-of-Cores-Used-During-Evaluation-vs-Execution-Speed-300x225.png 300w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/09\/Line-Plot-of-Number-of-Cores-Used-During-Evaluation-vs-Execution-Speed-1024x768.png 1024w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/09\/Line-Plot-of-Number-of-Cores-Used-During-Evaluation-vs-Execution-Speed-768x576.png 768w\" sizes=\"(max-width: 1280px) 100vw, 1280px\"><\/p>\n<p id=\"caption-attachment-10957\" class=\"wp-caption-text\">Line Plot of Number of Cores Used During Evaluation vs. Execution Speed<\/p>\n<\/div>\n<p>We can also make the model training process parallel during the model evaluation procedure.<\/p>\n<p>Although this is possible, should we?<\/p>\n<p>To explore this question, let&rsquo;s first consider the case where model training uses all cores and model evaluation uses a single core.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# define the model\r\nmodel = RandomForestClassifier(n_estimators=100, n_jobs=8)\r\n...\r\n# evaluate the model\r\nn_scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=1)<\/pre>\n<p>The complete example is listed below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\"># example of using multiple cores for model training but not model evaluation\r\nfrom time import time\r\nfrom sklearn.datasets import make_classification\r\nfrom sklearn.model_selection import cross_val_score\r\nfrom sklearn.model_selection import RepeatedStratifiedKFold\r\nfrom sklearn.ensemble import RandomForestClassifier\r\n# define dataset\r\nX, y = make_classification(n_samples=1000, n_features=20, n_informative=15, n_redundant=5, random_state=3)\r\n# define the model\r\nmodel = RandomForestClassifier(n_estimators=100, n_jobs=8)\r\n# define the evaluation procedure\r\ncv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)\r\n# record current time\r\nstart = time()\r\n# evaluate the model\r\nn_scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=1)\r\n# record current time\r\nend = time()\r\n# report execution time\r\nresult = end - start\r\nprint('%.3f seconds' % result)<\/pre>\n<p>Running the example evaluates the model using a single core, but each trained model uses a single core.<\/p>\n<p>In this case, we can see that the model evaluation takes more than 10 seconds, much longer than the 1 or 2 seconds when we use a single core for training and all cores for parallel model evaluation.<\/p>\n<p>How long does it take on your system? Share your results in the comments below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">10.461 seconds<\/pre>\n<p>What if we split the number of cores between the training and evaluation procedures?<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# define the model\r\nmodel = RandomForestClassifier(n_estimators=100, n_jobs=4)\r\n...\r\n# evaluate the model\r\nn_scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=4)<\/pre>\n<p>The complete example is listed below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\"># example of using multiple cores for model training and evaluation\r\nfrom time import time\r\nfrom sklearn.datasets import make_classification\r\nfrom sklearn.model_selection import cross_val_score\r\nfrom sklearn.model_selection import RepeatedStratifiedKFold\r\nfrom sklearn.ensemble import RandomForestClassifier\r\n# define dataset\r\nX, y = make_classification(n_samples=1000, n_features=20, n_informative=15, n_redundant=5, random_state=3)\r\n# define the model\r\nmodel = RandomForestClassifier(n_estimators=100, n_jobs=8)\r\n# define the evaluation procedure\r\ncv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=4)\r\n# record current time\r\nstart = time()\r\n# evaluate the model\r\nn_scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=4)\r\n# record current time\r\nend = time()\r\n# report execution time\r\nresult = end - start\r\nprint('%.3f seconds' % result)<\/pre>\n<p>Running the example evaluates the model using four cores, and each model is trained using four different cores.<\/p>\n<p>We can see an improvement over training with all cores and evaluating with one core, but at least for this model on this dataset, it is more efficient to use all cores for model evaluation and a single core for model training.<\/p>\n<p>How long does it take on your system? Share your results in the comments below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">3.434 seconds<\/pre>\n<\/p>\n<h2>Multi-Core Hyperparameter Tuning<\/h2>\n<p>It is common to tune the hyperparameters of a machine learning model using a grid search or a random search.<\/p>\n<p>The scikit-learn library provides these capabilities via the <a href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.model_selection.GridSearchCV.html\">GridSearchCV<\/a> and <a href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.model_selection.RandomizedSearchCV.html\">RandomizedSearchCV<\/a> classes respectively.<\/p>\n<p>Both of these search procedures can be made parallel by setting the <em>n_jobs<\/em> argument, assigning each hyperparameter configuration to a core for evaluation.<\/p>\n<p>The model evaluation itself could also be multi-core, as we saw in the previous section, and the model training for a given evaluation can also be training as we saw in the second before that. Therefore, the stack of potentially multi-core processes is starting to get challenging to configure.<\/p>\n<p>In this specific implementation, we can make the model training parallel, but we don&rsquo;t have control over how each model hyperparameter and how each model evaluation is made multi-core. The documentation is not clear at the time of writing, but I would guess that each model evaluation using a single core hyperparameter configuration is split into jobs.<\/p>\n<p>Let&rsquo;s explore the benefits of performing model hyperparameter tuning using multiple cores.<\/p>\n<p>First, let&rsquo;s evaluate a grid of different configurations of the random forest algorithm using a single core.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# define grid search\r\nsearch = GridSearchCV(model, grid, n_jobs=1, cv=cv)<\/pre>\n<p>The complete example is listed below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\"># example of tuning model hyperparameters with a single core\r\nfrom time import time\r\nfrom sklearn.datasets import make_classification\r\nfrom sklearn.model_selection import RepeatedStratifiedKFold\r\nfrom sklearn.ensemble import RandomForestClassifier\r\nfrom sklearn.model_selection import GridSearchCV\r\n# define dataset\r\nX, y = make_classification(n_samples=1000, n_features=20, n_informative=15, n_redundant=5, random_state=3)\r\n# define the model\r\nmodel = RandomForestClassifier(n_estimators=100, n_jobs=1)\r\n# define the evaluation procedure\r\ncv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)\r\n# define grid\r\ngrid = dict()\r\ngrid['max_features'] = [1, 2, 3, 4, 5]\r\n# define grid search\r\nsearch = GridSearchCV(model, grid, n_jobs=1, cv=cv)\r\n# record current time\r\nstart = time()\r\n# perform search\r\nsearch.fit(X, y)\r\n# record current time\r\nend = time()\r\n# report execution time\r\nresult = end - start\r\nprint('%.3f seconds' % result)<\/pre>\n<p>Running the example tests different values of the <em>max_features<\/em> configuration for random forest, where each configuration is evaluated using repeated k-fold cross-validation.<\/p>\n<p>In this case, the grid search on a single core takes about 28.838 seconds.<\/p>\n<p>How long does it take on your system? Share your results in the comments below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">28.838 seconds<\/pre>\n<p>We can now configure the grid search to use all available cores on the system, in this case, eight cores.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# define grid search\r\nsearch = GridSearchCV(model, grid, n_jobs=8, cv=cv)<\/pre>\n<p>We can then evaluate how long this multi-core grids search takes to execute. The complete example is listed below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\"># example of tuning model hyperparameters with 8 cores\r\nfrom time import time\r\nfrom sklearn.datasets import make_classification\r\nfrom sklearn.model_selection import RepeatedStratifiedKFold\r\nfrom sklearn.ensemble import RandomForestClassifier\r\nfrom sklearn.model_selection import GridSearchCV\r\n# define dataset\r\nX, y = make_classification(n_samples=1000, n_features=20, n_informative=15, n_redundant=5, random_state=3)\r\n# define the model\r\nmodel = RandomForestClassifier(n_estimators=100, n_jobs=1)\r\n# define the evaluation procedure\r\ncv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)\r\n# define grid\r\ngrid = dict()\r\ngrid['max_features'] = [1, 2, 3, 4, 5]\r\n# define grid search\r\nsearch = GridSearchCV(model, grid, n_jobs=8, cv=cv)\r\n# record current time\r\nstart = time()\r\n# perform search\r\nsearch.fit(X, y)\r\n# record current time\r\nend = time()\r\n# report execution time\r\nresult = end - start\r\nprint('%.3f seconds' % result)<\/pre>\n<p>Running the example reports execution time for the grid search.<\/p>\n<p>In this case, we see a factor of about four speed up from roughly 28.838 seconds to around 7.418 seconds.<\/p>\n<p>How long does it take on your system? Share your results in the comments below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">7.418 seconds<\/pre>\n<p>Intuitively, we would expect that making the grid search multi-core should be the focus and not model training.<\/p>\n<p>Nevertheless, we can divide the number of cores between model training and the grid search to see if it offers a benefit for this model on this dataset.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# define the model\r\nmodel = RandomForestClassifier(n_estimators=100, n_jobs=4)\r\n...\r\n# define grid search\r\nsearch = GridSearchCV(model, grid, n_jobs=4, cv=cv)<\/pre>\n<p>The complete example of multi-core model training and multi-core hyperparameter tuning is listed below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\"># example of multi-core model training and hyperparameter tuning\r\nfrom time import time\r\nfrom sklearn.datasets import make_classification\r\nfrom sklearn.model_selection import RepeatedStratifiedKFold\r\nfrom sklearn.ensemble import RandomForestClassifier\r\nfrom sklearn.model_selection import GridSearchCV\r\n# define dataset\r\nX, y = make_classification(n_samples=1000, n_features=20, n_informative=15, n_redundant=5, random_state=3)\r\n# define the model\r\nmodel = RandomForestClassifier(n_estimators=100, n_jobs=4)\r\n# define the evaluation procedure\r\ncv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)\r\n# define grid\r\ngrid = dict()\r\ngrid['max_features'] = [1, 2, 3, 4, 5]\r\n# define grid search\r\nsearch = GridSearchCV(model, grid, n_jobs=4, cv=cv)\r\n# record current time\r\nstart = time()\r\n# perform search\r\nsearch.fit(X, y)\r\n# record current time\r\nend = time()\r\n# report execution time\r\nresult = end - start\r\nprint('%.3f seconds' % result)<\/pre>\n<p>In this case, we do see a decrease in execution speed compared to a single core case, but not as much benefit as assigning all cores to the grid search process.<\/p>\n<p>How long does it take on your system? Share your results in the comments below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">14.148 seconds<\/pre>\n<\/p>\n<h2>Recommendations<\/h2>\n<p>This section lists some general recommendations when using multiple cores for machine learning.<\/p>\n<ul>\n<li>Confirm the number of cores available on your system.<\/li>\n<li>Consider using an AWS EC2 instance with many cores to get an immediate speed up.<\/li>\n<li>Check the API documentation to see if the model\/s you are using support multi-core training.<\/li>\n<li>Confirm multi-core training offers a measurable benefit on your system.<\/li>\n<li>When using k-fold cross-validation, it is probably better to assign cores to the resampling procedure and leave model training single core.<\/li>\n<li>When using hyperparamter tuning, it is probably better to make the search multi-core and leave the model training and evaluation single core.<\/li>\n<\/ul>\n<p>Do you have any recommendations of your own?<\/p>\n<h2>Further Reading<\/h2>\n<p>This section provides more resources on the topic if you are looking to go deeper.<\/p>\n<h3>Related Tutorials<\/h3>\n<ul>\n<li><a href=\"https:\/\/machinelearningmastery.com\/k-fold-cross-validation\/\">A Gentle Introduction to k-fold Cross-Validation<\/a><\/li>\n<\/ul>\n<h3>APIs<\/h3>\n<ul>\n<li><a href=\"https:\/\/scikit-learn.org\/stable\/developers\/performance.html\">How to optimize for speed, scikit-learn Documentation<\/a>.<\/li>\n<li><a href=\"https:\/\/joblib.readthedocs.io\/en\/latest\/\">Joblib: running Python functions as pipeline jobs<\/a><\/li>\n<li><a href=\"https:\/\/docs.python.org\/3\/library\/timeit.html\">timeit API<\/a>.<\/li>\n<li><a href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.ensemble.RandomForestClassifier.html\">sklearn.ensemble.RandomForestClassifier API<\/a>.<\/li>\n<li><a href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.model_selection.cross_val_score.html\">sklearn.model_selection.cross_val_score API<\/a>.<\/li>\n<li><a href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.model_selection.GridSearchCV.html\">sklearn.model_selection.GridSearchCV API<\/a>.<\/li>\n<li><a href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.model_selection.RandomizedSearchCV.html\">sklearn.model_selection.RandomizedSearchCV API<\/a>.<\/li>\n<li><a href=\"https:\/\/scikit-learn.org\/stable\/glossary.html#term-n-jobs\">n_jobs scikit-learn argument<\/a>.<\/li>\n<\/ul>\n<h3>Articles<\/h3>\n<ul>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Multi-core_processor\">Multi-core processor, Wikipedia<\/a>.<\/li>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Hyper-threading\">Hyper-threading, Wikipedia<\/a>.<\/li>\n<\/ul>\n<h2>Summary<\/h2>\n<p>In this tutorial, you discovered how to configure scikit-learn for multi-core machine learning.<\/p>\n<p>Specifically, you learned:<\/p>\n<ul>\n<li>How to train machine learning models using multiple cores.<\/li>\n<li>How to make the evaluation of machine learning models parallel.<\/li>\n<li>How to use multiple cores to tune machine learning model hyperparameters.<\/li>\n<\/ul>\n<p><strong>Do you have any questions?<\/strong><br \/>\nAsk your questions in the comments below and I will do my best to answer.<\/p>\n<p>The post <a rel=\"nofollow\" href=\"https:\/\/machinelearningmastery.com\/multi-core-machine-learning-in-python\/\">Multi-Core Machine Learning in Python With Scikit-Learn<\/a> appeared first on <a rel=\"nofollow\" href=\"https:\/\/machinelearningmastery.com\/\">Machine Learning Mastery<\/a>.<\/p>\n<\/div>\n<p><a href=\"https:\/\/machinelearningmastery.com\/multi-core-machine-learning-in-python\/\">Go to Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Author: Jason Brownlee Many computationally expensive tasks for machine learning can be made parallel by splitting the work across multiple CPU cores, referred to as [&hellip;] <span class=\"read-more-link\"><a class=\"read-more\" href=\"https:\/\/www.aiproblog.com\/index.php\/2020\/09\/20\/multi-core-machine-learning-in-python-with-scikit-learn\/\">Read More<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":3888,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[24],"tags":[],"_links":{"self":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/posts\/3887"}],"collection":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/comments?post=3887"}],"version-history":[{"count":0,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/posts\/3887\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/media\/3888"}],"wp:attachment":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/media?parent=3887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/categories?post=3887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/tags?post=3887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}