贝叶斯调整与bandits
baytune的Python项目详细描述
麻省理工学院从数据到人工智能实验室的开源项目。
一个简单的,可扩展的后台用于开发自动调整系统。
概述
bayesian tuning and bandits是一个简单、可扩展的后端,用于开发automl系统等自动调整系统。它目前被用于ATM(一个允许调整分类器的automl系统)和麻省理工学院的darpa Data driven discovery of models program系统。
- 自由软件:麻省理工学院许可证
- 文档:https://HDI-Project.github.io/BTB
- 主页:https://github.com/HDI-Project/BTB
btb正在积极开发中。如果您遇到任何问题,请报告。
安装
使用pip
安装安装btb的最简单方法是使用pip
。
pip install baytune
从源构建
您还可以克隆存储库并从源代码构建它。
git clone git@github.com:HDI-Project/BTB.git
cd BTB
make install
基本用法
调谐器
为了使用调谐器,我们将创建一个Tuner
实例来指示哪些参数
我们想要调整,他们的类型和我们想要尝试的值的范围。
>>>frombtb.tuningimportGP>>>frombtbimportHyperParameter,ParamTypes>>>tunables=[...('n_estimators',HyperParameter(ParamTypes.INT,[10,500])),...('max_depth',HyperParameter(ParamTypes.INT,[3,20]))...]>>>tuner=GP(tunables)
然后我们在一个循环中执行以下三个步骤。
让调谐器提出一组新的参数
>>>parameters=tuner.propose()>>>parameters{'n_estimators':297,'max_depth':3}
使用这些参数拟合并评分新模型
>>>model=RandomForestClassifier(**parameters)>>>model.fit(X_train,y_train)RandomForestClassifier(bootstrap=True,class_weight=None,criterion='gini',max_depth=3,max_features='auto',max_leaf_nodes=None,min_impurity_decrease=0.0,min_impurity_split=None,min_samples_leaf=1,min_samples_split=2,min_weight_fraction_leaf=0.0,n_estimators=297,n_jobs=1,oob_score=False,random_state=None,verbose=0,warm_start=False)>>>score=model.score(X_test,y_test)>>>score0.77
将使用的参数和获得的分数传回调谐器
tuner.add(parameters,score)
在每次迭代中,Tuner
将使用有关先前测试的信息
评估并提出具有最高概率的参数值集
获得最高的分数。
有关更详细的示例,请检查examples
文件夹中的脚本。
选择器
选择器与调谐器一起使用,以便查找 一旦对模型进行了适当的微调,就可以决定哪种模型似乎能得到最好的结果。
为了使用选择器,我们将为每个
我们想要尝试,以及Selector
实例。
>>> from sklearn.ensemble import RandomForestClassifier
>>> from sklearn.svm import SVC
>>> models = {
... 'RF': RandomForestClassifier,
... 'SVC': SVC
... }
>>> from btb.selection import UCB1
>>> selector = UCB1(['RF', 'SVC'])
>>> tuners = {
... 'RF': GP([
... ('n_estimators', HyperParameter(ParamTypes.INT, [10, 500])),
... ('max_depth', HyperParameter(ParamTypes.INT, [3, 20]))
... ]),
... 'SVC': GP([
... ('c', HyperParameter(ParamTypes.FLOAT_EXP, [0.01, 10.0])),
... ('gamma', HyperParameter(ParamTypes.FLOAT, [0.000000001, 0.0000001]))
... ])
... }
然后我们循环执行以下步骤。
将获得的所有分数传递给选择器,让它决定要测试哪个模型。
>>>next_choice=selector.select({'RF':tuners['RF'].y,'SVC':tuners['SVC'].y})>>>next_choice'RF'
从指定的调谐器获取一组新参数并创建一个模型实例。
>>>parameters=tuners[next_choice].propose()>>>parameters{'n_estimators':289,'max_depth':18}>>>model=models[next_choice](**parameters)
评估新模型实例的分数并将其传回调谐器
>>>model.fit(X_train,y_train)RandomForestClassifier(bootstrap=True,class_weight=None,criterion='gini',max_depth=18,max_features='auto',max_leaf_nodes=None,min_impurity_decrease=0.0,min_impurity_split=None,min_samples_leaf=1,min_samples_split=2,min_weight_fraction_leaf=0.0,n_estimators=289,n_jobs=1,oob_score=False,random_state=None,verbose=0,warm_start=False)>>>score=model.score(X_test,y_test)>>>score0.89>>>tuners[next_choice].add(parameters,score)
参考文献
如果您使用BTB,请考虑引用以下工作:
劳拉·古斯塔夫森。bayesian优化和bandits:automl的可扩展开源库。麻省理工学院经济学硕士论文,2018年6月。(pdf)
@MastersThesis{Laura:2018,title="Bayesian Tuning and Bandits: An Extensible, Open Source Library for AutoML",author="Laura Gustafson",month="May",year="2018",url="https://dai.lids.mit.edu/wp-content/uploads/2018/05/Laura_MEng_Final.pdf",type="M. Eng Thesis",address="Cambridge, MA",school="Massachusetts Institute of Technology",}
# History
## 0.2.5
### Bug Fixes
* Issue #115: HyperParameter subclass instantiation not working properly
## 0.2.4
### Internal Improvements
* Issue #62: Test for `None` in `HyperParameter.cast` instead of `HyperParameter.__init__`
### Bug fixes
* Issue #98: Categorical hyperparameters do not support `None` as input
* Issue #89: Fix the computation of `avg_rewards` in `BestKReward`
## 0.2.3
### Bug Fixes
* Issue #84: Error in GP tuning when only one parameter is present bug
* Issue #96: Fix pickling of HyperParameters
* Issue #98: Fix implementation of the GPEi tuner
## 0.2.2
### Internal Improvements
* Updated documentation
### Bug Fixes
* Issue #94: Fix unicode `param_type` caused error on python 2.
## 0.2.1
### Bug fixes
* Issue #74: `ParamTypes.STRING` tunables do not work
## 0.2.0
### New Features
* New Recommendation module
* New HyperParameter types
* Improved documentation and examples
* Fully tested Python 2.7, 3.4, 3.5 and 3.6 compatibility
* HyperParameter copy and deepcopy support
* Replace print statements with logging
### Internal Improvements
* Integrated with Travis-CI
* Exhaustive unit testing
* New implementation of HyperParameter
* Tuner builds a grid of real values instead of indices
* Resolve Issue #29: Make args explicit in `__init__` methods
* Resolve Issue #34: make all imports explicit
### Bug Fixes
* Fix error from mixing string/numerical hyperparameters
* Inverse transform for categorical hyperparameter returns single item
## 0.1.2
* Issue #47: Add missing requirements in v0.1.1 setup.py
* Issue #46: Error on v0.1.1: 'GP' object has no attribute 'X'
## 0.1.1
* First release.