Skip to content

Commit 2bf2e36

Browse files
authored
Merge pull request #52 from awslabs/mvs-notebook-love
Updating notebooks
2 parents bb9e844 + 1d3a97e commit 2bf2e36

File tree

12 files changed

+261
-212
lines changed

12 files changed

+261
-212
lines changed

sagemaker-python-sdk/1P_kmeans_highlevel/kmeans_mnist.ipynb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@
5555
},
5656
"outputs": [],
5757
"source": [
58-
"role='<your SageMaker execution role here>'\n",
58+
"from sagemaker import get_execution_role\n",
59+
"\n",
60+
"role = get_execution_role()\n",
5961
"bucket='<bucket-name>'"
6062
]
6163
},

sagemaker-python-sdk/1P_kmeans_lowlevel/kmeans_mnist_lowlevel.ipynb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@
5555
},
5656
"outputs": [],
5757
"source": [
58-
"role='<your SageMaker execution role here>'\n",
58+
"from sagemaker import get_execution_role\n",
59+
"\n",
60+
"role = get_execution_role()\n",
5961
"bucket='<bucket-name>'"
6062
]
6163
},

sagemaker-python-sdk/mxnet_gluon_cifar10/cifar10.ipynb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@
2525
"import sagemaker\n",
2626
"from sagemaker.mxnet import MXNet\n",
2727
"from mxnet import gluon\n",
28+
"from sagemaker import get_execution_role\n",
2829
"\n",
2930
"sagemaker_session = sagemaker.Session()\n",
3031
"\n",
31-
"# Replace with a role that gives SageMaker access to S3 and CloudWatch\n",
32-
"# see 1-Creating_a_role_allowing_SageMaker_to_access_S3_CloudWatch_ECR.ipynb\n",
33-
"role='SageMakerRole'"
32+
"role = get_execution_role()"
3433
]
3534
},
3635
{
@@ -238,11 +237,10 @@
238237
}
239238
],
240239
"metadata": {
241-
"notice": "Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the \"License\"). You may not use this file except in compliance with the License. A copy of the License is located at http://aws.amazon.com/apache2.0/ or in the \"license\" file accompanying this file. This file is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.",
242240
"kernelspec": {
243-
"display_name": "Python [conda env:mxnet_p27]",
241+
"display_name": "Environment (conda_mxnet_p27)",
244242
"language": "python",
245-
"name": "conda-env-mxnet_p27-py"
243+
"name": "conda_mxnet_p27"
246244
},
247245
"language_info": {
248246
"codemirror_mode": {
@@ -254,8 +252,9 @@
254252
"name": "python",
255253
"nbconvert_exporter": "python",
256254
"pygments_lexer": "ipython2",
257-
"version": "2.7.13"
258-
}
255+
"version": "2.7.11"
256+
},
257+
"notice": "Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the \"License\"). You may not use this file except in compliance with the License. A copy of the License is located at http://aws.amazon.com/apache2.0/ or in the \"license\" file accompanying this file. This file is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License."
259258
},
260259
"nbformat": 4,
261260
"nbformat_minor": 2

sagemaker-python-sdk/mxnet_gluon_mnist/mnist_with_gluon.ipynb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
"import sagemaker\n",
2222
"from sagemaker.mxnet import MXNet\n",
2323
"from mxnet import gluon\n",
24+
"from sagemaker import get_execution_role\n",
2425
"\n",
2526
"sagemaker_session = sagemaker.Session()\n",
2627
"\n",
27-
"# Replace with a role that gives SageMaker access to S3 and CloudWatch\n",
28-
"role='SageMakerRole'"
28+
"role = get_execution_role()"
2929
]
3030
},
3131
{
@@ -204,11 +204,10 @@
204204
}
205205
],
206206
"metadata": {
207-
"notice": "Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the \"License\"). You may not use this file except in compliance with the License. A copy of the License is located at http://aws.amazon.com/apache2.0/ or in the \"license\" file accompanying this file. This file is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.",
208207
"kernelspec": {
209-
"display_name": "Python [conda env:mxnet_p27]",
208+
"display_name": "Environment (conda_mxnet_p27)",
210209
"language": "python",
211-
"name": "conda-env-mxnet_p27-py"
210+
"name": "conda_mxnet_p27"
212211
},
213212
"language_info": {
214213
"codemirror_mode": {
@@ -220,8 +219,9 @@
220219
"name": "python",
221220
"nbconvert_exporter": "python",
222221
"pygments_lexer": "ipython2",
223-
"version": "2.7.13"
224-
}
222+
"version": "2.7.11"
223+
},
224+
"notice": "Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the \"License\"). You may not use this file except in compliance with the License. A copy of the License is located at http://aws.amazon.com/apache2.0/ or in the \"license\" file accompanying this file. This file is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License."
225225
},
226226
"nbformat": 4,
227227
"nbformat_minor": 2

sagemaker-python-sdk/mxnet_mnist/mxnet_mnist.ipynb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
},
1919
"outputs": [],
2020
"source": [
21+
"from sagemaker import get_execution_role\n",
22+
"\n",
2123
"#Bucket location to save your custom code in tar.gz format.\n",
2224
"custom_code_upload_location = 's3://<bucket-name>/customcode/mxnet_mnist'\n",
2325
"#Bucket location where results of model training are saved.\n",
2426
"model_artifacts_location = 's3://<bucket-name>/artifacts'\n",
25-
"role='<your SageMaker execution role here>'"
27+
"\n",
28+
"role = get_execution_role()"
2629
]
2730
},
2831
{

sagemaker-python-sdk/tensorflow_abalone_age_predictor_using_keras/abalone.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import numpy as np
22
import os
33
import tensorflow as tf
4-
from tensorflow.contrib.keras.python.keras.layers import Dense
54
from tensorflow.python.estimator.export.export import build_raw_serving_input_receiver_fn
65
from tensorflow.python.estimator.export.export_output import PredictOutput
76

@@ -22,9 +21,9 @@ def model_fn(features, labels, mode, params):
2221

2322
# 1. Configure the model via Keras functional api
2423

25-
first_hidden_layer = Dense(10, activation='relu', name='first-layer')(features[INPUT_TENSOR_NAME])
26-
second_hidden_layer = Dense(10, activation='relu')(first_hidden_layer)
27-
output_layer = Dense(1, activation='linear')(second_hidden_layer)
24+
first_hidden_layer = tf.keras.layers.Dense(10, activation='relu', name='first-layer')(features[INPUT_TENSOR_NAME])
25+
second_hidden_layer = tf.keras.layers.Dense(10, activation='relu')(first_hidden_layer)
26+
output_layer = tf.keras.layers.Dense(1, activation='linear')(second_hidden_layer)
2827

2928
predictions = tf.reshape(output_layer, [-1])
3029

sagemaker-python-sdk/tensorflow_abalone_age_predictor_using_keras/tensorflow_abalone_age_predictor_using_keras.ipynb

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
"source": [
77
"# Creating Estimators in tf.estimator with Keras\n",
88
"\n",
9-
"If you are getting started using SageMaker, check [iris-dnn-classifier-using-estimators/tutorial.ipynb](iris-dnn-classifier-using-estimators/tutorial.ipynb) first.\n",
10-
"\n",
11-
"This tutorial covers how to create your own `Estimator` using the building\n",
12-
"blocks provided in `tf.estimator`, which will predict the ages of\n",
9+
"This tutorial covers how to create your own training script using the building\n",
10+
"blocks provided in `tf.keras`, which will predict the ages of\n",
1311
"[abalones](https://en.wikipedia.org/wiki/Abalone) based on their physical\n",
1412
"measurements. You'll learn how to do the following:\n",
1513
"\n",
@@ -54,7 +52,7 @@
5452
"cell_type": "markdown",
5553
"metadata": {},
5654
"source": [
57-
"## Let's start by setting up the environment."
55+
"### Set up the environment"
5856
]
5957
},
6058
{
@@ -67,18 +65,18 @@
6765
"source": [
6866
"import os\n",
6967
"import sagemaker\n",
68+
"from sagemaker import get_execution_role\n",
7069
"\n",
7170
"sagemaker_session = sagemaker.Session()\n",
7271
"\n",
73-
"# Replace with a role (either name or full arn) that gives SageMaker access to S3 and cloudwatch\n",
74-
"role='SageMakerRole'"
72+
"role = get_execution_role()"
7573
]
7674
},
7775
{
7876
"cell_type": "markdown",
7977
"metadata": {},
8078
"source": [
81-
"## Uploading the data"
79+
"### Upload the data to a S3 bucket"
8280
]
8381
},
8482
{
@@ -96,7 +94,15 @@
9694
"cell_type": "markdown",
9795
"metadata": {},
9896
"source": [
99-
"# Complete source code"
97+
"**sagemaker_session.upload_data** will upload the abalone dataset from your machine to a bucket named **sagemaker-{your aws account number}**, if you don't have this bucket yet, sagemaker_session will create it for you."
98+
]
99+
},
100+
{
101+
"cell_type": "markdown",
102+
"metadata": {},
103+
"source": [
104+
"## Complete source code\n",
105+
"Here is the full code for the network model:"
100106
]
101107
},
102108
{
@@ -337,11 +343,13 @@
337343
" # Calculate loss using mean squared error\n",
338344
" loss = tf.losses.mean_squared_error(labels, predictions)\n",
339345
" ...\n",
340-
"```\n",
341-
"\n",
342-
"See the [tf.losses$API guide](https://www.tensorflow.org/api_docs/python/tf/losses) for a\n",
343-
"full list of loss functions and more details on supported arguments and usage.\n",
344-
"\n",
346+
"```"
347+
]
348+
},
349+
{
350+
"cell_type": "markdown",
351+
"metadata": {},
352+
"source": [
345353
"Supplementary metrics for evaluation can be added to an `eval_metric_ops` dict.\n",
346354
"The following code defines an `rmse` metric, which calculates the root mean\n",
347355
"squared error for the model predictions. Note that the `labels` tensor is cast\n",
@@ -375,9 +383,6 @@
375383
" loss=loss, global_step=tf.train.get_global_step())\n",
376384
"```\n",
377385
"\n",
378-
"For a full list of optimizers, and other details, see the\n",
379-
"@{$python/train#optimizers$API guide}.\n",
380-
"\n",
381386
"### The complete abalone `model_fn`\n",
382387
"\n",
383388
"Here's the final, complete `model_fn` for the abalone age predictor. The\n",
@@ -435,8 +440,7 @@
435440
"cell_type": "markdown",
436441
"metadata": {},
437442
"source": [
438-
"# Submitting script for training in SageMaker\n",
439-
"\n",
443+
"# Submitting script for training\n",
440444
"\n",
441445
"We can use the SDK to run our local training script on SageMaker infrastructure.\n",
442446
"\n",
@@ -456,10 +460,12 @@
456460
"from sagemaker.tensorflow import TensorFlow\n",
457461
"\n",
458462
"abalone_estimator = TensorFlow(entry_point='abalone.py',\n",
459-
" role=role,\n",
460-
" hyperparameters={'training_steps': 100, 'learning_rate': 0.001},\n",
461-
" train_instance_count=1,\n",
462-
" train_instance_type='ml.c4.xlarge')\n",
463+
" role=role,\n",
464+
" training_steps= 100, \n",
465+
" evaluation_steps= 100,\n",
466+
" hyperparameters={'learning_rate': 0.001},\n",
467+
" train_instance_count=1,\n",
468+
" train_instance_type='ml.c4.xlarge')\n",
463469
"\n",
464470
"abalone_estimator.fit(inputs)"
465471
]
@@ -470,11 +476,11 @@
470476
"source": [
471477
"`estimator.fit` will deploy a script in a container for training and returs the SageMaker model name using the following arguments:\n",
472478
"\n",
473-
"* `framework=tensorflow`. Tells submit_training that it is a tensorflow container.\n",
474-
"* `script=\"abalone.py\"`. The relative path to the script that will be deployed to the container.\n",
475-
"* `data=\"s3://bucket_name/abalone-estimator-training\"`. The S3 location of the bucket that we uploaded earlier.\n",
476-
"* `role=\"maeve-pullrole\"`. AWS role that gives your account access to SageMaker training and hosting\n",
477-
"* `hyperparameters={'training_steps' : 100}`. Model and training hyperparameters. \n",
479+
"* **`entry_point=\"abalone.py\"`** The path to the script that will be deployed to the container.\n",
480+
"* **`training_steps=100`** The number of training steps of the training job.\n",
481+
"* **`evaluation_steps=100`** The number of evaluation steps of the training job.\n",
482+
"* **`role`**. AWS role that gives your account access to SageMaker training and hosting\n",
483+
"* **`hyperparameters={'learning_rate' : 0.001}`**. Training hyperparameters. \n",
478484
"\n",
479485
"Running the code block above will do the following actions:\n",
480486
"* deploy your script in a container with tensorflow installed\n",
@@ -501,8 +507,7 @@
501507
},
502508
"outputs": [],
503509
"source": [
504-
"abalone_predictor = abalone_estimator.deploy(initial_instance_count=1,\n",
505-
" instance_type='ml.c4.xlarge')"
510+
"abalone_predictor = abalone_estimator.deploy(initial_instance_count=1, instance_type='ml.c4.xlarge')"
506511
]
507512
},
508513
{

0 commit comments

Comments
 (0)