def get_best_trials(self, num_trials: int = 1) -> List[trial_module.Trial]: """Returns the trials with the best objective values found so far. Arguments: num_trials: positive int, number of trials to return. Returns: List of KerasTuner Trials. """ if len(self.objective) > 1: raise ValueError( "Getting the best trials for multi-objective optimization " "is not supported. " ) maximizing = ( utils.format_goal(self.objective[0].direction) == "MAXIMIZE") # List all trials associated with the same study trial_list = self.service.list_trials() optimizer_trials = [t for t in trial_list if t["state"] == "COMPLETED"] if not optimizer_trials: return [] sorted_trials = sorted( optimizer_trials, key=lambda t: t["finalMeasurement"]["metrics"][0]["value"], reverse=maximizing, ) best_optimizer_trials = sorted_trials[:num_trials] best_trials = [] # Convert Optimizer trials to KerasTuner Trial instance for optimizer_trial in best_optimizer_trials: final_measurement = optimizer_trial["finalMeasurement"] kerastuner_trial = trial_module.Trial( hyperparameters=utils.convert_optimizer_trial_to_hps( self.hyperparameters.copy(), optimizer_trial ), trial_id=utils.get_trial_id(optimizer_trial), status=trial_module.TrialStatus.COMPLETED, ) # If trial had ended before having intermediate metric reporting, # set epoch = 1. kerastuner_trial.best_step = final_measurement.get("stepCount", 1) kerastuner_trial.score = final_measurement["metrics"][0]["value"] best_trials.append(kerastuner_trial) return best_trials
def get_best_trials(self, num_trials: int = 1) -> List[trial_module.Trial]: """Returns the trials with the best objective values found so far. Arguments: num_trials: positive int, number of trials to return. Returns: List of KerasTuner Trials. """ objective = self._get_objective() if len(objective) > 1: raise ValueError( "Getting the best trials for multi-objective optimization " "is not supported." ) maximizing = (utils.format_goal(objective[0].direction) == "MAXIMIZE") # List all trials associated with the same study trial_list = self.service.list_trials() optimizer_trials = [t for t in trial_list if t["state"] == "COMPLETED"] if not optimizer_trials: return [] sorted_trials = sorted( optimizer_trials, key=lambda t: t["finalMeasurement"]["metrics"][0].get("value"), reverse=maximizing, ) best_optimizer_trials = sorted_trials[:num_trials] best_trials = [] # Convert completed Optimizer trials to KerasTuner Trial instances. for optimizer_trial in best_optimizer_trials: kerastuner_trial = ( utils.convert_completed_optimizer_trial_to_keras_trial( optimizer_trial, self.hyperparameters.copy())) best_trials.append(kerastuner_trial) return best_trials