Ejemplo n.º 1
0
    def save_score_for_success_trial(self, hyperparams: HyperparameterSamples, score: float):
        self.trials.append(Trial(hyperparams, score, TRIAL_STATUS.SUCCESS))

        if self.print_success_trial:
            self.print_func('score: {}'.format(score))
            self.print_func(
                'hyperparams:\n{}'.format(json.dumps(hyperparams.to_nested_dict(), sort_keys=True, indent=4)))
Ejemplo n.º 2
0
def test_hyperparams_to_nested_dict():
    dict_values = {'hp': 1, 'stepa__hp': 2, 'stepa__stepb__hp': 3}
    r = HyperparameterSamples(**dict_values)

    r = r.to_nested_dict()

    expected_dict_values = {'hp': 1, 'stepa': {'hp': 2, 'stepb': {'hp': 3}}}
    assert r.to_nested_dict_as_dict_primitive() == expected_dict_values
Ejemplo n.º 3
0
def test_recursive_dict_to_nested_dict():
    dict_values = {'hp': 1, 'stepa__hp': 2, 'stepa__stepb__hp': 3}
    r = HyperparameterSamples(**dict_values)

    r = r.to_nested_dict()

    expected_dict_values = {'hp': 1, 'stepa': {'hp': 2, 'stepb': {'hp': 3}}}
    assert r == HyperparameterSamples(**expected_dict_values)
Ejemplo n.º 4
0
    def get_hyperparams(self, flat=True) -> HyperparameterSamples:
        hyperparams = dict()

        for k, v in self.steps.items():
            hparams = v.get_hyperparams()  # TODO: oop diamond problem?
            if hasattr(v, "hyperparams"):
                hparams.update(v.hyperparams)
            if len(hparams) > 0:
                hyperparams[k] = hparams

        hyperparams = HyperparameterSamples(hyperparams)
        if flat:
            hyperparams = hyperparams.to_flat()
        else:
            hyperparams = hyperparams.to_nested_dict()
        return hyperparams
Ejemplo n.º 5
0
def test_hyperparams_to_nested_dict_constructor():
    dict_values = {
        'hp': 1,
        'stepa__hp': 2,
        'stepa__stepb__hp': 3
    }
    r = HyperparameterSamples(dict_values)

    expected_dict_values = {
        'hp': 1,
        'stepa': {
            'hp': 2,
            'stepb': {
                'hp': 3
            }
        }
    }
    assert r.to_nested_dict() == expected_dict_values
    assert r == HyperparameterSamples(expected_dict_values)
Ejemplo n.º 6
0
 def create_new_trial(self, hyperparams: HyperparameterSamples):
     if self.print_new_trial:
         self.print_func('new trial:\n{}'.format(
             json.dumps(hyperparams.to_nested_dict(),
                        sort_keys=True,
                        indent=4)))