class_docstr: Tuner class for Keras models.
  
  This is the base `Tuner` 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.
  
  All Keras related logics are in `Tuner.run_trial()` and its subroutines.
  When subclassing `Tuner`, if not calling `super().run_trial()`, it can tune
  anything.
  
  Args:
      oracle: Instance of `Oracle` class.
      hypermodel: Instance of `HyperModel` class (or callable that takes
          hyperparameters and returns a `Model` instance). It is optional
          when `Tuner.run_trial()` is overriden and does not use
          `self.hypermodel`.
      max_model_size: Integer, maximum number of scalars in the parameters of
          a model. Models larger than this are rejected.
      optimizer: Optional optimizer. It is used to override the `optimizer`
          argument in the `compile` step for the models. If the hypermodel
          does not compile the models it generates, then this argument must be
          specified.
      loss: Optional loss. May be used to override the `loss` argument in the
          `compile` step for the models. If the hypermodel does not compile
          the models it generates, then this argument must be specified.
      metrics: Optional metrics. May be used to override the `metrics`
          argument in the `compile` step for the models. If the hypermodel
          does not compile the models it generates, then this argument must
          be specified.
      distribution_strategy: Optional instance of `tf.distribute.Strategy`.
          If specified, each trial will run under this scope. For example,
          `tf.distribute.MirroredStrategy(['/gpu:0', '/gpu:1'])` will run
          each trial on two GPUs. Currently only single-worker strategies are
          supported.
      directory: A string, the relative path to the working directory.
      project_name: A string, the name to use as prefix for files saved by
          this `Tuner`.
      tuner_id: Optional string, used as the ID of this `Tuner`.
      overwrite: Boolean, defaults to `False`. If `False`, reloads an
          existing project of the same name if one is found. Otherwise,
          overwrites the project.
      executions_per_trial: 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.
      **kwargs: Arguments for `BaseTuner`.
  
  Attributes:
      remaining_trials: Number of trials remaining, `None` if `max_trials` is
          not set. This is useful when resuming a previously stopped search.
docstr: Evaluates a set of hyperparameter values.
  
  This method is called multiple times during `search` to build and
  evaluate the models with different hyperparameters and return the
  objective value.
  
  Example:
  
  You can use it with `self.hypermodel` to build and fit the model.
  
  ```python
  def run_trial(self, trial, *args, **kwargs):
      hp = trial.hyperparameters
      model = self.hypermodel.build(hp)
      return self.hypermodel.fit(hp, model, *args, **kwargs)
  ```
  
  You can also use it as a black-box optimizer for anything.
  
  ```python
  def run_trial(self, trial, *args, **kwargs):
      hp = trial.hyperparameters
      x = hp.Float("x", -2.0, 2.0)
      y = x * x + 2 * x + 1
      return y
  ```
  
  Args:
      trial: A `Trial` instance that contains the information needed to
          run this trial. Hyperparameters can be accessed via
          `trial.hyperparameters`.
      *args: Positional arguments passed by `search`.
      **kwargs: Keyword arguments passed by `search`.
  
  Returns:
      A `History` object, which is the return value of `model.fit()`, a
      dictionary, a float, or a list of one of these types.
  
      If return a dictionary, it should be a dictionary of the metrics to
      track. The keys are the metric names, which contains the
      `objective` name. The values should be the metric values.
  
      If return a float, it should be the `objective` value.
  
      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.
docstr_tok: ["Evaluates","a","set","of","hyperparameter","values",".","This","method","is","called","multiple","times","during","`","search","`","to","build","and","evaluate","the","models","with","different","hyperparameters","and","return","the","objective","value",".","Example",":","You","can","use","it","with","`","self.hypermodel","`","to","build","and","fit","the","model",".","``","`","python","def","run_trial","(","self",",","trial",",","*","args",",","*","*","kwargs",")",":","hp","=","trial.hyperparameters","model","=","self.hypermodel.build","(","hp",")","return","self.hypermodel.fit","(","hp",",","model",",","*","args",",","*","*","kwargs",")","``","`","You","can","also","use","it","as","a","black-box","optimizer","for","anything",".","``","`","python","def","run_trial","(","self",",","trial",",","*","args",",","*","*","kwargs",")",":","hp","=","trial.hyperparameters","x","=","hp.Float","(","``","x","''",",","-2.0",",","2.0",")","y","=","x","*","x","+","2","*","x","+","1","return","y","``","`","Args",":","trial",":","A","`","Trial","`","instance","that","contains","the","information","needed","to","run","this","trial",".","Hyperparameters","can","be","accessed","via","`","trial.hyperparameters","`",".","*","args",":","Positional","arguments","passed","by","`","search","`",".","*","*","kwargs",":","Keyword","arguments","passed","by","`","search","`",".","Returns",":","A","`","History","`","object",",","which","is","the","return","value","of","`","model.fit","(",")","`",",","a","dictionary",",","a","float",",","or","a","list","of","one","of","these","types",".","If","return","a","dictionary",",","it","should","be","a","dictionary","of","the","metrics","to","track",".","The","keys","are","the","metric","names",",","which","contains","the","`","objective","`","name",".","The","values","should","be","the","metric","values",".","If","return","a","float",",","it","should","be","the","`","objective","`","value",".","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","."]
code: def run_trial(self, trial, *args, **kwargs):
      """Evaluates a set of hyperparameter values.
  
      This method is called multiple times during `search` to build and
      evaluate the models with different hyperparameters and return the
      objective value.
  
      Example:
  
      You can use it with `self.hypermodel` to build and fit the model.
  
      ```python
      def run_trial(self, trial, *args, **kwargs):
          hp = trial.hyperparameters
          model = self.hypermodel.build(hp)
          return self.hypermodel.fit(hp, model, *args, **kwargs)
      ```
  
      You can also use it as a black-box optimizer for anything.
  
      ```python
      def run_trial(self, trial, *args, **kwargs):
          hp = trial.hyperparameters
          x = hp.Float("x", -2.0, 2.0)
          y = x * x + 2 * x + 1
          return y
      ```
  
      Args:
          trial: A `Trial` instance that contains the information needed to
              run this trial. Hyperparameters can be accessed via
              `trial.hyperparameters`.
          *args: Positional arguments passed by `search`.
          **kwargs: Keyword arguments passed by `search`.
  
      Returns:
          A `History` object, which is the return value of `model.fit()`, a
          dictionary, a float, or a list of one of these types.
  
          If return a dictionary, it should be a dictionary of the metrics to
          track. The keys are the metric names, which contains the
          `objective` name. The values should be the metric values.
  
          If return a float, it should be the `objective` value.
  
          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.
      """
      # Not using `ModelCheckpoint` to support MultiObjective.
      # It can only track one of the metrics to save the best model.
      model_checkpoint = tuner_utils.SaveBestEpoch(
          objective=self.oracle.objective,
          filepath=self._get_checkpoint_fname(trial.trial_id),
      )
      original_callbacks = kwargs.pop("callbacks", [])
  
      # Run the training process multiple times.
      histories = []
      for execution in range(self.executions_per_trial):
          copied_kwargs = copy.copy(kwargs)
          callbacks = self._deepcopy_callbacks(original_callbacks)
          self._configure_tensorboard_dir(callbacks, trial, execution)
          callbacks.append(tuner_utils.TunerCallback(self, trial))
          # Only checkpoint the best epoch across all executions.
          callbacks.append(model_checkpoint)
          copied_kwargs["callbacks"] = callbacks
          obj_value = self._build_and_fit_model(
              trial, *args, **copied_kwargs
          )
  
          histories.append(obj_value)
      return histories
  
imports: import contextlib
  import copy
  import gc
  import math
  import os
  import numpy
  from keras_tuner import backend
  from keras_tuner import errors
  from keras_tuner import utils
  from keras_tuner.api_export import keras_tuner_export
  from keras_tuner.backend import config
  from keras_tuner.backend import keras
  from keras_tuner.engine import base_tuner
  from keras_tuner.engine import tuner_utils
label_desc: Use image node_id 7 for calling the Tuner obj's underlying member method code with example usage: obj.run_trial(trial) and returns: histories