Amazon Machine Learning

Amazon Machine Learning (ML) is a service that makes it easy for developers of all skill levels to use machine learning technology. The SDK for iOS provides a simple, high-level client designed to help you interface with Amazon Machine Learning service. The client enables you to call Amazon ML's real-time API to retrieve predictions from your models and enables you to build mobile applications that request and take actions on predictions. The client also enables you to retrieve the real-time prediction endpoint URLs for your ML models.

Integrate Amazon Machine Learning#

To use the Amazon Machine Learning mobile client, you’ll need to integrate the SDK for iOS into your app and import the necessary libraries. To do so, follow these steps:

#. Download the SDK and unzip it as described in Setup the SDK for iOS #. The instructions direct you to import the headers for the services you’ll be using. For Amazon Machine Learning, you need the following import.

Swift
import AWSMachineLearning
Objective C
#import <AWSMachineLearning/AWSMachineLearning.h>

Configure Credentials#

You can use Amazon Cognito to provide temporary AWS credentials to your application. These credentials let the app access your AWS resources. To create a credentials provider, follow the instructions at Providing AWS Credentials.

To use Amazon Machine Learning in an application, you must set the proper permissions. The following IAM policy allows the user to perform the actions shown in this tutorial on two actions identified by ARN.

{
    "Statement": [{
    "Effect": "Allow",
    "Action": [
        "machinelearning:GetMLModel",
        "machinelearning:Predict"
    ],
    "Resource": "arn:aws:machinelearning:use-east-1:11122233444:mlmodel/example-model-id"
    }]
}

This policy should be applied to roles assigned to the Amazon Cognito identity pool, but you will need to replace the Resource value with the correct account ID and ML Model ID. You can apply policies at the IAM console. To learn more about IAM policies, see Introduction to IAM.

Create an Amazon Machine Learning Client#

Once you've imported the necessary libraries and have your credentials object, you can instantiate AWSMachineLearningGetMLModelInput.

Swift
let getMlModelInput = AWSMachineLearningGetMLModelInput()
Objective C
AWSMachineLearningGetMLModelInput *getMLModelInput = [AWSMachineLearningGetMLModelInput new];

Making a Predict Request#

Prior to calling Predict, make sure you have not only a completed ML Model ID but also a created real-time endpoint for that ML Model ID. This cannot be done through the mobile SDK; you will have to use the Machine Learning Console or an alternate SDK. To validate that this ML can be used for real-time Predictions.

Swift
// Use a created model that has a created real-time endpoint
let mlModelId = "example-model-id";
// Call GetMLModel to get the realtime endpoint URL
let getMlModelInput = AWSMachineLearningGetMLModelInput()
getMlModelInput!.mlModelId = mlModelId;

machineLearning.getMLModel(getMlModelInput!).continueOnSuccessWith { (task) -> Any? in
    if let getMLModelOutput = task.result {

        if (getMLModelOutput.status != AWSMachineLearningEntityStatus.completed) {
                print("ML Model is not completed");
                return nil;
        }

        // Validate that the realtime endpoint is ready
        if (getMLModelOutput.endpointInfo!.endpointStatus != AWSMachineLearningRealtimeEndpointStatus.ready) {
            print("Realtime endpoint is not ready");
            return nil;
        }
    }

return nil
}
Objective C
// Use a created model that has a created real-time endpoint
NSString *MLModelId = @"example-model-id";

// Call GetMLModel to get the realtime endpoint URL
AWSMachineLearningGetMLModelInput *getMLModelInput = [AWSMachineLearningGetMLModelInput new];
getMLModelInput.MLModelId = MLModelId;

[[[MachineLearning getMLModel:getMLModelInput] continueWithSuccessBlock:^id(AWSTask *task) {
    AWSMachineLearningGetMLModelOutput *getMLModelOutput = task.result;

    // Validate that the ML model is completed
    if (getMLModelOutput.status != AWSMachineLearningEntityStatusCompleted) {
        NSLog(@"ML Model is not completed");
        return nil;
    }

    // Validate that the realtime endpoint is ready
    if (getMLModelOutput.endpointInfo.endpointStatus != AWSMachineLearningRealtimeEndpointStatusReady) {
        NSLog(@"Realtime endpoint is not ready");
        return nil;
    }
}

Once the real-time endpoint is ready, we can begin calling Predict. Note that you must pass the real-time endpoint through the PredictRequest.

Swift
// Create a Predict request with your ML Model id and the appropriate
let predictInput = AWSMachineLearningPredictInput()
predictInput!.predictEndpoint = getMLModelOutput.endpointInfo!.endpointUrl;
predictInput!.mlModelId = mlModelId;
predictInput!.record = record

return machineLearning.predict(predictInput!)
Objective C
// Create a Predict request with your ML Model id and the appropriate Record mapping.
AWSMachineLearningPredictInput *predictInput = [AWSMachineLearningPredictInput new];
predictInput.predictEndpoint = getMLModelOutput.endpointInfo.endpointUrl;
predictInput.MLModelId = MLModelId;
predictInput.record = @{};

// Call and return prediction
return [MachineLearning predict:predictInput];

Additional Resources