CINXE.COM
Getting started with KerasTuner
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="description" content="Keras documentation"> <meta name="author" content="Keras Team"> <link rel="shortcut icon" href="https://keras.io/img/favicon.ico"> <link rel="canonical" href="https://keras.io/guides/keras_tuner/getting_started/" /> <!-- Social --> <meta property="og:title" content="Keras documentation: Getting started with KerasTuner"> <meta property="og:image" content="https://keras.io/img/logo-k-keras-wb.png"> <meta name="twitter:title" content="Keras documentation: Getting started with KerasTuner"> <meta name="twitter:image" content="https://keras.io/img/k-keras-social.png"> <meta name="twitter:card" content="summary"> <title>Getting started with KerasTuner</title> <!-- Bootstrap core CSS --> <link href="/css/bootstrap.min.css" rel="stylesheet"> <!-- Custom fonts for this template --> <link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;600;700;800&display=swap" rel="stylesheet"> <!-- Custom styles for this template --> <link href="/css/docs.css" rel="stylesheet"> <link href="/css/monokai.css" rel="stylesheet"> <!-- Google Tag Manager --> <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); })(window,document,'script','dataLayer','GTM-5DNGF4N'); </script> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-175165319-128', 'auto'); ga('send', 'pageview'); </script> <!-- End Google Tag Manager --> <script async defer src="https://buttons.github.io/buttons.js"></script> </head> <body> <!-- Google Tag Manager (noscript) --> <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-5DNGF4N" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript> <!-- End Google Tag Manager (noscript) --> <div class='k-page'> <div class="k-nav" id="nav-menu"> <a href='/'><img src='/img/logo-small.png' class='logo-small' /></a> <div class="nav flex-column nav-pills" role="tablist" aria-orientation="vertical"> <a class="nav-link" href="/about/" role="tab" aria-selected="">About Keras</a> <a class="nav-link" href="/getting_started/" role="tab" aria-selected="">Getting started</a> <a class="nav-link active" href="/guides/" role="tab" aria-selected="">Developer guides</a> <a class="nav-sublink" href="/guides/functional_api/">The Functional API</a> <a class="nav-sublink" href="/guides/sequential_model/">The Sequential model</a> <a class="nav-sublink" href="/guides/making_new_layers_and_models_via_subclassing/">Making new layers & models via subclassing</a> <a class="nav-sublink" href="/guides/training_with_built_in_methods/">Training & evaluation with the built-in methods</a> <a class="nav-sublink" href="/guides/custom_train_step_in_jax/">Customizing `fit()` with JAX</a> <a class="nav-sublink" href="/guides/custom_train_step_in_tensorflow/">Customizing `fit()` with TensorFlow</a> <a class="nav-sublink" href="/guides/custom_train_step_in_torch/">Customizing `fit()` with PyTorch</a> <a class="nav-sublink" href="/guides/writing_a_custom_training_loop_in_jax/">Writing a custom training loop in JAX</a> <a class="nav-sublink" href="/guides/writing_a_custom_training_loop_in_tensorflow/">Writing a custom training loop in TensorFlow</a> <a class="nav-sublink" href="/guides/writing_a_custom_training_loop_in_torch/">Writing a custom training loop in PyTorch</a> <a class="nav-sublink" href="/guides/serialization_and_saving/">Serialization & saving</a> <a class="nav-sublink" href="/guides/customizing_saving_and_serialization/">Customizing saving & serialization</a> <a class="nav-sublink" href="/guides/writing_your_own_callbacks/">Writing your own callbacks</a> <a class="nav-sublink" href="/guides/transfer_learning/">Transfer learning & fine-tuning</a> <a class="nav-sublink" href="/guides/distributed_training_with_jax/">Distributed training with JAX</a> <a class="nav-sublink" href="/guides/distributed_training_with_tensorflow/">Distributed training with TensorFlow</a> <a class="nav-sublink" href="/guides/distributed_training_with_torch/">Distributed training with PyTorch</a> <a class="nav-sublink" href="/guides/distribution/">Distributed training with Keras 3</a> <a class="nav-sublink" href="/guides/migrating_to_keras_3/">Migrating Keras 2 code to Keras 3</a> <a class="nav-sublink active" href="/guides/keras_tuner/">Hyperparameter Tuning</a> <a class="nav-sublink2 active" href="/guides/keras_tuner/getting_started/">Getting started with KerasTuner</a> <a class="nav-sublink2" href="/guides/keras_tuner/distributed_tuning/">Distributed hyperparameter tuning with KerasTuner</a> <a class="nav-sublink2" href="/guides/keras_tuner/custom_tuner/">Tune hyperparameters in your custom training loop</a> <a class="nav-sublink2" href="/guides/keras_tuner/visualize_tuning/">Visualize the hyperparameter tuning process</a> <a class="nav-sublink2" href="/guides/keras_tuner/failed_trials/">Handling failed trials in KerasTuner</a> <a class="nav-sublink2" href="/guides/keras_tuner/tailor_the_search_space/">Tailor the search space</a> <a class="nav-sublink" href="/guides/keras_cv/">KerasCV</a> <a class="nav-sublink" href="/guides/keras_nlp/">KerasNLP</a> <a class="nav-sublink" href="/guides/keras_hub/">KerasHub</a> <a class="nav-link" href="/api/" role="tab" aria-selected="">Keras 3 API documentation</a> <a class="nav-link" href="/2.18/api/" role="tab" aria-selected="">Keras 2 API documentation</a> <a class="nav-link" href="/examples/" role="tab" aria-selected="">Code examples</a> <a class="nav-link" href="/keras_tuner/" role="tab" aria-selected="">KerasTuner: Hyperparameter Tuning</a> <a class="nav-link" href="/keras_hub/" role="tab" aria-selected="">KerasHub: Pretrained Models</a> <a class="nav-link" href="/keras_cv/" role="tab" aria-selected="">KerasCV: Computer Vision Workflows</a> <a class="nav-link" href="/keras_nlp/" role="tab" aria-selected="">KerasNLP: Natural Language Workflows</a> </div> </div> <div class='k-main'> <div class='k-main-top'> <script> function displayDropdownMenu() { e = document.getElementById("nav-menu"); if (e.style.display == "block") { e.style.display = "none"; } else { e.style.display = "block"; document.getElementById("dropdown-nav").style.display = "block"; } } function resetMobileUI() { if (window.innerWidth <= 840) { document.getElementById("nav-menu").style.display = "none"; document.getElementById("dropdown-nav").style.display = "block"; } else { document.getElementById("nav-menu").style.display = "block"; document.getElementById("dropdown-nav").style.display = "none"; } var navmenu = document.getElementById("nav-menu"); var menuheight = navmenu.clientHeight; var kmain = document.getElementById("k-main-id"); kmain.style.minHeight = (menuheight + 100) + 'px'; } window.onresize = resetMobileUI; window.addEventListener("load", (event) => { resetMobileUI() }); </script> <div id='dropdown-nav' onclick="displayDropdownMenu();"> <svg viewBox="-20 -20 120 120" width="60" height="60"> <rect width="100" height="20"></rect> <rect y="30" width="100" height="20"></rect> <rect y="60" width="100" height="20"></rect> </svg> </div> <form class="bd-search d-flex align-items-center k-search-form" id="search-form"> <input type="search" class="k-search-input" id="search-input" placeholder="Search Keras documentation..." aria-label="Search Keras documentation..." autocomplete="off"> <button class="k-search-btn"> <svg width="13" height="13" viewBox="0 0 13 13"><title>search</title><path d="m4.8495 7.8226c0.82666 0 1.5262-0.29146 2.0985-0.87438 0.57232-0.58292 0.86378-1.2877 0.87438-2.1144 0.010599-0.82666-0.28086-1.5262-0.87438-2.0985-0.59352-0.57232-1.293-0.86378-2.0985-0.87438-0.8055-0.010599-1.5103 0.28086-2.1144 0.87438-0.60414 0.59352-0.8956 1.293-0.87438 2.0985 0.021197 0.8055 0.31266 1.5103 0.87438 2.1144 0.56172 0.60414 1.2665 0.8956 2.1144 0.87438zm4.4695 0.2115 3.681 3.6819-1.259 1.284-3.6817-3.7 0.0019784-0.69479-0.090043-0.098846c-0.87973 0.76087-1.92 1.1413-3.1207 1.1413-1.3553 0-2.5025-0.46363-3.4417-1.3909s-1.4088-2.0686-1.4088-3.4239c0-1.3553 0.4696-2.4966 1.4088-3.4239 0.9392-0.92727 2.0864-1.3969 3.4417-1.4088 1.3553-0.011889 2.4906 0.45771 3.406 1.4088 0.9154 0.95107 1.379 2.0924 1.3909 3.4239 0 1.2126-0.38043 2.2588-1.1413 3.1385l0.098834 0.090049z"></path></svg> </button> </form> <script> var form = document.getElementById('search-form'); form.onsubmit = function(e) { e.preventDefault(); var query = document.getElementById('search-input').value; window.location.href = '/search.html?query=' + query; return False } </script> </div> <div class='k-main-inner' id='k-main-id'> <div class='k-location-slug'> <span class="k-location-slug-pointer">►</span> <a href='/guides/'>Developer guides</a> / <a href='/guides/keras_tuner/'>Hyperparameter Tuning</a> / Getting started with KerasTuner </div> <div class='k-content'> <h1 id="getting-started-with-kerastuner">Getting started with KerasTuner</h1> <p><strong>Authors:</strong> Luca Invernizzi, James Long, Francois Chollet, Tom O'Malley, Haifeng Jin<br> <strong>Date created:</strong> 2019/05/31<br> <strong>Last modified:</strong> 2021/10/27<br> <strong>Description:</strong> The basics of using KerasTuner to tune model hyperparameters.</p> <p><img class="k-inline-icon" src="https://colab.research.google.com/img/colab_favicon.ico"/> <a href="https://colab.research.google.com/github/keras-team/keras-io/blob/master/guides/ipynb/keras_tuner/getting_started.ipynb"><strong>View in Colab</strong></a> <span class="k-dot">•</span><img class="k-inline-icon" src="https://github.com/favicon.ico"/> <a href="https://github.com/keras-team/keras-io/blob/master/guides/keras_tuner/getting_started.py"><strong>GitHub source</strong></a></p> <div class="codehilite"><pre><span></span><code><span class="err">!</span><span class="n">pip</span> <span class="n">install</span> <span class="n">keras</span><span class="o">-</span><span class="n">tuner</span> <span class="o">-</span><span class="n">q</span> </code></pre></div> <hr /> <h2 id="introduction">Introduction</h2> <p>KerasTuner is a general-purpose hyperparameter tuning library. It has strong integration with Keras workflows, but it isn't limited to them: you could use it to tune scikit-learn models, or anything else. In this tutorial, you will see how to tune model architecture, training process, and data preprocessing steps with KerasTuner. Let's start from a simple example.</p> <hr /> <h2 id="tune-the-model-architecture">Tune the model architecture</h2> <p>The first thing we need to do is writing a function, which returns a compiled Keras model. It takes an argument <code>hp</code> for defining the hyperparameters while building the model.</p> <h3 id="define-the-search-space">Define the search space</h3> <p>In the following code example, we define a Keras model with two <code>Dense</code> layers. We want to tune the number of units in the first <code>Dense</code> layer. We just define an integer hyperparameter with <code>hp.Int('units', min_value=32, max_value=512, step=32)</code>, whose range is from 32 to 512 inclusive. When sampling from it, the minimum step for walking through the interval is 32.</p> <div class="codehilite"><pre><span></span><code><span class="kn">import</span> <span class="nn">keras</span> <span class="kn">from</span> <span class="nn">keras</span> <span class="kn">import</span> <span class="n">layers</span> <span class="k">def</span> <span class="nf">build_model</span><span class="p">(</span><span class="n">hp</span><span class="p">):</span> <span class="n">model</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">Sequential</span><span class="p">()</span> <span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">layers</span><span class="o">.</span><span class="n">Flatten</span><span class="p">())</span> <span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span> <span class="n">layers</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span> <span class="c1"># Define the hyperparameter.</span> <span class="n">units</span><span class="o">=</span><span class="n">hp</span><span class="o">.</span><span class="n">Int</span><span class="p">(</span><span class="s2">"units"</span><span class="p">,</span> <span class="n">min_value</span><span class="o">=</span><span class="mi">32</span><span class="p">,</span> <span class="n">max_value</span><span class="o">=</span><span class="mi">512</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">32</span><span class="p">),</span> <span class="n">activation</span><span class="o">=</span><span class="s2">"relu"</span><span class="p">,</span> <span class="p">)</span> <span class="p">)</span> <span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">layers</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="n">activation</span><span class="o">=</span><span class="s2">"softmax"</span><span class="p">))</span> <span class="n">model</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span> <span class="n">optimizer</span><span class="o">=</span><span class="s2">"adam"</span><span class="p">,</span> <span class="n">loss</span><span class="o">=</span><span class="s2">"categorical_crossentropy"</span><span class="p">,</span> <span class="n">metrics</span><span class="o">=</span><span class="p">[</span><span class="s2">"accuracy"</span><span class="p">],</span> <span class="p">)</span> <span class="k">return</span> <span class="n">model</span> </code></pre></div> <p>You can quickly test if the model builds successfully.</p> <div class="codehilite"><pre><span></span><code><span class="kn">import</span> <span class="nn">keras_tuner</span> <span class="n">build_model</span><span class="p">(</span><span class="n">keras_tuner</span><span class="o">.</span><span class="n">HyperParameters</span><span class="p">())</span> </code></pre></div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code><Sequential name=sequential, built=False> </code></pre></div> </div> <p>There are many other types of hyperparameters as well. We can define multiple hyperparameters in the function. In the following code, we tune whether to use a <code>Dropout</code> layer with <code>hp.Boolean()</code>, tune which activation function to use with <code>hp.Choice()</code>, tune the learning rate of the optimizer with <code>hp.Float()</code>.</p> <div class="codehilite"><pre><span></span><code><span class="k">def</span> <span class="nf">build_model</span><span class="p">(</span><span class="n">hp</span><span class="p">):</span> <span class="n">model</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">Sequential</span><span class="p">()</span> <span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">layers</span><span class="o">.</span><span class="n">Flatten</span><span class="p">())</span> <span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span> <span class="n">layers</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span> <span class="c1"># Tune number of units.</span> <span class="n">units</span><span class="o">=</span><span class="n">hp</span><span class="o">.</span><span class="n">Int</span><span class="p">(</span><span class="s2">"units"</span><span class="p">,</span> <span class="n">min_value</span><span class="o">=</span><span class="mi">32</span><span class="p">,</span> <span class="n">max_value</span><span class="o">=</span><span class="mi">512</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">32</span><span class="p">),</span> <span class="c1"># Tune the activation function to use.</span> <span class="n">activation</span><span class="o">=</span><span class="n">hp</span><span class="o">.</span><span class="n">Choice</span><span class="p">(</span><span class="s2">"activation"</span><span class="p">,</span> <span class="p">[</span><span class="s2">"relu"</span><span class="p">,</span> <span class="s2">"tanh"</span><span class="p">]),</span> <span class="p">)</span> <span class="p">)</span> <span class="c1"># Tune whether to use dropout.</span> <span class="k">if</span> <span class="n">hp</span><span class="o">.</span><span class="n">Boolean</span><span class="p">(</span><span class="s2">"dropout"</span><span class="p">):</span> <span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">layers</span><span class="o">.</span><span class="n">Dropout</span><span class="p">(</span><span class="n">rate</span><span class="o">=</span><span class="mf">0.25</span><span class="p">))</span> <span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">layers</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="n">activation</span><span class="o">=</span><span class="s2">"softmax"</span><span class="p">))</span> <span class="c1"># Define the optimizer learning rate as a hyperparameter.</span> <span class="n">learning_rate</span> <span class="o">=</span> <span class="n">hp</span><span class="o">.</span><span class="n">Float</span><span class="p">(</span><span class="s2">"lr"</span><span class="p">,</span> <span class="n">min_value</span><span class="o">=</span><span class="mf">1e-4</span><span class="p">,</span> <span class="n">max_value</span><span class="o">=</span><span class="mf">1e-2</span><span class="p">,</span> <span class="n">sampling</span><span class="o">=</span><span class="s2">"log"</span><span class="p">)</span> <span class="n">model</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span> <span class="n">optimizer</span><span class="o">=</span><span class="n">keras</span><span class="o">.</span><span class="n">optimizers</span><span class="o">.</span><span class="n">Adam</span><span class="p">(</span><span class="n">learning_rate</span><span class="o">=</span><span class="n">learning_rate</span><span class="p">),</span> <span class="n">loss</span><span class="o">=</span><span class="s2">"categorical_crossentropy"</span><span class="p">,</span> <span class="n">metrics</span><span class="o">=</span><span class="p">[</span><span class="s2">"accuracy"</span><span class="p">],</span> <span class="p">)</span> <span class="k">return</span> <span class="n">model</span> <span class="n">build_model</span><span class="p">(</span><span class="n">keras_tuner</span><span class="o">.</span><span class="n">HyperParameters</span><span class="p">())</span> </code></pre></div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code><Sequential name=sequential_1, built=False> </code></pre></div> </div> <p>As shown below, the hyperparameters are actual values. In fact, they are just functions returning actual values. For example, <code>hp.Int()</code> returns an <code>int</code> value. Therefore, you can put them into variables, for loops, or if conditions.</p> <div class="codehilite"><pre><span></span><code><span class="n">hp</span> <span class="o">=</span> <span class="n">keras_tuner</span><span class="o">.</span><span class="n">HyperParameters</span><span class="p">()</span> <span class="nb">print</span><span class="p">(</span><span class="n">hp</span><span class="o">.</span><span class="n">Int</span><span class="p">(</span><span class="s2">"units"</span><span class="p">,</span> <span class="n">min_value</span><span class="o">=</span><span class="mi">32</span><span class="p">,</span> <span class="n">max_value</span><span class="o">=</span><span class="mi">512</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">32</span><span class="p">))</span> </code></pre></div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>32 </code></pre></div> </div> <p>You can also define the hyperparameters in advance and keep your Keras code in a separate function.</p> <div class="codehilite"><pre><span></span><code><span class="k">def</span> <span class="nf">call_existing_code</span><span class="p">(</span><span class="n">units</span><span class="p">,</span> <span class="n">activation</span><span class="p">,</span> <span class="n">dropout</span><span class="p">,</span> <span class="n">lr</span><span class="p">):</span> <span class="n">model</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">Sequential</span><span class="p">()</span> <span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">layers</span><span class="o">.</span><span class="n">Flatten</span><span class="p">())</span> <span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">layers</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span><span class="n">units</span><span class="o">=</span><span class="n">units</span><span class="p">,</span> <span class="n">activation</span><span class="o">=</span><span class="n">activation</span><span class="p">))</span> <span class="k">if</span> <span class="n">dropout</span><span class="p">:</span> <span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">layers</span><span class="o">.</span><span class="n">Dropout</span><span class="p">(</span><span class="n">rate</span><span class="o">=</span><span class="mf">0.25</span><span class="p">))</span> <span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">layers</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="n">activation</span><span class="o">=</span><span class="s2">"softmax"</span><span class="p">))</span> <span class="n">model</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span> <span class="n">optimizer</span><span class="o">=</span><span class="n">keras</span><span class="o">.</span><span class="n">optimizers</span><span class="o">.</span><span class="n">Adam</span><span class="p">(</span><span class="n">learning_rate</span><span class="o">=</span><span class="n">lr</span><span class="p">),</span> <span class="n">loss</span><span class="o">=</span><span class="s2">"categorical_crossentropy"</span><span class="p">,</span> <span class="n">metrics</span><span class="o">=</span><span class="p">[</span><span class="s2">"accuracy"</span><span class="p">],</span> <span class="p">)</span> <span class="k">return</span> <span class="n">model</span> <span class="k">def</span> <span class="nf">build_model</span><span class="p">(</span><span class="n">hp</span><span class="p">):</span> <span class="n">units</span> <span class="o">=</span> <span class="n">hp</span><span class="o">.</span><span class="n">Int</span><span class="p">(</span><span class="s2">"units"</span><span class="p">,</span> <span class="n">min_value</span><span class="o">=</span><span class="mi">32</span><span class="p">,</span> <span class="n">max_value</span><span class="o">=</span><span class="mi">512</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">32</span><span class="p">)</span> <span class="n">activation</span> <span class="o">=</span> <span class="n">hp</span><span class="o">.</span><span class="n">Choice</span><span class="p">(</span><span class="s2">"activation"</span><span class="p">,</span> <span class="p">[</span><span class="s2">"relu"</span><span class="p">,</span> <span class="s2">"tanh"</span><span class="p">])</span> <span class="n">dropout</span> <span class="o">=</span> <span class="n">hp</span><span class="o">.</span><span class="n">Boolean</span><span class="p">(</span><span class="s2">"dropout"</span><span class="p">)</span> <span class="n">lr</span> <span class="o">=</span> <span class="n">hp</span><span class="o">.</span><span class="n">Float</span><span class="p">(</span><span class="s2">"lr"</span><span class="p">,</span> <span class="n">min_value</span><span class="o">=</span><span class="mf">1e-4</span><span class="p">,</span> <span class="n">max_value</span><span class="o">=</span><span class="mf">1e-2</span><span class="p">,</span> <span class="n">sampling</span><span class="o">=</span><span class="s2">"log"</span><span class="p">)</span> <span class="c1"># call existing model-building code with the hyperparameter values.</span> <span class="n">model</span> <span class="o">=</span> <span class="n">call_existing_code</span><span class="p">(</span> <span class="n">units</span><span class="o">=</span><span class="n">units</span><span class="p">,</span> <span class="n">activation</span><span class="o">=</span><span class="n">activation</span><span class="p">,</span> <span class="n">dropout</span><span class="o">=</span><span class="n">dropout</span><span class="p">,</span> <span class="n">lr</span><span class="o">=</span><span class="n">lr</span> <span class="p">)</span> <span class="k">return</span> <span class="n">model</span> <span class="n">build_model</span><span class="p">(</span><span class="n">keras_tuner</span><span class="o">.</span><span class="n">HyperParameters</span><span class="p">())</span> </code></pre></div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code><Sequential name=sequential_2, built=False> </code></pre></div> </div> <p>Each of the hyperparameters is uniquely identified by its name (the first argument). To tune the number of units in different <code>Dense</code> layers separately as different hyperparameters, we give them different names as <code>f"units_{i}"</code>.</p> <p>Notably, this is also an example of creating conditional hyperparameters. There are many hyperparameters specifying the number of units in the <code>Dense</code> layers. The number of such hyperparameters is decided by the number of layers, which is also a hyperparameter. Therefore, the total number of hyperparameters used may be different from trial to trial. Some hyperparameter is only used when a certain condition is satisfied. For example, <code>units_3</code> is only used when <code>num_layers</code> is larger than 3. With KerasTuner, you can easily define such hyperparameters dynamically while creating the model.</p> <div class="codehilite"><pre><span></span><code><span class="k">def</span> <span class="nf">build_model</span><span class="p">(</span><span class="n">hp</span><span class="p">):</span> <span class="n">model</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">Sequential</span><span class="p">()</span> <span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">layers</span><span class="o">.</span><span class="n">Flatten</span><span class="p">())</span> <span class="c1"># Tune the number of layers.</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">hp</span><span class="o">.</span><span class="n">Int</span><span class="p">(</span><span class="s2">"num_layers"</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">)):</span> <span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span> <span class="n">layers</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span> <span class="c1"># Tune number of units separately.</span> <span class="n">units</span><span class="o">=</span><span class="n">hp</span><span class="o">.</span><span class="n">Int</span><span class="p">(</span><span class="sa">f</span><span class="s2">"units_</span><span class="si">{</span><span class="n">i</span><span class="si">}</span><span class="s2">"</span><span class="p">,</span> <span class="n">min_value</span><span class="o">=</span><span class="mi">32</span><span class="p">,</span> <span class="n">max_value</span><span class="o">=</span><span class="mi">512</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">32</span><span class="p">),</span> <span class="n">activation</span><span class="o">=</span><span class="n">hp</span><span class="o">.</span><span class="n">Choice</span><span class="p">(</span><span class="s2">"activation"</span><span class="p">,</span> <span class="p">[</span><span class="s2">"relu"</span><span class="p">,</span> <span class="s2">"tanh"</span><span class="p">]),</span> <span class="p">)</span> <span class="p">)</span> <span class="k">if</span> <span class="n">hp</span><span class="o">.</span><span class="n">Boolean</span><span class="p">(</span><span class="s2">"dropout"</span><span class="p">):</span> <span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">layers</span><span class="o">.</span><span class="n">Dropout</span><span class="p">(</span><span class="n">rate</span><span class="o">=</span><span class="mf">0.25</span><span class="p">))</span> <span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">layers</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="n">activation</span><span class="o">=</span><span class="s2">"softmax"</span><span class="p">))</span> <span class="n">learning_rate</span> <span class="o">=</span> <span class="n">hp</span><span class="o">.</span><span class="n">Float</span><span class="p">(</span><span class="s2">"lr"</span><span class="p">,</span> <span class="n">min_value</span><span class="o">=</span><span class="mf">1e-4</span><span class="p">,</span> <span class="n">max_value</span><span class="o">=</span><span class="mf">1e-2</span><span class="p">,</span> <span class="n">sampling</span><span class="o">=</span><span class="s2">"log"</span><span class="p">)</span> <span class="n">model</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span> <span class="n">optimizer</span><span class="o">=</span><span class="n">keras</span><span class="o">.</span><span class="n">optimizers</span><span class="o">.</span><span class="n">Adam</span><span class="p">(</span><span class="n">learning_rate</span><span class="o">=</span><span class="n">learning_rate</span><span class="p">),</span> <span class="n">loss</span><span class="o">=</span><span class="s2">"categorical_crossentropy"</span><span class="p">,</span> <span class="n">metrics</span><span class="o">=</span><span class="p">[</span><span class="s2">"accuracy"</span><span class="p">],</span> <span class="p">)</span> <span class="k">return</span> <span class="n">model</span> <span class="n">build_model</span><span class="p">(</span><span class="n">keras_tuner</span><span class="o">.</span><span class="n">HyperParameters</span><span class="p">())</span> </code></pre></div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code><Sequential name=sequential_3, built=False> </code></pre></div> </div> <h3 id="start-the-search">Start the search</h3> <p>After defining the search space, we need to select a tuner class to run the search. You may choose from <code>RandomSearch</code>, <code>BayesianOptimization</code> and <code>Hyperband</code>, which correspond to different tuning algorithms. Here we use <code>RandomSearch</code> as an example.</p> <p>To initialize the tuner, we need to specify several arguments in the initializer.</p> <ul> <li><code>hypermodel</code>. The model-building function, which is <code>build_model</code> in our case.</li> <li><code>objective</code>. The name of the objective to optimize (whether to minimize or maximize is automatically inferred for built-in metrics). We will introduce how to use custom metrics later in this tutorial.</li> <li><code>max_trials</code>. The total number of trials to run during the search.</li> <li><code>executions_per_trial</code>. The number of models that should be built and fit for each trial. Different trials have different hyperparameter values. The executions within the same trial have the same hyperparameter values. The purpose of having multiple executions per trial is to reduce results variance and therefore be able to more accurately assess the performance of a model. If you want to get results faster, you could set <code>executions_per_trial=1</code> (single round of training for each model configuration).</li> <li><code>overwrite</code>. Control whether to overwrite the previous results in the same directory or resume the previous search instead. Here we set <code>overwrite=True</code> to start a new search and ignore any previous results.</li> <li><code>directory</code>. A path to a directory for storing the search results.</li> <li><code>project_name</code>. The name of the sub-directory in the <code>directory</code>.</li> </ul> <div class="codehilite"><pre><span></span><code><span class="n">tuner</span> <span class="o">=</span> <span class="n">keras_tuner</span><span class="o">.</span><span class="n">RandomSearch</span><span class="p">(</span> <span class="n">hypermodel</span><span class="o">=</span><span class="n">build_model</span><span class="p">,</span> <span class="n">objective</span><span class="o">=</span><span class="s2">"val_accuracy"</span><span class="p">,</span> <span class="n">max_trials</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">executions_per_trial</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">overwrite</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">directory</span><span class="o">=</span><span class="s2">"my_dir"</span><span class="p">,</span> <span class="n">project_name</span><span class="o">=</span><span class="s2">"helloworld"</span><span class="p">,</span> <span class="p">)</span> </code></pre></div> <p>You can print a summary of the search space:</p> <div class="codehilite"><pre><span></span><code><span class="n">tuner</span><span class="o">.</span><span class="n">search_space_summary</span><span class="p">()</span> </code></pre></div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Search space summary Default search space size: 5 num_layers (Int) {'default': None, 'conditions': [], 'min_value': 1, 'max_value': 3, 'step': 1, 'sampling': 'linear'} units_0 (Int) {'default': None, 'conditions': [], 'min_value': 32, 'max_value': 512, 'step': 32, 'sampling': 'linear'} activation (Choice) {'default': 'relu', 'conditions': [], 'values': ['relu', 'tanh'], 'ordered': False} dropout (Boolean) {'default': False, 'conditions': []} lr (Float) {'default': 0.0001, 'conditions': [], 'min_value': 0.0001, 'max_value': 0.01, 'step': None, 'sampling': 'log'} </code></pre></div> </div> <p>Before starting the search, let's prepare the MNIST dataset.</p> <div class="codehilite"><pre><span></span><code><span class="kn">import</span> <span class="nn">keras</span> <span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">),</span> <span class="p">(</span><span class="n">x_test</span><span class="p">,</span> <span class="n">y_test</span><span class="p">)</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">mnist</span><span class="o">.</span><span class="n">load_data</span><span class="p">()</span> <span class="n">x_train</span> <span class="o">=</span> <span class="n">x</span><span class="p">[:</span><span class="o">-</span><span class="mi">10000</span><span class="p">]</span> <span class="n">x_val</span> <span class="o">=</span> <span class="n">x</span><span class="p">[</span><span class="o">-</span><span class="mi">10000</span><span class="p">:]</span> <span class="n">y_train</span> <span class="o">=</span> <span class="n">y</span><span class="p">[:</span><span class="o">-</span><span class="mi">10000</span><span class="p">]</span> <span class="n">y_val</span> <span class="o">=</span> <span class="n">y</span><span class="p">[</span><span class="o">-</span><span class="mi">10000</span><span class="p">:]</span> <span class="n">x_train</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">expand_dims</span><span class="p">(</span><span class="n">x_train</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s2">"float32"</span><span class="p">)</span> <span class="o">/</span> <span class="mf">255.0</span> <span class="n">x_val</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">expand_dims</span><span class="p">(</span><span class="n">x_val</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s2">"float32"</span><span class="p">)</span> <span class="o">/</span> <span class="mf">255.0</span> <span class="n">x_test</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">expand_dims</span><span class="p">(</span><span class="n">x_test</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s2">"float32"</span><span class="p">)</span> <span class="o">/</span> <span class="mf">255.0</span> <span class="n">num_classes</span> <span class="o">=</span> <span class="mi">10</span> <span class="n">y_train</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">to_categorical</span><span class="p">(</span><span class="n">y_train</span><span class="p">,</span> <span class="n">num_classes</span><span class="p">)</span> <span class="n">y_val</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">to_categorical</span><span class="p">(</span><span class="n">y_val</span><span class="p">,</span> <span class="n">num_classes</span><span class="p">)</span> <span class="n">y_test</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">to_categorical</span><span class="p">(</span><span class="n">y_test</span><span class="p">,</span> <span class="n">num_classes</span><span class="p">)</span> </code></pre></div> <p>Then, start the search for the best hyperparameter configuration. All the arguments passed to <code>search</code> is passed to <code>model.fit()</code> in each execution. Remember to pass <code>validation_data</code> to evaluate the model.</p> <div class="codehilite"><pre><span></span><code><span class="n">tuner</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">x_train</span><span class="p">,</span> <span class="n">y_train</span><span class="p">,</span> <span class="n">epochs</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">validation_data</span><span class="o">=</span><span class="p">(</span><span class="n">x_val</span><span class="p">,</span> <span class="n">y_val</span><span class="p">))</span> </code></pre></div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Trial 3 Complete [00h 00m 19s] val_accuracy: 0.9665500223636627 </code></pre></div> </div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Best val_accuracy So Far: 0.9665500223636627 Total elapsed time: 00h 00m 40s </code></pre></div> </div> <p>During the <code>search</code>, the model-building function is called with different hyperparameter values in different trial. In each trial, the tuner would generate a new set of hyperparameter values to build the model. The model is then fit and evaluated. The metrics are recorded. The tuner progressively explores the space and finally finds a good set of hyperparameter values.</p> <h3 id="query-the-results">Query the results</h3> <p>When search is over, you can retrieve the best model(s). The model is saved at its best performing epoch evaluated on the <code>validation_data</code>.</p> <div class="codehilite"><pre><span></span><code><span class="c1"># Get the top 2 models.</span> <span class="n">models</span> <span class="o">=</span> <span class="n">tuner</span><span class="o">.</span><span class="n">get_best_models</span><span class="p">(</span><span class="n">num_models</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span> <span class="n">best_model</span> <span class="o">=</span> <span class="n">models</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="n">best_model</span><span class="o">.</span><span class="n">summary</span><span class="p">()</span> </code></pre></div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>/usr/local/python/3.10.13/lib/python3.10/site-packages/keras/src/saving/saving_lib.py:388: UserWarning: Skipping variable loading for optimizer 'adam', because it has 2 variables whereas the saved optimizer has 18 variables. trackable.load_own_variables(weights_store.get(inner_path)) /usr/local/python/3.10.13/lib/python3.10/site-packages/keras/src/saving/saving_lib.py:388: UserWarning: Skipping variable loading for optimizer 'adam', because it has 2 variables whereas the saved optimizer has 10 variables. trackable.load_own_variables(weights_store.get(inner_path)) </code></pre></div> </div> <pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><span style="font-weight: bold">Model: "sequential"</span> </pre> <pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓ ┃<span style="font-weight: bold"> Layer (type) </span>┃<span style="font-weight: bold"> Output Shape </span>┃<span style="font-weight: bold"> Param # </span>┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩ │ flatten (<span style="color: #0087ff; text-decoration-color: #0087ff">Flatten</span>) │ (<span style="color: #00af00; text-decoration-color: #00af00">32</span>, <span style="color: #00af00; text-decoration-color: #00af00">784</span>) │ <span style="color: #00af00; text-decoration-color: #00af00">0</span> │ ├─────────────────────────────────┼───────────────────────────┼────────────┤ │ dense (<span style="color: #0087ff; text-decoration-color: #0087ff">Dense</span>) │ (<span style="color: #00af00; text-decoration-color: #00af00">32</span>, <span style="color: #00af00; text-decoration-color: #00af00">416</span>) │ <span style="color: #00af00; text-decoration-color: #00af00">326,560</span> │ ├─────────────────────────────────┼───────────────────────────┼────────────┤ │ dense_1 (<span style="color: #0087ff; text-decoration-color: #0087ff">Dense</span>) │ (<span style="color: #00af00; text-decoration-color: #00af00">32</span>, <span style="color: #00af00; text-decoration-color: #00af00">512</span>) │ <span style="color: #00af00; text-decoration-color: #00af00">213,504</span> │ ├─────────────────────────────────┼───────────────────────────┼────────────┤ │ dense_2 (<span style="color: #0087ff; text-decoration-color: #0087ff">Dense</span>) │ (<span style="color: #00af00; text-decoration-color: #00af00">32</span>, <span style="color: #00af00; text-decoration-color: #00af00">32</span>) │ <span style="color: #00af00; text-decoration-color: #00af00">16,416</span> │ ├─────────────────────────────────┼───────────────────────────┼────────────┤ │ dropout (<span style="color: #0087ff; text-decoration-color: #0087ff">Dropout</span>) │ (<span style="color: #00af00; text-decoration-color: #00af00">32</span>, <span style="color: #00af00; text-decoration-color: #00af00">32</span>) │ <span style="color: #00af00; text-decoration-color: #00af00">0</span> │ ├─────────────────────────────────┼───────────────────────────┼────────────┤ │ dense_3 (<span style="color: #0087ff; text-decoration-color: #0087ff">Dense</span>) │ (<span style="color: #00af00; text-decoration-color: #00af00">32</span>, <span style="color: #00af00; text-decoration-color: #00af00">10</span>) │ <span style="color: #00af00; text-decoration-color: #00af00">330</span> │ └─────────────────────────────────┴───────────────────────────┴────────────┘ </pre> <pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><span style="font-weight: bold"> Total params: </span><span style="color: #00af00; text-decoration-color: #00af00">556,810</span> (2.12 MB) </pre> <pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><span style="font-weight: bold"> Trainable params: </span><span style="color: #00af00; text-decoration-color: #00af00">556,810</span> (2.12 MB) </pre> <pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><span style="font-weight: bold"> Non-trainable params: </span><span style="color: #00af00; text-decoration-color: #00af00">0</span> (0.00 B) </pre> <p>You can also print a summary of the search results.</p> <div class="codehilite"><pre><span></span><code><span class="n">tuner</span><span class="o">.</span><span class="n">results_summary</span><span class="p">()</span> </code></pre></div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Results summary Results in my_dir/helloworld Showing 10 best trials Objective(name="val_accuracy", direction="max") </code></pre></div> </div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Trial 2 summary Hyperparameters: num_layers: 3 units_0: 416 activation: relu dropout: True lr: 0.0001324166048504802 units_1: 512 units_2: 32 Score: 0.9665500223636627 </code></pre></div> </div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Trial 0 summary Hyperparameters: num_layers: 1 units_0: 128 activation: tanh dropout: False lr: 0.001425162921397599 Score: 0.9623999893665314 </code></pre></div> </div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Trial 1 summary Hyperparameters: num_layers: 2 units_0: 512 activation: tanh dropout: True lr: 0.0010584293918512798 units_1: 32 Score: 0.9606499969959259 </code></pre></div> </div> <p>You will find detailed logs, checkpoints, etc, in the folder <code>my_dir/helloworld</code>, i.e. <code>directory/project_name</code>.</p> <p>You can also visualize the tuning results using TensorBoard and HParams plugin. For more information, please following <a href="https://keras.io/guides/keras_tuner/visualize_tuning/">this link</a>.</p> <h3 id="retrain-the-model">Retrain the model</h3> <p>If you want to train the model with the entire dataset, you may retrieve the best hyperparameters and retrain the model by yourself.</p> <div class="codehilite"><pre><span></span><code><span class="c1"># Get the top 2 hyperparameters.</span> <span class="n">best_hps</span> <span class="o">=</span> <span class="n">tuner</span><span class="o">.</span><span class="n">get_best_hyperparameters</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> <span class="c1"># Build the model with the best hp.</span> <span class="n">model</span> <span class="o">=</span> <span class="n">build_model</span><span class="p">(</span><span class="n">best_hps</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="c1"># Fit with the entire dataset.</span> <span class="n">x_all</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">((</span><span class="n">x_train</span><span class="p">,</span> <span class="n">x_val</span><span class="p">))</span> <span class="n">y_all</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">((</span><span class="n">y_train</span><span class="p">,</span> <span class="n">y_val</span><span class="p">))</span> <span class="n">model</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="n">x_all</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="n">y_all</span><span class="p">,</span> <span class="n">epochs</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> </code></pre></div> <div class="codehilite"><pre><span></span><code>1/1875 [37m━━━━━━━━━━━━━━━━━━━━ 17:57 575ms/step - accuracy: 0.1250 - loss: 2.3113 </code></pre></div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>29/1875 [37m━━━━━━━━━━━━━━━━━━━━ 3s 2ms/step - accuracy: 0.1753 - loss: 2.2296 </p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>63/1875 [37m━━━━━━━━━━━━━━━━━━━━ 3s 2ms/step - accuracy: 0.2626 - loss: 2.1206</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>96/1875 ━[37m━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.3252 - loss: 2.0103</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>130/1875 ━[37m━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.3745 - loss: 1.9041</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>164/1875 ━[37m━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.4139 - loss: 1.8094</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>199/1875 ━━[37m━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.4470 - loss: 1.7246</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>235/1875 ━━[37m━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.4752 - loss: 1.6493</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>270/1875 ━━[37m━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.4982 - loss: 1.5857</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>305/1875 ━━━[37m━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.5182 - loss: 1.5293</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>339/1875 ━━━[37m━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.5354 - loss: 1.4800</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>374/1875 ━━━[37m━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.5513 - loss: 1.4340</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>409/1875 ━━━━[37m━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.5656 - loss: 1.3924</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>444/1875 ━━━━[37m━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.5785 - loss: 1.3545</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>478/1875 ━━━━━[37m━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.5899 - loss: 1.3208</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>513/1875 ━━━━━[37m━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.6006 - loss: 1.2887</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>548/1875 ━━━━━[37m━━━━━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.6104 - loss: 1.2592</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>583/1875 ━━━━━━[37m━━━━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.6195 - loss: 1.2318</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>618/1875 ━━━━━━[37m━━━━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.6279 - loss: 1.2063</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>653/1875 ━━━━━━[37m━━━━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.6358 - loss: 1.1823</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>688/1875 ━━━━━━━[37m━━━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.6431 - loss: 1.1598</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>723/1875 ━━━━━━━[37m━━━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.6500 - loss: 1.1387</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>758/1875 ━━━━━━━━[37m━━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.6564 - loss: 1.1189</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>793/1875 ━━━━━━━━[37m━━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.6625 - loss: 1.1002</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>828/1875 ━━━━━━━━[37m━━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.6682 - loss: 1.0826</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>863/1875 ━━━━━━━━━[37m━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.6736 - loss: 1.0658</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>899/1875 ━━━━━━━━━[37m━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.6788 - loss: 1.0495</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>935/1875 ━━━━━━━━━[37m━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.6838 - loss: 1.0339</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>970/1875 ━━━━━━━━━━[37m━━━━━━━━━━ 1s 1ms/step - accuracy: 0.6885 - loss: 1.0195</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1005/1875 ━━━━━━━━━━[37m━━━━━━━━━━ 1s 1ms/step - accuracy: 0.6929 - loss: 1.0058</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1041/1875 ━━━━━━━━━━━[37m━━━━━━━━━ 1s 1ms/step - accuracy: 0.6972 - loss: 0.9923</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1076/1875 ━━━━━━━━━━━[37m━━━━━━━━━ 1s 1ms/step - accuracy: 0.7012 - loss: 0.9798</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1111/1875 ━━━━━━━━━━━[37m━━━━━━━━━ 1s 1ms/step - accuracy: 0.7051 - loss: 0.9677</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1146/1875 ━━━━━━━━━━━━[37m━━━━━━━━ 1s 1ms/step - accuracy: 0.7088 - loss: 0.9561</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1182/1875 ━━━━━━━━━━━━[37m━━━━━━━━ 1s 1ms/step - accuracy: 0.7124 - loss: 0.9446</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1218/1875 ━━━━━━━━━━━━[37m━━━━━━━━ 0s 1ms/step - accuracy: 0.7159 - loss: 0.9336</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1254/1875 ━━━━━━━━━━━━━[37m━━━━━━━ 0s 1ms/step - accuracy: 0.7193 - loss: 0.9230</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1289/1875 ━━━━━━━━━━━━━[37m━━━━━━━ 0s 1ms/step - accuracy: 0.7225 - loss: 0.9131</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1324/1875 ━━━━━━━━━━━━━━[37m━━━━━━ 0s 1ms/step - accuracy: 0.7255 - loss: 0.9035</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1359/1875 ━━━━━━━━━━━━━━[37m━━━━━━ 0s 1ms/step - accuracy: 0.7284 - loss: 0.8943</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1394/1875 ━━━━━━━━━━━━━━[37m━━━━━━ 0s 1ms/step - accuracy: 0.7313 - loss: 0.8853</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1429/1875 ━━━━━━━━━━━━━━━[37m━━━━━ 0s 1ms/step - accuracy: 0.7341 - loss: 0.8767</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1465/1875 ━━━━━━━━━━━━━━━[37m━━━━━ 0s 1ms/step - accuracy: 0.7368 - loss: 0.8680</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1500/1875 ━━━━━━━━━━━━━━━━[37m━━━━ 0s 1ms/step - accuracy: 0.7394 - loss: 0.8599</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1535/1875 ━━━━━━━━━━━━━━━━[37m━━━━ 0s 1ms/step - accuracy: 0.7419 - loss: 0.8520</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1570/1875 ━━━━━━━━━━━━━━━━[37m━━━━ 0s 1ms/step - accuracy: 0.7443 - loss: 0.8444</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1605/1875 ━━━━━━━━━━━━━━━━━[37m━━━ 0s 1ms/step - accuracy: 0.7467 - loss: 0.8370</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1639/1875 ━━━━━━━━━━━━━━━━━[37m━━━ 0s 1ms/step - accuracy: 0.7489 - loss: 0.8299</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1674/1875 ━━━━━━━━━━━━━━━━━[37m━━━ 0s 1ms/step - accuracy: 0.7511 - loss: 0.8229</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1707/1875 ━━━━━━━━━━━━━━━━━━[37m━━ 0s 1ms/step - accuracy: 0.7532 - loss: 0.8164</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1741/1875 ━━━━━━━━━━━━━━━━━━[37m━━ 0s 1ms/step - accuracy: 0.7552 - loss: 0.8099</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1774/1875 ━━━━━━━━━━━━━━━━━━[37m━━ 0s 1ms/step - accuracy: 0.7572 - loss: 0.8038</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1809/1875 ━━━━━━━━━━━━━━━━━━━[37m━ 0s 1ms/step - accuracy: 0.7592 - loss: 0.7975</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1843/1875 ━━━━━━━━━━━━━━━━━━━[37m━ 0s 1ms/step - accuracy: 0.7611 - loss: 0.7915</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1875/1875 ━━━━━━━━━━━━━━━━━━━━ 3s 1ms/step - accuracy: 0.7629 - loss: 0.7858</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code><keras.src.callbacks.history.History at 0x7f31883d9e10> </code></pre></div> </div> <hr /> <h2 id="tune-model-training">Tune model training</h2> <p>To tune the model building process, we need to subclass the <code>HyperModel</code> class, which also makes it easy to share and reuse hypermodels.</p> <p>We need to override <code>HyperModel.build()</code> and <code>HyperModel.fit()</code> to tune the model building and training process respectively. A <code>HyperModel.build()</code> method is the same as the model-building function, which creates a Keras model using the hyperparameters and returns it.</p> <p>In <code>HyperModel.fit()</code>, you can access the model returned by <code>HyperModel.build()</code>,<code>hp</code> and all the arguments passed to <code>search()</code>. You need to train the model and return the training history.</p> <p>In the following code, we will tune the <code>shuffle</code> argument in <code>model.fit()</code>.</p> <p>It is generally not needed to tune the number of epochs because a built-in callback is passed to <code>model.fit()</code> to save the model at its best epoch evaluated by the <code>validation_data</code>.</p> <blockquote> <p><strong>Note</strong>: The <code>**kwargs</code> should always be passed to <code>model.fit()</code> because it contains the callbacks for model saving and tensorboard plugins.</p> </blockquote> <div class="codehilite"><pre><span></span><code><span class="k">class</span> <span class="nc">MyHyperModel</span><span class="p">(</span><span class="n">keras_tuner</span><span class="o">.</span><span class="n">HyperModel</span><span class="p">):</span> <span class="k">def</span> <span class="nf">build</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">hp</span><span class="p">):</span> <span class="n">model</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">Sequential</span><span class="p">()</span> <span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">layers</span><span class="o">.</span><span class="n">Flatten</span><span class="p">())</span> <span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span> <span class="n">layers</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span> <span class="n">units</span><span class="o">=</span><span class="n">hp</span><span class="o">.</span><span class="n">Int</span><span class="p">(</span><span class="s2">"units"</span><span class="p">,</span> <span class="n">min_value</span><span class="o">=</span><span class="mi">32</span><span class="p">,</span> <span class="n">max_value</span><span class="o">=</span><span class="mi">512</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">32</span><span class="p">),</span> <span class="n">activation</span><span class="o">=</span><span class="s2">"relu"</span><span class="p">,</span> <span class="p">)</span> <span class="p">)</span> <span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">layers</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="n">activation</span><span class="o">=</span><span class="s2">"softmax"</span><span class="p">))</span> <span class="n">model</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span> <span class="n">optimizer</span><span class="o">=</span><span class="s2">"adam"</span><span class="p">,</span> <span class="n">loss</span><span class="o">=</span><span class="s2">"categorical_crossentropy"</span><span class="p">,</span> <span class="n">metrics</span><span class="o">=</span><span class="p">[</span><span class="s2">"accuracy"</span><span class="p">],</span> <span class="p">)</span> <span class="k">return</span> <span class="n">model</span> <span class="k">def</span> <span class="nf">fit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">hp</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span> <span class="k">return</span> <span class="n">model</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="c1"># Tune whether to shuffle the data in each epoch.</span> <span class="n">shuffle</span><span class="o">=</span><span class="n">hp</span><span class="o">.</span><span class="n">Boolean</span><span class="p">(</span><span class="s2">"shuffle"</span><span class="p">),</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">,</span> <span class="p">)</span> </code></pre></div> <p>Again, we can do a quick check to see if the code works correctly.</p> <div class="codehilite"><pre><span></span><code><span class="n">hp</span> <span class="o">=</span> <span class="n">keras_tuner</span><span class="o">.</span><span class="n">HyperParameters</span><span class="p">()</span> <span class="n">hypermodel</span> <span class="o">=</span> <span class="n">MyHyperModel</span><span class="p">()</span> <span class="n">model</span> <span class="o">=</span> <span class="n">hypermodel</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="n">hp</span><span class="p">)</span> <span class="n">hypermodel</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">hp</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="mi">28</span><span class="p">,</span> <span class="mi">28</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="mi">10</span><span class="p">))</span> </code></pre></div> <p>1/4 ━━━━━[37m━━━━━━━━━━━━━━━ 0s 279ms/step - accuracy: 0.0000e+00 - loss: 12.2230</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>4/4 ━━━━━━━━━━━━━━━━━━━━ 0s 108ms/step - accuracy: 0.0679 - loss: 11.9568 </p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>4/4 ━━━━━━━━━━━━━━━━━━━━ 1s 109ms/step - accuracy: 0.0763 - loss: 11.8941</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code><keras.src.callbacks.history.History at 0x7f318865c100> </code></pre></div> </div> <hr /> <h2 id="tune-data-preprocessing">Tune data preprocessing</h2> <p>To tune data preprocessing, we just add an additional step in <code>HyperModel.fit()</code>, where we can access the dataset from the arguments. In the following code, we tune whether to normalize the data before training the model. This time we explicitly put <code>x</code> and <code>y</code> in the function signature because we need to use them.</p> <div class="codehilite"><pre><span></span><code><span class="k">class</span> <span class="nc">MyHyperModel</span><span class="p">(</span><span class="n">keras_tuner</span><span class="o">.</span><span class="n">HyperModel</span><span class="p">):</span> <span class="k">def</span> <span class="nf">build</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">hp</span><span class="p">):</span> <span class="n">model</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">Sequential</span><span class="p">()</span> <span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">layers</span><span class="o">.</span><span class="n">Flatten</span><span class="p">())</span> <span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span> <span class="n">layers</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span> <span class="n">units</span><span class="o">=</span><span class="n">hp</span><span class="o">.</span><span class="n">Int</span><span class="p">(</span><span class="s2">"units"</span><span class="p">,</span> <span class="n">min_value</span><span class="o">=</span><span class="mi">32</span><span class="p">,</span> <span class="n">max_value</span><span class="o">=</span><span class="mi">512</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">32</span><span class="p">),</span> <span class="n">activation</span><span class="o">=</span><span class="s2">"relu"</span><span class="p">,</span> <span class="p">)</span> <span class="p">)</span> <span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">layers</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="n">activation</span><span class="o">=</span><span class="s2">"softmax"</span><span class="p">))</span> <span class="n">model</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span> <span class="n">optimizer</span><span class="o">=</span><span class="s2">"adam"</span><span class="p">,</span> <span class="n">loss</span><span class="o">=</span><span class="s2">"categorical_crossentropy"</span><span class="p">,</span> <span class="n">metrics</span><span class="o">=</span><span class="p">[</span><span class="s2">"accuracy"</span><span class="p">],</span> <span class="p">)</span> <span class="k">return</span> <span class="n">model</span> <span class="k">def</span> <span class="nf">fit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">hp</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span> <span class="k">if</span> <span class="n">hp</span><span class="o">.</span><span class="n">Boolean</span><span class="p">(</span><span class="s2">"normalize"</span><span class="p">):</span> <span class="n">x</span> <span class="o">=</span> <span class="n">layers</span><span class="o">.</span><span class="n">Normalization</span><span class="p">()(</span><span class="n">x</span><span class="p">)</span> <span class="k">return</span> <span class="n">model</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="c1"># Tune whether to shuffle the data in each epoch.</span> <span class="n">shuffle</span><span class="o">=</span><span class="n">hp</span><span class="o">.</span><span class="n">Boolean</span><span class="p">(</span><span class="s2">"shuffle"</span><span class="p">),</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">,</span> <span class="p">)</span> <span class="n">hp</span> <span class="o">=</span> <span class="n">keras_tuner</span><span class="o">.</span><span class="n">HyperParameters</span><span class="p">()</span> <span class="n">hypermodel</span> <span class="o">=</span> <span class="n">MyHyperModel</span><span class="p">()</span> <span class="n">model</span> <span class="o">=</span> <span class="n">hypermodel</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="n">hp</span><span class="p">)</span> <span class="n">hypermodel</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">hp</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="mi">28</span><span class="p">,</span> <span class="mi">28</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="mi">10</span><span class="p">))</span> </code></pre></div> <p>1/4 ━━━━━[37m━━━━━━━━━━━━━━━ 0s 276ms/step - accuracy: 0.1250 - loss: 12.0090</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>4/4 ━━━━━━━━━━━━━━━━━━━━ 0s 94ms/step - accuracy: 0.0994 - loss: 12.1242 </p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>4/4 ━━━━━━━━━━━━━━━━━━━━ 1s 95ms/step - accuracy: 0.0955 - loss: 12.1594</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code><keras.src.callbacks.history.History at 0x7f31ba836200> </code></pre></div> </div> <p>If a hyperparameter is used both in <code>build()</code> and <code>fit()</code>, you can define it in <code>build()</code> and use <code>hp.get(hp_name)</code> to retrieve it in <code>fit()</code>. We use the image size as an example. It is both used as the input shape in <code>build()</code>, and used by data prerprocessing step to crop the images in <code>fit()</code>.</p> <div class="codehilite"><pre><span></span><code><span class="k">class</span> <span class="nc">MyHyperModel</span><span class="p">(</span><span class="n">keras_tuner</span><span class="o">.</span><span class="n">HyperModel</span><span class="p">):</span> <span class="k">def</span> <span class="nf">build</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">hp</span><span class="p">):</span> <span class="n">image_size</span> <span class="o">=</span> <span class="n">hp</span><span class="o">.</span><span class="n">Int</span><span class="p">(</span><span class="s2">"image_size"</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">28</span><span class="p">)</span> <span class="n">inputs</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">Input</span><span class="p">(</span><span class="n">shape</span><span class="o">=</span><span class="p">(</span><span class="n">image_size</span><span class="p">,</span> <span class="n">image_size</span><span class="p">))</span> <span class="n">outputs</span> <span class="o">=</span> <span class="n">layers</span><span class="o">.</span><span class="n">Flatten</span><span class="p">()(</span><span class="n">inputs</span><span class="p">)</span> <span class="n">outputs</span> <span class="o">=</span> <span class="n">layers</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span> <span class="n">units</span><span class="o">=</span><span class="n">hp</span><span class="o">.</span><span class="n">Int</span><span class="p">(</span><span class="s2">"units"</span><span class="p">,</span> <span class="n">min_value</span><span class="o">=</span><span class="mi">32</span><span class="p">,</span> <span class="n">max_value</span><span class="o">=</span><span class="mi">512</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">32</span><span class="p">),</span> <span class="n">activation</span><span class="o">=</span><span class="s2">"relu"</span><span class="p">,</span> <span class="p">)(</span><span class="n">outputs</span><span class="p">)</span> <span class="n">outputs</span> <span class="o">=</span> <span class="n">layers</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="n">activation</span><span class="o">=</span><span class="s2">"softmax"</span><span class="p">)(</span><span class="n">outputs</span><span class="p">)</span> <span class="n">model</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">Model</span><span class="p">(</span><span class="n">inputs</span><span class="p">,</span> <span class="n">outputs</span><span class="p">)</span> <span class="n">model</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span> <span class="n">optimizer</span><span class="o">=</span><span class="s2">"adam"</span><span class="p">,</span> <span class="n">loss</span><span class="o">=</span><span class="s2">"categorical_crossentropy"</span><span class="p">,</span> <span class="n">metrics</span><span class="o">=</span><span class="p">[</span><span class="s2">"accuracy"</span><span class="p">],</span> <span class="p">)</span> <span class="k">return</span> <span class="n">model</span> <span class="k">def</span> <span class="nf">fit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">hp</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">validation_data</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span> <span class="k">if</span> <span class="n">hp</span><span class="o">.</span><span class="n">Boolean</span><span class="p">(</span><span class="s2">"normalize"</span><span class="p">):</span> <span class="n">x</span> <span class="o">=</span> <span class="n">layers</span><span class="o">.</span><span class="n">Normalization</span><span class="p">()(</span><span class="n">x</span><span class="p">)</span> <span class="n">image_size</span> <span class="o">=</span> <span class="n">hp</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"image_size"</span><span class="p">)</span> <span class="n">cropped_x</span> <span class="o">=</span> <span class="n">x</span><span class="p">[:,</span> <span class="p">:</span><span class="n">image_size</span><span class="p">,</span> <span class="p">:</span><span class="n">image_size</span><span class="p">,</span> <span class="p">:]</span> <span class="k">if</span> <span class="n">validation_data</span><span class="p">:</span> <span class="n">x_val</span><span class="p">,</span> <span class="n">y_val</span> <span class="o">=</span> <span class="n">validation_data</span> <span class="n">cropped_x_val</span> <span class="o">=</span> <span class="n">x_val</span><span class="p">[:,</span> <span class="p">:</span><span class="n">image_size</span><span class="p">,</span> <span class="p">:</span><span class="n">image_size</span><span class="p">,</span> <span class="p">:]</span> <span class="n">validation_data</span> <span class="o">=</span> <span class="p">(</span><span class="n">cropped_x_val</span><span class="p">,</span> <span class="n">y_val</span><span class="p">)</span> <span class="k">return</span> <span class="n">model</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span> <span class="n">cropped_x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="c1"># Tune whether to shuffle the data in each epoch.</span> <span class="n">shuffle</span><span class="o">=</span><span class="n">hp</span><span class="o">.</span><span class="n">Boolean</span><span class="p">(</span><span class="s2">"shuffle"</span><span class="p">),</span> <span class="n">validation_data</span><span class="o">=</span><span class="n">validation_data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">,</span> <span class="p">)</span> <span class="n">tuner</span> <span class="o">=</span> <span class="n">keras_tuner</span><span class="o">.</span><span class="n">RandomSearch</span><span class="p">(</span> <span class="n">MyHyperModel</span><span class="p">(),</span> <span class="n">objective</span><span class="o">=</span><span class="s2">"val_accuracy"</span><span class="p">,</span> <span class="n">max_trials</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">overwrite</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">directory</span><span class="o">=</span><span class="s2">"my_dir"</span><span class="p">,</span> <span class="n">project_name</span><span class="o">=</span><span class="s2">"tune_hypermodel"</span><span class="p">,</span> <span class="p">)</span> <span class="n">tuner</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">x_train</span><span class="p">,</span> <span class="n">y_train</span><span class="p">,</span> <span class="n">epochs</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">validation_data</span><span class="o">=</span><span class="p">(</span><span class="n">x_val</span><span class="p">,</span> <span class="n">y_val</span><span class="p">))</span> </code></pre></div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Trial 3 Complete [00h 00m 04s] val_accuracy: 0.9567000269889832 </code></pre></div> </div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Best val_accuracy So Far: 0.9685999751091003 Total elapsed time: 00h 00m 13s </code></pre></div> </div> <h3 id="retrain-the-model">Retrain the model</h3> <p>Using <code>HyperModel</code> also allows you to retrain the best model by yourself.</p> <div class="codehilite"><pre><span></span><code><span class="n">hypermodel</span> <span class="o">=</span> <span class="n">MyHyperModel</span><span class="p">()</span> <span class="n">best_hp</span> <span class="o">=</span> <span class="n">tuner</span><span class="o">.</span><span class="n">get_best_hyperparameters</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span> <span class="n">model</span> <span class="o">=</span> <span class="n">hypermodel</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="n">best_hp</span><span class="p">)</span> <span class="n">hypermodel</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">best_hp</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">x_all</span><span class="p">,</span> <span class="n">y_all</span><span class="p">,</span> <span class="n">epochs</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> </code></pre></div> <div class="codehilite"><pre><span></span><code>1/1875 [37m━━━━━━━━━━━━━━━━━━━━ 9:00 289ms/step - accuracy: 0.0000e+00 - loss: 2.4352 </code></pre></div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>52/1875 [37m━━━━━━━━━━━━━━━━━━━━ 1s 996us/step - accuracy: 0.6035 - loss: 1.3521 </p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>110/1875 ━[37m━━━━━━━━━━━━━━━━━━━ 1s 925us/step - accuracy: 0.7037 - loss: 1.0231</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>171/1875 ━[37m━━━━━━━━━━━━━━━━━━━ 1s 890us/step - accuracy: 0.7522 - loss: 0.8572</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>231/1875 ━━[37m━━━━━━━━━━━━━━━━━━ 1s 877us/step - accuracy: 0.7804 - loss: 0.7590</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>291/1875 ━━━[37m━━━━━━━━━━━━━━━━━ 1s 870us/step - accuracy: 0.7993 - loss: 0.6932</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>350/1875 ━━━[37m━━━━━━━━━━━━━━━━━ 1s 867us/step - accuracy: 0.8127 - loss: 0.6467</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>413/1875 ━━━━[37m━━━━━━━━━━━━━━━━ 1s 856us/step - accuracy: 0.8238 - loss: 0.6079</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>476/1875 ━━━━━[37m━━━━━━━━━━━━━━━ 1s 848us/step - accuracy: 0.8326 - loss: 0.5774</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>535/1875 ━━━━━[37m━━━━━━━━━━━━━━━ 1s 849us/step - accuracy: 0.8394 - loss: 0.5536</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>600/1875 ━━━━━━[37m━━━━━━━━━━━━━━ 1s 841us/step - accuracy: 0.8458 - loss: 0.5309</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>661/1875 ━━━━━━━[37m━━━━━━━━━━━━━ 1s 840us/step - accuracy: 0.8511 - loss: 0.5123</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>723/1875 ━━━━━━━[37m━━━━━━━━━━━━━ 0s 837us/step - accuracy: 0.8559 - loss: 0.4955</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>783/1875 ━━━━━━━━[37m━━━━━━━━━━━━ 0s 838us/step - accuracy: 0.8600 - loss: 0.4811</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>847/1875 ━━━━━━━━━[37m━━━━━━━━━━━ 0s 834us/step - accuracy: 0.8640 - loss: 0.4671</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>912/1875 ━━━━━━━━━[37m━━━━━━━━━━━ 0s 830us/step - accuracy: 0.8677 - loss: 0.4544</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>976/1875 ━━━━━━━━━━[37m━━━━━━━━━━ 0s 827us/step - accuracy: 0.8709 - loss: 0.4429</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1040/1875 ━━━━━━━━━━━[37m━━━━━━━━━ 0s 825us/step - accuracy: 0.8738 - loss: 0.4325</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1104/1875 ━━━━━━━━━━━[37m━━━━━━━━━ 0s 822us/step - accuracy: 0.8766 - loss: 0.4229</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1168/1875 ━━━━━━━━━━━━[37m━━━━━━━━ 0s 821us/step - accuracy: 0.8791 - loss: 0.4140</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1233/1875 ━━━━━━━━━━━━━[37m━━━━━━━ 0s 818us/step - accuracy: 0.8815 - loss: 0.4056</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1296/1875 ━━━━━━━━━━━━━[37m━━━━━━━ 0s 817us/step - accuracy: 0.8837 - loss: 0.3980</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1361/1875 ━━━━━━━━━━━━━━[37m━━━━━━ 0s 815us/step - accuracy: 0.8858 - loss: 0.3907</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1424/1875 ━━━━━━━━━━━━━━━[37m━━━━━ 0s 814us/step - accuracy: 0.8877 - loss: 0.3840</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1488/1875 ━━━━━━━━━━━━━━━[37m━━━━━ 0s 813us/step - accuracy: 0.8895 - loss: 0.3776</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1550/1875 ━━━━━━━━━━━━━━━━[37m━━━━ 0s 813us/step - accuracy: 0.8912 - loss: 0.3718</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1613/1875 ━━━━━━━━━━━━━━━━━[37m━━━ 0s 813us/step - accuracy: 0.8928 - loss: 0.3662</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1678/1875 ━━━━━━━━━━━━━━━━━[37m━━━ 0s 811us/step - accuracy: 0.8944 - loss: 0.3607</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1744/1875 ━━━━━━━━━━━━━━━━━━[37m━━ 0s 809us/step - accuracy: 0.8959 - loss: 0.3555</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1810/1875 ━━━━━━━━━━━━━━━━━━━[37m━ 0s 808us/step - accuracy: 0.8973 - loss: 0.3504</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1874/1875 ━━━━━━━━━━━━━━━━━━━[37m━ 0s 807us/step - accuracy: 0.8987 - loss: 0.3457</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1875/1875 ━━━━━━━━━━━━━━━━━━━━ 2s 808us/step - accuracy: 0.8987 - loss: 0.3456</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code><keras.src.callbacks.history.History at 0x7f31884b3070> </code></pre></div> </div> <hr /> <h2 id="specify-the-tuning-objective">Specify the tuning objective</h2> <p>In all previous examples, we all just used validation accuracy (<code>"val_accuracy"</code>) as the tuning objective to select the best model. Actually, you can use any metric as the objective. The most commonly used metric is <code>"val_loss"</code>, which is the validation loss.</p> <h3 id="builtin-metric-as-the-objective">Built-in metric as the objective</h3> <p>There are many other built-in metrics in Keras you can use as the objective. Here is <a href="https://keras.io/api/metrics/">a list of the built-in metrics</a>.</p> <p>To use a built-in metric as the objective, you need to follow these steps:</p> <ul> <li>Compile the model with the the built-in metric. For example, you want to use <code>MeanAbsoluteError()</code>. You need to compile the model with <code>metrics=[MeanAbsoluteError()]</code>. You may also use its name string instead: <code>metrics=["mean_absolute_error"]</code>. The name string of the metric is always the snake case of the class name.</li> </ul> <ul> <li>Identify the objective name string. The name string of the objective is always in the format of <code>f"val_{metric_name_string}"</code>. For example, the objective name string of mean squared error evaluated on the validation data should be <code>"val_mean_absolute_error"</code>.</li> </ul> <ul> <li>Wrap it into <a href="/api/keras_tuner/tuners/objective#objective-class"><code>keras_tuner.Objective</code></a>. We usually need to wrap the objective into a <a href="/api/keras_tuner/tuners/objective#objective-class"><code>keras_tuner.Objective</code></a> object to specify the direction to optimize the objective. For example, we want to minimize the mean squared error, we can use <code>keras_tuner.Objective("val_mean_absolute_error", "min")</code>. The direction should be either <code>"min"</code> or <code>"max"</code>.</li> </ul> <ul> <li>Pass the wrapped objective to the tuner.</li> </ul> <p>You can see the following barebone code example.</p> <div class="codehilite"><pre><span></span><code><span class="k">def</span> <span class="nf">build_regressor</span><span class="p">(</span><span class="n">hp</span><span class="p">):</span> <span class="n">model</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">Sequential</span><span class="p">(</span> <span class="p">[</span> <span class="n">layers</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span><span class="n">units</span><span class="o">=</span><span class="n">hp</span><span class="o">.</span><span class="n">Int</span><span class="p">(</span><span class="s2">"units"</span><span class="p">,</span> <span class="mi">32</span><span class="p">,</span> <span class="mi">128</span><span class="p">,</span> <span class="mi">32</span><span class="p">),</span> <span class="n">activation</span><span class="o">=</span><span class="s2">"relu"</span><span class="p">),</span> <span class="n">layers</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span><span class="n">units</span><span class="o">=</span><span class="mi">1</span><span class="p">),</span> <span class="p">]</span> <span class="p">)</span> <span class="n">model</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span> <span class="n">optimizer</span><span class="o">=</span><span class="s2">"adam"</span><span class="p">,</span> <span class="n">loss</span><span class="o">=</span><span class="s2">"mean_squared_error"</span><span class="p">,</span> <span class="c1"># Objective is one of the metrics.</span> <span class="n">metrics</span><span class="o">=</span><span class="p">[</span><span class="n">keras</span><span class="o">.</span><span class="n">metrics</span><span class="o">.</span><span class="n">MeanAbsoluteError</span><span class="p">()],</span> <span class="p">)</span> <span class="k">return</span> <span class="n">model</span> <span class="n">tuner</span> <span class="o">=</span> <span class="n">keras_tuner</span><span class="o">.</span><span class="n">RandomSearch</span><span class="p">(</span> <span class="n">hypermodel</span><span class="o">=</span><span class="n">build_regressor</span><span class="p">,</span> <span class="c1"># The objective name and direction.</span> <span class="c1"># Name is the f"val_{snake_case_metric_class_name}".</span> <span class="n">objective</span><span class="o">=</span><span class="n">keras_tuner</span><span class="o">.</span><span class="n">Objective</span><span class="p">(</span><span class="s2">"val_mean_absolute_error"</span><span class="p">,</span> <span class="n">direction</span><span class="o">=</span><span class="s2">"min"</span><span class="p">),</span> <span class="n">max_trials</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">overwrite</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">directory</span><span class="o">=</span><span class="s2">"my_dir"</span><span class="p">,</span> <span class="n">project_name</span><span class="o">=</span><span class="s2">"built_in_metrics"</span><span class="p">,</span> <span class="p">)</span> <span class="n">tuner</span><span class="o">.</span><span class="n">search</span><span class="p">(</span> <span class="n">x</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span> <span class="n">y</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="n">validation_data</span><span class="o">=</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="mi">1</span><span class="p">)),</span> <span class="p">)</span> <span class="n">tuner</span><span class="o">.</span><span class="n">results_summary</span><span class="p">()</span> </code></pre></div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Trial 3 Complete [00h 00m 01s] val_mean_absolute_error: 0.39589792490005493 </code></pre></div> </div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Best val_mean_absolute_error So Far: 0.34321871399879456 Total elapsed time: 00h 00m 03s Results summary Results in my_dir/built_in_metrics Showing 10 best trials Objective(name="val_mean_absolute_error", direction="min") </code></pre></div> </div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Trial 1 summary Hyperparameters: units: 32 Score: 0.34321871399879456 </code></pre></div> </div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Trial 2 summary Hyperparameters: units: 128 Score: 0.39589792490005493 </code></pre></div> </div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Trial 0 summary Hyperparameters: units: 96 Score: 0.5005304217338562 </code></pre></div> </div> <h3 id="custom-metric-as-the-objective">Custom metric as the objective</h3> <p>You may implement your own metric and use it as the hyperparameter search objective. Here, we use mean squared error (MSE) as an example. First, we implement the MSE metric by subclassing <a href="/api/metrics/base_metric#metric-class"><code>keras.metrics.Metric</code></a>. Remember to give a name to your metric using the <code>name</code> argument of <code>super().__init__()</code>, which will be used later. Note: MSE is actually a build-in metric, which can be imported with <a href="/api/metrics/regression_metrics#meansquarederror-class"><code>keras.metrics.MeanSquaredError</code></a>. This is just an example to show how to use a custom metric as the hyperparameter search objective.</p> <p>For more information about implementing custom metrics, please see <a href="https://keras.io/api/metrics/#creating-custom-metrics">this tutorial</a>. If you would like a metric with a different function signature than <code>update_state(y_true, y_pred, sample_weight)</code>, you can override the <code>train_step()</code> method of your model following <a href="https://keras.io/guides/customizing_what_happens_in_fit/#going-lowerlevel">this tutorial</a>.</p> <div class="codehilite"><pre><span></span><code><span class="kn">from</span> <span class="nn">keras</span> <span class="kn">import</span> <span class="n">ops</span> <span class="k">class</span> <span class="nc">CustomMetric</span><span class="p">(</span><span class="n">keras</span><span class="o">.</span><span class="n">metrics</span><span class="o">.</span><span class="n">Metric</span><span class="p">):</span> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span> <span class="c1"># Specify the name of the metric as "custom_metric".</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">"custom_metric"</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="bp">self</span><span class="o">.</span><span class="n">sum</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">add_weight</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">"sum"</span><span class="p">,</span> <span class="n">initializer</span><span class="o">=</span><span class="s2">"zeros"</span><span class="p">)</span> <span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">add_weight</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">"count"</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="s2">"int32"</span><span class="p">,</span> <span class="n">initializer</span><span class="o">=</span><span class="s2">"zeros"</span><span class="p">)</span> <span class="k">def</span> <span class="nf">update_state</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">y_true</span><span class="p">,</span> <span class="n">y_pred</span><span class="p">,</span> <span class="n">sample_weight</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span> <span class="n">values</span> <span class="o">=</span> <span class="n">ops</span><span class="o">.</span><span class="n">square</span><span class="p">(</span><span class="n">y_true</span> <span class="o">-</span> <span class="n">y_pred</span><span class="p">)</span> <span class="n">count</span> <span class="o">=</span> <span class="n">ops</span><span class="o">.</span><span class="n">shape</span><span class="p">(</span><span class="n">y_true</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="k">if</span> <span class="n">sample_weight</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span> <span class="n">sample_weight</span> <span class="o">=</span> <span class="n">ops</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="n">sample_weight</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span> <span class="n">values</span> <span class="o">*=</span> <span class="n">sample_weight</span> <span class="n">count</span> <span class="o">*=</span> <span class="n">sample_weight</span> <span class="bp">self</span><span class="o">.</span><span class="n">sum</span><span class="o">.</span><span class="n">assign_add</span><span class="p">(</span><span class="n">ops</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">values</span><span class="p">))</span> <span class="bp">self</span><span class="o">.</span><span class="n">count</span><span class="o">.</span><span class="n">assign_add</span><span class="p">(</span><span class="n">count</span><span class="p">)</span> <span class="k">def</span> <span class="nf">result</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">sum</span> <span class="o">/</span> <span class="n">ops</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">count</span><span class="p">,</span> <span class="s2">"float32"</span><span class="p">)</span> <span class="k">def</span> <span class="nf">reset_state</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">sum</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="bp">self</span><span class="o">.</span><span class="n">count</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> </code></pre></div> <p>Run the search with the custom objective.</p> <div class="codehilite"><pre><span></span><code><span class="k">def</span> <span class="nf">build_regressor</span><span class="p">(</span><span class="n">hp</span><span class="p">):</span> <span class="n">model</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">Sequential</span><span class="p">(</span> <span class="p">[</span> <span class="n">layers</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span><span class="n">units</span><span class="o">=</span><span class="n">hp</span><span class="o">.</span><span class="n">Int</span><span class="p">(</span><span class="s2">"units"</span><span class="p">,</span> <span class="mi">32</span><span class="p">,</span> <span class="mi">128</span><span class="p">,</span> <span class="mi">32</span><span class="p">),</span> <span class="n">activation</span><span class="o">=</span><span class="s2">"relu"</span><span class="p">),</span> <span class="n">layers</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span><span class="n">units</span><span class="o">=</span><span class="mi">1</span><span class="p">),</span> <span class="p">]</span> <span class="p">)</span> <span class="n">model</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span> <span class="n">optimizer</span><span class="o">=</span><span class="s2">"adam"</span><span class="p">,</span> <span class="n">loss</span><span class="o">=</span><span class="s2">"mean_squared_error"</span><span class="p">,</span> <span class="c1"># Put custom metric into the metrics.</span> <span class="n">metrics</span><span class="o">=</span><span class="p">[</span><span class="n">CustomMetric</span><span class="p">()],</span> <span class="p">)</span> <span class="k">return</span> <span class="n">model</span> <span class="n">tuner</span> <span class="o">=</span> <span class="n">keras_tuner</span><span class="o">.</span><span class="n">RandomSearch</span><span class="p">(</span> <span class="n">hypermodel</span><span class="o">=</span><span class="n">build_regressor</span><span class="p">,</span> <span class="c1"># Specify the name and direction of the objective.</span> <span class="n">objective</span><span class="o">=</span><span class="n">keras_tuner</span><span class="o">.</span><span class="n">Objective</span><span class="p">(</span><span class="s2">"val_custom_metric"</span><span class="p">,</span> <span class="n">direction</span><span class="o">=</span><span class="s2">"min"</span><span class="p">),</span> <span class="n">max_trials</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">overwrite</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">directory</span><span class="o">=</span><span class="s2">"my_dir"</span><span class="p">,</span> <span class="n">project_name</span><span class="o">=</span><span class="s2">"custom_metrics"</span><span class="p">,</span> <span class="p">)</span> <span class="n">tuner</span><span class="o">.</span><span class="n">search</span><span class="p">(</span> <span class="n">x</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span> <span class="n">y</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="n">validation_data</span><span class="o">=</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="mi">1</span><span class="p">)),</span> <span class="p">)</span> <span class="n">tuner</span><span class="o">.</span><span class="n">results_summary</span><span class="p">()</span> </code></pre></div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Trial 3 Complete [00h 00m 01s] val_custom_metric: 0.2830956280231476 </code></pre></div> </div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Best val_custom_metric So Far: 0.2529197633266449 Total elapsed time: 00h 00m 02s Results summary Results in my_dir/custom_metrics Showing 10 best trials Objective(name="val_custom_metric", direction="min") </code></pre></div> </div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Trial 0 summary Hyperparameters: units: 32 Score: 0.2529197633266449 </code></pre></div> </div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Trial 2 summary Hyperparameters: units: 128 Score: 0.2830956280231476 </code></pre></div> </div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Trial 1 summary Hyperparameters: units: 96 Score: 0.4656866192817688 </code></pre></div> </div> <p>If your custom objective is hard to put into a custom metric, you can also evaluate the model by yourself in <code>HyperModel.fit()</code> and return the objective value. The objective value would be minimized by default. In this case, you don't need to specify the <code>objective</code> when initializing the tuner. However, in this case, the metric value will not be tracked in the Keras logs by only KerasTuner logs. Therefore, these values would not be displayed by any TensorBoard view using the Keras metrics.</p> <div class="codehilite"><pre><span></span><code><span class="k">class</span> <span class="nc">HyperRegressor</span><span class="p">(</span><span class="n">keras_tuner</span><span class="o">.</span><span class="n">HyperModel</span><span class="p">):</span> <span class="k">def</span> <span class="nf">build</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">hp</span><span class="p">):</span> <span class="n">model</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">Sequential</span><span class="p">(</span> <span class="p">[</span> <span class="n">layers</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span><span class="n">units</span><span class="o">=</span><span class="n">hp</span><span class="o">.</span><span class="n">Int</span><span class="p">(</span><span class="s2">"units"</span><span class="p">,</span> <span class="mi">32</span><span class="p">,</span> <span class="mi">128</span><span class="p">,</span> <span class="mi">32</span><span class="p">),</span> <span class="n">activation</span><span class="o">=</span><span class="s2">"relu"</span><span class="p">),</span> <span class="n">layers</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span><span class="n">units</span><span class="o">=</span><span class="mi">1</span><span class="p">),</span> <span class="p">]</span> <span class="p">)</span> <span class="n">model</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span> <span class="n">optimizer</span><span class="o">=</span><span class="s2">"adam"</span><span class="p">,</span> <span class="n">loss</span><span class="o">=</span><span class="s2">"mean_squared_error"</span><span class="p">,</span> <span class="p">)</span> <span class="k">return</span> <span class="n">model</span> <span class="k">def</span> <span class="nf">fit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">hp</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">validation_data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span> <span class="n">model</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="n">x_val</span><span class="p">,</span> <span class="n">y_val</span> <span class="o">=</span> <span class="n">validation_data</span> <span class="n">y_pred</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">x_val</span><span class="p">)</span> <span class="c1"># Return a single float to minimize.</span> <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">y_pred</span> <span class="o">-</span> <span class="n">y_val</span><span class="p">))</span> <span class="n">tuner</span> <span class="o">=</span> <span class="n">keras_tuner</span><span class="o">.</span><span class="n">RandomSearch</span><span class="p">(</span> <span class="n">hypermodel</span><span class="o">=</span><span class="n">HyperRegressor</span><span class="p">(),</span> <span class="c1"># No objective to specify.</span> <span class="c1"># Objective is the return value of `HyperModel.fit()`.</span> <span class="n">max_trials</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">overwrite</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">directory</span><span class="o">=</span><span class="s2">"my_dir"</span><span class="p">,</span> <span class="n">project_name</span><span class="o">=</span><span class="s2">"custom_eval"</span><span class="p">,</span> <span class="p">)</span> <span class="n">tuner</span><span class="o">.</span><span class="n">search</span><span class="p">(</span> <span class="n">x</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span> <span class="n">y</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="n">validation_data</span><span class="o">=</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="mi">1</span><span class="p">)),</span> <span class="p">)</span> <span class="n">tuner</span><span class="o">.</span><span class="n">results_summary</span><span class="p">()</span> </code></pre></div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Trial 3 Complete [00h 00m 01s] default_objective: 0.6571611521766413 </code></pre></div> </div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Best default_objective So Far: 0.40719249752993525 Total elapsed time: 00h 00m 02s Results summary Results in my_dir/custom_eval Showing 10 best trials Objective(name="default_objective", direction="min") </code></pre></div> </div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Trial 1 summary Hyperparameters: units: 128 Score: 0.40719249752993525 </code></pre></div> </div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Trial 0 summary Hyperparameters: units: 96 Score: 0.4992297225533352 </code></pre></div> </div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Trial 2 summary Hyperparameters: units: 32 Score: 0.6571611521766413 </code></pre></div> </div> <p>If you have multiple metrics to track in KerasTuner, but only use one of them as the objective, you can return a dictionary, whose keys are the metric names and the values are the metrics values, for example, return <code>{"metric_a": 1.0, "metric_b", 2.0}</code>. Use one of the keys as the objective name, for example, <code>keras_tuner.Objective("metric_a", "min")</code>.</p> <div class="codehilite"><pre><span></span><code><span class="k">class</span> <span class="nc">HyperRegressor</span><span class="p">(</span><span class="n">keras_tuner</span><span class="o">.</span><span class="n">HyperModel</span><span class="p">):</span> <span class="k">def</span> <span class="nf">build</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">hp</span><span class="p">):</span> <span class="n">model</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">Sequential</span><span class="p">(</span> <span class="p">[</span> <span class="n">layers</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span><span class="n">units</span><span class="o">=</span><span class="n">hp</span><span class="o">.</span><span class="n">Int</span><span class="p">(</span><span class="s2">"units"</span><span class="p">,</span> <span class="mi">32</span><span class="p">,</span> <span class="mi">128</span><span class="p">,</span> <span class="mi">32</span><span class="p">),</span> <span class="n">activation</span><span class="o">=</span><span class="s2">"relu"</span><span class="p">),</span> <span class="n">layers</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span><span class="n">units</span><span class="o">=</span><span class="mi">1</span><span class="p">),</span> <span class="p">]</span> <span class="p">)</span> <span class="n">model</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span> <span class="n">optimizer</span><span class="o">=</span><span class="s2">"adam"</span><span class="p">,</span> <span class="n">loss</span><span class="o">=</span><span class="s2">"mean_squared_error"</span><span class="p">,</span> <span class="p">)</span> <span class="k">return</span> <span class="n">model</span> <span class="k">def</span> <span class="nf">fit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">hp</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">validation_data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span> <span class="n">model</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="n">x_val</span><span class="p">,</span> <span class="n">y_val</span> <span class="o">=</span> <span class="n">validation_data</span> <span class="n">y_pred</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">x_val</span><span class="p">)</span> <span class="c1"># Return a dictionary of metrics for KerasTuner to track.</span> <span class="k">return</span> <span class="p">{</span> <span class="s2">"metric_a"</span><span class="p">:</span> <span class="o">-</span><span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">y_pred</span> <span class="o">-</span> <span class="n">y_val</span><span class="p">)),</span> <span class="s2">"metric_b"</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">square</span><span class="p">(</span><span class="n">y_pred</span> <span class="o">-</span> <span class="n">y_val</span><span class="p">)),</span> <span class="p">}</span> <span class="n">tuner</span> <span class="o">=</span> <span class="n">keras_tuner</span><span class="o">.</span><span class="n">RandomSearch</span><span class="p">(</span> <span class="n">hypermodel</span><span class="o">=</span><span class="n">HyperRegressor</span><span class="p">(),</span> <span class="c1"># Objective is one of the keys.</span> <span class="c1"># Maximize the negative MAE, equivalent to minimize MAE.</span> <span class="n">objective</span><span class="o">=</span><span class="n">keras_tuner</span><span class="o">.</span><span class="n">Objective</span><span class="p">(</span><span class="s2">"metric_a"</span><span class="p">,</span> <span class="s2">"max"</span><span class="p">),</span> <span class="n">max_trials</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">overwrite</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">directory</span><span class="o">=</span><span class="s2">"my_dir"</span><span class="p">,</span> <span class="n">project_name</span><span class="o">=</span><span class="s2">"custom_eval_dict"</span><span class="p">,</span> <span class="p">)</span> <span class="n">tuner</span><span class="o">.</span><span class="n">search</span><span class="p">(</span> <span class="n">x</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span> <span class="n">y</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="n">validation_data</span><span class="o">=</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="mi">1</span><span class="p">)),</span> <span class="p">)</span> <span class="n">tuner</span><span class="o">.</span><span class="n">results_summary</span><span class="p">()</span> </code></pre></div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Trial 3 Complete [00h 00m 01s] metric_a: -0.39470441501524833 </code></pre></div> </div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Best metric_a So Far: -0.3836997988261662 Total elapsed time: 00h 00m 02s Results summary Results in my_dir/custom_eval_dict Showing 10 best trials Objective(name="metric_a", direction="max") </code></pre></div> </div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Trial 1 summary Hyperparameters: units: 64 Score: -0.3836997988261662 </code></pre></div> </div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Trial 2 summary Hyperparameters: units: 32 Score: -0.39470441501524833 </code></pre></div> </div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Trial 0 summary Hyperparameters: units: 96 Score: -0.46081380465766364 </code></pre></div> </div> <hr /> <h2 id="tune-endtoend-workflows">Tune end-to-end workflows</h2> <p>In some cases, it is hard to align your code into build and fit functions. You can also keep your end-to-end workflow in one place by overriding <code>Tuner.run_trial()</code>, which gives you full control of a trial. You can see it as a black-box optimizer for anything.</p> <h3 id="tune-any-function">Tune any function</h3> <p>For example, you can find a value of <code>x</code>, which minimizes <code>f(x)=x*x+1</code>. In the following code, we just define <code>x</code> as a hyperparameter, and return <code>f(x)</code> as the objective value. The <code>hypermodel</code> and <code>objective</code> argument for initializing the tuner can be omitted.</p> <div class="codehilite"><pre><span></span><code><span class="k">class</span> <span class="nc">MyTuner</span><span class="p">(</span><span class="n">keras_tuner</span><span class="o">.</span><span class="n">RandomSearch</span><span class="p">):</span> <span class="k">def</span> <span class="nf">run_trial</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span> <span class="c1"># Get the hp from trial.</span> <span class="n">hp</span> <span class="o">=</span> <span class="n">trial</span><span class="o">.</span><span class="n">hyperparameters</span> <span class="c1"># Define "x" as a hyperparameter.</span> <span class="n">x</span> <span class="o">=</span> <span class="n">hp</span><span class="o">.</span><span class="n">Float</span><span class="p">(</span><span class="s2">"x"</span><span class="p">,</span> <span class="n">min_value</span><span class="o">=-</span><span class="mf">1.0</span><span class="p">,</span> <span class="n">max_value</span><span class="o">=</span><span class="mf">1.0</span><span class="p">)</span> <span class="c1"># Return the objective value to minimize.</span> <span class="k">return</span> <span class="n">x</span> <span class="o">*</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span> <span class="n">tuner</span> <span class="o">=</span> <span class="n">MyTuner</span><span class="p">(</span> <span class="c1"># No hypermodel or objective specified.</span> <span class="n">max_trials</span><span class="o">=</span><span class="mi">20</span><span class="p">,</span> <span class="n">overwrite</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">directory</span><span class="o">=</span><span class="s2">"my_dir"</span><span class="p">,</span> <span class="n">project_name</span><span class="o">=</span><span class="s2">"tune_anything"</span><span class="p">,</span> <span class="p">)</span> <span class="c1"># No need to pass anything to search()</span> <span class="c1"># unless you use them in run_trial().</span> <span class="n">tuner</span><span class="o">.</span><span class="n">search</span><span class="p">()</span> <span class="nb">print</span><span class="p">(</span><span class="n">tuner</span><span class="o">.</span><span class="n">get_best_hyperparameters</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"x"</span><span class="p">))</span> </code></pre></div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Trial 20 Complete [00h 00m 00s] default_objective: 1.6547719581194267 </code></pre></div> </div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Best default_objective So Far: 1.0013236767905302 Total elapsed time: 00h 00m 00s 0.03638236922645777 </code></pre></div> </div> <h3 id="keep-keras-code-separate">Keep Keras code separate</h3> <p>You can keep all your Keras code unchanged and use KerasTuner to tune it. It is useful if you cannot modify the Keras code for some reason.</p> <p>It also gives you more flexibility. You don't have to separate the model building and training code apart. However, this workflow would not help you save the model or connect with the TensorBoard plugins.</p> <p>To save the model, you can use <code>trial.trial_id</code>, which is a string to uniquely identify a trial, to construct different paths to save the models from different trials.</p> <div class="codehilite"><pre><span></span><code><span class="kn">import</span> <span class="nn">os</span> <span class="k">def</span> <span class="nf">keras_code</span><span class="p">(</span><span class="n">units</span><span class="p">,</span> <span class="n">optimizer</span><span class="p">,</span> <span class="n">saving_path</span><span class="p">):</span> <span class="c1"># Build model</span> <span class="n">model</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">Sequential</span><span class="p">(</span> <span class="p">[</span> <span class="n">layers</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span><span class="n">units</span><span class="o">=</span><span class="n">units</span><span class="p">,</span> <span class="n">activation</span><span class="o">=</span><span class="s2">"relu"</span><span class="p">),</span> <span class="n">layers</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span><span class="n">units</span><span class="o">=</span><span class="mi">1</span><span class="p">),</span> <span class="p">]</span> <span class="p">)</span> <span class="n">model</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span> <span class="n">optimizer</span><span class="o">=</span><span class="n">optimizer</span><span class="p">,</span> <span class="n">loss</span><span class="o">=</span><span class="s2">"mean_squared_error"</span><span class="p">,</span> <span class="p">)</span> <span class="c1"># Prepare data</span> <span class="n">x_train</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span> <span class="n">y_train</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="n">x_val</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span> <span class="n">y_val</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="c1"># Train & eval model</span> <span class="n">model</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">x_train</span><span class="p">,</span> <span class="n">y_train</span><span class="p">)</span> <span class="c1"># Save model</span> <span class="n">model</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">saving_path</span><span class="p">)</span> <span class="c1"># Return a single float as the objective value.</span> <span class="c1"># You may also return a dictionary</span> <span class="c1"># of {metric_name: metric_value}.</span> <span class="n">y_pred</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">x_val</span><span class="p">)</span> <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">y_pred</span> <span class="o">-</span> <span class="n">y_val</span><span class="p">))</span> <span class="k">class</span> <span class="nc">MyTuner</span><span class="p">(</span><span class="n">keras_tuner</span><span class="o">.</span><span class="n">RandomSearch</span><span class="p">):</span> <span class="k">def</span> <span class="nf">run_trial</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span> <span class="n">hp</span> <span class="o">=</span> <span class="n">trial</span><span class="o">.</span><span class="n">hyperparameters</span> <span class="k">return</span> <span class="n">keras_code</span><span class="p">(</span> <span class="n">units</span><span class="o">=</span><span class="n">hp</span><span class="o">.</span><span class="n">Int</span><span class="p">(</span><span class="s2">"units"</span><span class="p">,</span> <span class="mi">32</span><span class="p">,</span> <span class="mi">128</span><span class="p">,</span> <span class="mi">32</span><span class="p">),</span> <span class="n">optimizer</span><span class="o">=</span><span class="n">hp</span><span class="o">.</span><span class="n">Choice</span><span class="p">(</span><span class="s2">"optimizer"</span><span class="p">,</span> <span class="p">[</span><span class="s2">"adam"</span><span class="p">,</span> <span class="s2">"adadelta"</span><span class="p">]),</span> <span class="n">saving_path</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s2">"/tmp"</span><span class="p">,</span> <span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="n">trial</span><span class="o">.</span><span class="n">trial_id</span><span class="si">}</span><span class="s2">.keras"</span><span class="p">),</span> <span class="p">)</span> <span class="n">tuner</span> <span class="o">=</span> <span class="n">MyTuner</span><span class="p">(</span> <span class="n">max_trials</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">overwrite</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">directory</span><span class="o">=</span><span class="s2">"my_dir"</span><span class="p">,</span> <span class="n">project_name</span><span class="o">=</span><span class="s2">"keep_code_separate"</span><span class="p">,</span> <span class="p">)</span> <span class="n">tuner</span><span class="o">.</span><span class="n">search</span><span class="p">()</span> <span class="c1"># Retraining the model</span> <span class="n">best_hp</span> <span class="o">=</span> <span class="n">tuner</span><span class="o">.</span><span class="n">get_best_hyperparameters</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span> <span class="n">keras_code</span><span class="p">(</span><span class="o">**</span><span class="n">best_hp</span><span class="o">.</span><span class="n">values</span><span class="p">,</span> <span class="n">saving_path</span><span class="o">=</span><span class="s2">"/tmp/best_model.keras"</span><span class="p">)</span> </code></pre></div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Trial 3 Complete [00h 00m 00s] default_objective: 0.18014027375230962 </code></pre></div> </div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Best default_objective So Far: 0.18014027375230962 Total elapsed time: 00h 00m 03s </code></pre></div> </div> <p>1/4 ━━━━━[37m━━━━━━━━━━━━━━━ 0s 172ms/step - loss: 0.5030</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>4/4 ━━━━━━━━━━━━━━━━━━━━ 0s 60ms/step - loss: 0.5288 </p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>4/4 ━━━━━━━━━━━━━━━━━━━━ 0s 61ms/step - loss: 0.5367</p> <p>1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 27ms/step</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> </code></pre></div> </div> <p>1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 28ms/step</p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>0.5918120126201316 </code></pre></div> </div> <hr /> <h2 id="kerastuner-includes-premade-tunable-applications-hyperresnet-and-hyperxception">KerasTuner includes pre-made tunable applications: HyperResNet and HyperXception</h2> <p>These are ready-to-use hypermodels for computer vision.</p> <p>They come pre-compiled with <code>loss="categorical_crossentropy"</code> and <code>metrics=["accuracy"]</code>.</p> <div class="codehilite"><pre><span></span><code><span class="kn">from</span> <span class="nn">keras_tuner.applications</span> <span class="kn">import</span> <span class="n">HyperResNet</span> <span class="n">hypermodel</span> <span class="o">=</span> <span class="n">HyperResNet</span><span class="p">(</span><span class="n">input_shape</span><span class="o">=</span><span class="p">(</span><span class="mi">28</span><span class="p">,</span> <span class="mi">28</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="n">classes</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span> <span class="n">tuner</span> <span class="o">=</span> <span class="n">keras_tuner</span><span class="o">.</span><span class="n">RandomSearch</span><span class="p">(</span> <span class="n">hypermodel</span><span class="p">,</span> <span class="n">objective</span><span class="o">=</span><span class="s2">"val_accuracy"</span><span class="p">,</span> <span class="n">max_trials</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">overwrite</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">directory</span><span class="o">=</span><span class="s2">"my_dir"</span><span class="p">,</span> <span class="n">project_name</span><span class="o">=</span><span class="s2">"built_in_hypermodel"</span><span class="p">,</span> <span class="p">)</span> </code></pre></div> </div> <div class='k-outline'> <div class='k-outline-depth-1'> <a href='#getting-started-with-kerastuner'>Getting started with KerasTuner</a> </div> <div class='k-outline-depth-2'> ◆ <a href='#introduction'>Introduction</a> </div> <div class='k-outline-depth-2'> ◆ <a href='#tune-the-model-architecture'>Tune the model architecture</a> </div> <div class='k-outline-depth-3'> <a href='#define-the-search-space'>Define the search space</a> </div> <div class='k-outline-depth-3'> <a href='#start-the-search'>Start the search</a> </div> <div class='k-outline-depth-3'> <a href='#query-the-results'>Query the results</a> </div> <div class='k-outline-depth-3'> <a href='#retrain-the-model'>Retrain the model</a> </div> <div class='k-outline-depth-2'> ◆ <a href='#tune-model-training'>Tune model training</a> </div> <div class='k-outline-depth-2'> ◆ <a href='#tune-data-preprocessing'>Tune data preprocessing</a> </div> <div class='k-outline-depth-3'> <a href='#retrain-the-model'>Retrain the model</a> </div> <div class='k-outline-depth-2'> ◆ <a href='#specify-the-tuning-objective'>Specify the tuning objective</a> </div> <div class='k-outline-depth-3'> <a href='#builtin-metric-as-the-objective'>Built-in metric as the objective</a> </div> <div class='k-outline-depth-3'> <a href='#custom-metric-as-the-objective'>Custom metric as the objective</a> </div> <div class='k-outline-depth-2'> ◆ <a href='#tune-endtoend-workflows'>Tune end-to-end workflows</a> </div> <div class='k-outline-depth-3'> <a href='#tune-any-function'>Tune any function</a> </div> <div class='k-outline-depth-3'> <a href='#keep-keras-code-separate'>Keep Keras code separate</a> </div> <div class='k-outline-depth-2'> ◆ <a href='#kerastuner-includes-premade-tunable-applications-hyperresnet-and-hyperxception'>KerasTuner includes pre-made tunable applications: HyperResNet and HyperXception</a> </div> </div> </div> </div> </div> </body> <footer style="float: left; width: 100%; padding: 1em; border-top: solid 1px #bbb;"> <a href="https://policies.google.com/terms">Terms</a> | <a href="https://policies.google.com/privacy">Privacy</a> </footer> </html>