首页 > 解决方案 > 在 SageMaker 的训练脚本中调用 PyTorch Estimator 超参数

问题描述

在 [PyTorch Estimator for SageMaker][1] 中,它说如下。

hyperparameters (dict) -- 将用于训练的超参数(默认值:无)。超参数可作为 dict[str, str] 访问 SageMaker 上的训练代码。为方便起见,它接受其他类型的键和值,但在训练之前会调用 str() 来转换它们。

estimator = PyTorch(entry_point='test_trainer.py',
                   source_dir = 'code',
                    role = role,
                   framework_version = '1.5.0',
                   py_version = 'py3',
                   instance_count = 1,
                   instance_type = 'ml.g4dn.2xlarge',
                   hyperparameters={"epochs": 1,
                                     "num_labels": 2,
                                     "backend": "gloo"
                         }}

所以,如果我如上所述声明我的估计器并通过我的 test_trainer.py 拟合估计器,我应该能够在我的 test_trainer.py 中访问这些超参数值。但是我应该如何调用这个超参数才能访问这些超参数值?

任何资源将不胜感激。[1]:https ://sagemaker.readthedocs.io/en/stable/frameworks/pytorch/sagemaker.pytorch.html

标签: pytorchamazon-sagemaker

解决方案


SM_HPS='{"batch-size": "256", "learning-rate": "0.0001","communicator": "pure_nccl"}'
Contains a JSON encoded dictionary with the user provided hyperparameters. Example usage:

import os
import json

hyperparameters = json.loads(os.environ['SM_HPS']))
# {"batch-size": 256, "learning-rate": 0.0001, "communicator": "pure_nccl"}

AWS 文档


推荐阅读