-
Notifications
You must be signed in to change notification settings - Fork 26
[WIP][New Model] Dynamic Programming Decision Trees #176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
KohlerHECTOR
wants to merge
13
commits into
autogluon:main
Choose a base branch
from
KohlerHECTOR:dpdt-model
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8218de3
first commit to add dpdt
KohlerHECTOR 38d660f
Changed to AdaBoostDPDT to have compat with predict_proba
KohlerHECTOR 3738c81
Progress on BoostedDPDT; added memory estimate (estimators * dpdt tre…
KohlerHECTOR 250df43
added configs and registered model and tests
KohlerHECTOR 0534f8e
Fixed some typos
KohlerHECTOR c8f8df7
Fixed some cpus stuff
KohlerHECTOR 11324dc
updated with time limit
KohlerHECTOR 2ca10a3
Merge remote-tracking branch 'origin/main' into dpdt-model
LennartPurucker 2096a9b
maint: minor refactor and make test run
LennartPurucker 6dbe698
add: preprocessing for nan and cat handling
LennartPurucker 996e72d
add/fix: search space for HPO of dpdt
LennartPurucker ac10777
add: state after EBM rerun
LennartPurucker e070555
Merge remote-tracking branch 'origin/main' into dpdt-model
LennartPurucker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
Empty file.
This file contains hidden or 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,88 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from autogluon.common.utils.pandas_utils import get_approximate_df_mem_usage | ||
from autogluon.common.utils.resource_utils import ResourceManager | ||
from autogluon.core.models import AbstractModel | ||
|
||
if TYPE_CHECKING: | ||
import pandas as pd | ||
|
||
|
||
class BoostedDPDTModel(AbstractModel): | ||
ag_key = "BOOSTEDDPDT" | ||
ag_name = "boosted_dpdt" | ||
|
||
def get_model_cls(self): | ||
from dpdt import AdaBoostDPDT | ||
|
||
if self.problem_type in ["binary", "multiclass"]: | ||
model_cls = AdaBoostDPDT | ||
else: | ||
raise AssertionError(f"Unsupported problem_type: {self.problem_type}") | ||
return model_cls | ||
|
||
def _fit(self, X: pd.DataFrame, y: pd.Series, num_cpus: int = 1, **kwargs): | ||
model_cls = self.get_model_cls() | ||
hyp = self._get_model_params() | ||
if num_cpus < 1: | ||
num_cpus = 'best' | ||
self.model = model_cls( | ||
**hyp, | ||
n_jobs=num_cpus, | ||
) | ||
X = self.preprocess(X) | ||
self.model = self.model.fit( | ||
X=X, | ||
y=y, | ||
) | ||
|
||
|
||
def _set_default_params(self): | ||
default_params = { | ||
"random_state": 42, | ||
} | ||
for param, val in default_params.items(): | ||
self._set_default_param_value(param, val) | ||
|
||
@classmethod | ||
def supported_problem_types(cls) -> list[str] | None: | ||
return ["binary", "multiclass"] | ||
|
||
def _get_default_resources(self) -> tuple[int, int]: | ||
import torch | ||
# logical=False is faster in training | ||
num_cpus = ResourceManager.get_cpu_count_psutil(logical=False) | ||
num_gpus = 0 | ||
return num_cpus, num_gpus | ||
|
||
def _estimate_memory_usage(self, X: pd.DataFrame, **kwargs) -> int: | ||
hyperparameters = self._get_model_params() | ||
return self.estimate_memory_usage_static(X=X, problem_type=self.problem_type, num_classes=self.num_classes, hyperparameters=hyperparameters, **kwargs) | ||
|
||
@classmethod | ||
def _estimate_memory_usage_static( | ||
cls, | ||
*, | ||
X: pd.DataFrame, | ||
hyperparameters: dict = None, | ||
**kwargs, | ||
) -> int: | ||
if hyperparameters is None: | ||
hyperparameters = {} | ||
|
||
dataset_size_mem_est = 10 * hyperparameters.get('cart_nodes_list')[0] * get_approximate_df_mem_usage(X).sum() | ||
baseline_overhead_mem_est = 3e8 # 300 MB generic overhead | ||
|
||
mem_estimate = dataset_size_mem_est + baseline_overhead_mem_est | ||
|
||
return mem_estimate | ||
|
||
@classmethod | ||
def _class_tags(cls): | ||
return {"can_estimate_memory_usage_static": True} | ||
|
||
def _more_tags(self) -> dict: | ||
"""DPDT does not yet support refit full.""" | ||
return {"can_refit_full": False} |
This file contains hidden or 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
Empty file.
This file contains hidden or 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,58 @@ | ||
from autogluon.common.space import Categorical, Real, Int | ||
import numpy as np | ||
|
||
from tabrepo.benchmark.models.ag.dpdt.dpdt_model import BoostedDPDTModel | ||
from tabrepo.utils.config_utils import ConfigGenerator | ||
|
||
name = 'BoostedDPDT' | ||
manual_configs = [ | ||
{}, | ||
] | ||
|
||
# get config from paper | ||
|
||
# Generate 1000 samples from log-normal distribution | ||
# Parameters: mu = log(0.01), sigma = log(10.0) | ||
mu = float(np.log(0.01)) | ||
sigma = float(np.log(10.0)) | ||
samples = np.random.lognormal(mean=mu, sigma=sigma, size=1000) | ||
|
||
# Generate 1000 samples from q_log_uniform_values distribution | ||
# Parameters: min=1.5, max=50.5, q=1 | ||
min_val = 1.5 | ||
max_val = 50.5 | ||
q = 1 | ||
# Generate log-uniform samples and quantize | ||
log_min = np.log(min_val) | ||
log_max = np.log(max_val) | ||
log_uniform_samples = np.random.uniform(log_min, log_max, size=1000) | ||
min_samples_leaf_samples = np.round(np.exp(log_uniform_samples) / q) * q | ||
min_samples_leaf_samples = np.clip(min_samples_leaf_samples, min_val, max_val).astype(int) | ||
|
||
# Generate 1000 samples for min_weight_fraction_leaf | ||
# Values: [0.0, 0.01], probabilities: [0.95, 0.05] | ||
min_weight_fraction_leaf_samples = np.random.choice([0.0, 0.01], size=1000, p=[0.95, 0.05]) | ||
|
||
# Generate 1000 samples for max_features | ||
# Values: ["sqrt", "log2", 10000], probabilities: [0.5, 0.25, 0.25] | ||
max_features_samples = np.random.choice(["sqrt", "log2", 10000], size=1000, p=[0.5, 0.25, 0.25]) | ||
|
||
search_space = { | ||
'learning_rate': Categorical(*samples), # log_normal distribution equivalent | ||
'n_estimators': 1000, # Fixed value as per old config | ||
'max_depth': Categorical(2, 2, 2, 2, 3, 3, 3, 3, 3, 3), | ||
'min_samples_split': Categorical(*np.random.choice([2, 3], size=1000, p=[0.95, 0.05])), | ||
'min_impurity_decrease': Categorical(*np.random.choice([0, 0.01, 0.02, 0.05], size=1000, p=[0.85, 0.05, 0.05, 0.05])), | ||
'cart_nodes_list': Categorical((8, 4), (4, 8), (16, 2), (4, 4, 2)), | ||
'min_samples_leaf': Categorical(*min_samples_leaf_samples), # q_log_uniform equivalent | ||
'min_weight_fraction_leaf': Categorical(*min_weight_fraction_leaf_samples), | ||
'max_features': Categorical(*max_features_samples), | ||
'random_state': Categorical(0, 1, 2, 3, 4) | ||
} | ||
|
||
gen_boosteddpdt = ConfigGenerator(model_cls=BoostedDPDTModel, manual_configs=manual_configs, search_space=search_space) | ||
|
||
|
||
def generate_configs_boosted_dpdt(num_random_configs=200): | ||
config_generator = ConfigGenerator(name=name, manual_configs=manual_configs, search_space=search_space) | ||
return config_generator.generate_all_configs(num_random_configs=num_random_configs) |
This file contains hidden or 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 hidden or 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,17 @@ | ||
import pytest | ||
|
||
|
||
def test_dpdt(): | ||
model_hyperparameters = {"n_estimators": 2, "cart_nodes_list":(4,3)} | ||
|
||
try: | ||
from autogluon.tabular.testing import FitHelper | ||
from tabrepo.benchmark.models.ag.tabicl.tabicl_model import BoostedDPDTModel | ||
model_cls = BoostedDPDTModel | ||
FitHelper.verify_model(model_cls=model_cls, model_hyperparameters=model_hyperparameters) | ||
except ImportError as err: | ||
pytest.skip( | ||
f"Import Error, skipping test... " | ||
f"Ensure you have the proper dependencies installed to run this test:\n" | ||
f"{err}" | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think num_cpus would never be below 1, did you want to do
<=
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello, I will remove it. It is just by experience with the joblib library in which to use all available cpus one write
n_jobs = -1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yes. Here num_cpus might be a string called "auto" in edge cases (not within TabArena benchmarks)