Skip to content

Commit

Permalink
naming is hard.
Browse files Browse the repository at this point in the history
  • Loading branch information
trivialfis committed Mar 22, 2023
1 parent bc56079 commit c4948a3
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 33 deletions.
11 changes: 6 additions & 5 deletions demo/guide-python/multioutput_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
.. note::
The feature is experimental. For the monolithic strategy, many features are missing.
The feature is experimental. For the `multi_output_tree` strategy, many features are
missing.
"""

Expand Down Expand Up @@ -121,15 +122,15 @@ def rmse(predt: np.ndarray, dtrain: xgb.DMatrix) -> Tuple[str, float]:
args = parser.parse_args()
# Train with builtin RMSE objective
# - One model per output.
rmse_model(args.plot == 1, "composite")
rmse_model(args.plot == 1, "one_output_per_tree")

# - One model for all outputs, this is still working in progress, many features are
# missing.
rmse_model(args.plot == 1, "monolithic")
rmse_model(args.plot == 1, "multi_output_tree")

# Train with custom objective.
# - One model per output.
custom_rmse_model(args.plot == 1, "composite")
custom_rmse_model(args.plot == 1, "one_output_per_tree")
# - One model for all outputs, this is still working in progress, many features are
# missing.
custom_rmse_model(args.plot == 1, "monolithic")
custom_rmse_model(args.plot == 1, "multi_output_tree")
6 changes: 3 additions & 3 deletions doc/parameter.rst
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ Parameters for Tree Booster
list is a group of indices of features that are allowed to interact with each other.
See :doc:`/tutorials/feature_interaction_constraint` for more information.

* ``multi_strategy``, [default = ``composite``]
* ``multi_strategy``, [default = ``one_output_per_tree``]

.. versionadded:: 2.0.0

Expand All @@ -235,8 +235,8 @@ Parameters for Tree Booster
- The strategy used for training multi-target models, including multi-target regression
and multi-class classification. See :doc:`/tutorials/multioutput` for more information.

- ``composite``: One model for each target.
- ``monolithic``: Use multi-target trees.
- ``one_output_per_tree``: One model for each target.
- ``multi_output_tree``: Use multi-target trees.

.. _cat-param:

Expand Down
7 changes: 4 additions & 3 deletions doc/tutorials/multioutput.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ Training with Vector Leaf

XGBoost can optionally build multi-output trees with the size of leaf equals to the number
of targets when the tree method `hist` is used. The behavior can be controlled by the
``multi_strategy`` training parameter, which can take the value `composite` (the default)
for building one model per-target or `monolithic` for building multi-output trees.
``multi_strategy`` training parameter, which can take the value `one_output_per_tree` (the
default) for building one model per-target or `multi_output_tree` for building
multi-output trees.

.. code-block:: python
clf = xgb.XGBClassifier(tree_method="hist", multi_strategy="monolithic")
clf = xgb.XGBClassifier(tree_method="hist", multi_strategy="multi_output_tree")
See :ref:`sphx_glr_python_examples_multioutput_regression.py` for a worked example with
regression.
8 changes: 4 additions & 4 deletions include/xgboost/learner.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ struct LearnerModelParamLegacy;
* \brief Strategy for building multi-target models.
*/
enum class MultiStrategy : std::int32_t {
kComposite = 0,
kMonolithic = 1,
kOneOutputPerTree = 0,
kMultiOutputTree = 1,
};

/**
Expand Down Expand Up @@ -317,7 +317,7 @@ struct LearnerModelParam {
/**
* \brief Strategy for building multi-target models.
*/
MultiStrategy multi_strategy{MultiStrategy::kComposite};
MultiStrategy multi_strategy{MultiStrategy::kOneOutputPerTree};

LearnerModelParam() = default;
// As the old `LearnerModelParamLegacy` is still used by binary IO, we keep
Expand All @@ -338,7 +338,7 @@ struct LearnerModelParam {

void Copy(LearnerModelParam const& that);
[[nodiscard]] bool IsVectorLeaf() const noexcept {
return multi_strategy == MultiStrategy::kMonolithic;
return multi_strategy == MultiStrategy::kMultiOutputTree;
}
[[nodiscard]] bst_target_t OutputLength() const noexcept { return this->num_output_group; }
[[nodiscard]] bst_target_t LeafLength() const noexcept {
Expand Down
4 changes: 2 additions & 2 deletions python-package/xgboost/sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,8 @@ def task(i: int) -> float:
regression and multi-class classification. See :doc:`/tutorials/multioutput` for
more information.
- ``composite``: One model for each target.
- ``monolithic``: Use multi-target trees.
- ``one_output_per_tree``: One model for each target.
- ``multi_output_tree``: Use multi-target trees.
eval_metric : Optional[Union[str, List[str], Callable]]
Expand Down
8 changes: 5 additions & 3 deletions python-package/xgboost/testing/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import pytest

hypothesis = pytest.importorskip("hypothesis")
from hypothesis import strategies # pylint:disable=wrong-import-position
strategies = pytest.importorskip("hypothesis.strategies")


exact_parameter_strategy = strategies.fixed_dictionaries(
{
Expand Down Expand Up @@ -46,7 +46,9 @@
"max_depth": strategies.integers(1, 11),
"max_leaves": strategies.integers(0, 1024),
"max_bin": strategies.integers(2, 512),
"multi_strategy": strategies.sampled_from(["monolithic", "composite"]),
"multi_strategy": strategies.sampled_from(
["multi_output_tree", "one_output_per_tree"]
),
"grow_policy": strategies.sampled_from(["lossguide", "depthwise"]),
"min_child_weight": strategies.floats(0.5, 2.0),
# We cannot enable subsampling as the training loss can increase
Expand Down
12 changes: 6 additions & 6 deletions src/learner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ struct LearnerTrainParam : public XGBoostParameter<LearnerTrainParam> {
std::string booster;
std::string objective;
// This is a training parameter and is not saved (nor loaded) in the model.
MultiStrategy multi_strategy{MultiStrategy::kComposite};
MultiStrategy multi_strategy{MultiStrategy::kOneOutputPerTree};

// declare parameters
DMLC_DECLARE_PARAMETER(LearnerTrainParam) {
Expand All @@ -339,12 +339,12 @@ struct LearnerTrainParam : public XGBoostParameter<LearnerTrainParam> {
.set_default("reg:squarederror")
.describe("Objective function used for obtaining gradient.");
DMLC_DECLARE_FIELD(multi_strategy)
.add_enum("composite", MultiStrategy::kComposite)
.add_enum("monolithic", MultiStrategy::kMonolithic)
.set_default(MultiStrategy::kComposite)
.add_enum("one_output_per_tree", MultiStrategy::kOneOutputPerTree)
.add_enum("multi_output_tree", MultiStrategy::kMultiOutputTree)
.set_default(MultiStrategy::kOneOutputPerTree)
.describe(
"Strategy used for training multi-target models. `monolithic` means building one "
"single tree for all targets.");
"Strategy used for training multi-target models. `multi_output_tree` means building "
"one single tree for all targets.");
}
};

Expand Down
2 changes: 1 addition & 1 deletion tests/cpp/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ inline LearnerModelParam MakeMP(bst_feature_t n_features, float base_score, uint
int32_t device = Context::kCpuId) {
size_t shape[1]{1};
LearnerModelParam mparam(n_features, linalg::Tensor<float, 1>{{base_score}, shape, device},
n_groups, 1, MultiStrategy::kComposite);
n_groups, 1, MultiStrategy::kOneOutputPerTree);
return mparam;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/cpp/predictor/test_predictor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ void TestVectorLeafPrediction(Context const *ctx) {

LearnerModelParam mparam{static_cast<bst_feature_t>(kCols),
linalg::Vector<float>{{0.5}, {1}, Context::kCpuId}, 1, 3,
MultiStrategy::kMonolithic};
MultiStrategy::kMultiOutputTree};

std::vector<std::unique_ptr<RegTree>> trees;
trees.emplace_back(new RegTree{mparam.LeafLength(), mparam.num_feature});
Expand Down
4 changes: 2 additions & 2 deletions tests/cpp/test_multi_target.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ TEST(MultiStrategy, Configure) {
auto p_fmat = RandomDataGenerator{12ul, 3ul, 0.0}.GenerateDMatrix();
p_fmat->Info().labels.Reshape(p_fmat->Info().num_row_, 2);
std::unique_ptr<Learner> learner{Learner::Create({p_fmat})};
learner->SetParams(Args{{"multi_strategy", "monolithic"}, {"num_target", "2"}});
learner->SetParams(Args{{"multi_strategy", "multi_output_tree"}, {"num_target", "2"}});
learner->Configure();
ASSERT_EQ(learner->Groups(), 2);

learner->SetParams(Args{{"multi_strategy", "monolithic"}, {"num_target", "0"}});
learner->SetParams(Args{{"multi_strategy", "multi_output_tree"}, {"num_target", "0"}});
ASSERT_THROW({ learner->Configure(); }, dmlc::Error);
}
} // namespace xgboost
6 changes: 3 additions & 3 deletions tests/python/test_basic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ def run_model_json_io(self, parameters: dict, ext: str) -> None:
from_ubjraw = xgb.Booster()
from_ubjraw.load_model(ubj_raw)

if parameters.get("multi_strategy", None) != "monolithic":
if parameters.get("multi_strategy", None) != "multi_output_tree":
# old binary model is not supported.
old_from_json = from_jraw.save_raw(raw_format="deprecated")
old_from_ubj = from_ubjraw.save_raw(raw_format="deprecated")
Expand All @@ -341,7 +341,7 @@ def run_model_json_io(self, parameters: dict, ext: str) -> None:
pretty = json.dumps(json.loads(raw_json), indent=2) + "\n\n"
bst.load_model(bytearray(pretty, encoding="ascii"))

if parameters.get("multi_strategy", None) != "monolithic":
if parameters.get("multi_strategy", None) != "multi_output_tree":
# old binary model is not supported.
old_from_json = from_jraw.save_raw(raw_format="deprecated")
old_from_ubj = from_ubjraw.save_raw(raw_format="deprecated")
Expand All @@ -363,7 +363,7 @@ def test_model_json_io(self, ext: str) -> None:
parameters = {
"booster": "gbtree",
"tree_method": "hist",
"multi_strategy": "monolithic",
"multi_strategy": "multi_output_tree",
"objective": "multi:softmax",
}
self.run_model_json_io(parameters, ext)
Expand Down

0 comments on commit c4948a3

Please sign in to comment.