CINXE.COM
HyperParameters
<!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/hyperparameters/" /> <!-- Social --> <meta property="og:title" content="Keras documentation: HyperParameters"> <meta property="og:image" content="https://keras.io/img/logo-k-keras-wb.png"> <meta name="twitter:title" content="Keras documentation: HyperParameters"> <meta name="twitter:image" content="https://keras.io/img/k-keras-social.png"> <meta name="twitter:card" content="summary"> <title>HyperParameters</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 active" href="/api/keras_tuner/hyperparameters/">HyperParameters</a> <a class="nav-sublink2" 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> / HyperParameters </div> <div class='k-content'> <h1 id="hyperparameters">HyperParameters</h1> <p><span style="float:right;"><a href="https://github.com/keras-team/keras-tuner/tree/v1.4.7/keras_tuner/src/engine/hyperparameters/hyperparameters.py#L29">[source]</a></span></p> <h3 id="hyperparameters-class"><code>HyperParameters</code> class</h3> <div class="codehilite"><pre><span></span><code><span class="n">keras_tuner</span><span class="o">.</span><span class="n">HyperParameters</span><span class="p">()</span> </code></pre></div> <p>Container for both a hyperparameter space, and current values.</p> <p>A <code>HyperParameters</code> instance can be pass to <code>HyperModel.build(hp)</code> as an argument to build a model.</p> <p>To prevent the users from depending on inactive hyperparameter values, only active hyperparameters should have values in <code>HyperParameters.values</code>.</p> <p><strong>Attributes</strong></p> <ul> <li><strong>space</strong>: A list of <code>HyperParameter</code> objects.</li> <li><strong>values</strong>: A dict mapping hyperparameter names to current values.</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/hyperparameters/hyperparameters.py#L496">[source]</a></span></p> <h3 id="boolean-method"><code>Boolean</code> method</h3> <div class="codehilite"><pre><span></span><code><span class="n">HyperParameters</span><span class="o">.</span><span class="n">Boolean</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">parent_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">parent_values</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> </code></pre></div> <p>Choice between True and False.</p> <p><strong>Arguments</strong></p> <ul> <li><strong>name</strong>: A string. the name of parameter. Must be unique for each <code>HyperParameter</code> instance in the search space.</li> <li><strong>default</strong>: Boolean, the default value to return for the parameter. If unspecified, the default value will be False.</li> <li><strong>parent_name</strong>: Optional string, specifying the name of the parent <code>HyperParameter</code> to use as the condition to activate the current <code>HyperParameter</code>.</li> <li><strong>parent_values</strong>: Optional list of the values of the parent <code>HyperParameter</code> to use as the condition to activate the current <code>HyperParameter</code>.</li> </ul> <p><strong>Returns</strong></p> <p>The value of the hyperparameter, or None if the hyperparameter is not active.</p> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras-tuner/tree/v1.4.7/keras_tuner/src/engine/hyperparameters/hyperparameters.py#L258">[source]</a></span></p> <h3 id="choice-method"><code>Choice</code> method</h3> <div class="codehilite"><pre><span></span><code><span class="n">HyperParameters</span><span class="o">.</span><span class="n">Choice</span><span class="p">(</span> <span class="n">name</span><span class="p">,</span> <span class="n">values</span><span class="p">,</span> <span class="n">ordered</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">parent_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">parent_values</span><span class="o">=</span><span class="kc">None</span> <span class="p">)</span> </code></pre></div> <p>Choice of one value among a predefined set of possible values.</p> <p><strong>Arguments</strong></p> <ul> <li><strong>name</strong>: A string. the name of parameter. Must be unique for each <code>HyperParameter</code> instance in the search space.</li> <li><strong>values</strong>: A list of possible values. Values must be int, float, str, or bool. All values must be of the same type.</li> <li><strong>ordered</strong>: Optional boolean, whether the values passed should be considered to have an ordering. Defaults to <code>True</code> for float/int values. Must be <code>False</code> for any other values.</li> <li><strong>default</strong>: Optional default value to return for the parameter. If unspecified, the default value will be:<ul> <li>None if None is one of the choices in <code>values</code></li> <li>The first entry in <code>values</code> otherwise.</li> </ul> </li> <li><strong>parent_name</strong>: Optional string, specifying the name of the parent <code>HyperParameter</code> to use as the condition to activate the current <code>HyperParameter</code>.</li> <li><strong>parent_values</strong>: Optional list of the values of the parent <code>HyperParameter</code> to use as the condition to activate the current <code>HyperParameter</code>.</li> </ul> <p><strong>Returns</strong></p> <p>The value of the hyperparameter, or None if the hyperparameter is not active.</p> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras-tuner/tree/v1.4.7/keras_tuner/src/engine/hyperparameters/hyperparameters.py#L525">[source]</a></span></p> <h3 id="fixed-method"><code>Fixed</code> method</h3> <div class="codehilite"><pre><span></span><code><span class="n">HyperParameters</span><span class="o">.</span><span class="n">Fixed</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">parent_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">parent_values</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> </code></pre></div> <p>Fixed, untunable value.</p> <p><strong>Arguments</strong></p> <ul> <li><strong>name</strong>: A string. the name of parameter. Must be unique for each <code>HyperParameter</code> instance in the search space.</li> <li><strong>value</strong>: The value to use (can be any JSON-serializable Python type).</li> <li><strong>parent_name</strong>: Optional string, specifying the name of the parent <code>HyperParameter</code> to use as the condition to activate the current <code>HyperParameter</code>.</li> <li><strong>parent_values</strong>: Optional list of the values of the parent <code>HyperParameter</code> to use as the condition to activate the current <code>HyperParameter</code>.</li> </ul> <p><strong>Returns</strong></p> <p>The value of the hyperparameter, or None if the hyperparameter is not active.</p> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras-tuner/tree/v1.4.7/keras_tuner/src/engine/hyperparameters/hyperparameters.py#L401">[source]</a></span></p> <h3 id="float-method"><code>Float</code> method</h3> <div class="codehilite"><pre><span></span><code><span class="n">HyperParameters</span><span class="o">.</span><span class="n">Float</span><span class="p">(</span> <span class="n">name</span><span class="p">,</span> <span class="n">min_value</span><span class="p">,</span> <span class="n">max_value</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">sampling</span><span class="o">=</span><span class="s2">"linear"</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">parent_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">parent_values</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="p">)</span> </code></pre></div> <p>Floating point value hyperparameter.</p> <p>Example #1:</p> <div class="codehilite"><pre><span></span><code><span class="n">hp</span><span class="o">.</span><span class="n">Float</span><span class="p">(</span> <span class="s2">"image_rotation_factor"</span><span class="p">,</span> <span class="n">min_value</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">max_value</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> </code></pre></div> <p>All values in interval [0, 1] have equal probability of being sampled.</p> <p>Example #2:</p> <div class="codehilite"><pre><span></span><code><span class="n">hp</span><span class="o">.</span><span class="n">Float</span><span class="p">(</span> <span class="s2">"image_rotation_factor"</span><span class="p">,</span> <span class="n">min_value</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">max_value</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">0.2</span><span class="p">)</span> </code></pre></div> <p><code>step</code> is the minimum distance between samples. The possible values are [0, 0.2, 0.4, 0.6, 0.8, 1.0].</p> <p>Example #3:</p> <div class="codehilite"><pre><span></span><code><span class="n">hp</span><span class="o">.</span><span class="n">Float</span><span class="p">(</span> <span class="s2">"learning_rate"</span><span class="p">,</span> <span class="n">min_value</span><span class="o">=</span><span class="mf">0.001</span><span class="p">,</span> <span class="n">max_value</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">sampling</span><span class="o">=</span><span class="s2">"log"</span><span class="p">)</span> </code></pre></div> <p>When <code>sampling="log"</code>, the <code>step</code> is multiplied between samples. The possible values are [0.001, 0.01, 0.1, 1, 10].</p> <p><strong>Arguments</strong></p> <ul> <li><strong>name</strong>: A string. the name of parameter. Must be unique for each <code>HyperParameter</code> instance in the search space.</li> <li><strong>min_value</strong>: Float, the lower bound of the range.</li> <li><strong>max_value</strong>: Float, the upper bound of the range.</li> <li><strong>step</strong>: Optional float, the distance between two consecutive samples in the range. If left unspecified, it is possible to sample any value in the interval. If <code>sampling="linear"</code>, it will be the minimum additve between two samples. If <code>sampling="log"</code>, it will be the minimum multiplier between two samples.</li> <li><strong>sampling</strong>: String. One of "linear", "log", "reverse_log". Defaults to "linear". When sampling value, it always start from a value in range [0.0, 1.0). The <code>sampling</code> argument decides how the value is projected into the range of [min_value, max_value]. "linear": min_value + value * (max_value - min_value) "log": min_value * (max_value / min_value) ^ value "reverse_log": (max_value - min_value * ((max_value / min_value) ^ (1 - value) - 1))</li> <li><strong>default</strong>: Float, the default value to return for the parameter. If unspecified, the default value will be <code>min_value</code>.</li> <li><strong>parent_name</strong>: Optional string, specifying the name of the parent <code>HyperParameter</code> to use as the condition to activate the current <code>HyperParameter</code>.</li> <li><strong>parent_values</strong>: Optional list of the values of the parent <code>HyperParameter</code> to use as the condition to activate the current <code>HyperParameter</code>.</li> </ul> <p><strong>Returns</strong></p> <p>The value of the hyperparameter, or None if the hyperparameter is not active.</p> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras-tuner/tree/v1.4.7/keras_tuner/src/engine/hyperparameters/hyperparameters.py#L302">[source]</a></span></p> <h3 id="int-method"><code>Int</code> method</h3> <div class="codehilite"><pre><span></span><code><span class="n">HyperParameters</span><span class="o">.</span><span class="n">Int</span><span class="p">(</span> <span class="n">name</span><span class="p">,</span> <span class="n">min_value</span><span class="p">,</span> <span class="n">max_value</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">sampling</span><span class="o">=</span><span class="s2">"linear"</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">parent_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">parent_values</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="p">)</span> </code></pre></div> <p>Integer hyperparameter.</p> <p>Note that unlike Python's <code>range</code> function, <code>max_value</code> is <em>included</em> in the possible values this parameter can take on.</p> <p>Example #1:</p> <div class="codehilite"><pre><span></span><code><span class="n">hp</span><span class="o">.</span><span class="n">Int</span><span class="p">(</span> <span class="s2">"n_layers"</span><span class="p">,</span> <span class="n">min_value</span><span class="o">=</span><span class="mi">6</span><span class="p">,</span> <span class="n">max_value</span><span class="o">=</span><span class="mi">12</span><span class="p">)</span> </code></pre></div> <p>The possible values are [6, 7, 8, 9, 10, 11, 12].</p> <p>Example #2:</p> <div class="codehilite"><pre><span></span><code><span class="n">hp</span><span class="o">.</span><span class="n">Int</span><span class="p">(</span> <span class="s2">"n_layers"</span><span class="p">,</span> <span class="n">min_value</span><span class="o">=</span><span class="mi">6</span><span class="p">,</span> <span class="n">max_value</span><span class="o">=</span><span class="mi">13</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span> </code></pre></div> <p><code>step</code> is the minimum distance between samples. The possible values are [6, 9, 12].</p> <p>Example #3:</p> <div class="codehilite"><pre><span></span><code><span class="n">hp</span><span class="o">.</span><span class="n">Int</span><span class="p">(</span> <span class="s2">"batch_size"</span><span class="p">,</span> <span class="n">min_value</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">max_value</span><span class="o">=</span><span class="mi">32</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">sampling</span><span class="o">=</span><span class="s2">"log"</span><span class="p">)</span> </code></pre></div> <p>When <code>sampling="log"</code> the <code>step</code> is multiplied between samples. The possible values are [2, 4, 8, 16, 32].</p> <p><strong>Arguments</strong></p> <ul> <li><strong>name</strong>: A string. the name of parameter. Must be unique for each <code>HyperParameter</code> instance in the search space.</li> <li><strong>min_value</strong>: Integer, the lower limit of range, inclusive.</li> <li><strong>max_value</strong>: Integer, the upper limit of range, inclusive.</li> <li><strong>step</strong>: Optional integer, the distance between two consecutive samples in the range. If left unspecified, it is possible to sample any integers in the interval. If <code>sampling="linear"</code>, it will be the minimum additve between two samples. If <code>sampling="log"</code>, it will be the minimum multiplier between two samples.</li> <li><strong>sampling</strong>: String. One of "linear", "log", "reverse_log". Defaults to "linear". When sampling value, it always start from a value in range [0.0, 1.0). The <code>sampling</code> argument decides how the value is projected into the range of [min_value, max_value]. "linear": min_value + value * (max_value - min_value) "log": min_value * (max_value / min_value) ^ value "reverse_log": (max_value - min_value * ((max_value / min_value) ^ (1 - value) - 1))</li> <li><strong>default</strong>: Integer, default value to return for the parameter. If unspecified, the default value will be <code>min_value</code>.</li> <li><strong>parent_name</strong>: Optional string, specifying the name of the parent <code>HyperParameter</code> to use as the condition to activate the current <code>HyperParameter</code>.</li> <li><strong>parent_values</strong>: Optional list of the values of the parent <code>HyperParameter</code> to use as the condition to activate the current <code>HyperParameter</code>.</li> </ul> <p><strong>Returns</strong></p> <p>The value of the hyperparameter, or None if the hyperparameter is not active.</p> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras-tuner/tree/v1.4.7/keras_tuner/src/engine/hyperparameters/hyperparameters.py#L87">[source]</a></span></p> <h3 id="conditionalscope-method"><code>conditional_scope</code> method</h3> <div class="codehilite"><pre><span></span><code><span class="n">HyperParameters</span><span class="o">.</span><span class="n">conditional_scope</span><span class="p">(</span><span class="n">parent_name</span><span class="p">,</span> <span class="n">parent_values</span><span class="p">)</span> </code></pre></div> <p>Opens a scope to create conditional HyperParameters.</p> <p>All <code>HyperParameter</code>s created under this scope will only be active when the parent <code>HyperParameter</code> specified by <code>parent_name</code> is equal to one of the values passed in <code>parent_values</code>.</p> <p>When the condition is not met, creating a <code>HyperParameter</code> under this scope will register the <code>HyperParameter</code>, but will return <code>None</code> rather than a concrete value.</p> <p>Note that any Python code under this scope will execute regardless of whether the condition is met.</p> <p>This feature is for the <code>Tuner</code> to collect more information of the search space and the current trial. It is especially useful for model selection. If the parent <code>HyperParameter</code> is for model selection, the <code>HyperParameter</code>s in a model should only be active when the model selected, which can be implemented using <code>conditional_scope</code>.</p> <p><strong>Examples</strong></p> <div class="codehilite"><pre><span></span><code><span class="k">def</span> <span class="nf">MyHyperModel</span><span class="p">(</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">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">Input</span><span class="p">(</span><span class="n">shape</span><span class="o">=</span><span class="p">(</span><span class="mi">32</span><span class="p">,</span> <span class="mi">32</span><span class="p">,</span> <span class="mi">3</span><span class="p">)))</span> <span class="n">model_type</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">"model_type"</span><span class="p">,</span> <span class="p">[</span><span class="s2">"mlp"</span><span class="p">,</span> <span class="s2">"cnn"</span><span class="p">])</span> <span class="k">with</span> <span class="n">hp</span><span class="o">.</span><span class="n">conditional_scope</span><span class="p">(</span><span class="s2">"model_type"</span><span class="p">,</span> <span class="p">[</span><span class="s2">"mlp"</span><span class="p">]):</span> <span class="k">if</span> <span class="n">model_type</span> <span class="o">==</span> <span class="s2">"mlp"</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">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">Dense</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="s1">'relu'</span><span class="p">))</span> <span class="k">with</span> <span class="n">hp</span><span class="o">.</span><span class="n">conditional_scope</span><span class="p">(</span><span class="s2">"model_type"</span><span class="p">,</span> <span class="p">[</span><span class="s2">"cnn"</span><span class="p">]):</span> <span class="k">if</span> <span class="n">model_type</span> <span class="o">==</span> <span class="s2">"cnn"</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">Conv2D</span><span class="p">(</span><span class="mi">64</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="n">activation</span><span class="o">=</span><span class="s1">'relu'</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">GlobalAveragePooling2D</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">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="s1">'softmax'</span><span class="p">))</span> <span class="k">return</span> <span class="n">model</span> </code></pre></div> <p><strong>Arguments</strong></p> <ul> <li><strong>parent_name</strong>: A string, specifying the name of the parent <code>HyperParameter</code> to use as the condition to activate the current <code>HyperParameter</code>.</li> <li><strong>parent_values</strong>: A list of the values of the parent <code>HyperParameter</code> to use as the condition to activate the current <code>HyperParameter</code>.</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/hyperparameters/hyperparameters.py#L238">[source]</a></span></p> <h3 id="get-method"><code>get</code> method</h3> <div class="codehilite"><pre><span></span><code><span class="n">HyperParameters</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">name</span><span class="p">)</span> </code></pre></div> <p>Return the current value of this hyperparameter set.</p> <hr /> </div> <div class='k-outline'> <div class='k-outline-depth-1'> <a href='#hyperparameters'>HyperParameters</a> </div> <div class='k-outline-depth-3'> <a href='#hyperparameters-class'><code>HyperParameters</code> class</a> </div> <div class='k-outline-depth-3'> <a href='#boolean-method'><code>Boolean</code> method</a> </div> <div class='k-outline-depth-3'> <a href='#choice-method'><code>Choice</code> method</a> </div> <div class='k-outline-depth-3'> <a href='#fixed-method'><code>Fixed</code> method</a> </div> <div class='k-outline-depth-3'> <a href='#float-method'><code>Float</code> method</a> </div> <div class='k-outline-depth-3'> <a href='#int-method'><code>Int</code> method</a> </div> <div class='k-outline-depth-3'> <a href='#conditionalscope-method'><code>conditional_scope</code> method</a> </div> <div class='k-outline-depth-3'> <a href='#get-method'><code>get</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>