cost_model.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import numpy as np
  2. from .utils import *
  3. try:
  4. import xgboost as xgb
  5. except ImportError:
  6. xgb = None
  7. class XGBoostCostModel():
  8. def __init__(self, loss_type, num_threads=None, log_interval=25, upper_model=None):
  9. assert xgb is not None, "missing requirements, please install deepspeed w. 'autotuning_ml' extra."
  10. self.loss_type = loss_type
  11. if loss_type == "reg":
  12. self.xgb_params = {
  13. "max_depth": 3,
  14. "gamma": 0.0001,
  15. "min_child_weight": 1,
  16. "subsample": 1.0,
  17. "eta": 0.3,
  18. "lambda": 1.0,
  19. "alpha": 0,
  20. "objective": "reg:linear",
  21. }
  22. elif loss_type == "rank":
  23. self.xgb_params = {
  24. "max_depth": 3,
  25. "gamma": 0.0001,
  26. "min_child_weight": 1,
  27. "subsample": 1.0,
  28. "eta": 0.3,
  29. "lambda": 1.0,
  30. "alpha": 0,
  31. "objective": "rank:pairwise",
  32. }
  33. else:
  34. raise RuntimeError("Invalid loss type: " + loss_type)
  35. self.xgb_params["verbosity"] = 0
  36. if num_threads:
  37. self.xgb_params["nthread"] = num_threads
  38. def fit(self, xs, ys):
  39. x_train = np.array(xs, dtype=np.float32)
  40. y_train = np.array(ys, dtype=np.float32)
  41. y_max = np.max(y_train)
  42. y_train = y_train / max(y_max, 1e-9)
  43. index = np.random.permutation(len(x_train))
  44. dtrain = xgb.DMatrix(x_train[index], y_train[index])
  45. self.bst = xgb.train(self.xgb_params, dtrain)
  46. def predict(self, xs):
  47. features = xgb.DMatrix(xs)
  48. return self.bst.predict(features)