def objective(trial):
        # type: (optuna.trial.Trial) -> float

        clf = tf.estimator.DNNClassifier(
            hidden_units=[],
            feature_columns=[
                tf.feature_column.numeric_column(key="x", shape=[20])
            ],
            model_dir=None,
            n_classes=2,
            config=tf.estimator.RunConfig(save_summary_steps=10,
                                          save_checkpoints_steps=10),
        )
        hook = TensorFlowPruningHook(
            trial=trial,
            estimator=clf,
            metric="accuracy",
            run_every_steps=5,
        )
        train_spec = tf.estimator.TrainSpec(input_fn=fixed_value_input_fn,
                                            max_steps=100,
                                            hooks=[hook])
        eval_spec = tf.estimator.EvalSpec(input_fn=fixed_value_input_fn,
                                          steps=1,
                                          hooks=[])
        tf.estimator.train_and_evaluate(estimator=clf,
                                        train_spec=train_spec,
                                        eval_spec=eval_spec)
        return 1.0
Exemple #2
0
def test_init_with_is_higher_better(is_higher_better):
    # type: (bool) -> None

    clf = tf.estimator.DNNClassifier(
        hidden_units=[],
        feature_columns=[
            tf.feature_column.numeric_column(key="x", shape=[20])
        ],
        model_dir=None,
        n_classes=2,
        config=tf.estimator.RunConfig(save_summary_steps=10,
                                      save_checkpoints_steps=10),
    )

    study = optuna.create_study()
    trial_id = study._storage.create_new_trial(study._study_id)

    with pytest.raises(ValueError):
        TensorFlowPruningHook(
            trial=optuna.trial.Trial(study, trial_id),
            estimator=clf,
            metric="accuracy",
            run_every_steps=5,
            is_higher_better=is_higher_better,
        )