Skip to content

Commit 7047101

Browse files
eslesar-awsmvsusp
authored andcommitted
Edited the tf script mode notebook (#90)
* edited tf script mode notebook
1 parent a1916a8 commit 7047101

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

examples/script_mode_train_any_tf_script_in_sage_maker.ipynb

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44
"cell_type": "markdown",
55
"metadata": {},
66
"source": [
7-
"# Using the Script Mode to train any TensorFlow script from GitHub in SageMaker\n",
7+
"# Use Script Mode to train any TensorFlow script from GitHub in SageMaker\n",
88
"\n",
9-
"In this tutorial, we show how simple it is to train a TensorFlow script in SageMaker using the new Script Mode Tensorflow Container.\n",
9+
"In this tutorial, you train a TensorFlow script in SageMaker using the new Script Mode Tensorflow Container.\n",
1010
"\n",
11-
"The example we chose is [Multi-layer Recurrent Neural Networks (LSTM, RNN) for character-level language models in Python using Tensorflow](https://github.com/sherjilozair/char-rnn-tensorflow) but this same technique can be used to other scripts or repositories including [TensorFlow Model Zoo](https://github.com/tensorflow/models) and [TensorFlow benchmark scripts](https://github.com/tensorflow/benchmarks/tree/master/scripts/tf_cnn_benchmarks)."
11+
"For this example, you use [Multi-layer Recurrent Neural Networks (LSTM, RNN) for character-level language models in Python using Tensorflow](https://github.com/sherjilozair/char-rnn-tensorflow), but you can use the same technique for other scripts or repositories. For example, [TensorFlow Model Zoo](https://github.com/tensorflow/models) and [TensorFlow benchmark scripts](https://github.com/tensorflow/benchmarks/tree/master/scripts/tf_cnn_benchmarks)."
1212
]
1313
},
1414
{
1515
"cell_type": "markdown",
1616
"metadata": {},
1717
"source": [
18-
"## Setting up the environment\n",
19-
"Let's start by creating a SageMaker session and specifying:\n",
20-
"- The S3 bucket and prefix that you want to use for training and model data. It should be within the same region as the Notebook Instance, training, and hosting.\n",
21-
"- The IAM role allows SageMaker services to access your data. See the documentation [for how to create these](https://docs.aws.amazon.com/sagemaker/latest/dg/sagemaker-roles.html).\n"
18+
"## Set up the environment\n",
19+
"Let's start by creating a SageMaker session and specifying the following:\n",
20+
"- The S3 bucket and prefix to use for training and model data. The bucket should be in the same region as the Notebook Instance, training instance(s), and hosting instance(s). This example uses the default bucket that a SageMaker `Session` creates.\n",
21+
"- The IAM role that allows SageMaker services to access your data. For more information about using IAM roles in SageMaker, see [Amazon SageMaker Roles](https://docs.aws.amazon.com/sagemaker/latest/dg/sagemaker-roles.html).\n"
2222
]
2323
},
2424
{
@@ -40,7 +40,8 @@
4040
"cell_type": "markdown",
4141
"metadata": {},
4242
"source": [
43-
"### Clone the repository"
43+
"### Clone the repository\n",
44+
"Run the following command to clone the repository that contains the example:"
4445
]
4546
},
4647
{
@@ -73,7 +74,8 @@
7374
"cell_type": "markdown",
7475
"metadata": {},
7576
"source": [
76-
"### Getting the data"
77+
"### Get the data\n",
78+
"For training data, use plain text versions of Sherlock Holmes stories."
7779
]
7880
},
7981
{
@@ -90,7 +92,7 @@
9092
"cell_type": "markdown",
9193
"metadata": {},
9294
"source": [
93-
"## Testing locally"
95+
"## Test locally"
9496
]
9597
},
9698
{
@@ -130,7 +132,7 @@
130132
"metadata": {},
131133
"source": [
132134
"\n",
133-
"We can use [Local Mode](https://github.com/aws/sagemaker-python-sdk#local-mode) to simulate SageMaker locally before submit training:"
135+
"Use [Local Mode](https://github.com/aws/sagemaker-python-sdk#local-mode) to run the script locally in the notebook instance before you run a SageMaker training job:"
134136
]
135137
},
136138
{
@@ -147,7 +149,7 @@
147149
"\n",
148150
"estimator = ScriptModeTensorFlow(entry_point='train.py',\n",
149151
" source_dir='char-rnn-tensorflow',\n",
150-
" train_instance_type='local', \n",
152+
" train_instance_type='local', # Run in local mode\n",
151153
" train_instance_count=1,\n",
152154
" hyperparameters=hyperparameters,\n",
153155
" role=role)\n",
@@ -161,14 +163,14 @@
161163
"source": [
162164
"## How Script Mode executes the script in the container\n",
163165
"\n",
164-
"The cell above downloads a Python 3 CPU container locally and simulates SageMaker training. When training starts, script mode installs the user script as a Python module. The module name matches the script name. In the case above, **train.py** is transformed into a Python module named **train**.\n",
166+
"The above cell downloads a Python 3 CPU container locally and simulates a SageMaker training job. When training starts, script mode installs the user script as a Python module. The module name matches the script name. In this case, **train.py** is transformed into a Python module named **train**.\n",
165167
"\n",
166-
"After that, the Python interpreter executes the user module, passing **hyperparameters** as script arguments. The example above will be executed as follow:\n",
168+
"After that, the Python interpreter executes the user module, passing **hyperparameters** as script arguments. The example above is executed as follows:\n",
167169
"```bash\n",
168170
"python -m train --num-epochs 1 --data-dir /opt/ml/input/data/training --save-dir /opt/ml/model\n",
169171
"```\n",
170172
"\n",
171-
"A user provide script consumes the hyperparameters using any argument parsing library, [in the example above](https://github.com/sherjilozair/char-rnn-tensorflow/blob/master/train.py#L11):\n",
173+
"The **train** module consumes the hyperparameters using any argument parsing library. [The example we're using](https://github.com/sherjilozair/char-rnn-tensorflow/blob/master/train.py#L11) uses the Python [argparse](https://docs.python.org/3/library/argparse.html) library:\n",
172174
"\n",
173175
"```python\n",
174176
"parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)\n",
@@ -183,16 +185,16 @@
183185
"\n",
184186
"Let's explain the values of **--data_dir** and **--save-dir**:\n",
185187
"\n",
186-
"- **/opt/ml/input/data/training** is the directory inside the container where the training data is downloaded. The data was downloaded in this folder because **training** is the channel name defined in ```estimator.fit({'training': inputs})```. See [training data](https://docs.aws.amazon.com/sagemaker/latest/dg/your-algorithms-training-algo.html#your-algorithms-training-algo-running-container-trainingdata) for more information. \n",
188+
"- **/opt/ml/input/data/training** is the directory inside the container where the training data is downloaded. The data is downloaded to this folder because **training** is the channel name defined in ```estimator.fit({'training': inputs})```. See [training data](https://docs.aws.amazon.com/sagemaker/latest/dg/your-algorithms-training-algo.html#your-algorithms-training-algo-running-container-trainingdata) for more information. \n",
187189
"\n",
188-
"- **/opt/ml/model** use this directory to save models, checkpoints or any other data. Any data saved in this folder is saved in the S3 bucket defined for training. See [model data](https://docs.aws.amazon.com/sagemaker/latest/dg/your-algorithms-training-algo.html#your-algorithms-training-algo-envvariables) for more information.\n",
190+
"- **/opt/ml/model** use this directory to save models, checkpoints, or any other data. Any data saved in this folder is saved in the S3 bucket defined for training. See [model data](https://docs.aws.amazon.com/sagemaker/latest/dg/your-algorithms-training-algo.html#your-algorithms-training-algo-envvariables) for more information.\n",
189191
"\n",
190192
"### Reading additional information from the container\n",
191193
"\n",
192-
"Very often, a user script needs additional information from the container that is not available in ```hyperparameters```.\n",
193-
"SageMaker Containers writes this information as **environment variables** that are available inside the script.\n",
194+
"Often, a user script needs additional information from the container that is not available in ```hyperparameters```.\n",
195+
"SageMaker containers write this information as **environment variables** that are available inside the script.\n",
194196
"\n",
195-
"For example, the example above can read information about the **training** channel provided in the training job request:\n",
197+
"For example, the example above can read information about the **training** channel provided in the training job request by adding the environment variable `SM_CHANNEL_TRAINING` as the default value for the `--data_dir` argument:\n",
196198
"\n",
197199
"```python\n",
198200
"if __name__ == '__main__':\n",
@@ -201,7 +203,7 @@
201203
" parser.add_argument('--data_dir', type=str, default=os.environ['SM_CHANNEL_TRAINING'])\n",
202204
"```\n",
203205
"\n",
204-
"Script Mode displays the list of the environment variables available in the training logs. You can find the [entire list here](https://github.com/aws/sagemaker-containers/blob/master/README.md#environment-variables-full-specification)."
206+
"Script mode displays the list of available environment variables in the training logs. You can find the [entire list here](https://github.com/aws/sagemaker-containers/blob/master/README.md#environment-variables-full-specification)."
205207
]
206208
},
207209
{
@@ -215,7 +217,7 @@
215217
"cell_type": "markdown",
216218
"metadata": {},
217219
"source": [
218-
"We need to upload the dataset to an S3 bucket so SageMaker can access the data during training.\n"
220+
"After you test the training job locally, upload the dataset to an S3 bucket so SageMaker can access the data during training.\n"
219221
]
220222
},
221223
{
@@ -231,7 +233,7 @@
231233
"cell_type": "markdown",
232234
"metadata": {},
233235
"source": [
234-
"You can change the estimator argument **train_instance_type** to any SageMaker ml instance available for training. For example:"
236+
"To train in SageMaker, change the estimator argument **train_instance_type** to any SageMaker ml instance available for training. For example:"
235237
]
236238
},
237239
{
@@ -268,7 +270,7 @@
268270
"cell_type": "markdown",
269271
"metadata": {},
270272
"source": [
271-
"Script Mode will install your source_dir in the container as a [Python package](https://github.com/aws/sagemaker-containers/blob/master/src/sagemaker_containers/_modules.py#L100). You can include a [requirements.txt file in the root folder of your source_dir to install any pip dependencies](https://github.com/aws/sagemaker-containers/blob/master/src/sagemaker_containers/_modules.py#L111). You can, for example, install the lastest version of tensorflow in the container:\n",
273+
"Script Mode installs the contents of your `source_dir` folder in the container as a [Python package](https://github.com/aws/sagemaker-containers/blob/master/src/sagemaker_containers/_modules.py#L100). You can include a [requirements.txt file in the root folder of your source_dir to install any pip dependencies](https://github.com/aws/sagemaker-containers/blob/master/src/sagemaker_containers/_modules.py#L111). You can, for example, install the lastest version of TensorFlow in the container:\n",
272274
"\n",
273275
"content of requirements.txt\n",
274276
"```\n",
@@ -281,7 +283,7 @@
281283
"metadata": {},
282284
"source": [
283285
"# Installing apt-get packages and other dependencies\n",
284-
"You can define a setup.py file in your source_dir to install other dependencies. The example below will install [TensorFlow for C](https://www.tensorflow.org/install/lang_c) in the container."
286+
"You can define a `setup.py` file in your `source_dir` folder to install other dependencies. The example below installs [TensorFlow for C](https://www.tensorflow.org/install/lang_c) in the container."
285287
]
286288
},
287289
{
@@ -385,21 +387,21 @@
385387
],
386388
"metadata": {
387389
"kernelspec": {
388-
"display_name": "Python 2",
390+
"display_name": "Python 3",
389391
"language": "python",
390-
"name": "python2"
392+
"name": "python3"
391393
},
392394
"language_info": {
393395
"codemirror_mode": {
394396
"name": "ipython",
395-
"version": 2
397+
"version": 3
396398
},
397399
"file_extension": ".py",
398400
"mimetype": "text/x-python",
399401
"name": "python",
400402
"nbconvert_exporter": "python",
401-
"pygments_lexer": "ipython2",
402-
"version": "2.7.15"
403+
"pygments_lexer": "ipython3",
404+
"version": "3.6.5"
403405
}
404406
},
405407
"nbformat": 4,

0 commit comments

Comments
 (0)