Spaces:
Sleeping
Sleeping
File size: 15,763 Bytes
33d4721 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
import argparse
import json
import os
from functools import partial
import joblib
import numpy as np
import optuna
import pandas as pd
from datasets import load_dataset, load_from_disk
from huggingface_hub import HfApi
from sklearn import pipeline, preprocessing
from sklearn.compose import ColumnTransformer
from autotrain import logger
from autotrain.trainers.common import (
ALLOW_REMOTE_CODE,
monitor,
pause_space,
remove_autotrain_data,
save_training_params,
)
from autotrain.trainers.tabular import utils
from autotrain.trainers.tabular.params import TabularParams
def parse_args():
# get training_config.json from the end user
parser = argparse.ArgumentParser()
parser.add_argument("--training_config", type=str, required=True)
return parser.parse_args()
def optimize(trial, model_name, xtrain, xvalid, ytrain, yvalid, eval_metric, task, preprocessor):
"""
Optimize the model based on the given trial and parameters.
Parameters:
trial (dict or optuna.trial.Trial): The trial object or dictionary containing hyperparameters.
model_name (str): The name of the model to be used (e.g., "xgboost").
xtrain (pd.DataFrame or np.ndarray): Training features.
xvalid (pd.DataFrame or np.ndarray): Validation features.
ytrain (pd.Series or np.ndarray): Training labels.
yvalid (pd.Series or np.ndarray): Validation labels.
eval_metric (str): The evaluation metric to be used for optimization.
task (str): The type of task (e.g., "binary_classification", "multi_class_classification", "single_column_regression").
preprocessor (object): The preprocessor object to be applied to the data.
Returns:
float or tuple: If trial is a dictionary, returns a tuple containing the models, preprocessor, and metric dictionary.
Otherwise, returns the loss value based on the evaluation metric.
"""
if isinstance(trial, dict):
params = trial
else:
params = utils.get_params(trial, model_name, task)
labels = None
if task == "multi_class_classification":
labels = np.unique(ytrain)
metrics = utils.TabularMetrics(sub_task=task, labels=labels)
if task in ("binary_classification", "multi_class_classification", "single_column_regression"):
ytrain = ytrain.ravel()
yvalid = yvalid.ravel()
if preprocessor is not None:
try:
xtrain = preprocessor.fit_transform(xtrain)
xvalid = preprocessor.transform(xvalid)
except ValueError:
logger.info("Preprocessing failed, using nan_to_num")
train_cols = xtrain.columns.tolist()
valid_cols = xvalid.columns.tolist()
xtrain = np.nan_to_num(xtrain)
xvalid = np.nan_to_num(xvalid)
# convert back to dataframe
xtrain = pd.DataFrame(xtrain, columns=train_cols)
xvalid = pd.DataFrame(xvalid, columns=valid_cols)
xtrain = preprocessor.fit_transform(xtrain)
xvalid = preprocessor.transform(xvalid)
if model_name == "xgboost":
params["eval_metric"] = eval_metric
_model = utils.TabularModel(model_name, preprocessor=None, sub_task=task, params=params)
model = _model.pipeline
models = []
if task in ("multi_label_classification", "multi_column_regression"):
# also multi_column_regression
ypred = []
models = [model] * ytrain.shape[1]
for idx, _m in enumerate(models):
if model_name == "xgboost":
_m.fit(
xtrain,
ytrain[:, idx],
model__eval_set=[(xvalid, yvalid[:, idx])],
model__verbose=False,
)
else:
_m.fit(xtrain, ytrain[:, idx])
if task == "multi_column_regression":
ypred_temp = _m.predict(xvalid)
else:
if _model.use_predict_proba:
ypred_temp = _m.predict_proba(xvalid)[:, 1]
else:
ypred_temp = _m.predict(xvalid)
ypred.append(ypred_temp)
ypred = np.column_stack(ypred)
else:
models = [model]
if model_name == "xgboost":
model.fit(
xtrain,
ytrain,
model__eval_set=[(xvalid, yvalid)],
model__verbose=False,
)
else:
models[0].fit(xtrain, ytrain)
if _model.use_predict_proba:
ypred = models[0].predict_proba(xvalid)
else:
ypred = models[0].predict(xvalid)
if task == "multi_class_classification":
if ypred.reshape(xvalid.shape[0], -1).shape[1] != len(labels):
ypred_ohe = np.zeros((xvalid.shape[0], len(labels)))
ypred_ohe[np.arange(xvalid.shape[0]), ypred] = 1
ypred = ypred_ohe
if task == "binary_classification":
if ypred.reshape(xvalid.shape[0], -1).shape[1] != 2:
ypred = np.column_stack([1 - ypred, ypred])
# calculate metric
metric_dict = metrics.calculate(yvalid, ypred)
# change eval_metric key to loss
if eval_metric in metric_dict:
metric_dict["loss"] = metric_dict[eval_metric]
logger.info(f"Metrics: {metric_dict}")
if isinstance(trial, dict):
return models, preprocessor, metric_dict
return metric_dict["loss"]
@monitor
def train(config):
"""
Train a tabular model based on the provided configuration.
Args:
config (dict or TabularParams): Configuration parameters for training. If a dictionary is provided, it will be converted to a TabularParams object.
Raises:
Exception: If `valid_data` is None, indicating that a valid split for tabular training was not provided.
The function performs the following steps:
1. Loads the training and validation datasets from disk or a specified data path.
2. Identifies and processes categorical and numerical columns.
3. Encodes target columns for classification tasks.
4. Constructs preprocessing pipelines for numerical and categorical data.
5. Determines the sub-task (e.g., binary classification, multi-class classification, regression).
6. Optimizes the model using Optuna for hyperparameter tuning.
7. Saves the best model and target encoders to disk.
8. Creates and saves a model card.
9. Optionally pushes the model to the Hugging Face Hub.
Note:
The function expects the configuration to contain various parameters such as `data_path`, `train_split`, `valid_split`, `categorical_columns`, `numerical_columns`, `model`, `task`, `num_trials`, `time_limit`, `project_name`, `token`, `username`, and `push_to_hub`.
"""
if isinstance(config, dict):
config = TabularParams(**config)
logger.info("Starting training...")
logger.info(f"Training config: {config}")
train_data = None
valid_data = None
if config.data_path == f"{config.project_name}/autotrain-data":
logger.info("loading dataset from disk")
train_data = load_from_disk(config.data_path)[config.train_split]
else:
if ":" in config.train_split:
dataset_config_name, split = config.train_split.split(":")
train_data = load_dataset(
config.data_path,
name=dataset_config_name,
split=split,
token=config.token,
trust_remote_code=ALLOW_REMOTE_CODE,
)
else:
train_data = load_dataset(
config.data_path,
split=config.train_split,
token=config.token,
trust_remote_code=ALLOW_REMOTE_CODE,
)
train_data = train_data.to_pandas()
if config.valid_split is not None:
if config.data_path == f"{config.project_name}/autotrain-data":
logger.info("loading dataset from disk")
valid_data = load_from_disk(config.data_path)[config.valid_split]
else:
if ":" in config.valid_split:
dataset_config_name, split = config.valid_split.split(":")
valid_data = load_dataset(
config.data_path,
name=dataset_config_name,
split=split,
token=config.token,
trust_remote_code=ALLOW_REMOTE_CODE,
)
else:
valid_data = load_dataset(
config.data_path,
split=config.valid_split,
token=config.token,
trust_remote_code=ALLOW_REMOTE_CODE,
)
valid_data = valid_data.to_pandas()
if valid_data is None:
raise Exception("valid_data is None. Please provide a valid_split for tabular training.")
# determine which columns are categorical
if config.categorical_columns is None:
config.categorical_columns = utils.get_categorical_columns(train_data)
if config.numerical_columns is None:
config.numerical_columns = utils.get_numerical_columns(train_data)
_id_target_cols = (
[config.id_column] + config.target_columns if config.id_column is not None else config.target_columns
)
config.numerical_columns = [c for c in config.numerical_columns if c not in _id_target_cols]
config.categorical_columns = [c for c in config.categorical_columns if c not in _id_target_cols]
useful_columns = config.categorical_columns + config.numerical_columns
logger.info(f"Categorical columns: {config.categorical_columns}")
logger.info(f"Numerical columns: {config.numerical_columns}")
# convert object columns to categorical
for col in config.categorical_columns:
train_data[col] = train_data[col].astype("category")
valid_data[col] = valid_data[col].astype("category")
logger.info(f"Useful columns: {useful_columns}")
target_encoders = {}
if config.task == "classification":
for target_column in config.target_columns:
target_encoder = preprocessing.LabelEncoder()
target_encoder.fit(train_data[target_column])
target_encoders[target_column] = target_encoder
# encode target columns in train and valid data
for k, v in target_encoders.items():
train_data.loc[:, k] = v.transform(train_data[k])
valid_data.loc[:, k] = v.transform(valid_data[k])
numeric_transformer = "passthrough"
categorical_transformer = "passthrough"
transformers = []
preprocessor = None
numeric_steps = []
imputer = utils.get_imputer(config.numerical_imputer)
scaler = utils.get_scaler(config.numeric_scaler)
if imputer is not None:
numeric_steps.append(("num_imputer", imputer))
if scaler is not None:
numeric_steps.append(("num_scaler", scaler))
if len(numeric_steps) > 0:
numeric_transformer = pipeline.Pipeline(numeric_steps)
transformers.append(("numeric", numeric_transformer, config.numerical_columns))
categorical_steps = []
imputer = utils.get_imputer(config.categorical_imputer)
if imputer is not None:
categorical_steps.append(("cat_imputer", imputer))
if len(config.categorical_columns) > 0:
if config.model in ("xgboost", "lightgbm", "randomforest", "catboost", "extratrees"):
categorical_steps.append(
(
"cat_encoder",
preprocessing.OrdinalEncoder(
handle_unknown="use_encoded_value",
categories="auto",
unknown_value=np.nan,
),
)
)
else:
categorical_steps.append(
(
"cat_encoder",
preprocessing.OneHotEncoder(handle_unknown="ignore"),
)
)
if len(categorical_steps) > 0:
categorical_transformer = pipeline.Pipeline(categorical_steps)
transformers.append(("categorical", categorical_transformer, config.categorical_columns))
if len(transformers) > 0:
preprocessor = ColumnTransformer(transformers=transformers, verbose=True, n_jobs=-1)
logger.info(f"Preprocessor: {preprocessor}")
xtrain = train_data[useful_columns].reset_index(drop=True)
xvalid = valid_data[useful_columns].reset_index(drop=True)
ytrain = train_data[config.target_columns].values
yvalid = valid_data[config.target_columns].values
# determine sub_task
if config.task == "classification":
if len(target_encoders) == 1:
if len(target_encoders[config.target_columns[0]].classes_) == 2:
sub_task = "binary_classification"
else:
sub_task = "multi_class_classification"
else:
sub_task = "multi_label_classification"
else:
if len(config.target_columns) > 1:
sub_task = "multi_column_regression"
else:
sub_task = "single_column_regression"
eval_metric, direction = utils.get_metric_direction(sub_task)
logger.info(f"Sub task: {sub_task}")
args = {
"model_name": config.model,
"xtrain": xtrain,
"xvalid": xvalid,
"ytrain": ytrain,
"yvalid": yvalid,
"eval_metric": eval_metric,
"task": sub_task,
"preprocessor": preprocessor,
}
optimize_func = partial(optimize, **args)
study = optuna.create_study(direction=direction, study_name="AutoTrain")
study.optimize(optimize_func, n_trials=config.num_trials, timeout=config.time_limit)
best_params = study.best_params
logger.info(f"Best params: {best_params}")
best_models, best_preprocessors, best_metrics = optimize(best_params, **args)
models = (
[pipeline.Pipeline([("preprocessor", best_preprocessors), ("model", m)]) for m in best_models]
if best_preprocessors is not None
else best_models
)
joblib.dump(
models[0] if len(models) == 1 else models,
os.path.join(config.project_name, "model.joblib"),
)
joblib.dump(target_encoders, os.path.join(config.project_name, "target_encoders.joblib"))
model_card = utils.create_model_card(config, sub_task, best_params, best_metrics)
if model_card is not None:
with open(os.path.join(config.project_name, "README.md"), "w") as fp:
fp.write(f"{model_card}")
# remove token key from training_params.json located in output directory
# first check if file exists
if os.path.exists(f"{config.project_name}/training_params.json"):
training_params = json.load(open(f"{config.project_name}/training_params.json"))
training_params.pop("token")
json.dump(training_params, open(f"{config.project_name}/training_params.json", "w"))
# save model card to output directory as README.md
with open(f"{config.project_name}/README.md", "w") as f:
f.write(model_card)
if config.push_to_hub:
remove_autotrain_data(config)
save_training_params(config)
logger.info("Pushing model to hub...")
api = HfApi(token=config.token)
api.create_repo(repo_id=f"{config.username}/{config.project_name}", repo_type="model", private=True)
api.upload_folder(
folder_path=config.project_name, repo_id=f"{config.username}/{config.project_name}", repo_type="model"
)
pause_space(config)
if __name__ == "__main__":
args = parse_args()
training_config = json.load(open(args.training_config))
config = TabularParams(**training_config)
train(config)
|