简体   繁体   English

AWS SageMaker 训练脚本:如何传递自定义用户参数

[英]AWS SageMaker training script: how to pass custom user parameters

I am training a classifier using Scikit-learn with the SageMaker Python SDK.我正在使用 Scikit-learn 和 SageMaker Python SDK 训练分类器。
The overall process involves three sequential phases:整个过程包括三个连续的阶段:

  1. hyper-parameters tuning job using training and validation data sets使用训练和验证数据集的超参数调整工作
  2. training job with the best hyper-parameters founded in 1. and the whole data set (training + validation from 1.)使用在 1. 中建立的最佳超参数和整个数据集(从 1. 开始的训练 + 验证)进行训练
  3. training a calibrated model with the provided the "prefit" model from 2. and an additional data set for calibration.使用 2. 提供的“prefit”model 和用于校准的附加数据集训练校准后的 model。

The reason I need to split the process is to save the un-calibrated model created at step 2.我需要拆分流程的原因是为了保存在步骤 2 中创建的未校准的 model。

For each of this step I prepare a training script as explained in: https://sagemaker.readthedocs.io/en/stable/using_sklearn.html#prepare-a-scikit-learn-training-script对于这一步中的每一步,我都准备了一个训练脚本,如下所述: https://sagemaker.readthedocs.io/en/stable/using_sklearn.html#prepare-a-scikit-learn-training-script

The three scripts are very similar and to avoid code redundancy I would like to use a single script with additional logic inside for the three situation.这三个脚本非常相似,为了避免代码冗余,我想在这三种情况下使用一个带有附加逻辑的脚本。 More precisely, I would like to pass additional custom parameters to the .fit methods of the sagemaker.tuner.HyperparameterTuner and sagemaker.sklearn.estimator.SKLearn objects in order to be able to action the logic in the script depending on the usage (phase 1. ,2. or 3.).更准确地说,我想将额外的自定义参数传递给sagemaker.tuner.HyperparameterTunersagemaker.sklearn.estimator.SKLearn对象的.fit方法,以便能够根据使用情况(阶段1. ,2. 或 3.).

I already tried by hacking the SM_CHANNEL_XXX我已经尝试破解SM_CHANNEL_XXX
parser.add_argument('--myparam', type=str, default=os.environ.get('SM_CHANNEL_MYPRAM')) while invoking .fit(inputs={'train': ..., 'test': ..., 'myparam': myvalue}) but it expect a valid s3 URI.调用parser.add_argument('--myparam', type=str, default=os.environ.get('SM_CHANNEL_MYPRAM')) .fit(inputs={'train': ..., 'test': ..., 'myparam': myvalue})但它需要一个有效的 s3 URI。

Any idea on how to pass extra custom parameters to the training scripts?关于如何将额外的自定义参数传递给训练脚本的任何想法?

According to Sagemaker documentation seen here , you can access the hyperparameters in your training script as command line arguments, in the following way, eg:根据此处看到的 Sagemaker 文档,您可以通过以下方式将训练脚本中的超参数作为命令行 arguments 访问,例如:

parser = argparse.ArgumentParser()
parser.add_argument('--epochs', type=int, default=10)
parser.add_argument('--batch_size', type=int, default=100)
parser.add_argument('--learning_rate', type=float, default=0.1)

You can pass hyperparameters not in the fit method but right in to step before when you create the estimator.您可以不在 fit 方法中传递超参数,而是在创建估计器之前直接传递超参数。 The example in the docs would be:文档中的示例是:

sklearn_estimator = SKLearn('sklearn-train.py',
                        train_instance_type='ml.m4.xlarge',
                        framework_version='0.20.0',
                        hyperparameters = {'epochs': 20, 'batch-size': 64, 'learning- 
rate': 0.1})
sklearn_estimator.fit({'train': 's3://my-data-bucket/path/to/my/training/data',
                    'test': 's3://my-data-bucket/path/to/my/test/data'})

This is how you bring your parameters (from your notebook) into the training script for access via parser.add_argument.这就是您将参数(从笔记本中)带入训练脚本以通过 parser.add_argument 访问的方式。 If you have only one script you can handle your logic inside your script.如果您只有一个脚本,您可以在脚本中处理您的逻辑。 But this does not add custom parameters to the .fit method of sagemaker.tuner.HyperparameterTuner.但这不会向 sagemaker.tuner.HyperparameterTuner 的 .fit 方法添加自定义参数。

I use the following sequence to optimize the parameters in the script and then apply the best parameter (using also only one training script).我使用以下序列来优化脚本中的参数,然后应用最佳参数(也仅使用一个训练脚本)。 Maybe you apply this to your case.也许您将此应用于您的案例。 You should be able to save intermediate models with joblib.dump in your script:您应该能够在脚本中使用 joblib.dump 保存中间模型:

param_grid = [{'vect__ngram_range': [(1, 1)],
           'vect__stop_words': [stop, None],
           'clf__penalty': ['l1', 'l2'],
           'clf__C': [1.0, 10.0, 100.0]},
          {'vect__ngram_range': [(1, 1)],
           'vect__stop_words': [stop, None],
           'vect__use_idf':[False],
           'vect__norm':[None],
           'clf__penalty': ['l1', 'l2'],
           'clf__C': [1.0, 10.0, 100.0]},
          ]

lr_tfidf = Pipeline([('vect', tfidf),
                 ('clf', LogisticRegression(random_state=0))])

gs_lr_tfidf = GridSearchCV(lr_tfidf, param_grid,
                       scoring='accuracy',
                       cv=5,
                       verbose=1,
                       n_jobs=-1)


gs_lr_tfidf.fit(X_train, y_train)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 AWS SageMaker Pipeline 训练组件中的训练脚本将自定义文件上传到 s3 - Upload custom file to s3 from training script in training component of AWS SageMaker Pipeline AWS sagemaker-container:如何创建 resourceconfig.json 或将其传递给培训框架? - AWS sagemaker-container: How to create or pass the resourceconfig.json to framework for training? AWS Sagemaker - 自定义培训作业不保存 Model output - AWS Sagemaker - Custom Training Job not saving Model output AWS Sagemaker 多项训练作业 - AWS Sagemaker Multiple Training Jobs 如何在 Sagemaker 脚本模式下恢复训练作业? - How can I resume a training job in Sagemaker script mode? AWS Sagemaker:我可以将 sagemaker.workflow.parameters.ParameterString 传递给 SKLearnProcessor - AWS Sagemaker: Can I pass a sagemaker.workflow.parameters.ParameterString to an SKLearnProcessor 使用带有训练脚本的拥抱面估计器和直接在 AWS sagemaker 中使用笔记本有什么区别? - what is the difference between using a hugging face estimator with training script and directly using a notebook in AWS sagemaker? AWS sagemaker 培训工作 (Tensorflow) 在 Epoch 1 停止 - AWS sagemaker training job (Tensorflow) halts at Epoch 1 将配置文件传递给 Sagemaker 训练程序 - Pass a config file to Sagemaker training program amazon sagemaker 自定义代码增量训练 - incremental training on custom code in amazon sagemaker
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM