diff --git a/under_development/tensorflow_iris_byom/iris_dnn_classifier.py b/under_development/tensorflow_iris_byom/iris_dnn_classifier.py index ec8440dbc5..b2b17ecf40 100644 --- a/under_development/tensorflow_iris_byom/iris_dnn_classifier.py +++ b/under_development/tensorflow_iris_byom/iris_dnn_classifier.py @@ -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])] @@ -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), @@ -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) \ No newline at end of file + shuffle=True) diff --git a/under_development/tensorflow_iris_byom/tensorflow_BYOM_iris.ipynb b/under_development/tensorflow_iris_byom/tensorflow_BYOM_iris.ipynb index 33c57a9ef7..eb917129df 100644 --- a/under_development/tensorflow_iris_byom/tensorflow_BYOM_iris.ipynb +++ b/under_development/tensorflow_iris_byom/tensorflow_BYOM_iris.ipynb @@ -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." ] }, { @@ -68,47 +68,7 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", - "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" ] }, { @@ -124,6 +84,7 @@ "metadata": {}, "outputs": [], "source": [ + "from iris_dnn_classifier import estimator_fn\n", "classifier = estimator_fn(run_config = None, params = None)" ] }, @@ -180,6 +141,7 @@ }, "outputs": [], "source": [ + "from iris_dnn_classifier import train_input_fn\n", "train_func = train_input_fn('.', params = None)" ] }, @@ -220,8 +182,11 @@ "metadata": {}, "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", @@ -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. " ] }, { @@ -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." ] }, { @@ -335,15 +300,13 @@ "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": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "sagemaker.Session().delete_endpoint(predictor.endpoint)" @@ -351,24 +314,24 @@ } ], "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