You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Getting-Started.md 5.5 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Getting Started
  2. <!-- ### Welcome to FLAML, a Fast Library for Automated Machine Learning & Tuning! -->
  3. FLAML is a lightweight Python library that finds accurate machine
  4. learning models automatically, efficiently and economically. It frees users from selecting learners and hyperparameters for each learner.
  5. ### Main Features
  6. 1. For common machine learning tasks like classification and regression, it quickly finds quality models for user-provided data with low computational resources. It supports both classifcal machine learning models and deep neural networks.
  7. 2. It is easy to customize or extend. Users can find their desired customizability from a smooth range: minimal customization (computational resource budget), medium customization (e.g., scikit-style learner, search space and metric), or full customization (arbitrary training and evaluation code). Users can customize only when and what they need to, and leave the rest to the library.
  8. 3. It supports fast and economical automatic tuning, capable of handling large search space with heterogeneous evaluation cost and complex constraints/guidance/early stopping. FLAML is powered by a new, [cost-effective
  9. hyperparameter optimization](Use-Cases/Tune-User-Defined-Function#hyperparameter-optimization-algorithm)
  10. and learner selection method invented by Microsoft Research.
  11. ### Quickstart
  12. Install FLAML from pip: `pip install flaml`. Find more options in [Installation](Installation).
  13. There are several ways of using flaml:
  14. #### [Task-oriented AutoML](Use-Cases/task-oriented-automl)
  15. For example, with three lines of code, you can start using this economical and fast AutoML engine as a scikit-learn style estimator.
  16. ```python
  17. from flaml import AutoML
  18. automl = AutoML()
  19. automl.fit(X_train, y_train, task="classification", time_budget=60)
  20. ```
  21. It automatically tunes the hyperparameters and selects the best model from default learners such as LightGBM, XGBoost, random forest etc. for the specified time budget 60 seconds. [Customizing](Use-Cases/task-oriented-automl#customize-automlfit) the optimization metrics, learners and search spaces etc. is very easy. For example,
  22. ```python
  23. automl.add_learner("mylgbm", MyLGBMEstimator)
  24. automl.fit(X_train, y_train, task="classification", metric=custom_metric, estimator_list=["mylgbm"], time_budget=60)
  25. ```
  26. #### [Tune user-defined function](Use-Cases/Tune-User-Defined-Function)
  27. You can run generic hyperparameter tuning for a custom function (machine learning or beyond). For example,
  28. ```python
  29. from flaml import tune
  30. from flaml.model import LGBMEstimator
  31. def train_lgbm(config: dict) -> dict:
  32. # convert config dict to lgbm params
  33. params = LGBMEstimator(**config).params
  34. # train the model
  35. train_set = lightgbm.Dataset(csv_file_name)
  36. model = lightgbm.train(params, train_set)
  37. # evaluate the model
  38. pred = model.predict(X_test)
  39. mse = mean_squared_error(y_test, pred)
  40. # return eval results as a dictionary
  41. return {"mse": mse}
  42. # load a built-in search space from flaml
  43. flaml_lgbm_search_space = LGBMEstimator.search_space(X_train.shape)
  44. # specify the search space as a dict from hp name to domain; you can define your own search space same way
  45. config_search_space = {hp: space["domain"] for hp, space in flaml_lgbm_search_space.items()}
  46. # give guidance about hp values corresponding to low training cost, i.e., {"n_estimators": 4, "num_leaves": 4}
  47. low_cost_partial_config = {
  48. hp: space["low_cost_init_value"]
  49. for hp, space in flaml_lgbm_search_space.items()
  50. if "low_cost_init_value" in space
  51. }
  52. # run the tuning, minimizing mse, with total time budget 3 seconds
  53. analysis = tune.run(
  54. train_lgbm, metric="mse", mode="min", config=config_search_space,
  55. low_cost_partial_config=low_cost_partial_config, time_budget_s=3, num_samples=-1,
  56. )
  57. ```
  58. Please see this [script](https://github.com/microsoft/FLAML/blob/main/test/tune_example.py) for the complete version of the above example.
  59. #### [Zero-shot AutoML](Use-Cases/Zero-Shot-AutoML)
  60. FLAML offers a unique, seamless and effortless way to leverage AutoML for the commonly used classifiers and regressors such as LightGBM and XGBoost. For example, if you are using `lightgbm.LGBMClassifier` as your current learner, all you need to do is to replace `from ligthgbm import LGBMClassifier` by:
  61. ```python
  62. from flaml.default import LGBMClassifier
  63. ```
  64. Then, you can use it just like you use the original `LGMBClassifier`. Your other code can remain unchanged. When you call the `fit()` function from `flaml.default.LGBMClassifier`, it will automatically instantiate a good data-dependent hyperparameter configuration for your dataset, which is expected to work better than the default configuration.
  65. ### Where to Go Next?
  66. * Understand the use cases for [Task-oriented AutoML](Use-Cases/task-oriented-automl), [Tune user-defined function](Use-Cases/Tune-User-Defined-Function) and [Zero-shot AutoML](Use-Cases/Zero-Shot-AutoML).
  67. * Find code examples under "Examples": from [AutoML - Classification](Examples/AutoML-Classification) to [Tune - PyTorch](Examples/Tune-PyTorch).
  68. * Watch [video tutorials](https://www.youtube.com/channel/UCfU0zfFXHXdAd5x-WvFBk5A).
  69. * Learn about [research](Research) around FLAML.
  70. * Refer to [SDK](reference/automl) and [FAQ](FAQ).
  71. If you like our project, please give it a [star](https://github.com/microsoft/FLAML/stargazers) on GitHub. If you are interested in contributing, please read [Contributor's Guide](Contribute).