|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | +from time import time |
| 4 | + |
| 5 | +import logging |
| 6 | +logging.basicConfig(level=logging.DEBUG) |
| 7 | + |
| 8 | +try: |
| 9 | + import Pyro4 |
| 10 | + skip_container_test = False |
| 11 | +except ImportError: |
| 12 | + skip_container_test = True |
| 13 | + |
| 14 | + |
| 15 | +def test_whitebox_without_container(): |
| 16 | + from hpolib.benchmarks.ml.xgboost_benchmark import XGBoostBenchmark as Benchmark |
| 17 | + b = Benchmark(task_id=167199, rng=0) |
| 18 | + cs = b.get_configuration_space(seed=0) |
| 19 | + |
| 20 | + start = time() |
| 21 | + configuration = cs.get_default_configuration() |
| 22 | + assert configuration['colsample_bylevel'] == 1.0 |
| 23 | + assert len(configuration.keys()) == 6 |
| 24 | + |
| 25 | + n_estimator = 32 |
| 26 | + subsample = 1 |
| 27 | + result_dict = b.objective_function(configuration, n_estimators=n_estimator, subsample=subsample, rng=0) |
| 28 | + valid_loss = result_dict['function_value'] |
| 29 | + train_loss = result_dict['train_loss'] |
| 30 | + |
| 31 | + result_dict = b.objective_function_test(configuration, n_estimators=n_estimator, rng=0) |
| 32 | + test_loss = result_dict['function_value'] |
| 33 | + |
| 34 | + assert np.isclose(train_loss, 0.1071, atol=0.001) |
| 35 | + assert np.isclose(valid_loss, 0.3873, atol=0.001) |
| 36 | + assert np.isclose(test_loss, 0.38181, atol=0.001) |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.skipif(skip_container_test, reason="Requires singularity and flask") |
| 40 | +def test_whitebox_with_container(): |
| 41 | + from hpolib.container.benchmarks.ml.xgboost_benchmark import XGBoostBenchmark as Benchmark |
| 42 | + b = Benchmark(container_source='library://keggensperger/automl/', |
| 43 | + container_name='xgboost_benchmark', |
| 44 | + task_id=167199, |
| 45 | + rng=0) |
| 46 | + |
| 47 | + cs = b.get_configuration_space() |
| 48 | + configuration = cs.get_default_configuration() |
| 49 | + assert configuration['colsample_bylevel'] == 1.0 |
| 50 | + assert len(configuration.keys()) == 6 |
| 51 | + |
| 52 | + n_estimator = 32 |
| 53 | + subsample = 1 |
| 54 | + result_dict = b.objective_function(configuration, n_estimators=n_estimator, subsample=subsample) |
| 55 | + valid_loss = result_dict['function_value'] |
| 56 | + train_loss = result_dict['train_loss'] |
| 57 | + result_dict = b.objective_function_test(configuration, n_estimators=n_estimator) |
| 58 | + test_loss = result_dict['function_value'] |
| 59 | + |
| 60 | + print(train_loss, valid_loss, test_loss) |
| 61 | + assert np.isclose(train_loss, 0.1071, atol=0.001) |
| 62 | + assert np.isclose(valid_loss, 0.3873, atol=0.001) |
| 63 | + assert np.isclose(test_loss, 0.38181, atol=0.001) |
| 64 | + |
0 commit comments