@@ -19,49 +19,56 @@ Training RL models using ``RLEstimator`` is a two-step process:
19
19
You should prepare your script in a separate source file than the notebook, terminal session, or source file you're
20
20
using to submit the script to SageMaker via an ``RLEstimator ``. This will be discussed in further detail below.
21
21
22
- Suppose that you already have a training script called ``coach-train.py ``.
22
+ Suppose that you already have a training script called ``coach-train.py `` and have an RL image in your ECR registry
23
+ called ``123123123123.dkr.ecr.us-west-2.amazonaws.com/your-rl-registry:your-cool-image-tag `` in ``us-west-2 `` region.
23
24
You can then create an ``RLEstimator `` with keyword arguments to point to this script and define how SageMaker runs it:
24
25
25
26
.. code :: python
26
27
27
28
from sagemaker.rl import RLEstimator, RLToolkit, RLFramework
28
29
29
- rl_estimator = RLEstimator(entry_point = ' coach-train.py' ,
30
- toolkit = RLToolkit.COACH ,
31
- toolkit_version = ' 0.11.1' ,
32
- framework = RLFramework.TENSORFLOW ,
33
- role = ' SageMakerRole' ,
34
- instance_type = ' ml.p3.2xlarge' ,
35
- instance_count = 1 )
30
+ # Train my estimator
31
+ rl_estimator = RLEstimator(
32
+ entry_point = ' coach-train.py' ,
33
+ image_uri = ' 123123123123.dkr.ecr.us-west-2.amazonaws.com/your-rl-registry:your-cool-image-tag' ,
34
+ role = ' SageMakerRole' ,
35
+ instance_type = ' ml.c4.2xlarge' ,
36
+ instance_count = 1
37
+ )
38
+
39
+
40
+ .. tip ::
41
+ Refer to `SageMaker RL Docker Containers <#sagemaker-rl-docker-containers >`_ for the more information on how to
42
+ build your custom RL image.
36
43
37
44
After that, you simply tell the estimator to start a training job:
38
45
39
46
.. code :: python
40
47
41
48
rl_estimator.fit()
42
49
50
+
43
51
In the following sections, we'll discuss how to prepare a training script for execution on SageMaker
44
52
and how to run that script on SageMaker using ``RLEstimator ``.
45
53
46
54
47
55
Preparing the RL Training Script
48
56
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
49
57
50
- Your RL training script must be a Python 3.5 compatible source file from MXNet framework or Python 3.6 for TensorFlow.
51
-
52
58
The training script is very similar to a training script you might run outside of SageMaker, but you
53
59
can access useful properties about the training environment through various environment variables, such as
54
60
55
- * ``SM_MODEL_DIR ``: A string representing the path to the directory to write model artifacts to.
56
- These artifacts are uploaded to S3 for model hosting.
57
- * ``SM_NUM_GPUS ``: An integer representing the number of GPUs available to the host.
58
- * ``SM_OUTPUT_DATA_DIR ``: A string representing the filesystem path to write output artifacts to. Output artifacts may
59
- include checkpoints, graphs, and other files to save, not including model artifacts. These artifacts are compressed
60
- and uploaded to S3 to the same S3 prefix as the model artifacts.
61
+ * ``SM_MODEL_DIR ``: A string representing the path to the directory to write model artifacts to.
62
+ These artifacts are uploaded to S3 for model hosting.
63
+ * ``SM_NUM_GPUS ``: An integer representing the number of GPUs available to the host.
64
+ * ``SM_OUTPUT_DATA_DIR ``: A string representing the filesystem path to write output artifacts to. Output artifacts may
65
+ include checkpoints, graphs, and other files to save, not including model artifacts. These artifacts are compressed
66
+ and uploaded to S3 to the same S3 prefix as the model artifacts.
61
67
62
68
For the exhaustive list of available environment variables, see the
63
69
`SageMaker Containers documentation <https://github.com/aws/sagemaker-containers#list-of-provided-environment-variables-by-sagemaker-containers >`__.
64
70
71
+ Note that your RL training script must have the Python version compatible with your custom RL Docker image.
65
72
66
73
RL Estimators
67
74
-------------
@@ -81,28 +88,16 @@ these in the constructor, either positionally or as keyword arguments.
81
88
endpoints use this role to access training data and model artifacts.
82
89
After the endpoint is created, the inference code might use the IAM
83
90
role, if accessing AWS resource.
84
- - ``instance_count `` Number of Amazon EC2 instances to use for
85
- training.
91
+ - ``instance_count `` Number of Amazon EC2 instances to use for training.
86
92
- ``instance_type `` Type of EC2 instance to use for training, for
87
93
example, 'ml.m4.xlarge'.
88
94
89
- You must as well include either:
90
-
91
- - ``toolkit `` RL toolkit (Ray RLlib or Coach) you want to use for executing your model training code.
92
-
93
- - ``toolkit_version `` RL toolkit version you want to be use for executing your model training code.
94
-
95
- - ``framework `` Framework (MXNet or TensorFlow) you want to be used as
96
- a toolkit backed for reinforcement learning training.
97
-
98
- or provide:
95
+ You must also provide:
99
96
100
- - ``image_uri `` An alternative Docker image to use for training and
101
- serving. If specified, the estimator will use this image for training and
102
- hosting, instead of selecting the appropriate SageMaker official image based on
103
- framework_version and py_version. Refer to: `SageMaker RL Docker Containers
104
- <#sagemaker-rl-docker-containers> `_ for details on what the Official images support
105
- and where to find the source code to build your custom image.
97
+ - ``image_uri `` An alternative Docker image to use for training and serving.
98
+ If specified, the estimator will use this image for training and
99
+ hosting. Refer to: `SageMaker RL Docker Containers <#sagemaker-rl-docker-containers >`_
100
+ for the source code to build your custom RL image.
106
101
107
102
108
103
Optional arguments
@@ -140,10 +135,8 @@ Deploying RL Models
140
135
After an RL Estimator has been fit, you can host the newly created model in SageMaker.
141
136
142
137
After calling ``fit ``, you can call ``deploy `` on an ``RLEstimator `` Estimator to create a SageMaker Endpoint.
143
- The Endpoint runs one of the SageMaker-provided model server based on the ``framework `` parameter
144
- specified in the ``RLEstimator `` constructor and hosts the model produced by your training script,
145
- which was run when you called ``fit ``. This was the model you saved to ``model_dir ``.
146
- In case if ``image_uri `` was specified it would use provided image for the deployment.
138
+ The Endpoint runs provided image specified with ``image_uri `` and hosts the model produced by your
139
+ training script, which was run when you called ``fit ``. This is the model you saved to ``model_dir ``.
147
140
148
141
``deploy `` returns a ``sagemaker.mxnet.MXNetPredictor `` for MXNet or
149
142
``sagemaker.tensorflow.TensorFlowPredictor `` for TensorFlow.
@@ -153,19 +146,22 @@ In case if ``image_uri`` was specified it would use provided image for the deplo
153
146
.. code :: python
154
147
155
148
# Train my estimator
156
- rl_estimator = RLEstimator(entry_point = ' coach-train.py' ,
157
- toolkit = RLToolkit.COACH ,
158
- toolkit_version = ' 0.11.0' ,
159
- framework = RLFramework.MXNET ,
160
- role = ' SageMakerRole' ,
161
- instance_type = ' ml.c4.2xlarge' ,
162
- instance_count = 1 )
149
+ region = ' us-west-2' # the AWS region of your training job
150
+ rl_estimator = RLEstimator(
151
+ entry_point = ' coach-train.py' ,
152
+ image_uri = f ' 123123123123.dkr.ecr. { region} .amazonaws.com/your-rl-registry:your-cool-image-tag ' ,
153
+ role = ' SageMakerRole' ,
154
+ instance_type = ' ml.c4.2xlarge' ,
155
+ instance_count = 1
156
+ )
163
157
164
158
rl_estimator.fit()
165
159
166
160
# Deploy my estimator to a SageMaker Endpoint and get a MXNetPredictor
167
- predictor = rl_estimator.deploy(instance_type = ' ml.m4.xlarge' ,
168
- initial_instance_count = 1 )
161
+ predictor = rl_estimator.deploy(
162
+ instance_type = ' ml.m4.xlarge' ,
163
+ initial_instance_count = 1
164
+ )
169
165
170
166
response = predictor.predict(data)
171
167
@@ -193,10 +189,8 @@ attach will block and display log messages from the training job, until the trai
193
189
194
190
The ``attach `` method accepts the following arguments:
195
191
196
- - ``training_job_name: `` The name of the training job to attach
197
- to.
198
- - ``sagemaker_session: `` The Session used
199
- to interact with SageMaker
192
+ - ``training_job_name: `` The name of the training job to attach to.
193
+ - ``sagemaker_session: `` The Session used to interact with SageMaker
200
194
201
195
RL Training Examples
202
196
--------------------
@@ -212,4 +206,6 @@ These are also available in SageMaker Notebook Instance hosted Jupyter notebooks
212
206
SageMaker RL Docker Containers
213
207
------------------------------
214
208
215
- For more about the Docker images themselves, visit `the SageMaker RL containers repository <https://github.com/aws/sagemaker-rl-container >`_.
209
+ For more information about how build your own RL image and use script mode with your image, see
210
+ `Building your image section on SageMaker RL containers repository <https://github.com/aws/sagemaker-rl-container?tab=readme-ov-file#building-your-image/ >`_
211
+ and `Bring your own model with Amazon SageMaker script mode <https://aws.amazon.com/blogs/machine-learning/bring-your-own-model-with-amazon-sagemaker-script-mode/ >`_.
0 commit comments