【问题标题】:How to use AWS SageMaker to do XGBoost hyperparameter tuning externally?如何使用 AWS SageMaker 在外部进行 XGBoost 超参数调优?
【发布时间】:2020-01-20 19:54:02
【问题描述】:

这里没有偏见,但我发现很难在 AWS 文档中找到任何内容。 Microsoft Azure 对我来说更容易。

这是我现在拥有的:

  • 完全使用 Python 构建的二进制分类应用程序,其中 xgboost 是 ML 模型。这里 xgboost 有一组从 SageMaker 获得的优化超参数。
  • 用于启动 xgboost 的超参数调整作业的 SageMaker 笔记本。然后我手动将超参数复制粘贴到 Python 应用中的 xgboost 模型中进行预测。

如您所见,我的做法远非理想。我现在要做的是在 Python 应用程序中添加一段代码,以自动启动 SageMaker 中的超参数作业并返回最佳模型。这样,超参数作业就自动完成了,我不需要再次进行复制和粘贴。

但是,我还没有做到这一点。我按照documentation 安装 Python SageMaker API。我还有以下代码在 SageMaker 笔记本中进行 XGBoost 超参数调整:

 def train_xgb_sagemaker(df_train, df_test):
    pd.concat([df_train['show_status'], df_train.drop(['show_status'], axis=1)], axis=1).to_csv('train.csv',
                                                                                                index=False,
                                                                                                header=False)
    pd.concat([df_test['show_status'], df_test.drop(['show_status'], axis=1)], axis=1).to_csv('validation.csv',
                                                                                              index=False, header=False)

    boto3.Session().resource('s3').Bucket(bucket, prefix).upload_file(
        'train.csv')

    boto3.Session().resource('s3').Bucket(bucket, prefix).upload_file(
        'validation.csv')

    s3_input_train = sagemaker.s3_input(s3_data='s3://{}/{}/train'.format(bucket, prefix), content_type='csv')
    s3_input_validation = sagemaker.s3_input(s3_data='s3://{}/{}/validation/'.format(bucket, prefix), content_type='csv')

    print('train_path: ', s3_input_train)
    print('validation_path: ', s3_input_validation)

    # hyperparameter tuning of XGBoost - SageMaker
    sess = sagemaker.Session()

    container = get_image_uri(region, 'xgboost', 0.90 - 1)
    xgb = sagemaker.estimator.Estimator(container,
                                        role,
                                        train_instance_count=1,
                                        train_instance_type='ml.m4.xlarge',
                                        output_path='s3://{}/{}/output'.format(params['BUCKET'], prefix),
                                        sagemaker_session=sess)

    xgb.set_hyperparameters(eval_metric='auc',
                            objective='binary:logistic',
                            num_round=100,
                            rate_drop=0.3,
                            tweedie_variance_power=1.4)

    hyperparameter_ranges = {'eta': ContinuousParameter(0, 1),
                             'min_child_weight': ContinuousParameter(1, 10),
                             'alpha': ContinuousParameter(0, 2),
                             'max_depth': IntegerParameter(1, 10),
                             'num_round': IntegerParameter(1, 300)}

    objective_metric_name = 'validation:auc'

    tuner = HyperparameterTuner(xgb,
                                objective_metric_name,
                                hyperparameter_ranges,
                                max_jobs=20,
                                max_parallel_jobs=3)

    tuner.fit({'train': s3_input_train, 'validation': s3_input_validation}, include_cls_metadata=False)

    smclient.describe_hyper_parameter_tuning_job(
        HyperParameterTuningJobName=tuner.latest_tuning_job.job_name)['HyperParameterTuningJobStatus']

    print('Please check hyperparameter tuning for best models!')
    time.sleep(4000)
    # best_model_path = 's3://{}/{}/output/{}/output/model.tar.gz'.format(bucket, prefix, tuner.best_training_job())
    return tuner.best_training_job()

所以问题是如何将这段代码嵌入到我的 Python 应用程序中,以便我可以在一个地方完成所有操作?非常感谢您提供的任何提示,因为我这几天一直在纠结这个问题!

【问题讨论】:

    标签: python amazon-web-services amazon-sagemaker hyperparameters


    【解决方案1】:

    实际上有一个 Python SDK 调用来部署性能最佳的超参数调整作业模型:

    tuner.deploy()
    

    查找相关文档here

    【讨论】:

    • 谢谢!您有任何示例代码供我遵循吗?我很难遵循文档...非常感谢!
    • 只需在上面的 tuner.fit() 之后添加 tuner.deploy(instance_type='ml.t2.medium', initial_instance_count=1) :)。我不知道是否有这个特定 API 调用的示例。但请注意,即使没有流量,部署的实例也会产生费用!
    猜你喜欢
    • 2020-06-10
    • 1970-01-01
    • 2020-09-10
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    相关资源
    最近更新 更多