{"id":23578,"date":"2021-10-17T21:21:14","date_gmt":"2021-10-17T12:21:14","guid":{"rendered":"https:\/\/www.souichi.club\/?p=23578"},"modified":"2022-03-19T17:21:46","modified_gmt":"2022-03-19T08:21:46","slug":"randomizedsearchcv-runtimeerror","status":"publish","type":"post","link":"https:\/\/www.souichi.club\/en\/deep-learning\/randomizedsearchcv-runtimeerror\/","title":{"rendered":"RunTimeError when searching for hyperparameters in RandomizedSearchCV"},"content":{"rendered":"\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_83 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a143a7b9f611\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #454e5c;color:#454e5c\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #454e5c;color:#454e5c\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a143a7b9f611\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.souichi.club\/en\/deep-learning\/randomizedsearchcv-runtimeerror\/#RandomizedSearchCV\" >RandomizedSearchCV<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.souichi.club\/en\/deep-learning\/randomizedsearchcv-runtimeerror\/#Environment\" >Environment<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.souichi.club\/en\/deep-learning\/randomizedsearchcv-runtimeerror\/#How\" >How<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"RandomizedSearchCV\"><\/span>RandomizedSearchCV<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>When I used RandomizedSearchCV to search for the best hyperparameters for Deep Neural Network, the following error message was displayed and a RunTimeError for Cannot Clone object was raised. <\/p>\n\n\n<pre><code class=\"lang-shell\">RuntimeError: Cannot clone object &lt;tensorflow.python.keras.wrappers.scikit_learn.KerasRegressor object at 0x000001CEEFBA8550&gt;, as the constructor either does not set or modifies parameter learning_rate<\/code><\/pre>\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a class=\"r_image\" href=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/randomizedsearchcv-runtimeerror-02.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/randomizedsearchcv-runtimeerror-02.png?resize=768%2C414&#038;ssl=1\" alt=\"RunTimeError\" class=\"wp-image-23569\" width=\"768\" height=\"414\" srcset=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/randomizedsearchcv-runtimeerror-02.png?resize=1024%2C552&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/randomizedsearchcv-runtimeerror-02.png?resize=300%2C162&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/randomizedsearchcv-runtimeerror-02.png?resize=768%2C414&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/randomizedsearchcv-runtimeerror-02.png?resize=1536%2C828&amp;ssl=1 1536w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/randomizedsearchcv-runtimeerror-02.png?w=1836&amp;ssl=1 1836w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Environment\"><\/span>Environment<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The environment in which the error occurred is as follows.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\">\n<table class=\"r_table01\">\n<tbody>\n<tr>\n<td class=\"r_td30\">\n<p>OS<\/p>\n<\/td>\n<td>\n<p>Windows 10 Home\uff0864 Bit\uff09<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>Anacond<\/p>\n<\/td>\n<td>\n<p>conda 4.10.3<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>Python<\/p>\n<\/td>\n<td>\n<p>&nbsp;3.8.5<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>Keras<\/p>\n<\/td>\n<td>\n<p>Keras 2.4.3<\/p>\n<p>Keras-Applications 1.0.8<\/p>\n<p>Keras-Preprocessing 1.1.2<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>scikit-learn<\/p>\n<\/td>\n<td>\n<p>&nbsp;Ver 0.24.2<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>numpy<\/p>\n<\/td>\n<td>\n<p>&nbsp;1.20.3<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How\"><\/span>How<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Some articles said that it was a bug in scikit-learn and that downgrading the version to 0.21.2 would fix it, but it did not.<\/p>\n\n\n\n<p>Fixed the _search.py file in the Python sklearn library.<\/p>\n\n\n\n<p>I think the location of the file depends on the environment, but since I am using Anaconda, I modified the file in the following path.<\/p>\n\n\n<pre><code class=\"lang-shell\">C:\\Users\\User Name\\Anaconda3\\Lib\\site-packages\\sklearn\\model_selection\\_search.py<\/code><\/pre>\n\n\n<p>Lines 876-877 have been revised as follows.<\/p>\n\n\n<pre><code class=\"lang-python\">Before\nself.best_estimator_ = clone(clone(base_estimator).set_params(\n**self.best_params_))\n\u3000\u3000\u3000\u3000\u2193\nAfter\nself.best_estimator_ = clone(base_estimator).set_params(\n**self.best_params_)<\/code><\/pre>\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a class=\"r_image\" href=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/randomizedsearchcv-runtimeerror-03.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/randomizedsearchcv-runtimeerror-03.png?resize=768%2C414&#038;ssl=1\" alt=\"sklearn _search.py Rev\" class=\"wp-image-23571\" width=\"768\" height=\"414\" srcset=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/randomizedsearchcv-runtimeerror-03.png?resize=1024%2C552&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/randomizedsearchcv-runtimeerror-03.png?resize=300%2C162&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/randomizedsearchcv-runtimeerror-03.png?resize=768%2C414&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/randomizedsearchcv-runtimeerror-03.png?resize=1536%2C827&amp;ssl=1 1536w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/randomizedsearchcv-runtimeerror-03.png?w=1838&amp;ssl=1 1838w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/a><\/figure>\n\n\n\n<p>It seems that RunTimeError was caused by the fact that the error checking was done more strictly.<\/p>\n\n\n\n<p>In addition, I think this fix is only a remedy, but for now, RunTimeError has been resolved.<\/p>\n\n\n\n<p>This is the end of this article.<\/p>\n\n\n<div class=\"postscript_balloon\">Finally.<\/div>\n\n\n<p>I hope this article will be useful to someone somewhere.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When I used RandomizedSearchCV to search for the best hyperparameters for Deep Neural Network, I got a RunTimeError for the Cannot Clone object.<\/p>\n","protected":false},"author":5,"featured_media":23569,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"en_US","_original_post":"https:\/\/www.souichi.club\/?p=23564","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[154],"tags":[],"class_list":["post-23578","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-deep-learning","en-US"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/randomizedsearchcv-runtimeerror-02.png?fit=1836%2C990&ssl=1","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.souichi.club\/wp-json\/wp\/v2\/posts\/23578","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.souichi.club\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.souichi.club\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.souichi.club\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.souichi.club\/wp-json\/wp\/v2\/comments?post=23578"}],"version-history":[{"count":1,"href":"https:\/\/www.souichi.club\/wp-json\/wp\/v2\/posts\/23578\/revisions"}],"predecessor-version":[{"id":24529,"href":"https:\/\/www.souichi.club\/wp-json\/wp\/v2\/posts\/23578\/revisions\/24529"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.souichi.club\/wp-json\/wp\/v2\/media\/23569"}],"wp:attachment":[{"href":"https:\/\/www.souichi.club\/wp-json\/wp\/v2\/media?parent=23578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.souichi.club\/wp-json\/wp\/v2\/categories?post=23578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.souichi.club\/wp-json\/wp\/v2\/tags?post=23578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}