def test_get_new_suggestions(self): gridSearchAlgorithm = GridSearchAlgorithm() new_trials = gridSearchAlgorithm.get_new_suggestions( self.study.id, self.trials, 1) # Assert getting two trials self.assertEqual(len(new_trials), 1)
def test_get_four_new_suggestions(self): gridSearchAlgorithm = GridSearchAlgorithm() new_trials = gridSearchAlgorithm.get_new_suggestions( self.study.id, self.trials, 4) # Assert getting two trials self.assertEqual(len(new_trials), 4) # 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.assertEqual(new_parameter_values_json["hidden1"], 40) new_trial = new_trials[1] new_parameter_values = new_trial.parameter_values new_parameter_values_json = json.loads(new_parameter_values) self.assertEqual(new_parameter_values_json["hidden1"], 160) new_trial = new_trials[2] new_parameter_values = new_trial.parameter_values new_parameter_values_json = json.loads(new_parameter_values) self.assertEqual(new_parameter_values_json["hidden1"], 280) new_trial = new_trials[3] new_parameter_values = new_trial.parameter_values new_parameter_values_json = json.loads(new_parameter_values) self.assertEqual(new_parameter_values_json["hidden1"], 400)
def test_get_two_new_suggestions(self): gridSearchAlgorithm = GridSearchAlgorithm() new_trials = gridSearchAlgorithm.get_new_suggestions( self.study.id, self.trials, 2) # Assert getting two trials self.assertEqual(len(new_trials), 2) # 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.assertEqual(new_parameter_values_json["hidden2"], "8") self.assertEqual(new_parameter_values_json["optimizer"], "sgd") self.assertEqual(new_parameter_values_json["batch_normalization"], "true")
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() 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 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 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): gridSearchAlgorithm = GridSearchAlgorithm() self.assertEqual(gridSearchAlgorithm.__class__, GridSearchAlgorithm)