MXNet

MXNet Estimator

class sagemaker.mxnet.estimator.MXNet(entry_point, source_dir=None, hyperparameters=None, py_version='py2', **kwargs)

Bases: sagemaker.estimator.Framework

Handle end-to-end training and deployment of custom MXNet code.

This Estimator executes an MXNet script in a managed MXNet execution environment, within a SageMaker Training Job. The managed MXNet environment is an Amazon-built Docker container that executes functions defined in the supplied entry_point Python script.

Training is started by calling fit() on this Estimator. After training is complete, calling deploy() creates a hosted SageMaker endpoint and returns an MXNetPredictor instance that can be used to perform inference against the hosted model.

Technical documentation on preparing MXNet scripts for SageMaker training and using the MXNet Estimator is avaialble on the project home-page: https://github.com/aws/sagemaker-python-sdk

Parameters:
  • entry_point (str) – Path (absolute or relative) to the Python source file which should be executed as the entry point to training. This should be compatible with either Python 2.7 or Python 3.5.
  • source_dir (str) – Path (absolute or relative) to a directory with any other training source code dependencies aside from tne entry point file (default: None). Structure within this directory are preserved when training on Amazon SageMaker.
  • hyperparameters (dict) – Hyperparameters that will be used for training (default: None). The hyperparameters are made accessible as a dict[str, str] to the training code on SageMaker. For convenience, this accepts other types for keys and values, but str() will be called to convert them before training.
  • py_version (str) – Python version you want to use for executing your model training code (default: ‘py2’). One of ‘py2’ or ‘py3’.
  • **kwargs – Additional kwargs passed to the Framework constructor.
train_image()

Return the Docker image to use for training.

The fit() method, which does the model training, calls this method to find the image to use for model training.

Returns:The URI of the Docker image.
Return type:str
create_model(model_server_workers=None)

Create a SageMaker MXNetModel object that can be deployed to an Endpoint.

Parameters:model_server_workers (int) – Optional. The number of worker processes used by the inference server. If None, server will use one worker per vCPU.
Returns:
A SageMaker MXNetModel object.
See MXNetModel() for full details.
Return type:sagemaker.mxnet.model.MXNetModel
classmethod attach(training_job_name, sagemaker_session=None)

Attach to an existing training job.

Create an Estimator bound to an existing training job. After attaching, if the training job is in a Complete status, it can be deploy``ed to create a SageMaker ``Endpoint and return a Predictor.

If the training job is in progress, attach will block and display log messages from the training job, until the training job completes.

Parameters:
  • training_job_name (str) – The name of the training job to attach to.
  • sagemaker_session (sagemaker.session.Session) – Session object which manages interactions with Amazon SageMaker APIs and any other AWS services needed. If not specified, the estimator creates one using the default AWS configuration chain.
Returns:

Estimator with the attached training job.

Return type:

sagemaker.mxnet.estimator.MXNet

Raises:

ValueError – If training_job_name is None or the image name does not match the framework.

MXNet Model

class sagemaker.mxnet.model.MXNetModel(model_data, role, entry_point, image=None, py_version='py2', predictor_cls=<class 'sagemaker.mxnet.model.MXNetPredictor'>, model_server_workers=None, **kwargs)

Bases: sagemaker.model.FrameworkModel

An MXNet SageMaker Model that can be deployed to a SageMaker Endpoint.

Initialize an MXNetModel.

Parameters:
  • model_data (str) – The S3 location of a SageMaker model data .tar.gz file.
  • role (str) – An AWS IAM role (either name or full ARN). The Amazon SageMaker training jobs and APIs that create Amazon SageMaker endpoints use this role to access training data and model artifacts. After the endpoint is created, the inference code might use the IAM role, if it needs to access an AWS resource.
  • entry_point (str) – Path (absolute or relative) to the Python source file which should be executed as the entry point to model hosting. This should be compatible with either Python 2.7 or Python 3.5.
  • image (str) – A Docker image URI (default: None). If not specified, a default image for MXNet will be used.
  • py_version (str) – Python version you want to use for executing your model training code (default: ‘py2’).
  • predictor_cls (callable[str, sagemaker.session.Session]) – A function to call to create a predictor with an endpoint name and SageMaker Session. If specified, deploy() returns the result of invoking this function on the created endpoint name.
  • model_server_workers (int) – Optional. The number of worker processes used by the inference server. If None, server will use one worker per vCPU.
  • **kwargs – Keyword arguments passed to the FrameworkModel initializer.
prepare_container_def(instance_type)

Return a container definition with framework configuration set in model environment variables.

Parameters:instance_type (str) – The EC2 instance type to deploy this Model to. For example, ‘ml.p2.xlarge’.
Returns:A container definition object usable with the CreateModel API.
Return type:dict[str, str]

MXNet Predictor

class sagemaker.mxnet.model.MXNetPredictor(endpoint_name, sagemaker_session=None)

Bases: sagemaker.predictor.RealTimePredictor

A RealTimePredictor for inference against MXNet Endpoints.

This is able to serialize Python lists and numpy arrays to multidimensional tensors for MXNet inference.

Initialize an MXNetPredictor.

Parameters:
  • endpoint_name (str) – The name of the endpoint to perform inference on.
  • sagemaker_session (sagemaker.session.Session) – Session object which manages interactions with Amazon SageMaker APIs and any other AWS services needed. If not specified, the estimator creates one using the default AWS configuration chain.