-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix multi-output with alternating strategies. (#9933)
--------- Co-authored-by: Philip Hyunsu Cho <[email protected]>
- Loading branch information
1 parent
5f7b5a6
commit 621348a
Showing
6 changed files
with
123 additions
and
73 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
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,105 @@ | ||
from typing import Any, Dict | ||
|
||
from hypothesis import given, note, settings, strategies | ||
|
||
import xgboost as xgb | ||
from xgboost import testing as tm | ||
from xgboost.testing.params import ( | ||
exact_parameter_strategy, | ||
hist_cache_strategy, | ||
hist_multi_parameter_strategy, | ||
hist_parameter_strategy, | ||
) | ||
from xgboost.testing.updater import ResetStrategy, train_result | ||
|
||
|
||
class TestTreeMethodMulti: | ||
@given( | ||
exact_parameter_strategy, strategies.integers(1, 20), tm.multi_dataset_strategy | ||
) | ||
@settings(deadline=None, print_blob=True) | ||
def test_exact(self, param: dict, num_rounds: int, dataset: tm.TestDataset) -> None: | ||
if dataset.name.endswith("-l1"): | ||
return | ||
param["tree_method"] = "exact" | ||
param = dataset.set_params(param) | ||
result = train_result(param, dataset.get_dmat(), num_rounds) | ||
assert tm.non_increasing(result["train"][dataset.metric]) | ||
|
||
@given( | ||
exact_parameter_strategy, | ||
hist_parameter_strategy, | ||
hist_cache_strategy, | ||
strategies.integers(1, 20), | ||
tm.multi_dataset_strategy, | ||
) | ||
@settings(deadline=None, print_blob=True) | ||
def test_approx( | ||
self, | ||
param: Dict[str, Any], | ||
hist_param: Dict[str, Any], | ||
cache_param: Dict[str, Any], | ||
num_rounds: int, | ||
dataset: tm.TestDataset, | ||
) -> None: | ||
param["tree_method"] = "approx" | ||
param = dataset.set_params(param) | ||
param.update(hist_param) | ||
param.update(cache_param) | ||
result = train_result(param, dataset.get_dmat(), num_rounds) | ||
note(str(result)) | ||
assert tm.non_increasing(result["train"][dataset.metric]) | ||
|
||
@given( | ||
exact_parameter_strategy, | ||
hist_multi_parameter_strategy, | ||
hist_cache_strategy, | ||
strategies.integers(1, 20), | ||
tm.multi_dataset_strategy, | ||
) | ||
@settings(deadline=None, print_blob=True) | ||
def test_hist( | ||
self, | ||
param: Dict[str, Any], | ||
hist_param: Dict[str, Any], | ||
cache_param: Dict[str, Any], | ||
num_rounds: int, | ||
dataset: tm.TestDataset, | ||
) -> None: | ||
if dataset.name.endswith("-l1"): | ||
return | ||
param["tree_method"] = "hist" | ||
param = dataset.set_params(param) | ||
param.update(hist_param) | ||
param.update(cache_param) | ||
result = train_result(param, dataset.get_dmat(), num_rounds) | ||
note(str(result)) | ||
assert tm.non_increasing(result["train"][dataset.metric]) | ||
|
||
|
||
def test_multiclass() -> None: | ||
X, y = tm.datasets.make_classification( | ||
128, n_features=12, n_informative=10, n_classes=4 | ||
) | ||
clf = xgb.XGBClassifier( | ||
multi_strategy="multi_output_tree", callbacks=[ResetStrategy()], n_estimators=10 | ||
) | ||
clf.fit(X, y, eval_set=[(X, y)]) | ||
assert clf.objective == "multi:softprob" | ||
assert tm.non_increasing(clf.evals_result()["validation_0"]["mlogloss"]) | ||
|
||
proba = clf.predict_proba(X) | ||
assert proba.shape == (y.shape[0], 4) | ||
|
||
|
||
def test_multilabel() -> None: | ||
X, y = tm.datasets.make_multilabel_classification(128) | ||
clf = xgb.XGBClassifier( | ||
multi_strategy="multi_output_tree", callbacks=[ResetStrategy()], n_estimators=10 | ||
) | ||
clf.fit(X, y, eval_set=[(X, y)]) | ||
assert clf.objective == "binary:logistic" | ||
assert tm.non_increasing(clf.evals_result()["validation_0"]["logloss"]) | ||
|
||
proba = clf.predict_proba(X) | ||
assert proba.shape == y.shape |
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