亚马逊有关于如何在iOS上使用他们的机器学习平台的文档,但没有Swift实现,我无法将其转换为Swift。这是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;
}
}
AWSMachineLearningPredictInput *predictInput = [AWSMachineLearningPredictInput new];
predictInput.predictEndpoint = getMLModelOutput.endpointInfo.endpointUrl;
predictInput.MLModelId = MLModelId;
predictInput.record = @{};
// Call and return prediction
return [MachineLearning predict:predictInput];
这是我尝试的快速代码
var getMLModelInput = AWSMachineLearningGetMLModelInput()
// Use a created model that has a created real-time endpoint
let MLModelId = "example-model-id"
// Call GetMLModel to get the realtime endpoint URL
getMLModelInput.MLModelId = MLModelId;
let task = AWSMachineLearning.getMLModel(getMLModelInput) // This line won't work because the method .getMLModel expects and instance of AWSMachineLearning
我试图在用于上传到s3的代码之后模拟我的Swift代码:
let transferManager = AWSS3TransferManager.defaultS3TransferManager()
let testFileURL1 = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent("tmp")
let uploadRequest1 : AWSS3TransferManagerUploadRequest = AWSS3TransferManagerUploadRequest()
let data = userCSV.dataUsingEncoding(NSUTF8StringEncoding)
data!.writeToURL(testFileURL1, atomically: true)
uploadRequest1.bucket = "users/1"
uploadRequest1.key = "tmpkey.csv"
uploadRequest1.body = testFileURL1
let task = transferManager.upload(uploadRequest1)
task.continueWithBlock { (task) -> AnyObject! in
if task.error != nil {
print("Error: \(task.error)")
} else {
print("Upload successful")
}
return nil
}
但我无法弄清楚如何为机器学习模型构建任务对象。任何帮助将不胜感激!
答案 0 :(得分:1)
AWS网站上的代码段在开头缺少一行:
AWSMachineLearning *MachineLearning = [AWSMachineLearning defaultMachineLearning];
你可以像这样把它翻译成Swift
let MachineLearning = AWSMachineLearning.defaultMachineLearning()
然后你可以这样打电话:
let MachineLearning = AWSMachineLearning.defaultMachineLearning()
let getMLModelInput = AWSMachineLearningGetMLModelInput()
// Use a created model that has a created real-time endpoint
getMLModelInput.MLModelId = "example-model-id"
MachineLearning.getMLModel(getMLModelInput).continueWithBlock { (task) -> AnyObject? in
//
}
您应该查看此集成test case以获取更多详细信息。