-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-15780 - add weak_learner_parameters to API
- Loading branch information
Showing
8 changed files
with
189 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,43 @@ | ||
options = dict( | ||
def update_param(name, param): | ||
if name == 'weak_learner_params': | ||
param['type'] = 'KeyValue' | ||
param['default_value'] = None | ||
return param | ||
return None # param untouched | ||
|
||
extensions = dict( | ||
__imports__=""" | ||
import ast | ||
import json | ||
from h2o.estimators.estimator_base import H2OEstimator | ||
from h2o.exceptions import H2OValueError | ||
from h2o.frame import H2OFrame | ||
from h2o.utils.typechecks import assert_is_type, Enum, numeric | ||
""", | ||
) | ||
|
||
doc = dict( | ||
__class__=""" | ||
Builds an AdaBoost model | ||
""" | ||
) | ||
|
||
overrides = dict( | ||
weak_learner_params=dict( | ||
getter=""" | ||
if self._parms.get("{sname}") != None: | ||
return json.loads(self._parms.get("{sname}")) | ||
else: | ||
self._parms["{sname}"] = None | ||
""", | ||
setter=""" | ||
assert_is_type({pname}, None, {ptype}) | ||
if {pname} is not None and {pname} != "": | ||
for k in {pname}: | ||
weak_learner_params[k] = weak_learner_params[k] | ||
self._parms["{sname}"] = str(json.dumps({pname})) | ||
else: | ||
self._parms["{sname}"] = None | ||
""" | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
h2o-py/tests/testdir_algos/adaboost/pyunit_adaboost_weak_learner_params_smoke.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import sys, os | ||
sys.path.insert(1, os.path.join("..","..","..")) | ||
import h2o | ||
from tests import pyunit_utils | ||
from h2o.estimators import H2OAdaBoostEstimator | ||
|
||
|
||
def adaboost(): | ||
print("AdaBoost Weak Learner Params Smoke Test - test only that parameters are correctly passed to backend") | ||
|
||
train = h2o.import_file(pyunit_utils.locate("smalldata/prostate/prostate.csv")) | ||
train["CAPSULE"] = train["CAPSULE"].asfactor() | ||
|
||
common_adaboost_def = {"nlearners": 10, "seed": 0xBEEF, "learn_rate": 0.6} | ||
common_adaboost_train = {"training_frame": train, "y": "CAPSULE"} | ||
|
||
adaboost_model = H2OAdaBoostEstimator( | ||
weak_learner="DRF", | ||
weak_learner_params={ | ||
'ntrees': 10, | ||
'histogram_type': "UniformAdaptive" | ||
}, | ||
**common_adaboost_def | ||
) | ||
assert isinstance(adaboost_model.weak_learner_params, dict) | ||
adaboost_model.train(**common_adaboost_train) | ||
assert adaboost_model._model_json is not None | ||
|
||
adaboost_model = H2OAdaBoostEstimator( | ||
weak_learner="GBM", | ||
weak_learner_params={ | ||
'ntrees': 10, | ||
'histogram_type': "UniformAdaptive", | ||
"learn_rate": 0.1 | ||
}, | ||
**common_adaboost_def | ||
) | ||
assert isinstance(adaboost_model.weak_learner_params, dict) | ||
adaboost_model.train(**common_adaboost_train) | ||
assert adaboost_model._model_json is not None | ||
|
||
adaboost_model = H2OAdaBoostEstimator( | ||
weak_learner="GLM", | ||
weak_learner_params={ | ||
'max_iterations': 10 | ||
}, | ||
**common_adaboost_def | ||
) | ||
assert isinstance(adaboost_model.weak_learner_params, dict) | ||
adaboost_model.train(**common_adaboost_train) | ||
assert adaboost_model._model_json is not None | ||
|
||
adaboost_model = H2OAdaBoostEstimator( | ||
weak_learner="DEEP_LEARNING", | ||
weak_learner_params={ | ||
'nepochs': 10, | ||
'hidden': [2, 2, 4] | ||
}, | ||
**common_adaboost_def | ||
) | ||
assert isinstance(adaboost_model.weak_learner_params, dict) | ||
adaboost_model.train(**common_adaboost_train) | ||
assert adaboost_model._model_json is not None | ||
|
||
|
||
if __name__ == "__main__": | ||
pyunit_utils.standalone_test(adaboost) | ||
else: | ||
adaboost() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
h2o-r/tests/testdir_algos/adaboost/runit_adaboost_weak_learner_smoke.R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
setwd(normalizePath(dirname(R.utils::commandArgs(asValues=TRUE)$"f"))) | ||
source("../../../scripts/h2o-r-test-setup.R") | ||
|
||
|
||
test.adaBoost.smoke <- function() { | ||
f <- "https://s3.amazonaws.com/h2o-public-test-data/smalldata/prostate/prostate.csv" | ||
data <- h2o.importFile(f) | ||
|
||
# Set predictors and response; set response as a factor | ||
data["CAPSULE"] <- as.factor(data["CAPSULE"]) | ||
predictors <- c("AGE","RACE","DPROS","DCAPS","PSA","VOL","GLEASON") | ||
response <- "CAPSULE" | ||
|
||
h2o_adaboost <- h2o.adaBoost(nlearners = 5, x = predictors, y = response, training_frame = data, seed = 1234, weak_learner = "DRF", weak_learner_params = list(ntrees=3, max_depth=2, histogram_type="UniformAdaptive")) | ||
expect_equal(is.null(h2o_adaboost), FALSE) | ||
h2o_adaboost <- h2o.adaBoost(nlearners = 5, x = predictors, y = response, training_frame = data, seed = 1234, weak_learner = "GBM", weak_learner_params = list(ntrees=3, max_depth=2, histogram_type="UniformAdaptive")) | ||
expect_equal(is.null(h2o_adaboost), FALSE) | ||
h2o_adaboost <- h2o.adaBoost(nlearners = 5, x = predictors, y = response, training_frame = data, seed = 1234, weak_learner = "GLM", weak_learner_params = list(max_iterations=3)) | ||
expect_equal(is.null(h2o_adaboost), FALSE) | ||
h2o_adaboost <- h2o.adaBoost(nlearners = 5, x = predictors, y = response, training_frame = data, seed = 1234, weak_learner = "DEEP_LEARNING", weak_learner_params = list(nepochs=3, hidden=list(2,1,2))) | ||
expect_equal(is.null(h2o_adaboost), FALSE) | ||
} | ||
|
||
doTest("adaBoost: Smoke Test For Weak Learner Params - only that is pass through the API", test.adaBoost.smoke) |