CINXE.COM
The base Tuner class
<!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/api/keras_tuner/tuners/base_tuner/" /> <!-- Social --> <meta property="og:title" content="Keras documentation: The base Tuner class"> <meta property="og:image" content="https://keras.io/img/logo-k-keras-wb.png"> <meta name="twitter:title" content="Keras documentation: The base Tuner class"> <meta name="twitter:image" content="https://keras.io/img/k-keras-social.png"> <meta name="twitter:card" content="summary"> <title>The base Tuner class</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" href="/guides/" role="tab" aria-selected="">Developer guides</a> <a class="nav-link active" href="/api/" role="tab" aria-selected="">Keras 3 API documentation</a> <a class="nav-sublink" href="/api/models/">Models API</a> <a class="nav-sublink" href="/api/layers/">Layers API</a> <a class="nav-sublink" href="/api/callbacks/">Callbacks API</a> <a class="nav-sublink" href="/api/ops/">Ops API</a> <a class="nav-sublink" href="/api/optimizers/">Optimizers</a> <a class="nav-sublink" href="/api/metrics/">Metrics</a> <a class="nav-sublink" href="/api/losses/">Losses</a> <a class="nav-sublink" href="/api/data_loading/">Data loading</a> <a class="nav-sublink" href="/api/datasets/">Built-in small datasets</a> <a class="nav-sublink" href="/api/applications/">Keras Applications</a> <a class="nav-sublink" href="/api/mixed_precision/">Mixed precision</a> <a class="nav-sublink" href="/api/distribution/">Multi-device distribution</a> <a class="nav-sublink" href="/api/random/">RNG API</a> <a class="nav-sublink" href="/api/utils/">Utilities</a> <a class="nav-sublink active" href="/api/keras_tuner/">KerasTuner</a> <a class="nav-sublink2" href="/api/keras_tuner/hyperparameters/">HyperParameters</a> <a class="nav-sublink2 active" href="/api/keras_tuner/tuners/">Tuners</a> <a class="nav-sublink2" href="/api/keras_tuner/oracles/">Oracles</a> <a class="nav-sublink2" href="/api/keras_tuner/hypermodels/">HyperModels</a> <a class="nav-sublink2" href="/api/keras_tuner/errors/">Errors</a> <a class="nav-sublink" href="/api/keras_cv/">KerasCV</a> <a class="nav-sublink" href="/api/keras_nlp/">KerasNLP</a> <a class="nav-sublink" href="/api/keras_hub/">KerasHub</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='/api/'>Keras 3 API documentation</a> / <a href='/api/keras_tuner/'>KerasTuner</a> / <a href='/api/keras_tuner/tuners/'>Tuners</a> / The base Tuner class </div> <div class='k-content'> <h1 id="the-base-tuner-class">The base Tuner class</h1> <p><span style="float:right;"><a href="https://github.com/keras-team/keras-tuner/tree/v1.4.7/keras_tuner/src/engine/tuner.py#L35">[source]</a></span></p> <h3 id="tuner-class"><code>Tuner</code> class</h3> <div class="codehilite"><pre><span></span><code><span class="n">keras_tuner</span><span class="o">.</span><span class="n">Tuner</span><span class="p">(</span> <span class="n">oracle</span><span class="p">,</span> <span class="n">hypermodel</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">max_model_size</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">optimizer</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">loss</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">metrics</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">distribution_strategy</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">directory</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">project_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">logger</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">tuner_id</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">overwrite</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">executions_per_trial</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span> <span class="p">)</span> </code></pre></div> <p>Tuner class for Keras models.</p> <p>This is the base <code>Tuner</code> class for all tuners for Keras models. It manages the building, training, evaluation and saving of the Keras models. New tuners can be created by subclassing the class.</p> <p>All Keras related logics are in <code>Tuner.run_trial()</code> and its subroutines. When subclassing <code>Tuner</code>, if not calling <code>super().run_trial()</code>, it can tune anything.</p> <p><strong>Arguments</strong></p> <ul> <li><strong>oracle</strong>: Instance of <code>Oracle</code> class.</li> <li><strong>hypermodel</strong>: Instance of <code>HyperModel</code> class (or callable that takes hyperparameters and returns a <code>Model</code> instance). It is optional when <code>Tuner.run_trial()</code> is overriden and does not use <code>self.hypermodel</code>.</li> <li><strong>max_model_size</strong>: Integer, maximum number of scalars in the parameters of a model. Models larger than this are rejected.</li> <li><strong>optimizer</strong>: Optional optimizer. It is used to override the <code>optimizer</code> argument in the <code>compile</code> step for the models. If the hypermodel does not compile the models it generates, then this argument must be specified.</li> <li><strong>loss</strong>: Optional loss. May be used to override the <code>loss</code> argument in the <code>compile</code> step for the models. If the hypermodel does not compile the models it generates, then this argument must be specified.</li> <li><strong>metrics</strong>: Optional metrics. May be used to override the <code>metrics</code> argument in the <code>compile</code> step for the models. If the hypermodel does not compile the models it generates, then this argument must be specified.</li> <li><strong>distribution_strategy</strong>: Optional instance of <a href="https://www.tensorflow.org/api_docs/python/tf/distribute/Strategy"><code>tf.distribute.Strategy</code></a>. If specified, each trial will run under this scope. For example, <code>tf.distribute.MirroredStrategy(['/gpu:0', '/gpu:1'])</code> will run each trial on two GPUs. Currently only single-worker strategies are supported.</li> <li><strong>directory</strong>: A string, the relative path to the working directory.</li> <li><strong>project_name</strong>: A string, the name to use as prefix for files saved by this <code>Tuner</code>.</li> <li><strong>tuner_id</strong>: Optional string, used as the ID of this <code>Tuner</code>.</li> <li><strong>overwrite</strong>: Boolean, defaults to <code>False</code>. If <code>False</code>, reloads an existing project of the same name if one is found. Otherwise, overwrites the project.</li> <li><strong>executions_per_trial</strong>: Integer, the number of executions (training a model from scratch, starting from a new initialization) to run per trial (model configuration). Model metrics may vary greatly depending on random initialization, hence it is often a good idea to run several executions per trial in order to evaluate the performance of a given set of hyperparameter values.</li> <li><strong>**kwargs</strong>: Arguments for <code>BaseTuner</code>.</li> </ul> <p><strong>Attributes</strong></p> <ul> <li><strong>remaining_trials</strong>: Number of trials remaining, <code>None</code> if <code>max_trials</code> is not set. This is useful when resuming a previously stopped search.</li> </ul> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras-tuner/tree/v1.4.7/keras_tuner/src/engine/base_tuner.py#L369">[source]</a></span></p> <h3 id="getbesthyperparameters-method"><code>get_best_hyperparameters</code> method</h3> <div class="codehilite"><pre><span></span><code><span class="n">Tuner</span><span class="o">.</span><span class="n">get_best_hyperparameters</span><span class="p">(</span><span class="n">num_trials</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> </code></pre></div> <p>Returns the best hyperparameters, as determined by the objective.</p> <p>This method can be used to reinstantiate the (untrained) best model found during the search process.</p> <p><strong>Example</strong></p> <div class="codehilite"><pre><span></span><code><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">tuner</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> </code></pre></div> <p><strong>Arguments</strong></p> <ul> <li><strong>num_trials</strong>: Optional number of <code>HyperParameters</code> objects to return.</li> </ul> <p><strong>Returns</strong></p> <p>List of <code>HyperParameter</code> objects sorted from the best to the worst.</p> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras-tuner/tree/v1.4.7/keras_tuner/src/engine/tuner.py#L381">[source]</a></span></p> <h3 id="getbestmodels-method"><code>get_best_models</code> method</h3> <div class="codehilite"><pre><span></span><code><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">1</span><span class="p">)</span> </code></pre></div> <p>Returns the best model(s), as determined by the tuner's objective.</p> <p>The models are loaded with the weights corresponding to their best checkpoint (at the end of the best epoch of best trial).</p> <p>This method is for querying the models trained during the search. For best performance, it is recommended to retrain your Model on the full dataset using the best hyperparameters found during <code>search</code>, which can be obtained using <code>tuner.get_best_hyperparameters()</code>.</p> <p><strong>Arguments</strong></p> <ul> <li><strong>num_models</strong>: Optional number of best models to return. Defaults to 1.</li> </ul> <p><strong>Returns</strong></p> <p>List of trained model instances sorted from the best to the worst.</p> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras-tuner/tree/v1.4.7/keras_tuner/src/engine/base_tuner.py#L439">[source]</a></span></p> <h3 id="getstate-method"><code>get_state</code> method</h3> <div class="codehilite"><pre><span></span><code><span class="n">Tuner</span><span class="o">.</span><span class="n">get_state</span><span class="p">()</span> </code></pre></div> <p>Returns the current state of this object.</p> <p>This method is called during <code>save</code>.</p> <p><strong>Returns</strong></p> <p>A dictionary of serializable objects as the state.</p> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras-tuner/tree/v1.4.7/keras_tuner/src/engine/tuner.py#L319">[source]</a></span></p> <h3 id="loadmodel-method"><code>load_model</code> method</h3> <div class="codehilite"><pre><span></span><code><span class="n">Tuner</span><span class="o">.</span><span class="n">load_model</span><span class="p">(</span><span class="n">trial</span><span class="p">)</span> </code></pre></div> <p>Loads a Model from a given trial.</p> <p>For models that report intermediate results to the <code>Oracle</code>, generally <code>load_model</code> should load the best reported <code>step</code> by relying of <code>trial.best_step</code>.</p> <p><strong>Arguments</strong></p> <ul> <li><strong>trial</strong>: A <code>Trial</code> instance, the <code>Trial</code> corresponding to the model to load.</li> </ul> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras-tuner/tree/v1.4.7/keras_tuner/src/engine/tuner.py#L356">[source]</a></span></p> <h3 id="onepochbegin-method"><code>on_epoch_begin</code> method</h3> <div class="codehilite"><pre><span></span><code><span class="n">Tuner</span><span class="o">.</span><span class="n">on_epoch_begin</span><span class="p">(</span><span class="n">trial</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">epoch</span><span class="p">,</span> <span class="n">logs</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> </code></pre></div> <p>Called at the beginning of an epoch.</p> <p><strong>Arguments</strong></p> <ul> <li><strong>trial</strong>: A <code>Trial</code> instance.</li> <li><strong>model</strong>: A Keras <code>Model</code>.</li> <li><strong>epoch</strong>: The current epoch number.</li> <li><strong>logs</strong>: Additional metrics.</li> </ul> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras-tuner/tree/v1.4.7/keras_tuner/src/engine/tuner.py#L334">[source]</a></span></p> <h3 id="onbatchbegin-method"><code>on_batch_begin</code> method</h3> <div class="codehilite"><pre><span></span><code><span class="n">Tuner</span><span class="o">.</span><span class="n">on_batch_begin</span><span class="p">(</span><span class="n">trial</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">batch</span><span class="p">,</span> <span class="n">logs</span><span class="p">)</span> </code></pre></div> <p>Called at the beginning of a batch.</p> <p><strong>Arguments</strong></p> <ul> <li><strong>trial</strong>: A <code>Trial</code> instance.</li> <li><strong>model</strong>: A Keras <code>Model</code>.</li> <li><strong>batch</strong>: The current batch number within the current epoch.</li> <li><strong>logs</strong>: Additional metrics.</li> </ul> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras-tuner/tree/v1.4.7/keras_tuner/src/engine/tuner.py#L345">[source]</a></span></p> <h3 id="onbatchend-method"><code>on_batch_end</code> method</h3> <div class="codehilite"><pre><span></span><code><span class="n">Tuner</span><span class="o">.</span><span class="n">on_batch_end</span><span class="p">(</span><span class="n">trial</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">batch</span><span class="p">,</span> <span class="n">logs</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> </code></pre></div> <p>Called at the end of a batch.</p> <p><strong>Arguments</strong></p> <ul> <li><strong>trial</strong>: A <code>Trial</code> instance.</li> <li><strong>model</strong>: A Keras <code>Model</code>.</li> <li><strong>batch</strong>: The current batch number within the current epoch.</li> <li><strong>logs</strong>: Additional metrics.</li> </ul> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras-tuner/tree/v1.4.7/keras_tuner/src/engine/tuner.py#L367">[source]</a></span></p> <h3 id="onepochend-method"><code>on_epoch_end</code> method</h3> <div class="codehilite"><pre><span></span><code><span class="n">Tuner</span><span class="o">.</span><span class="n">on_epoch_end</span><span class="p">(</span><span class="n">trial</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">epoch</span><span class="p">,</span> <span class="n">logs</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> </code></pre></div> <p>Called at the end of an epoch.</p> <p><strong>Arguments</strong></p> <ul> <li><strong>trial</strong>: A <code>Trial</code> instance.</li> <li><strong>model</strong>: A Keras <code>Model</code>.</li> <li><strong>epoch</strong>: The current epoch number.</li> <li><strong>logs</strong>: Dict. Metrics for this epoch. This should include the value of the objective for this epoch.</li> </ul> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras-tuner/tree/v1.4.7/keras_tuner/src/engine/tuner.py#L247">[source]</a></span></p> <h3 id="runtrial-method"><code>run_trial</code> method</h3> <div class="codehilite"><pre><span></span><code><span class="n">Tuner</span><span class="o">.</span><span class="n">run_trial</span><span class="p">(</span><span class="n">trial</span><span class="p">,</span> <span class="p">)</span> </code></pre></div> <p>Evaluates a set of hyperparameter values.</p> <p>This method is called multiple times during <code>search</code> to build and evaluate the models with different hyperparameters and return the objective value.</p> <p><strong>Example</strong></p> <p>You can use it with <code>self.hypermodel</code> to build and fit the model.</p> <div class="codehilite"><pre><span></span><code><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="n">hp</span> <span class="o">=</span> <span class="n">trial</span><span class="o">.</span><span class="n">hyperparameters</span> <span class="n">model</span> <span class="o">=</span> <span class="bp">self</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="k">return</span> <span class="bp">self</span><span class="o">.</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="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> </code></pre></div> <p>You can also use it as a black-box optimizer for anything.</p> <div class="codehilite"><pre><span></span><code><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="n">hp</span> <span class="o">=</span> <span class="n">trial</span><span class="o">.</span><span class="n">hyperparameters</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="o">-</span><span class="mf">2.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">)</span> <span class="n">y</span> <span class="o">=</span> <span class="n">x</span> <span class="o">*</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span> <span class="k">return</span> <span class="n">y</span> </code></pre></div> <p><strong>Arguments</strong></p> <ul> <li><strong>trial</strong>: A <code>Trial</code> instance that contains the information needed to run this trial. Hyperparameters can be accessed via <code>trial.hyperparameters</code>.</li> <li><strong>*args</strong>: Positional arguments passed by <code>search</code>.</li> <li><strong>**kwargs</strong>: Keyword arguments passed by <code>search</code>.</li> </ul> <p><strong>Returns</strong></p> <p>A <code>History</code> object, which is the return value of <code>model.fit()</code>, a dictionary, a float, or a list of one of these types.</p> <p>If return a dictionary, it should be a dictionary of the metrics to track. The keys are the metric names, which contains the <code>objective</code> name. The values should be the metric values.</p> <p>If return a float, it should be the <code>objective</code> value.</p> <p>If evaluating the model for multiple times, you may return a list of results of any of the types above. The final objective value is the average of the results in the list.</p> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras-tuner/tree/v1.4.7/keras_tuner/src/engine/base_tuner.py#L411">[source]</a></span></p> <h3 id="resultssummary-method"><code>results_summary</code> method</h3> <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><span class="n">num_trials</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span> </code></pre></div> <p>Display tuning results summary.</p> <p>The method prints a summary of the search results including the hyperparameter values and evaluation results for each trial.</p> <p><strong>Arguments</strong></p> <ul> <li><strong>num_trials</strong>: Optional number of trials to display. Defaults to 10.</li> </ul> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras-tuner/tree/v1.4.7/keras_tuner/src/engine/base_tuner.py#L297">[source]</a></span></p> <h3 id="savemodel-method"><code>save_model</code> method</h3> <div class="codehilite"><pre><span></span><code><span class="n">Tuner</span><span class="o">.</span><span class="n">save_model</span><span class="p">(</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span> </code></pre></div> <p>Saves a Model for a given trial.</p> <p><strong>Arguments</strong></p> <ul> <li><strong>trial_id</strong>: The ID of the <code>Trial</code> corresponding to this Model.</li> <li><strong>model</strong>: The trained model.</li> <li><strong>step</strong>: Integer, for models that report intermediate results to the <code>Oracle</code>, the step the saved file correspond to. For example, for Keras models this is the number of epochs trained.</li> </ul> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras-tuner/tree/v1.4.7/keras_tuner/src/engine/base_tuner.py#L194">[source]</a></span></p> <h3 id="search-method"><code>search</code> method</h3> <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="o">*</span><span class="n">fit_args</span><span class="p">,</span> <span class="o">**</span><span class="n">fit_kwargs</span><span class="p">)</span> </code></pre></div> <p>Performs a search for best hyperparameter configuations.</p> <p><strong>Arguments</strong></p> <ul> <li><strong>*fit_args</strong>: Positional arguments that should be passed to <code>run_trial</code>, for example the training and validation data.</li> <li><strong>**fit_kwargs</strong>: Keyword arguments that should be passed to <code>run_trial</code>, for example the training and validation data.</li> </ul> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras-tuner/tree/v1.4.7/keras_tuner/src/engine/base_tuner.py#L392">[source]</a></span></p> <h3 id="searchspacesummary-method"><code>search_space_summary</code> method</h3> <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><span class="n">extended</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> </code></pre></div> <p>Print search space summary.</p> <p>The methods prints a summary of the hyperparameters in the search space, which can be called before calling the <code>search</code> method.</p> <p><strong>Arguments</strong></p> <ul> <li><strong>extended</strong>: Optional boolean, whether to display an extended summary. Defaults to False.</li> </ul> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras-tuner/tree/v1.4.7/keras_tuner/src/engine/base_tuner.py#L442">[source]</a></span></p> <h3 id="setstate-method"><code>set_state</code> method</h3> <div class="codehilite"><pre><span></span><code><span class="n">Tuner</span><span class="o">.</span><span class="n">set_state</span><span class="p">(</span><span class="n">state</span><span class="p">)</span> </code></pre></div> <p>Sets the current state of this object.</p> <p>This method is called during <code>reload</code>.</p> <p><strong>Arguments</strong></p> <ul> <li><strong>state</strong>: A dictionary of serialized objects as the state to restore.</li> </ul> <hr /> </div> <div class='k-outline'> <div class='k-outline-depth-1'> <a href='#the-base-tuner-class'>The base Tuner class</a> </div> <div class='k-outline-depth-3'> <a href='#tuner-class'><code>Tuner</code> class</a> </div> <div class='k-outline-depth-3'> <a href='#getbesthyperparameters-method'><code>get_best_hyperparameters</code> method</a> </div> <div class='k-outline-depth-3'> <a href='#getbestmodels-method'><code>get_best_models</code> method</a> </div> <div class='k-outline-depth-3'> <a href='#getstate-method'><code>get_state</code> method</a> </div> <div class='k-outline-depth-3'> <a href='#loadmodel-method'><code>load_model</code> method</a> </div> <div class='k-outline-depth-3'> <a href='#onepochbegin-method'><code>on_epoch_begin</code> method</a> </div> <div class='k-outline-depth-3'> <a href='#onbatchbegin-method'><code>on_batch_begin</code> method</a> </div> <div class='k-outline-depth-3'> <a href='#onbatchend-method'><code>on_batch_end</code> method</a> </div> <div class='k-outline-depth-3'> <a href='#onepochend-method'><code>on_epoch_end</code> method</a> </div> <div class='k-outline-depth-3'> <a href='#runtrial-method'><code>run_trial</code> method</a> </div> <div class='k-outline-depth-3'> <a href='#resultssummary-method'><code>results_summary</code> method</a> </div> <div class='k-outline-depth-3'> <a href='#savemodel-method'><code>save_model</code> method</a> </div> <div class='k-outline-depth-3'> <a href='#search-method'><code>search</code> method</a> </div> <div class='k-outline-depth-3'> <a href='#searchspacesummary-method'><code>search_space_summary</code> method</a> </div> <div class='k-outline-depth-3'> <a href='#setstate-method'><code>set_state</code> method</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>