Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TF IRIS BYOM is ready. #129

Merged
merged 4 commits into from
Dec 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions under_development/tensorflow_iris_byom/iris_dnn_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import numpy as np
import tensorflow as tf

INPUT_TENSOR_NAME = 'x'

INPUT_TENSOR_NAME = 'inputs'

def estimator_fn(run_config, params):
feature_columns = [tf.feature_column.numeric_column(INPUT_TENSOR_NAME, shape=[4])]
Expand All @@ -12,22 +11,14 @@ def estimator_fn(run_config, params):
n_classes=3,
config=run_config)


def serving_input_fn():
feature_spec = {INPUT_TENSOR_NAME: tf.FixedLenFeature(dtype=tf.float32, shape=[4])}
return tf.estimator.export.build_parsing_serving_input_receiver_fn(feature_spec)()


def train_input_fn(training_dir, params):
"""Returns input function that would feed the model during training"""
return _generate_input_fn(training_dir, 'iris_training.csv')


def eval_input_fn(training_dir, params):
"""Returns input function that would feed the model during evaluation"""
return _generate_input_fn(training_dir, 'iris_test.csv')


def _generate_input_fn(training_dir, training_filename):
training_set = tf.contrib.learn.datasets.base.load_csv_with_header(
filename=os.path.join(training_dir, training_filename),
Expand All @@ -38,4 +29,4 @@ def _generate_input_fn(training_dir, training_filename):
x={INPUT_TENSOR_NAME: np.array(training_set.data)},
y=np.array(training_set.target),
num_epochs=None,
shuffle=True)
shuffle=True)
75 changes: 19 additions & 56 deletions under_development/tensorflow_iris_byom/tensorflow_BYOM_iris.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
"\n",
"This notebook can be compared to [Iris classification example notebook](../tensorflow_iris_dnn_classifier_using_estimators/tensorflow_iris_dnn_classifier_using_estimators.ipynb) in terms of its functionality. We will do the same classification task, but we will train the same network locally in the box from where this notebook is being run. We then setup a real-time hosted endpoint in SageMaker.\n",
"\n",
"Consider the following model definition for IRIS classification. This mdoe uses the ``tensorflow.estimator.DNNClassifier`` which is a pre-defined enstimator module for its model definition. The model definition is the same as the one used in the [Iris classification example notebook](../tensorflow_iris_dnn_classifier_using_estimators/tensorflow_iris_dnn_classifier_using_estimators.ipynb)\n",
"Consider the following model definition for IRIS classification. This mode uses the ``tensorflow.estimator.DNNClassifier`` which is a pre-defined estimator module for its model definition. The model definition is the same as the one used in the [Iris classification example notebook](../tensorflow_iris_dnn_classifier_using_estimators/tensorflow_iris_dnn_classifier_using_estimators.ipynb)\n",
"\n",
"## Prequisites and Preprocessing\n",
"### Permissions and environment variables\n",
"\n",
"Here we set up the linkage and authentication to AWS services. In this notebook we only need the roles used to give learning and hosting access to your data. The Sagemaker SDK will use S3 defualt buckets when needed. Supply the role in the variable below."
"Here we set up the linkage and authentication to AWS services. In this notebook we only need the roles used to give learning and hosting access to your data. The Sagemaker SDK will use S3 defualt buckets when needed. If the ``get_execution_role`` does not return a role with the appropriate permissions, you'll need to specify an IAM role arn that does."
]
},
{
Expand Down Expand Up @@ -68,47 +68,7 @@
"metadata": {},
"outputs": [],
"source": [
"import os\n",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typos in markdown above... "mdoe" instead of "mode". And "enstimator" should be "estimator". Also instead of "Supply the role in the variable below", let's go with "If the get_execution_role() function does not return a role with the appropriate permissions, you'll need to specify an IAM role arn that does."

"import numpy as np\n",
"import tensorflow as tf\n",
"\n",
"INPUT_TENSOR_NAME = 'x'\n",
"\n",
"\n",
"def estimator_fn(run_config, params):\n",
" feature_columns = [tf.feature_column.numeric_column(INPUT_TENSOR_NAME, shape=[4])]\n",
" return tf.estimator.DNNClassifier(feature_columns=feature_columns,\n",
" hidden_units=[10, 20, 10],\n",
" n_classes=3,\n",
" config=run_config)\n",
"\n",
"\n",
"def serving_input_fn():\n",
" feature_spec = {INPUT_TENSOR_NAME: tf.FixedLenFeature(dtype=tf.float32, shape=[4])}\n",
" return tf.estimator.export.build_parsing_serving_input_receiver_fn(feature_spec)()\n",
"\n",
"\n",
"def train_input_fn(training_dir, params):\n",
" \"\"\"Returns input function that would feed the model during training\"\"\"\n",
" return _generate_input_fn(training_dir, 'iris_training.csv')\n",
"\n",
"\n",
"def eval_input_fn(training_dir, params):\n",
" \"\"\"Returns input function that would feed the model during evaluation\"\"\"\n",
" return _generate_input_fn(training_dir, 'iris_test.csv')\n",
"\n",
"\n",
"def _generate_input_fn(training_dir, training_filename):\n",
" training_set = tf.contrib.learn.datasets.base.load_csv_with_header(\n",
" filename=os.path.join(training_dir, training_filename),\n",
" target_dtype=np.int,\n",
" features_dtype=np.float32)\n",
"\n",
" return tf.estimator.inputs.numpy_input_fn(\n",
" x={INPUT_TENSOR_NAME: np.array(training_set.data)},\n",
" y=np.array(training_set.target),\n",
" num_epochs=None,\n",
" shuffle=True)"
"!cat iris_dnn_classifier.py"
]
},
{
Expand All @@ -124,6 +84,7 @@
"metadata": {},
"outputs": [],
"source": [
"from iris_dnn_classifier import estimator_fn\n",
"classifier = estimator_fn(run_config = None, params = None)"
]
},
Expand Down Expand Up @@ -180,6 +141,7 @@
},
"outputs": [],
"source": [
"from iris_dnn_classifier import train_input_fn\n",
"train_func = train_input_fn('.', params = None)"
]
},
Expand Down Expand Up @@ -220,8 +182,11 @@
"metadata": {},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In markdown above:

  • Let's replace "export\Servo" backslashes with forward slashes "export/Servo"

"outputs": [],
"source": [
"from iris_dnn_classifier import serving_input_fn\n",
"\n",
"exported_model = classifier.export_savedmodel(export_dir_base = 'export/Servo/', \n",
" serving_input_receiver_fn = serving_input_fn)\n",
"\n",
"print (exported_model)\n",
"import tarfile\n",
"with tarfile.open('model.tar.gz', mode='w:gz') as archive:\n",
Expand Down Expand Up @@ -278,7 +243,7 @@
"source": [
"### Create endpoint\n",
"\n",
"Now the model is ready to be deployed at a SageMaker endpoint. We can use the ``sagemaker.mxnet.model.TensorFlowModel.deploy`` method to do this. Unless you have created or prefer other instances, we recommend using 1 ``'ml.c4.xlarge'`` instance for this training. These are supplied as arguments. "
"Now the model is ready to be deployed at a SageMaker endpoint. We can use the ``sagemaker.mxnet.model.TensorFlowModel.deploy`` method to do this. Unless you have created or prefer other instances, we recommend using 1 ``'ml.c4.xlarge'`` instance for this example. These are supplied as arguments. "
]
},
{
Expand All @@ -298,7 +263,7 @@
"source": [
"### Validate the endpoint for use\n",
"\n",
"We can now use this endpoint to classify. Run a sample prediction on a sample to ensure that it works. Expect result ``1`` for this particular sample."
"We can now use this endpoint to classify. Run an example prediction on a sample to ensure that it works."
]
},
{
Expand Down Expand Up @@ -335,40 +300,38 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"If you do not want continued use of the endpoint, you can remove it. Remember, open endpoints are charged. If this is a simple test or practice, it is recommended to delete them."
"If you do not want to continue using the endpoint, you can remove it. Remember, open endpoints are charged. If this is a simple test or practice, it is recommended to delete them."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the above:

  • Replace "instance for this training" with "instance for this example." Just to avoid confusion with the model training process.
  • Can we consistently expect 1? I got 2 with a score of 0.5236916542053223. Didn't have time to dig in but I expect it's due to initial randomization.
  • Instead of "Run a sample prediction on a sample..." can we change to "Run and example prediction on a sample..." just to avoid confusion?
  • Change "continied use of" to "to continue using".

"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"sagemaker.Session().delete_endpoint(predictor.endpoint)"
]
}
],
"metadata": {
"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.",
"kernelspec": {
"display_name": "Environment (conda_tensorflow_p36)",
"display_name": "conda_tensorflow_p27",
"language": "python",
"name": "conda_tensorflow_p36"
"name": "conda_tensorflow_p27"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
"pygments_lexer": "ipython2",
"version": "2.7.11"
},
"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."
},
"nbformat": 4,
"nbformat_minor": 2
Expand Down