-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add XGBPipeLine for benchmark and Fix bugs in TabularLightningModule
- Loading branch information
1 parent
d8b79bc
commit 687371b
Showing
7 changed files
with
97 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
hparams_range = { | ||
'max_leaves' : ['suggest_int', ['max_leaves', 300, 4000]], | ||
'n_estimators' : ['suggest_int', ['n_estimators', 10, 3000]], | ||
'learning_rate' : ['suggest_float', ['learning_rate',0, 1]], | ||
'max_depth' : ['suggest_int', ['max_depth', 3, 20]], | ||
'scale_pos_weight' : ['suggest_int', ['scale_pos_weight', 1, 100]], | ||
} |
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,4 +1,6 @@ | ||
from .xgb_pipeline import XGBPipeLine | ||
from .vime_pipeline import VIMEPipeLine | ||
from .subtab_pipeline import SubTabPipeLine | ||
from .scarf_pipeline import SCARFPipeLine | ||
__all__ = ["VIMEPipeLine", "SubTabPipeLine", "SCARFPipeLine"] | ||
|
||
__all__ = ["XGBPipeLine", "VIMEPipeLine", "SubTabPipeLine", "SCARFPipeLine"] |
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,74 @@ | ||
import argparse | ||
import pandas as pd | ||
from typing import List, Dict, Any, Union, Type | ||
|
||
from dataclasses import dataclass, field, asdict | ||
from .pipeline import PipeLine | ||
from xgboost import XGBClassifier, XGBRegressor | ||
from hparams_range.xgb import hparams_range | ||
|
||
@dataclass | ||
class XGBConfig: | ||
|
||
max_leaves: int | ||
|
||
n_estimators: int | ||
|
||
learning_rate: float | ||
|
||
max_depth: int | ||
|
||
scale_pos_weight: int | ||
|
||
early_stopping_rounds: int | ||
|
||
# # task: str = field(default=None) | ||
|
||
# def __post_init__(self): | ||
# if self.task is None: | ||
# raise ValueError("The task of the problem must be specified in the 'task' attribute.") | ||
# elif (type(self.task) is not str or (self.task != "regression" and self.task != "classification")): | ||
# raise ValueError(f"{self.task} is not a valid task. Choices are: ['regression', 'classification']") | ||
|
||
class XGBModule(object): | ||
def __init__(self, model_class: Union[XGBClassifier, XGBRegressor]): | ||
self.model_class = model_class | ||
|
||
def __call__(self, config: XGBConfig): | ||
return self.model_class(**asdict(config)) | ||
|
||
class XGBPipeLine(PipeLine): | ||
def __init__(self, args: argparse.Namespace, data: pd.DataFrame, label: pd.Series, continuous_cols: List[str], category_cols: List[str], output_dim: int, metric: str, metric_hparams: Dict[str, Any] = {}): | ||
super().__init__(args, data, label, continuous_cols, category_cols, output_dim, metric, metric_hparams) | ||
|
||
def initialize(self): | ||
self.config_class = XGBConfig | ||
if self.output_dim == 1: | ||
self.pl_module_class = XGBRegressor | ||
else: | ||
self.pl_module_class = XGBClassifier | ||
self.pl_module_class = XGBModule(self.pl_module_class) | ||
|
||
self.hparams_range = hparams_range | ||
|
||
def _get_config(self, hparams: Dict[str, Any]): | ||
# hparams["task"] = "regression" if self.output_dim == 1 else "classification" | ||
hparams["early_stopping_rounds"] = self.args.second_phase_patience | ||
|
||
return self.config_class(**hparams) | ||
# return asdict(self.config_class(**hparams)) | ||
|
||
def fit_model(self, pl_module: XGBModule, config: XGBConfig): | ||
|
||
pl_module.fit(self.X_train, self.y_train, eval_set=[(self.X_valid, self.y_valid)], verbose = 0) | ||
|
||
return pl_module | ||
|
||
def evaluate(self, pl_module: XGBModule, config: XGBConfig, X: pd.DataFrame, y: pd.Series): | ||
|
||
preds = pl_module.predict(X) | ||
|
||
score = self.metric(preds, y) | ||
|
||
return score | ||
|
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