【问题标题】:Using Ray-Tune with sklearn's RandomForestClassifier将 Ray-Tune 与 sklearn 的 RandomForestClassifier 结合使用
【发布时间】:2020-11-01 12:41:31
【问题描述】:

将不同的基础和文档示例放在一起,我设法想出了这个:

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

def objective(config, reporter):
  for i in range(config['iterations']):
    model = RandomForestClassifier(random_state=0, n_jobs=-1, max_depth=None, n_estimators= int(config['n_estimators']), min_samples_split=int(config['min_samples_split']), min_samples_leaf=int(config['min_samples_leaf']))
    model.fit(X_train, y_train)
    y_pred = model.predict(X_test)
    # Feed the score back to tune?
    reporter(precision=precision_score(y_test, y_pred, average='macro'))

space = {'n_estimators': (100,200),
        'min_samples_split': (2, 10),
        'min_samples_leaf': (1, 5)}

algo = BayesOptSearch(
    space,
    metric="precision",
    mode="max",
    utility_kwargs={
        "kind": "ucb",
        "kappa": 2.5,
        "xi": 0.0
    },
    verbose=3
    )

scheduler = AsyncHyperBandScheduler(metric="precision", mode="max")
config = {
    "num_samples": 1000,
    "config": {
        "iterations": 10,
    }
}
results = run(objective,
    name="my_exp",
    search_alg=algo,
    scheduler=scheduler,
    stop={"training_iteration": 400, "precision": 0.80},
    resources_per_trial={"cpu":2, "gpu":0.5},
    **config)

print(results.dataframe())
print("Best config: ", results.get_best_config(metric="precision"))

它运行,我能够在一切结束时获得最佳配置。但是,我的疑问主要在于objective 函数。我写的正确吗?没有我能找到的样本

跟进问题:

  1. 配置对象中的num_samples 是什么?是从每次试验的整体训练数据中提取的样本数量吗?

【问题讨论】:

    标签: machine-learning random-forest ray ray-tune


    【解决方案1】:

    Tune 现在有原生 sklearn 绑定:https://github.com/ray-project/tune-sklearn

    你可以试一试吗?


    要回答您最初的问题,目标函数看起来不错; num_samples 是你想尝试的超参数配置的总数。

    另外,您需要从训练函数中删除 forloop:

    def objective(config, reporter):
        model = RandomForestClassifier(random_state=0, n_jobs=-1, max_depth=None, n_estimators= int(config['n_estimators']), min_samples_split=int(config['min_samples_split']), min_samples_leaf=int(config['min_samples_leaf']))
        model.fit(X_train, y_train)
        y_pred = model.predict(X_test)
        # Feed the score back to tune
        reporter(precision=precision_score(y_test, y_pred, average='macro'))
    

    【讨论】:

    • 我也一直在尝试那个包。我无法弄清楚如何将某些参数(如n_estimators)转换为整数。贝叶斯空间需要使用TuneSearchCV 的元组。示例位于此处:github.com/ray-project/tune-sklearn/blob/master/examples/… 使用随机搜索。
    • 知道了;更新了上面的答案,一旦我们让 TuneSearchCV 支持多种类型,也会再次发表评论。
    • 非常感谢。 :) 你的回答让我放心。我很期待Tune-Sklearn 上的工作
    • (顺便说一句,刚刚仔细查看了您的函数,您需要删除目标函数中的 forloop)
    • 哦,我明白了!完成后,我还可以删除 config 对象中的 config 键和值吗?您告诉我删除 for 循环的原因是因为冗余?配置对象中的num_samples 已经多次运行跟踪,对吗?
    猜你喜欢
    • 2019-08-03
    • 2021-12-15
    • 2021-11-05
    • 2022-12-03
    • 1970-01-01
    • 2021-10-17
    • 2018-05-06
    • 2014-06-11
    • 2013-03-29
    相关资源
    最近更新 更多