def test_complete_and_get_new_suggestions(self): tpeAlgorithm = TpeAlgorithm() new_trials = tpeAlgorithm.get_new_suggestions(self.study.id, [], 1) new_trials[0].status = "Completed" new_trials[0].objective_value = 0.6 new_trials[0].save() new_trials = tpeAlgorithm.get_new_suggestions(self.study.id, [], 1) new_trials[0].status = "Completed" new_trials[0].objective_value = 0.7 new_trials[0].save() new_trials = tpeAlgorithm.get_new_suggestions(self.study.id, [], 3) # Assert getting two trials self.assertEqual(len(new_trials), 3) # Assert getting the trials new_trial = new_trials[0] new_parameter_values = new_trial.parameter_values new_parameter_values_json = json.loads(new_parameter_values) self.assertTrue( 0.99 >= new_parameter_values_json["l1_normalization"] >= 0.01) self.assertTrue( 0.5 >= new_parameter_values_json["learning_rate"] >= 0.01) self.assertTrue( new_parameter_values_json["hidden2"] in [8, 16, 32, 64]) self.assertTrue(new_parameter_values_json["optimizer"] in ["sgd", "adagrad", "adam", "ftrl"])
def test_get_multiple_new_suggestions(self): tpeAlgorithm = TpeAlgorithm() # Assert getting one trial new_trials = tpeAlgorithm.get_new_suggestions(self.study.id, number=1) self.assertEqual(len(new_trials), 1) # Assert getting multiple trials new_trials = tpeAlgorithm.get_new_suggestions(self.study.id, number=10) self.assertEqual(len(new_trials), 10)
def test_get_new_suggestions(self): tpeAlgorithm = TpeAlgorithm() new_trials = tpeAlgorithm.get_new_suggestions( self.study.id, number=1) new_trial = new_trials[0] new_parameter_values_json = json.loads(new_trial.parameter_values) #self.assertTrue(0.99 >= new_parameter_values_json["l1_normalization"] >= 0.01) #self.assertTrue(0.5 >= new_parameter_values_json["learning_rate"] >= 0.01) self.assertTrue(new_parameter_values_json["l1_normalization"] >= 0.01) self.assertTrue(new_parameter_values_json["learning_rate"] >= 0.01)
def test_get_new_suggestions(self): tpeAlgorithm = TpeAlgorithm() new_trials = tpeAlgorithm.get_new_suggestions(self.study.id, number=1) new_trial = new_trials[0] new_parameter_values_json = json.loads(new_trial.parameter_values) self.assertTrue( 0.99 >= new_parameter_values_json["l1_normalization"] >= 0.01) self.assertTrue( 0.5 >= new_parameter_values_json["learning_rate"] >= 0.01) self.assertTrue( new_parameter_values_json["hidden2"] in [8, 16, 32, 64]) self.assertTrue(new_parameter_values_json["optimizer"] in ["sgd", "adagrad", "adam", "ftrl"])
def v1_study_suggestions(request, study_name): # Create the trial if request.method == "POST": data = json.loads(request.body) trials_number = 1 # TODO: Use the trial name to create trial object trial_name = "Trial" if "trials_number" in data: trials_number = data["trials_number"] if "trial_name" in data: trial_name = data["trial_name"] study = Study.objects.get(name=study_name) trials = Trial.objects.filter(study_name=study_name) trials = [trial for trial in trials] if study.algorithm == "RandomSearch": algorithm = RandomSearchAlgorithm() elif study.algorithm == "GridSearch": algorithm = GridSearchAlgorithm() elif study.algorithm == "BayesianOptimization": algorithm = BayesianOptimization() elif study.algorithm == "TPE": algorithm = TpeAlgorithm() elif study.algorithm == "HyperoptRandomSearch": algorithm = HyperoptRandomSearchAlgorithm elif study.algorithm == "SimulateAnneal": algorithm = SimulateAnnealAlgorithm() elif study.algorithm == "QuasiRandomSearch": algorithm = QuasiRandomSearchAlgorithm() elif study.algorithm == "ChocolateRandomSearch": algorithm = ChocolateRandomSearchAlgorithm() elif study.algorithm == "ChocolateGridSearch": algorithm = ChocolateGridSearchAlgorithm() elif study.algorithm == "ChocolateBayes": algorithm = ChocolateBayesAlgorithm() elif study.algorithm == "CMAES": algorithm = CmaesAlgorithm() elif study.algorithm == "MOCMAES": algorithm = MocmaesAlgorithm() elif study.algorithm == "SkoptBayesianOptimization": algorithm = SkoptBayesianOptimization() else: return JsonResponse( {"error": "Unknown algorithm: {}".format(study.algorithm)}) new_trials = algorithm.get_new_suggestions(study.name, trials, trials_number) return JsonResponse( {"data": [trial.to_json() for trial in new_trials]}) else: return JsonResponse({"error": "Unsupported http method"})
def v1_study_suggestions(request, study_id): # Create the trial if request.method == "POST": data = json.loads(request.body) trials_number = 1 trial_name = "Trial" if "trials_number" in data: trials_number = data["trials_number"] if "trial_name" in data: trial_name = data["trial_name"] study = Study.objects.get(id=study_id) trials = Trial.objects.filter(study_id=study_id) trials = [trial for trial in trials] if study.algorithm == "RandomSearchAlgorithm": algorithm = RandomSearchAlgorithm() elif study.algorithm == "GridSearchAlgorithm": algorithm = GridSearchAlgorithm() elif study.algorithm == "BayesianOptimization": algorithm = BayesianOptimization() elif study.algorithm == "TpeAlgorithm": algorithm = TpeAlgorithm() elif study.algorithm == "SimulateAnnealAlgorithm": algorithm = TpeAlgorithm() else: return JsonResponse( {"error": "Unknown algorithm: {}".format(study.algorithm)}) new_trials = algorithm.get_new_suggestions(study.id, trials, trials_number) return JsonResponse( {"data": [trial.to_json() for trial in new_trials]}) else: return JsonResponse({"error": "Unsupported http method"})
def get_suggestions(self, study_name, trials_number=1): study = Study.objects.get(name=study_name) trials = Trial.objects.filter(study_name=study_name) if study.algorithm == "RandomSearch": algorithm = RandomSearchAlgorithm() elif study.algorithm == "GridSearch": algorithm = GridSearchAlgorithm() elif study.algorithm == "BayesianOptimization": algorithm = BayesianOptimization() elif study.algorithm == "TPE": algorithm = TpeAlgorithm() elif study.algorithm == "HyperoptRandomSearch": algorithm = HyperoptRandomSearchAlgorithm elif study.algorithm == "SimulateAnneal": algorithm = SimulateAnnealAlgorithm() # elif study.algorithm == "QuasiRandomSearch": # algorithm = QuasiRandomSearchAlgorithm() # elif study.algorithm == "ChocolateRandomSearch": # algorithm = ChocolateRandomSearchAlgorithm() # elif study.algorithm == "ChocolateGridSearch": # algorithm = ChocolateGridSearchAlgorithm() # elif study.algorithm == "ChocolateBayes": # algorithm = ChocolateBayesAlgorithm() # elif study.algorithm == "CMAES": # algorithm = CmaesAlgorithm() # elif study.algorithm == "MOCMAES": # algorithm = MocmaesAlgorithm() elif study.algorithm == "SkoptBayesianOptimization": algorithm = SkoptBayesianOptimization() else: raise ValueError('Error, Unknown algorithm: {}'.format( study.algorithm)) new_trials = algorithm.get_new_suggestions(study.name, trials, trials_number) return new_trials
def test_init(self): instance = TpeAlgorithm() self.assertTrue(isinstance(instance, AbstractSuggestionAlgorithm)) self.assertEqual(instance.__class__, TpeAlgorithm)