MXNet Classes¶
MXNet Estimator¶
-
class
sagemaker.mxnet.estimator.MXNet(entry_point, framework_version=None, py_version=None, source_dir=None, hyperparameters=None, image_uri=None, distribution=None, **kwargs)¶ Bases:
sagemaker.estimator.FrameworkHandle end-to-end training and deployment of custom MXNet code.
This
Estimatorexecutes an MXNet script in a managed MXNet execution environment.The managed MXNet environment is an Amazon-built Docker container that executes functions defined in the supplied
entry_pointPython script.Training is started by calling
fit()on this Estimator. After training is complete, callingdeploy()creates a hosted SageMaker endpoint and returns anMXNetPredictorinstance 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 available 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. If
source_diris specified, thenentry_pointmust point to a file located at the root ofsource_dir.framework_version (str) – MXNet version you want to use for executing your model training code. Defaults to None. Required unless
image_uriis provided. List of supported versions. https://github.com/aws/sagemaker-python-sdk#mxnet-sagemaker-estimators.py_version (str) – Python version you want to use for executing your model training code. One of ‘py2’ or ‘py3’. Defaults to
None. Required unlessimage_uriis provided.source_dir (str) – Path (absolute, relative or an S3 URI) to a directory with any other training source code dependencies aside from the entry point file (default: None). If
source_diris an S3 URI, it must point to a tar.gz file. 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.image_uri (str) –
If specified, the estimator will use this image for training and hosting, instead of selecting the appropriate SageMaker official image based on framework_version and py_version. It can be an ECR url or dockerhub image and tag.
Examples
123412341234.dkr.ecr.us-west-2.amazonaws.com/my-custom-image:1.0custom-image:latest
If
framework_versionorpy_versionareNone, thenimage_uriis required. If alsoNone, then aValueErrorwill be raised.distribution (dict) –
A dictionary with information on how to run distributed training (default: None). Currently we support distributed training with parameter server and MPI [Horovod]. To enable parameter server use the following setup:
{ 'parameter_server': { 'enabled': True } }
To enable MPI:
{ 'mpi': { 'enabled': True } }
Option parameters within
mpiareprocesses_per_hostandcustom_mpi_options.{ 'mpi': { 'enabled': True, 'processes_per_host': 2, 'custom_mpi_options': '-verbose --NCCL_DEBUG=INFO' } }
**kwargs – Additional kwargs passed to the
Frameworkconstructor.
Tip
You can find additional parameters for initializing this class at
FrameworkandEstimatorBase.-
create_model(model_server_workers=None, role=None, vpc_config_override='VPC_CONFIG_DEFAULT', entry_point=None, source_dir=None, dependencies=None, image_uri=None, **kwargs)¶ Create a SageMaker
MXNetModelobject that can be deployed to anEndpoint.- 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.
role (str) – The
ExecutionRoleArnIAM Role ARN for theModel, which is also used during transform jobs. If not specified, the role from the Estimator will be used.vpc_config_override (dict[str, list[str]]) –
Optional override for VpcConfig set on the model. Default: use subnets and security groups from this Estimator.
’Subnets’ (list[str]): List of subnet ids.
’SecurityGroupIds’ (list[str]): List of security group ids.
entry_point (str) – Path (absolute or relative) to the local Python source file which should be executed as the entry point to training. If
source_diris specified, thenentry_pointmust point to a file located at the root ofsource_dir. If not specified, the training entry point is used.source_dir (str) – Path (absolute or relative) to a directory with any other serving source code dependencies aside from the entry point file. If not specified, the model source directory from training is used.
dependencies (list[str]) – A list of paths to directories (absolute or relative) with any additional libraries that will be exported to the container. If not specified, the dependencies from training are used. This is not supported with “local code” in Local Mode.
image_uri (str) –
If specified, the estimator will use this image for hosting, instead of selecting the appropriate SageMaker official image based on framework_version and py_version. It can be an ECR url or dockerhub image and tag.
Examples
123412341234.dkr.ecr.us-west-2.amazonaws.com/my-custom-image:1.0custom-image:latest
**kwargs – Additional kwargs passed to the
MXNetModelconstructor.
- Returns
A SageMaker
MXNetModelobject. SeeMXNetModel()for full details.- Return type
MXNet Model¶
-
class
sagemaker.mxnet.model.MXNetModel(model_data, role, entry_point, framework_version=None, py_version=None, image_uri=None, predictor_cls=<class 'sagemaker.mxnet.model.MXNetPredictor'>, model_server_workers=None, **kwargs)¶ Bases:
sagemaker.model.FrameworkModelAn MXNet SageMaker
Modelthat can be deployed to a SageMakerEndpoint.Initialize an MXNetModel.
- Parameters
model_data (str) – The S3 location of a SageMaker model data
.tar.gzfile.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. If
source_diris specified, thenentry_pointmust point to a file located at the root ofsource_dir.framework_version (str) – MXNet version you want to use for executing your model training code. Defaults to
None. Required unlessimage_uriis provided.py_version (str) – Python version you want to use for executing your model training code. Defaults to
None. Required unlessimage_uriis provided.image_uri (str) – A Docker image URI (default: None). If not specified, a default image for MXNet will be used. If
framework_versionorpy_versionareNone, thenimage_uriis required. Ifimage_uriis alsoNone, then aValueErrorwill be raised.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 superclass
FrameworkModeland, subsequently, its superclassModel.
Tip
You can find additional parameters for initializing this class at
FrameworkModelandModel.-
register(content_types, response_types, inference_instances, transform_instances, model_package_name=None, model_package_group_name=None, image_uri=None, model_metrics=None, metadata_properties=None, marketplace_cert=False, approval_status=None, description=None, drift_check_baselines=None, customer_metadata_properties=None, domain=None)¶ Creates a model package for creating SageMaker models or listing on Marketplace.
- Parameters
content_types (list) – The supported MIME types for the input data.
response_types (list) – The supported MIME types for the output data.
inference_instances (list) – A list of the instance types that are used to generate inferences in real-time.
transform_instances (list) – A list of the instance types on which a transformation job can be run or on which an endpoint can be deployed.
model_package_name (str) – Model Package name, exclusive to model_package_group_name, using model_package_name makes the Model Package un-versioned (default: None).
model_package_group_name (str) – Model Package Group name, exclusive to model_package_name, using model_package_group_name makes the Model Package versioned (default: None).
image_uri (str) – Inference image uri for the container. Model class’ self.image will be used if it is None (default: None).
model_metrics (ModelMetrics) – ModelMetrics object (default: None).
metadata_properties (MetadataProperties) – MetadataProperties (default: None).
marketplace_cert (bool) – A boolean value indicating if the Model Package is certified for AWS Marketplace (default: False).
approval_status (str) – Model Approval Status, values can be “Approved”, “Rejected”, or “PendingManualApproval” (default: “PendingManualApproval”).
description (str) – Model Package description (default: None).
drift_check_baselines (DriftCheckBaselines) – DriftCheckBaselines object (default: None).
customer_metadata_properties (dict[str, str]) – A dictionary of key-value paired metadata properties (default: None).
domain (str) – Domain values can be “COMPUTER_VISION”, “NATURAL_LANGUAGE_PROCESSING”, “MACHINE_LEARNING” (default: None).
- Returns
A sagemaker.model.ModelPackage instance.
-
prepare_container_def(instance_type=None, accelerator_type=None, serverless_inference_config=None)¶ Return a container definition with framework configuration.
Framework configuration is set in model environment variables.
- Parameters
instance_type (str) – The EC2 instance type to deploy this Model to. For example, ‘ml.p2.xlarge’.
accelerator_type (str) – The Elastic Inference accelerator type to deploy to the instance for loading and making inferences to the model. For example, ‘ml.eia1.medium’.
serverless_inference_config (sagemaker.serverless.ServerlessInferenceConfig) – Specifies configuration related to serverless endpoint. Instance type is not provided in serverless inference. So this is used to find image URIs.
- Returns
A container definition object usable with the CreateModel API.
- Return type
-
serving_image_uri(region_name, instance_type, accelerator_type=None, serverless_inference_config=None)¶ Create a URI for the serving image.
- Parameters
region_name (str) – AWS region where the image is uploaded.
instance_type (str) – SageMaker instance type. Used to determine device type (cpu/gpu/family-specific optimized).
accelerator_type (str) – The Elastic Inference accelerator type to deploy to the instance for loading and making inferences to the model (default: None). For example, ‘ml.eia1.medium’.
serverless_inference_config (sagemaker.serverless.ServerlessInferenceConfig) – Specifies configuration related to serverless endpoint. Instance type is not provided in serverless inference. So this is used to determine device type.
- Returns
The appropriate image URI based on the given parameters.
- Return type
MXNet Predictor¶
-
class
sagemaker.mxnet.model.MXNetPredictor(endpoint_name, sagemaker_session=None, serializer=<sagemaker.serializers.JSONSerializer object>, deserializer=<sagemaker.deserializers.JSONDeserializer object>)¶ Bases:
sagemaker.predictor.PredictorA Predictor for inference against MXNet Endpoints.
This is able to serialize Python lists, dictionaries, 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.
serializer (callable) – Optional. Default serializes input data to json. Handles dicts, lists, and numpy arrays.
deserializer (callable) – Optional. Default parses the response using
json.load(...).