8000 tsai/optuna.html at gh-pages · yuqinie98/tsai · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
989 lines (961 loc) · 58.9 KB

optuna.html

File metadata and controls

989 lines (961 loc) · 58.9 KB
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./models.minirocketplus_pytorch.html" class="sidebar-item-text sidebar-link">MINIROCKETPlus Pytorch</a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-10" aria-expanded="false">Tabular models</a>
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-10" aria-expanded="false">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-10" class="collapse list-unstyled sidebar-section depth2 ">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./models.tabmodel.html" class="sidebar-item-text sidebar-link">TabModel</a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./models.tabtransformer.html" class="sidebar-item-text sidebar-link">TabTransformer</a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./models.gatedtabtransformer.html" class="sidebar-item-text sidebar-link">GatedTabTransformer</a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./models.tabfusiontransformer.html" class="sidebar-item-text sidebar-link">TabFusionTransformer</a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-11" aria-expanded="false">Miscellaneous</a>
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-11" aria-expanded="false">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-11" class="collapse list-unstyled sidebar-section depth2 ">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./models.multiinputnet.html" class="sidebar-item-text sidebar-link">MultiInputNet</a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./models.misc.html" class="sidebar-item-text sidebar-link">Miscellaneous</a>
</div>
</li>
</ul>
</li>
</ul>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-12" aria-expanded="true">Training</a>
<a class="sidebar-item-toggle text-start" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-12" aria-expanded="true">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-12" class="collapse list-unstyled sidebar-section depth1 show">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./learner.html" class="sidebar-item-text sidebar-link">Learner</a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./tslearner.html" class="sidebar-item-text sidebar-link">TSLearner</a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./optimizer.html" class="sidebar-item-text sidebar-link">Optimizers</a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./metrics.html" class="sidebar-item-text sidebar-link">Metrics</a>
</div>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-13" aria-expanded="false">Callbacks</a>
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-13" aria-expanded="false">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-13" class="collapse list-unstyled sidebar-section depth2 ">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./callback.core.html" class="sidebar-item-text sidebar-link">Callback</a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./callback.predictiondynamics.html" class="sidebar-item-text sidebar-link">PredictionDynamics</a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./callback.noisy_student.html" class="sidebar-item-text sidebar-link">Noisy student</a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./callback.mvp.html" class="sidebar-item-text sidebar-link">MVP (aka TSBERT)</a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./callback.experimental.html" class="sidebar-item-text sidebar-link">Experimental Callbacks</a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./calibration.html" class="sidebar-item-text sidebar-link">Calibration</a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-14" aria-expanded="true">HPO &amp; experiment tracking</a>
<a class="sidebar-item-toggle text-start" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-14" aria-expanded="true">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-14" class="collapse list-unstyled sidebar-section depth1 show">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./optuna.html" class="sidebar-item-text sidebar-link active">Optuna</a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./wandb.html" class="sidebar-item-text sidebar-link">Weights &amp; Biases Sweeps</a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./inference.html" class="sidebar-item-text sidebar-link">Inference</a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./analysis.html" class="sidebar-item-text sidebar-link">Analysis</a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./models.explainability.html" class="sidebar-item-text sidebar-link">Explainability</a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./utils.html" class="sidebar-item-text sidebar-link">Utilities</a>
</div>
</li>
</ul>
</div>
</nav>
<!-- margin-sidebar -->
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
<nav id="TOC" role="doc-toc" class="toc-active">
<h2 id="toc-title">On this page</h2>
<ul>
<li><a href="#run_optuna_study" id="toc-run_optuna_study" class="nav-link active" data-scroll-target="#run_optuna_study">run_optuna_study</a></li>
</ul>
<div class="toc-actions"><div><i class="bi bi-github"></i></div><div class="action-links"><p><a href="https://github.com/timeseriesAI/tsai/issues/new" class="toc-action">Report an issue</a></p></div></div></nav>
</div>
<!-- main -->
<main class="content" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title d-none d-lg-block">Optuna</h1>
</div>
<div class="quarto-title-meta">
</div>
</header>
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->
<blockquote class="blockquote">
<p>A hyperparameter optimization framework</p>
</blockquote>
<p>Optuna is an automatic hyperparameter optimization software framework, particularly designed for machine learning. It features an imperative, define-by-run style user API. Thanks to our define-by-run API, the code written with Optuna enjoys high modularity, and the user of Optuna can dynamically construct the search spaces for the hyperparameters.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> run_optuna_study(objective, resume<span class="op">=</span><span class="va">None</span>, study_type<span class="op">=</span><span class="va">None</span>, multivariate<span class="op">=</span><span class="va">True</span>, search_space<span class="op">=</span><span class="va">None</span>, evaluate<span class="op">=</span><span class="va">None</span>, seed<span class="op">=</span><span class="va">None</span>, sampler<span class="op">=</span><span class="va">None</span>, pruner<span class="op">=</span><span class="va">None</span>, </span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a> study_name<span class="op">=</span><span class="va">None</span>, direction<span class="op">=</span><span class="st">'maximize'</span>, load_if_exists<span class="op">=</span><span class="va">False</span>, n_trials<span class="op">=</span><span class="va">None</span>, timeout<span class="op">=</span><span class="va">None</span>, gc_after_trial<span class="op">=</span><span class="va">False</span>, show_progress_bar<span class="op">=</span><span class="va">True</span>, </span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a> save_study<span class="op">=</span><span class="va">True</span>, path<span class="op">=</span><span class="st">'optuna'</span>, show_plots<span class="op">=</span><span class="va">True</span>):</span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a> <span class="co">r"""Creates and runs an optuna study.</span></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="co"> Args: </span></span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a><span class="co"> objective: A callable that implements objective function.</span></span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a><span class="co"> resume: Path to a previously saved study.</span></span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a><span class="co"> study_type: Type of study selected (bayesian, gridsearch, randomsearch). Based on this a sampler will be build if sampler is None. </span></span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a><span class="co"> If a sampler is passed, this has no effect.</span></span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a><span class="co"> multivariate: If this is True, the multivariate TPE is used when suggesting parameters. The multivariate TPE is reported to outperform </span></span>
<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a><span class="co"> the independent TPE.</span></span>
<span id="cb1-13"><a href="#cb1-13" aria-hidden="true" tabindex="-1"></a><span class="co"> search_space: Search space required when running a gridsearch (if you don't pass a sampler).</span></span>
<span id="cb1-14"><a href="#cb1-14" aria-hidden="true" tabindex="-1"></a><span class="co"> evaluate: Allows you to pass a specific set of hyperparameters that will be evaluated.</span></span>
<span id="cb1-15"><a href="#cb1-15" aria-hidden="true" tabindex="-1"></a><span class="co"> seed: Fixed seed used by samplers.</span></span>
<span id="cb1-16"><a href="#cb1-16" aria-hidden="true" tabindex="-1"></a><span class="co"> sampler: A sampler object that implements background algorithm for value suggestion. If None is specified, TPESampler is used during </span></span>
<span id="cb1-17"><a href="#cb1-17" aria-hidden="true" tabindex="-1"></a><span class="co"> single-objective optimization and NSGAIISampler during multi-objective optimization. See also samplers.</span></span>
<span id="cb1-18"><a href="#cb1-18" aria-hidden="true" tabindex="-1"></a><span class="co"> pruner: A pruner object that decides early stopping of unpromising trials. If None is specified, MedianPruner is used as the default. </span></span>
<span id="cb1-19"><a href="#cb1-19" aria-hidden="true" tabindex="-1"></a><span class="co"> See also pruners.</span></span>
<span id="cb1-20"><a href="#cb1-20" aria-hidden="true" tabindex="-1"></a><span class="co"> study_name: Study’s name. If this argument is set to None, a unique name is generated automatically.</span></span>
<span id="cb1-21"><a href="#cb1-21" aria-hidden="true" tabindex="-1"></a><span class="co"> direction: A sequence of directions during multi-objective optimization.</span></span>
<span id="cb1-22"><a href="#cb1-22" aria-hidden="true" tabindex="-1"></a><span class="co"> n_trials: The number of trials. If this argument is set to None, there is no limitation on the number of trials. If timeout is also set to </span></span>
<span id="cb1-23"><a href="#cb1-23" aria-hidden="true" tabindex="-1"></a><span class="co"> None, the study continues to create trials until it receives a termination signal such as Ctrl+C or SIGTERM.</span></span>
<span id="cb1-24"><a href="#cb1-24" aria-hidden="true" tabindex="-1"></a><span class="co"> timeout: Stop study after the given number of second(s). If this argument is set to None, the study is executed without time limitation. </span></span>
<span id="cb1-25"><a href="#cb1-25" aria-hidden="true" tabindex="-1"></a><span class="co"> If n_trials is also set to None, the study continues to create trials until it receives a termination signal such as </span></span>
<span id="cb1-26"><a href="#cb1-26" aria-hidden="true" tabindex="-1"></a><span class="co"> Ctrl+C or SIGTERM.</span></span>
<span id="cb1-27"><a href="#cb1-27" aria-hidden="true" tabindex="-1"></a><span class="co"> gc_after_trial: Flag to execute garbage collection at the end of each trial. By default, garbage collection is enabled, just in case. </span></span>
<span id="cb1-28"><a href="#cb1-28" aria-hidden="true" tabindex="-1"></a><span class="co"> You can turn it off with this argument if memory is safely managed in your objective function.</span></span>
<span id="cb1-29"><a href="#cb1-29" aria-hidden="true" tabindex="-1"></a><span class="co"> show_progress_bar: Flag to show progress bars or not. To disable progress bar, set this False.</span></span>
<span id="cb1-30"><a href="#cb1-30" aria-hidden="true" tabindex="-1"></a><span class="co"> save_study: Save your study when finished/ interrupted.</span></span>
<span id="cb1-31"><a href="#cb1-31" aria-hidden="true" tabindex="-1"></a><span class="co"> path: Folder where the study will be saved.</span></span>
<span id="cb1-32"><a href="#cb1-32" aria-hidden="true" tabindex="-1"></a><span class="co"> show_plots: Flag to control whether plots are shown at the end of the study.</span></span>
<span id="cb1-33"><a href="#cb1-33" aria-hidden="true" tabindex="-1"></a><span class="co"> """</span></span>
<span id="cb1-34"><a href="#cb1-34" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb1-35"><a href="#cb1-35" aria-hidden="true" tabindex="-1"></a> <span class="cf">try</span>: <span class="im">import</span> optuna</span>
<span id="cb1-36"><a href="#cb1-36" aria-hidden="true" tabindex="-1"></a> <span class="cf">except</span> <span class="pp">ImportError</span>: <span class="cf">raise</span> <span class="pp">ImportError</span>(<span class="st">'You need to install optuna to use run_optuna_study'</span>)</span>
<span id="cb1-37"><a href="#cb1-37" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-38"><a href="#cb1-38" aria-hidden="true" tabindex="-1"></a> <span class="co"># Sampler</span></span>
<span id="cb1-39"><a href="#cb1-39" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> sampler <span class="kw">is</span> <span class="va">None</span>:</span>
<span id="cb1-40"><a href="#cb1-40" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> study_type <span class="kw">is</span> <span class="va">None</span> <span class="kw">or</span> <span class="st">"bayes"</span> <span class="kw">in</span> study_type.lower(): </span>
<span id="cb1-41"><a href="#cb1-41" aria-hidden="true" tabindex="-1"></a> sampler <span class="op">=</span> optuna.samplers.TPESampler(seed<span class="op">=</span>seed, multivariate<span class="op">=</span>multivariate)</span>
<span id="cb1-42"><a href="#cb1-42" aria-hidden="true" tabindex="-1"></a> <span class="cf">elif</span> <span class="st">"grid"</span> <span class="kw">in</span> study_type.lower():</span>
<span id="cb1-43"><a href="#cb1-43" aria-hidden="true" tabindex="-1"></a> <span class="cf">assert</span> search_space, <span class="ss">f"you need to pass a search_space dict to run a gridsearch"</span></span>
<span id="cb1-44"><a href="#cb1-44" aria-hidden="true" tabindex="-1"></a> sampler <span class="op">=</span> optuna.samplers.GridSampler(search_space)</span>
<span id="cb1-45"><a href="#cb1-45" aria-hidden="true" tabindex="-1"></a> <span class="cf">elif</span> <span class="st">"random"</span> <span class="kw">in</span> study_type.lower(): </span>
<span id="cb1-46"><a href="#cb1-46" aria-hidden="true" tabindex="-1"></a> sampler <span class="op">=</span> optuna.samplers.RandomSampler(seed<span class="op">=</span>seed)</span>
<span id="cb1-47"><a href="#cb1-47" aria-hidden="true" tabindex="-1"></a> <span class="cf">assert</span> sampler, <span class="st">"you need to either select a study type (bayesian, gridsampler, randomsampler) or pass a sampler"</span></span>
<span id="cb1-48"><a href="#cb1-48" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-49"><a href="#cb1-49" aria-hidden="true" tabindex="-1"></a> <span class="co"># Study</span></span>
<span id="cb1-50"><a href="#cb1-50" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> resume: </span>
<span id="cb1-51"><a href="#cb1-51" aria-hidden="true" tabindex="-1"></a> <span class="cf">try</span>:</span>
<span id="cb1-52"><a href="#cb1-52" aria-hidden="true" tabindex="-1"></a> study <span class="op">=</span> joblib.load(resume)</span>
<span id="cb1-53"><a href="#cb1-53" aria-hidden="true" tabindex="-1"></a> <span class="cf">except</span>: </span>
<span id="cb1-54"><a href="#cb1-54" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f"joblib.load(</span><span class="sc">{</span>resume<span class="sc">}</span><span class="ss">) couldn't recover any saved study. Check the path."</span>)</span>
<span id="cb1-55"><a href="#cb1-55" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span></span>
<span id="cb1-56"><a href="#cb1-56" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="st">"Best trial until now:"</span>)</span>
<span id="cb1-57"><a href="#cb1-57" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="st">" Value: "</span>, study.best_trial.value)</span>
<span id="cb1-58"><a href="#cb1-58" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="st">" Params: "</span>)</span>
<span id="cb1-59"><a href="#cb1-59" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> key, value <span class="kw">in</span> study.best_trial.params.items():</span>
<span id="cb1-60"><a href="#cb1-60" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f" </span><span class="sc">{</span>key<span class="sc">}</span><span class="ss">: </span><span class="sc">{</span>value<span class="sc">}</span><span class="ss">"</span>)</span>
<span id="cb1-61"><a href="#cb1-61" aria-hidden="true" tabindex="-1"></a> <span class="cf">else</span>: </span>
<span id="cb1-62"><a href="#cb1-62" aria-hidden="true" tabindex="-1"></a> study <span class="op">=</span> optuna.create_study(sampler<span class="op">=</span>sampler, pruner<span class="op">=</span>pruner, study_name<span class="op">=</span>study_name, direction<span class="op">=</span>direction)</span>
<span id="cb1-63"><a href="#cb1-63" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> evaluate: study.enqueue_trial(evaluate)</span>
<span id="cb1-64"><a href="#cb1-64" aria-hidden="true" tabindex="-1"></a> <span class="cf">try</span>:</span>
<span id="cb1-65"><a href="#cb1-65" aria-hidden="true" tabindex="-1"></a> study.optimize(objective, n_trials<span class="op">=</span>n_trials, timeout<span class="op">=</span>timeout, gc_after_trial<span class="op">=</span>gc_after_trial, show_progress_bar<span class="op">=</span>show_progress_bar)</span>
<span id="cb1-66"><a href="#cb1-66" aria-hidden="true" tabindex="-1"></a> <span class="cf">except</span> <span class="pp">KeyboardInterrupt</span>:</span>
<span id="cb1-67"><a href="#cb1-67" aria-hidden="true" tabindex="-1"></a> <span class="cf">pass</span></span>
<span id="cb1-68"><a href="#cb1-68" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-69"><a href="#cb1-69" aria-hidden="true" tabindex="-1"></a> <span class="co"># Save</span></span>
<span id="cb1-70"><a href="#cb1-70" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> save_study:</span>
<span id="cb1-71"><a href="#cb1-71" aria-hidden="true" tabindex="-1"></a> full_path <span class="op">=</span> Path(path)<span class="op">/</span><span class="ss">f'</span><span class="sc">{</span>study<span class="sc">.</span>study_name<span class="sc">}</span><span class="ss">.pkl'</span></span>
<span id="cb1-72"><a href="#cb1-72" aria-hidden="true" tabindex="-1"></a> full_path.parent.mkdir(parents<span class="op">=</span><span class="va">True</span>, exist_ok<span class="op">=</span><span class="va">True</span>)</span>
<span id="cb1-73"><a href="#cb1-73" aria-hidden="true" tabindex="-1"></a> joblib.dump(study, full_path)</span>
<span id="cb1-74"><a href="#cb1-74" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f'</span><span class="ch">\n</span><span class="ss">Optuna study saved to </span><span class="sc">{</span>full_path<span class="sc">}</span><span class="ss">'</span>)</span>
<span id="cb1-75"><a href="#cb1-75" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f"To reload the study run: study = joblib.load('</span><span class="sc">{</span>full_path<span class="sc">}</span><span class="ss">')"</span>)</span>
<span id="cb1-76"><a href="#cb1-76" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-77"><a href="#cb1-77" aria-hidden="true" tabindex="-1"></a> <span class="co"># Plots</span></span>
<span id="cb1-78"><a href="#cb1-78" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> show_plots <span class="kw">and</span> <span class="bu">len</span>(study.trials) <span class="op">&gt;</span> <span class="dv">1</span>:</span>
<span id="cb1-79"><a href="#cb1-79" aria-hidden="true" tabindex="-1"></a> <span class="cf">try</span>: display(optuna.visualization.plot_optimization_history(study))</span>
<span id="cb1-80"><a href="#cb1-80" aria-hidden="true" tabindex="-1"></a> <span class="cf">except</span>: <span class="cf">pass</span></span>
<span id="cb1-81"><a href="#cb1-81" aria-hidden="true" tabindex="-1"></a> <span class="cf">try</span>: display(optuna.visualization.plot_param_importances(study))</span>
<span id="cb1-82"><a href="#cb1-82" aria-hidden="true" tabindex="-1"></a> <span class="cf">except</span>: <span class="cf">pass</span></span>
<span id="cb1-83"><a href="#cb1-83" aria-hidden="true" tabindex="-1"></a> <span class="cf">try</span>: display(optuna.visualization.plot_slice(study))</span>
<span id="cb1-84"><a href="#cb1-84" aria-hidden="true" tabindex="-1"></a> <span class="cf">except</span>: <span class="cf">pass</span></span>
<span id="cb1-85"><a href="#cb1-85" aria-hidden="true" tabindex="-1"></a> <span class="cf">try</span>: display(optuna.visualization.plot_parallel_coordinate(study))</span>
<span id="cb1-86"><a href="#cb1-86" aria-hidden="true" tabindex="-1"></a> <span class="cf">except</span>: <span class="cf">pass</span></span>
<span id="cb1-87"><a href="#cb1-87" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-88"><a href="#cb1-88" aria-hidden="true" tabindex="-1"></a> <span class="co"># Study stats</span></span>
<span id="cb1-89"><a href="#cb1-89" aria-hidden="true" tabindex="-1"></a> <span class="cf">try</span>:</span>
<span id="cb1-90"><a href="#cb1-90" aria-hidden="true" tabindex="-1"></a> pruned_trials <span class="op">=</span> [t <span class="cf">for</span> t <span class="kw">in</span> study.trials <span class="cf">if</span> t.state <span class="op">==</span> optuna.trial.TrialState.PRUNED]</span>
<span id="cb1-91"><a href="#cb1-91" aria-hidden="true" tabindex="-1"></a> complete_trials <span class="op">=</span> [t <span class="cf">for</span> t <span class="kw">in</span> study.trials <span class="cf">if</span> t.state <span class="op">==</span> optuna.trial.TrialState.COMPLETE]</span>
<span id="cb1-92"><a href="#cb1-92" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f"</span><span class="ch">\n</span><span class="ss">Study statistics : "</span>)</span>
<span id="cb1-93"><a href="#cb1-93" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f" Study name : </span><span class="sc">{</span>study<span class="sc">.</span>study_name<span class="sc">}</span><span class="ss">"</span>)</span>
<span id="cb1-94"><a href="#cb1-94" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f" # finished trials : </span><span class="sc">{</span><span class="bu">len</span>(study.trials)<span class="sc">}</span><span class="ss">"</span>)</span>
<span id="cb1-95"><a href="#cb1-95" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f" # pruned trials : </span><span class="sc">{</span><span class="bu">len</span>(pruned_trials)<span class="sc">}</span><span class="ss">"</span>)</span>
<span id="cb1-96"><a href="#cb1-96" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f" # complete trials : </span><span class="sc">{</span><span class="bu">len</span>(complete_trials)<span class="sc">}</span><span class="ss">"</span>)</span>
<span id="cb1-97"><a href="#cb1-97" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb1-98"><a href="#cb1-98" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f"</span><span class="ch">\n</span><span class="ss">Best trial :"</span>)</span>
<span id="cb1-99"><a href="#cb1-99" aria-hidden="true" tabindex="-1"></a> trial <span class="op">=</span> study.best_trial</span>
<span id="cb1-100"><a href="#cb1-100" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f" value : </span><span class="sc">{</span>trial<span class="sc">.</span>value<span class="sc">}</span><span class="ss">"</span>)</span>
<span id="cb1-101"><a href="#cb1-101" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f" best_params = </span><span class="sc">{</span>trial<span class="sc">.</span>params<span class="sc">}</span><span class="ch">\n</span><span class="ss">"</span>)</span>
<span id="cb1-102"><a href="#cb1-102" aria-hidden="true" tabindex="-1"></a> <span class="cf">except</span>:</span>
<span id="cb1-103"><a href="#cb1-103" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="st">'</span><span class="ch">\n</span><span class="st">No finished trials yet.'</span>)</span>
<span id="cb1-104"><a href="#cb1-104" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> study</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<hr>
<p><a href="https://github.com/timeseriesAI/tsai/blob/main/tsai/optuna.py#L16" target="_blank" style="float:right; font-size:smaller">source</a></p>
<section id="run_optuna_study" class="level3">
<h3 class="anchored" data-anchor-id="run_optuna_study">run_optuna_study</h3>
<blockquote class="blockquote">
<pre><code> run_optuna_study (objective, resume=None, study_type=None,
multivariate=True, search_space=None, evaluate=None,
seed=None, sampler=None, pruner=None, study_name=None,
direction='maximize', load_if_exists=False,
n_trials=None, timeout=None, gc_after_trial=False,
show_progress_bar=True, save_study=True, path='optuna',
show_plots=True)</code></pre>
</blockquote>
<p>Creates and runs an optuna study.</p>
<p>Args: objective: A callable that implements objective function. resume: Path to a previously saved study. study_type: Type of study selected (bayesian, gridsearch, randomsearch). Based on this a sampler will be build if sampler is None. If a sampler is passed, this has no effect. multivariate: If this is True, the multivariate TPE is used when suggesting parameters. The multivariate TPE is reported to outperform the independent TPE. search_space: Search space required when running a gridsearch (if you don’t pass a sampler). evaluate: Allows you to pass a specific set of hyperparameters that will be evaluated. seed: Fixed seed used by samplers. sampler: A sampler object that implements background algorithm for value suggestion. If None is specified, TPESampler is used during single-objective optimization and NSGAIISampler during multi-objective optimization. See also samplers. pruner: A pruner object that decides early stopping of unpromising trials. If None is specified, MedianPruner is used as the default. See also pruners. study_name: Study’s name. If this argument is set to None, a unique name is generated automatically. direction: A sequence of directions during multi-objective optimization. n_trials: The number of trials. If this argument is set to None, there is no limitation on the number of trials. If timeout is also set to None, the study continues to create trials until it receives a termination signal such as Ctrl+C or SIGTERM. timeout: Stop study after the given number of second(s). If this argument is set to None, the study is executed without time limitation. If n_trials is also set to None, the study continues to create trials until it receives a termination signal such as Ctrl+C or SIGTERM. gc_after_trial: Flag to execute garbage collection at the end of each trial. By default, garbage collection is enabled, just in case. You can turn it off with this argument if memory is safely managed in your objective function. show_progress_bar: Flag to show progress bars or not. To disable progress bar, set this False. save_study: Save your study when finished/ interrupted. path: Folder where the study will be saved. show_plots: Flag to control whether plots are shown at the end of the study.</p>
</section>
</main> <!-- /main -->
<script id="quarto-html-after-body" type="application/javascript">
window.document.addEventListener("DOMContentLoaded", function (event) {
const toggleBodyColorMode = (bsSheetEl) => {
const mode = bsSheetEl.getAttribute("data-mode");
const bodyEl = window.document.querySelector("body");
if (mode === "dark") {
bodyEl.classList.add("quarto-dark");
bodyEl.classList.remove("quarto-light");
} else {
bodyEl.classList.add("quarto-light");
bodyEl.classList.remove("quarto-dark");
}
}
const toggleBodyColorPrimary = () => {
const bsSheetEl = window.document.querySelector("link#quarto-bootstrap");
if (bsSheetEl) {
toggleBodyColorMode(bsSheetEl);
}
}
toggleBodyColorPrimary();
const icon = "";
const anchorJS = new window.AnchorJS();
anchorJS.options = {
placement: 'right',
icon: icon
};
anchorJS.add('.anchored');
const clipboard = new window.ClipboardJS('.code-copy-button', {
target: function(trigger) {
return trigger.previousElementSibling;
}
});
clipboard.on('success', function(e) {
// button target
const button = e.trigger;
// don't keep focus
button.blur();
// flash "checked"
button.classList.add('code-copy-button-checked');
var currentTitle = button.getAttribute("title");
button.setAttribute("title", "Copied!");
let tooltip;
if (window.bootstrap) {
button.setAttribute("data-bs-toggle", "tooltip");
button.setAttribute("data-bs-placement", "left");
button.setAttribute("data-bs-title", "Copied!");
tooltip = new bootstrap.Tooltip(button,
{ trigger: "manual",
customClass: "code-copy-button-tooltip",
offset: [0, -8]});
tooltip.show();
}
setTimeout(function() {
if (tooltip) {
tooltip.hide();
button.removeAttribute("data-bs-title");
button.removeAttribute("data-bs-toggle");
button.removeAttribute("data-bs-placement");
}
button.setAttribute("title", currentTitle);
button.classList.remove('code-copy-button-checked');
}, 1000);
// clear code selection
e.clearSelection();
});
function tippyHover(el, contentFn) {
const config = {
allowHTML: true,
content: contentFn,
maxWidth: 500,
delay: 100,
arrow: false,
appendTo: function(el) {
return el.parentElement;
},
interactive: true,
interactiveBorder: 10,
theme: 'quarto',
placement: 'bottom-start'
};
window.tippy(el, config);
}
const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]');
for (var i=0; i<noterefs.length; i++) {
const ref = noterefs[i];
tippyHover(ref, function() {
// use id or data attribute instead here
let href = ref.getAttribute('data-footnote-href') || ref.getAttribute('href');
try { href = new URL(href).hash; } catch {}
const id = href.replace(/^#\/?/, "");
const note = window.document.getElementById(id);
return note.innerHTML;
});
}
const findCites = (el) => {
const parentEl = el.parentElement;
if (parentEl) {
const cites = parentEl.dataset.cites;
if (cites) {
return {
el,
cites: cites.split(' ')
};
} else {
return findCites(el.parentElement)
}
} else {
return undefined;
}
};
var bibliorefs = window.document.querySelectorAll('a[role="doc-biblioref"]');
for (var i=0; i<bibliorefs.length; i++) {
const ref = bibliorefs[i];
const citeInfo = findCites(ref);
if (citeInfo) {
tippyHover(citeInfo.el, function() {
var popup = window.document.createElement('div');
citeInfo.cites.forEach(function(cite) {
var citeDiv = window.document.createElement('div');
citeDiv.classList.add('hanging-indent');
citeDiv.classList.add('csl-entry');
var biblioDiv = window.document.getElementById('ref-' + cite);
if (biblioDiv) {
citeDiv.innerHTML = biblioDiv.innerHTML;
}
popup.appendChild(citeDiv);
});
return popup.innerHTML;
});
}
}
});
</script>
</div> <!-- /content -->
</body></html>
0