diff --git a/README.rst b/README.rst index 1bb4d00679..e654b44e70 100644 --- a/README.rst +++ b/README.rst @@ -21,6 +21,7 @@ Table of Contents 4. `TensorFlow SageMaker Estimators <#tensorflow-sagemaker-estimators>`__ 5. `AWS SageMaker Estimators <#aws-sagemaker-estimators>`__ 6. `BYO Docker Containers with SageMaker Estimators <#byo-docker-containers-with-sagemaker-estimators>`__ +7. `BYO Model <#byo-model>`__ Getting SageMaker Python SDK @@ -93,7 +94,7 @@ SageMaker Python SDK Overview SageMaker Python SDK provides several high-level abstractions for working with Amazon SageMaker. These are: -- **Estimators**: Encapsulate training on SageMaker. Can be ``fit()`` to run training, then the resulting model ``deploy()`` ed to a SageMaker Endpoint. +- **Estimators**: Encapsulate training on SageMaker. Can be ``fit()`` to run training, then the resulting model ``deploy()`` ed to a SageMaker Endpoint. - **Models**: Encapsulate built ML models. Can be ``deploy()`` ed to a SageMaker Endpoint. - **Predictors**: Provide real-time inference and transformation using Python data-types against a SageMaker Endpoint. - **Session**: Provides a collection of convience methods for working with SageMaker resources. @@ -177,7 +178,7 @@ You don't have to use all the arguments, arguments you don't care about can be i pass **Note: Writing a training script that imports correctly** -When SageMaker runs your training script, it imports it as a Python module and then invokes ``train`` on the imported module. Consequently, you should not include any statements that won't execute successfully in SageMaker when your module is imported. For example, don't attempt to open any local files in top-level statements in your training script. +When SageMaker runs your training script, it imports it as a Python module and then invokes ``train`` on the imported module. Consequently, you should not include any statements that won't execute successfully in SageMaker when your module is imported. For example, don't attempt to open any local files in top-level statements in your training script. If you want to run your training script locally via the Python interpreter, look at using a ``___name__ == '__main__'`` guard, discussed in more detail here: https://stackoverflow.com/questions/419163/what-does-if-name-main-do . @@ -410,7 +411,7 @@ The MXNet Endpoint you create with ``deploy`` runs a SageMaker MXNet model serve You can configure two components of the SageMaker MXNet model server: Model loading and model serving. Model loading is the process of deserializing your saved model back into an MXNet model. Serving is the process of translating InvokeEndpoint requests to inference calls on the loaded model. -As with MXNet training, you configure the MXNet model server by defining functions in the Python source file you passed to the MXNet constructor. +As with MXNet training, you configure the MXNet model server by defining functions in the Python source file you passed to the MXNet constructor. Model loading ^^^^^^^^^^^^^ @@ -697,19 +698,19 @@ The Docker images extend Ubuntu 16.04. TensorFlow SageMaker Estimators ------------------------------- -TensorFlow SageMaker Estimators allow you to run your own TensorFlow -training algorithms on SageMaker Learner, and to host your own TensorFlow +TensorFlow SageMaker Estimators allow you to run your own TensorFlow +training algorithms on SageMaker Learner, and to host your own TensorFlow models on SageMaker Hosting. Training with TensorFlow ~~~~~~~~~~~~~~~~~~~~~~~~ -Training TensorFlow models using a ``sagemaker.tensorflow.TensorFlow`` +Training TensorFlow models using a ``sagemaker.tensorflow.TensorFlow`` is a two-step process. -First, you prepare your training script, then second, you run it on +First, you prepare your training script, then second, you run it on SageMaker Learner via the ``sagemaker.tensorflow.TensorFlow`` estimator. -Suppose that you already have a TensorFlow training script called +Suppose that you already have a TensorFlow training script called ``tf-train.py``. You can train this script in SageMaker Learner as follows: @@ -727,7 +728,7 @@ constructor keyword arguments define how SageMaker runs your training script and are discussed, in detail, in a later section. In the following sections, we'll discuss how to prepare a training script for execution on -SageMaker, then how to run that script on SageMaker using a ``sagemaker.tensorflow.TensorFlow`` +SageMaker, then how to run that script on SageMaker using a ``sagemaker.tensorflow.TensorFlow`` estimator. Preparing the TensorFlow training script @@ -744,7 +745,7 @@ version is **1.4.0**. This training script **must contain** the following functi Creating a ``model_fn`` ^^^^^^^^^^^^^^^^^^^^^^^ -A ``model_fn`` is a function that contains all the logic to support training, evaluation, +A ``model_fn`` is a function that contains all the logic to support training, evaluation, and prediction. The basic skeleton for a ``model_fn`` looks like this: .. code:: python @@ -771,8 +772,8 @@ The ``model_fn`` must accept four positional arguments: - ``TRAIN``: the ``model_fn`` was invoked in **training** mode. - ``EVAL``: the ``model_fn`` was invoked in **evaluation** mode. - ``PREDICT``: the ``model_fn`` was invoked in **predict** mode. -- ``hyperparameters``: The hyperparameters passed to SageMaker TrainingJob that runs - your TensorFlow training script. You can use this to pass hyperparameters to your +- ``hyperparameters``: The hyperparameters passed to SageMaker TrainingJob that runs + your TensorFlow training script. You can use this to pass hyperparameters to your training script. Example of a complete ``model_fn`` @@ -821,21 +822,21 @@ Example of a complete ``model_fn`` Distributed training '''''''''''''''''''' -When distributed training happens, a copy of the same neural network will be sent to -multiple training instances. Each instance will train with a batch of the dataset, +When distributed training happens, a copy of the same neural network will be sent to +multiple training instances. Each instance will train with a batch of the dataset, calculate loss and minimize the optimizer. One entire loop of this process is called training step. A `global step `_ is a global counter shared between the instances. It is necessary for distributed training, so the optimizer -can keep track of the number of training steps across instances. The only change in the -previous complete ``model_fn`` to enable distributed training is to pass in the global +can keep track of the number of training steps across instances. The only change in the +previous complete ``model_fn`` to enable distributed training is to pass in the global step into the ``optimizer.minimize`` function: .. code:: python - + train_op = optimizer.minimize(loss, tf.train.get_or_create_global_step()) -More information about distributed training can be find in talk from the TensorFlow Dev Summit 2017 +More information about distributed training can be find in talk from the TensorFlow Dev Summit 2017 `Distributed TensorFlow `_. @@ -845,8 +846,8 @@ More details on how to create a ``model_fn`` can be find in `Constructing the mo Creating ``train_input_fn`` and ``eval_input_fn`` functions ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -The ``train_input_fn`` is used to pass ``features`` and ``labels`` to the ``model_fn`` -in **training** mode. The ``eval_input_fn`` is used to ``features`` and ``labels`` to the +The ``train_input_fn`` is used to pass ``features`` and ``labels`` to the ``model_fn`` +in **training** mode. The ``eval_input_fn`` is used to ``features`` and ``labels`` to the ``model_fn`` in **evaluation** mode. The basic skeleton for the ``train_input_fn`` looks like this: @@ -859,7 +860,7 @@ The basic skeleton for the ``train_input_fn`` looks like this: # 2. Preprocess the dataset # 3. Return 1) a mapping of feature columns to Tensors with # the corresponding feature data, and 2) a Tensor containing labels - return feature_cols, labels + return feature_cols, labels An ``eval_input_fn`` follows the same format: @@ -871,7 +872,7 @@ An ``eval_input_fn`` follows the same format: # 2. Preprocess the dataset # 3. Return 1) a mapping of feature columns to Tensors with # the corresponding feature data, and 2) a Tensor containing labels - return feature_cols, labels + return feature_cols, labels Example of a complete ``train_input_fn`` and ``eval_input_fn`` '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' @@ -904,12 +905,12 @@ More details on how to create input functions can be find in `Building Input Fun Creating a ``serving_input_fn`` ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -During training, ``train_input_fn`` ingests data and prepares it for use by the model. -At the end of training, similarly, ``serving_input_fn`` is used to create the model that +During training, ``train_input_fn`` ingests data and prepares it for use by the model. +At the end of training, similarly, ``serving_input_fn`` is used to create the model that is exported for TensorFlow Serving. This function has the following purposes: - To add placeholders to the graph that the serving system will feed with inference requests. -- To add any additional ops needed to convert data from the input format into the feature Tensors +- To add any additional ops needed to convert data from the input format into the feature Tensors expected by the model. The basic skeleton for the ``serving_input_fn`` looks like this: @@ -920,7 +921,7 @@ The basic skeleton for the ``serving_input_fn`` looks like this: # Logic to the following: # 1. Defines placeholders that TensorFlow serving will feed with inference requests # 2. Preprocess input data - # 3. Returns a tf.estimator.export.ServingInputReceiver object, which packages the placeholders + # 3. Returns a tf.estimator.export.ServingInputReceiver object, which packages the placeholders and the resulting feature Tensors together. Example of a complete ``serving_input_fn`` @@ -943,7 +944,7 @@ More examples on how to create a TensorFlow training script can be find in the ` Support for pre-made ``tf.estimator`` and ``Keras`` models ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -In addition to ``model_fn``, ``sagemaker.tensorflow.TensorFlow`` supports pre-canned ``tf.estimator`` +In addition to ``model_fn``, ``sagemaker.tensorflow.TensorFlow`` supports pre-canned ``tf.estimator`` and ``Keras`` models. Using a pre-made ``tensorflow.estimator`` instead of a ``model_fn`` @@ -952,20 +953,20 @@ Using a pre-made ``tensorflow.estimator`` instead of a ``model_fn`` Pre-canned estimators are machine learning estimators premade for general purpose problems. ``tf.estimator`` provides the following pre-canned estimators: -- `tf.estimator.LinearClassifier `_: Constructs +- `tf.estimator.LinearClassifier `_: Constructs a linear classification model. -- `tf.estimator.LinearRegressor `_: Constructs +- `tf.estimator.LinearRegressor `_: Constructs a linear regression model. -- `tf.estimator.DNNClassifier `_: Constructs +- `tf.estimator.DNNClassifier `_: Constructs a neural network classification model. -- `tf.estimator.DNNRegressor `_: Construct +- `tf.estimator.DNNRegressor `_: Construct a neural network regression model. -- `tf.estimator.DNNLinearCombinedClassifier `_: Constructs +- `tf.estimator.DNNLinearCombinedClassifier `_: Constructs a neural network and linear combined classification model. -- `tf.estimator.DNNLinearCombinedRegressor `_: Constructs +- `tf.estimator.DNNLinearCombinedRegressor `_: Constructs a neural network and linear combined regression model. -To use a pre-canned ``tensorflow.estimator`` instead of creating a ``model_fn``, you need to write a ``estimator_fn``. +To use a pre-canned ``tensorflow.estimator`` instead of creating a ``model_fn``, you need to write a ``estimator_fn``. The base skeleton for the ``estimator_fn`` looks like this: .. code:: python @@ -998,7 +999,7 @@ An example on how to create a TensorFlow training script with an ``estimator_fn` Using a ``Keras`` model instead of a ``model_fn`` ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -``tf.keras`` is an full implementation inside TensorFlow of the Keras API. To use a ``tf.keras`` +``tf.keras`` is an full implementation inside TensorFlow of the Keras API. To use a ``tf.keras`` model for training instead of ``model_fn``, you need to write a ``keras_model_fn``. The base skeleton of a ``keras_model_fn`` looks like this: @@ -1054,7 +1055,7 @@ The ``TensorFlow`` constructor takes both required and optional arguments. Required arguments '''''''''''''''''' -The following are required arguments to the TensorFlow constructor. +The following are required arguments to the TensorFlow constructor. - ``entry_point (str)`` Path (absolute or relative) to the Python file which should be executed as the entry point to training. @@ -1067,7 +1068,7 @@ The following are required arguments to the TensorFlow constructor. training. - ``train_instance_type (str)`` Type of EC2 instance to use for training, for example, 'ml.c4.xlarge'. -- ``training_steps (int)`` Perform this many steps of training. ``None``, means train forever. +- ``training_steps (int)`` Perform this many steps of training. ``None``, means train forever. - ``evaluation_steps (int)`` Perform this many steps of evaluation. ``None``, means that evaluation runs until input from ``eval_input_fn`` is exhausted (or another exception is raised). @@ -1083,7 +1084,7 @@ you can specify these as keyword arguments. on SageMaker. - ``hyperparameters (dict[str,ANY])`` Hyperparameters that will be used for training. Will be made accessible as a dict[] to the training code on - SageMaker. Some hyperparameters will be interpreted by TensorFlow and can be use to + SageMaker. Some hyperparameters will be interpreted by TensorFlow and can be use to fine tune training. See `Optional Hyperparameters <#optional-hyperparameters>`_. - ``train_volume_size (int)`` Size in GB of the EBS volume to use for storing input data during training. Must be large enough to the store training @@ -1095,10 +1096,10 @@ you can specify these as keyword arguments. are stored to a default bucket. If the bucket with the specific name does not exist, the estimator creates the bucket during the ``fit`` method execution. -- ``checkpoint_path`` S3 location where checkpoint data will saved and restored. - The default location is *bucket_name/job_name/checkpoint*. If the location - already has checkpoints before the training starts, the model will restore - state from the last saved checkpoint. It is very useful to restart a training. +- ``checkpoint_path`` S3 location where checkpoint data will saved and restored. + The default location is *bucket_name/job_name/checkpoint*. If the location + already has checkpoints before the training starts, the model will restore + state from the last saved checkpoint. It is very useful to restart a training. See `Restoring from checkpoints <#restoring-from-checkpoints>`_. - ``output_kms_key`` Optional KMS key ID to optionally encrypt training output with. @@ -1110,8 +1111,8 @@ you can specify these as keyword arguments. Optional Hyperparameters '''''''''''''''''''''''' -These hyperparameters are used by TensorFlow to fine tune the training. -You need to add them inside the hyperparameters dictionary in the +These hyperparameters are used by TensorFlow to fine tune the training. +You need to add them inside the hyperparameters dictionary in the ``TensorFlow`` estimator constructor. - ``save_summary_steps (int)`` Save summaries every this many steps. @@ -1138,10 +1139,10 @@ both required and optional arguments. Required argument ''''''''''''''''' -- ``inputs (str)``: A S3 URI, for example ``s3://my-bucket/my-training-data``, which contains +- ``inputs (str)``: A S3 URI, for example ``s3://my-bucket/my-training-data``, which contains the dataset that will be used for training. When the training job starts in SageMaker the container will download the dataset. Both ``train_input_fn`` and ``eval_input_fn`` functions - have a parameter called ``training_dir`` which contains the directory inside the container + have a parameter called ``training_dir`` which contains the directory inside the container where the dataset was saved into. See `Creating train_input_fn and eval_input_fn functions`_. Optional arguments @@ -1151,8 +1152,8 @@ Optional arguments training script to complete before returning. - ``logs (bool)``: Defaults to True, whether to show logs produced by training job in the Python session. Only meaningful when wait is True. -- ``run_tensorboard_locally (bool)``: Defaults to False. Executes TensorBoard in a different - process with downloaded checkpoint information. Requires modules TensorBoard and AWS CLI. +- ``run_tensorboard_locally (bool)``: Defaults to False. Executes TensorBoard in a different + process with downloaded checkpoint information. Requires modules TensorBoard and AWS CLI. installed. Terminates TensorBoard when the execution ends. See `Running TensorBoard`_. - ``job_name (str)``: Training job name. If not specified, the estimator generates a default job name, based on the training image name and current timestamp. @@ -1168,15 +1169,15 @@ Calling ``fit`` starts a SageMaker training job. The training job will execute t - starts a Docker container optimized for TensorFlow, see `SageMaker TensorFlow Docker containers`_. - downloads the dataset. - setup up distributed training. - - starts asynchronous training, executing the ``model_fn`` function defined in your script - in **training** mode; i.e., ``features`` and ``labels`` are fed by a batch of the + - starts asynchronous training, executing the ``model_fn`` function defined in your script + in **training** mode; i.e., ``features`` and ``labels`` are fed by a batch of the training dataset defined by ``train_input_fn``. See `Creating train_input_fn and eval_input_fn functions`_. The training job finishes after the number of training steps reaches the value defined by the ``TensorFlow`` estimator parameter ``training_steps`` is finished or when the training job execution time reaches the ``TensorFlow`` estimator parameter ``train_max_run``. -When the training job finishes, a `TensorFlow serving `_ +When the training job finishes, a `TensorFlow serving `_ with the result of the training is generated and saved to the S3 location define by the ``TensorFlow`` estimator parameter ``output_path``. @@ -1188,7 +1189,7 @@ During the training job, the first EC2 instance that is executing the training i All instances execute the training loop, feeding the ``model_fn`` with ``train_input_fn``. Every ``min_eval_frequency`` steps (see `Optional Hyperparameters`_), the ``master`` instance will execute the ``model_fn`` in **evaluation** mode; i.e., ``features`` and ``labels`` are -fed with the evaluation dataset defined by ``eval_input_fn``. See `Creating train_input_fn and eval_input_fn functions`_. +fed with the evaluation dataset defined by ``eval_input_fn``. See `Creating train_input_fn and eval_input_fn functions`_. For more information on training and evaluation process, see `tf.estimator.train_and_evaluate `_. @@ -1197,20 +1198,20 @@ For more information on fit, see `SageMaker Python SDK Overview <#sagemaker-pyth TensorFlow serving models ^^^^^^^^^^^^^^^^^^^^^^^^^ -After your training job is complete in SageMaker and the ``fit`` call ends, the training job -will generate a `TensorFlow serving `_ -model ready for deployment. Your TensorFlow serving model will be available in the S3 location -``output_path`` that you specified when you created your `sagemaker.tensorflow.TensorFlow` +After your training job is complete in SageMaker and the ``fit`` call ends, the training job +will generate a `TensorFlow serving `_ +model ready for deployment. Your TensorFlow serving model will be available in the S3 location +``output_path`` that you specified when you created your `sagemaker.tensorflow.TensorFlow` estimator. Restoring from checkpoints ^^^^^^^^^^^^^^^^^^^^^^^^^^ While your training job is executing, TensorFlow will generate checkpoints and save them in the S3 -location defined by ``checkpoint_path`` parameter in the ``TensorFlow`` constructor. +location defined by ``checkpoint_path`` parameter in the ``TensorFlow`` constructor. These checkpoints can be used to restore a previous session or to evaluate the current training using ``TensorBoard``. -To restore a previous session, you just need to create a new ``sagemaker.tensorflow.TensorFlow`` +To restore a previous session, you just need to create a new ``sagemaker.tensorflow.TensorFlow`` estimator pointing to the previous checkpoint path: .. code:: python @@ -1227,20 +1228,20 @@ estimator pointing to the previous checkpoint path: Running TensorBoard ^^^^^^^^^^^^^^^^^^^ -When the ``fit`` parameter ``run_tensorboard_locally`` is set ``True``, all the checkpoint data -located in ``checkpoint_path`` will be downloaded to a local temporary folder and a local -``TensorBoard`` application will be watching that temporary folder. +When the ``fit`` parameter ``run_tensorboard_locally`` is set ``True``, all the checkpoint data +located in ``checkpoint_path`` will be downloaded to a local temporary folder and a local +``TensorBoard`` application will be watching that temporary folder. Every time a new checkpoint is created by the training job in the S3 bucket, ``fit`` will download that checkpoint to the same temporary folder and update ``TensorBoard``. -When the ``fit`` method starts the training, it will log the port that ``TensorBoard`` is using -to display metrics. The default port is **6006**, but another port can be chosen depending on +When the ``fit`` method starts the training, it will log the port that ``TensorBoard`` is using +to display metrics. The default port is **6006**, but another port can be chosen depending on availability. The port number will increase until finds an available port. After that, the port number will be printed in stdout. It takes a few minutes to provision containers and start the training job. TensorBoard will start to display metrics shortly after that. -You can access TensorBoard locally at http://localhost:6006 or using your SakeMaker workspace at -`https*workspace_base_url*proxy/6006/ `_ (TensorBoard will not work if you forget to put the slash, +You can access TensorBoard locally at http://localhost:6006 or using your SakeMaker workspace at +`https*workspace_base_url*proxy/6006/ `_ (TensorBoard will not work if you forget to put the slash, '/', in end of the url). If TensorBoard started on a different port, adjust these URLs to match. @@ -1249,7 +1250,7 @@ Deploying TensorFlow Serving models After a ``TensorFlow`` Estimator has been fit, it saves a ``TensorFlow Serving`` model in the S3 location defined by ``output_path``. You can call ``deploy`` on a ``TensorFlow`` -estimator to create a SageMaker Endpoint. +estimator to create a SageMaker Endpoint. A common usage of the ``deploy`` method, after the ``TensorFlow`` estimator has been fit look like this: @@ -1265,7 +1266,7 @@ like this: predictor = estimator.deploy(initial_instance_count=1, instance_type='ml.c4.xlarge') -The code block above deploys a SageMaker Endpoint with one instance of the type 'ml.c4.xlarge'. +The code block above deploys a SageMaker Endpoint with one instance of the type 'ml.c4.xlarge'. What happens when deploy is called ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -1299,18 +1300,18 @@ The following code adds a prediction request to the previous code example: result = predictor.predict([6.4, 3.2, 4.5, 1.5]) -The ``predictor.predict`` method call takes one parameter, the input ``data`` for which you want the ``SageMaker Endpoint`` +The ``predictor.predict`` method call takes one parameter, the input ``data`` for which you want the ``SageMaker Endpoint`` to provide inference. ``predict`` will serialize the input data, and send it in as request to the ``SageMaker Endpoint`` by an ``InvokeEndpoint`` SageMaker operation. ``InvokeEndpoint`` operation requests can be made by ``predictor.predict``, by -boto3 ``SageMaker.runtime`` client or by AWS CLI. +boto3 ``SageMaker.runtime`` client or by AWS CLI. -The ``SageMaker Endpoint`` web server will process the request, make an inference using the deployed model, and return a response. +The ``SageMaker Endpoint`` web server will process the request, make an inference using the deployed model, and return a response. The ``result`` returned by ``predict`` is a Python dictionary with the model prediction. In the code example above, the prediction ``result`` looks like this: .. code:: python - {'result': + {'result': {'classifications': [ {'classes': [ {'label': '0', 'score': 0.0012890376383438706}, @@ -1323,8 +1324,8 @@ a Python dictionary with the model prediction. In the code example above, the pr Specifying the output of a prediction request ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -The format of the prediction ``result`` is determined by the parameter ``export_outputs`` of the `tf.estimator.EstimatorSpec `_ that you returned when you created your ``model_fn``, see -`Example of a complete model_fn`_ for an example of ``export_outputs``. +The format of the prediction ``result`` is determined by the parameter ``export_outputs`` of the `tf.estimator.EstimatorSpec `_ that you returned when you created your ``model_fn``, see +`Example of a complete model_fn`_ for an example of ``export_outputs``. More information on how to create ``export_outputs`` can find in `specifying the outputs of a custom model `_. @@ -1373,7 +1374,7 @@ An example of ``input_fn`` for the content-type "application/python-pickle" can else: # Handle other content-types here or raise an Exception # if the content type is not supported. - pass + pass Overriding output precessing with an ``output_fn`` ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -1391,7 +1392,7 @@ An example of ``output_fn`` for the accept type "application/python-pickle" can else: # Handle other content-types here or raise an Exception # if the content type is not supported. - pass + pass A example with ``input_fn`` and ``output_fn`` above can be found in `here `_. @@ -1423,7 +1424,7 @@ The Docker images extend Ubuntu 16.04. AWS SageMaker Estimators ------------------------ -Amazon SageMaker provides several built-in machine learning algorithms that you can use for a variety of problem types. +Amazon SageMaker provides several built-in machine learning algorithms that you can use for a variety of problem types. The full list of algorithms is available on the AWS website: https://docs.aws.amazon.com/sagemaker/latest/dg/algos.html @@ -1524,6 +1525,36 @@ Example code using the TensorFlow predictor: :: from sagemaker.tensorflow import TensorFlowPredictor - + predictor = TensorFlowPredictor('myexistingendpoint') result = predictor.predict(['my request body']) + + +BYO Model +----------------------------------------------- +You can also create an endpoint from an existing model rather than training one - i.e. bring your own model. + +First, package the files for the trained model into a ``.tar.gz`` file, and upload the archive to S3. + +Next, create a ``Model`` object that corresponds to the framework that you are using: `MXNetModel `__ or `TensorFlowModel `__. + +Example code using ``MXNetModel``: + +.. code:: python + + from sagemaker.mxnet.model import MXNetModel + + sagemaker_model = MXNetModel(model_data='s3://path/to/model.tar.gz', + role='arn:aws:iam::accid:sagemaker-role', + entry_point='entry_point.py') + +After that, invoke the ``deploy()`` method on the ``Model``: + +.. code:: python + + predictor = sagemaker_model.deploy(initial_instance_count=1, + instance_type='ml.m4.xlarge') + +This returns a predictor the same way an ``Estimator`` does when ``deploy()`` is called. You can now get inferences just like with any other model deployed on Amazon SageMaker. + +A full example is available in the `Amazon SageMaker examples repository `__.