[英]AWS Sagemaker and Java SDK
I have an AWS Sagemaker endpoint and I am trying to call it using the Java SDK.我有一个 AWS Sagemaker 端点,我正在尝试使用 Java SDK 调用它。 However I am getting this error:
但是我收到此错误:
Exception in thread "main" java.lang.NoSuchMethodError: com.amazonaws.services.sagemakerruntime.AmazonSageMakerRuntimeClient.beforeClientExecution(Lcom/amazonaws/AmazonWebServiceRequest;)Lcom/amazonaws/AmazonWebServiceRequest;
at com.amazonaws.services.sagemakerruntime.AmazonSageMakerRuntimeClient.invokeEndpoint(AmazonSageMakerRuntimeClient.java:150)
at TestAmazon.main(TestAmazon.java:72)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
When I run the invoke-endpoint on the CLI it works as expected.当我在 CLI 上运行 invoke-endpoint 时,它按预期工作。
The code:代码:
InvokeEndpointRequest invokeEndpointRequest = new InvokeEndpointRequest();
invokeEndpointRequest.setContentType("application/x-image");
ByteBuffer buf = ByteBuffer.wrap(read_buf);
invokeEndpointRequest.setBody(buf);
invokeEndpointRequest.setEndpointName("imageclassification-ep--2018-04-17-19-47-00");
invokeEndpointRequest.setAccept("application/json");
AmazonSageMakerRuntime amazonSageMaker = AmazonSageMakerRuntimeClientBuilder.defaultClient();
InvokeEndpointResult invokeEndpointResult = amazonSageMaker.invokeEndpoint(invokeEndpointRequest);
Any help would be much appreciated.任何帮助将不胜感激。
So I found the issue to my problem it was that I was using:所以我发现我的问题是我正在使用:
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-bom</artifactId>
<version>1.11.83</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Instead of the latest version 1.11.313而不是最新版本 1.11.313
Anyone reading this thread and wanting to use SageMaker and Java V2 (not V1), see this GitHub location:任何阅读此线程并想要使用 SageMaker 和 Java V2(而非 V1)的人,请参阅此 GitHub 位置:
https://github.com/aws-doc-sdk-examples/tree/master/javav2/example_code/sagemaker https://github.com/aws-doc-sdk-examples/tree/master/javav2/example_code/sagemaker
There are many examples of how to work with this API.有许多关于如何使用此 API 的示例。 For example, the following code example demonstrates how to start a model training job for Amazon SageMaker.
例如,以下代码示例演示了如何为 Amazon SageMaker 启动模型训练作业。
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sagemaker.SageMakerClient;
import software.amazon.awssdk.services.sagemaker.model.S3DataSource;
import software.amazon.awssdk.services.sagemaker.model.DataSource;
import software.amazon.awssdk.services.sagemaker.model.Channel;
import software.amazon.awssdk.services.sagemaker.model.ResourceConfig;
import software.amazon.awssdk.services.sagemaker.model.TrainingInstanceType;
import software.amazon.awssdk.services.sagemaker.model.CheckpointConfig;
import software.amazon.awssdk.services.sagemaker.model.OutputDataConfig;
import software.amazon.awssdk.services.sagemaker.model.StoppingCondition;
import software.amazon.awssdk.services.sagemaker.model.AlgorithmSpecification;
import software.amazon.awssdk.services.sagemaker.model.TrainingInputMode;
import software.amazon.awssdk.services.sagemaker.model.CreateTrainingJobRequest;
import software.amazon.awssdk.services.sagemaker.model.CreateTrainingJobResponse;
import software.amazon.awssdk.services.sagemaker.model.SageMakerException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//snippet-end:[sagemaker.java2.train_job.import]
/**
* To setup the model data and other requirements to make this Java V2 example work, follow this AWS tutorial prior to running this Java code example.
* https://aws.amazon.com/getting-started/hands-on/build-train-deploy-machine-learning-model-sagemaker/
*/
public class CreateTrainingJob {
public static void main(String[] args) {
final String USAGE = "\n" +
"Usage:\n" +
" CreateTrainingJob <s3UriData><s3Uri><trainingJobName><roleArn><s3OutputPath><channelName><trainingImage>\n\n" +
"Where:\n" +
" s3UriData - The location where the training data is located (ie, s3://trainbucket/train.csv).\n\n" +
" s3Uri - The S3 path where you want Amazon SageMaker to store checkpoints (ie, s3://trainbucket).\n\n" +
" trainingJobName - The name of the training job. \n\n" +
" roleArn - The Amazon Resource Name (ARN) of the IAM role that Amazon SageMaker uses.\n\n" +
" s3OutputPath - The output path located in a S3 bucket (i.e., s3://trainbucket/sagemaker).\n\n" +
" channelName - The channel name \n\n" +
" trainingImage - The training image.";
if (args.length < 7) {
System.out.println(USAGE);
System.exit(1);
}
/* Read the name from command args */
String s3UriData = args[0];
String s3Uri = args[1];
String trainingJobName = args[2];
String roleArn = args[3];
String s3OutputPath = args[4];
String channelName = args[5];
String trainingImage = args[6];
Region region = Region.US_WEST_2;
SageMakerClient sageMakerClient = SageMakerClient.builder()
.region(region)
.build();
trainJob(sageMakerClient, s3UriData, s3Uri, trainingJobName, roleArn, s3OutputPath, channelName, trainingImage);
}
//snippet-start:[sagemaker.java2.train_job.main]
public static void trainJob(SageMakerClient sageMakerClient,
String s3UriData,
String s3Uri,
String trainingJobName,
String roleArn,
String s3OutputPath,
String channelName,
String trainingImage) {
try {
S3DataSource s3DataSource = S3DataSource.builder()
.s3Uri(s3UriData)
.s3DataType("S3Prefix")
.s3DataDistributionType("FullyReplicated")
.build();
DataSource dataSource = DataSource.builder()
.s3DataSource(s3DataSource)
.build();
Channel channel = Channel.builder()
.channelName(channelName)
.contentType("csv")
.dataSource(dataSource)
.build();
// Build a LIST of CHannels
List<Channel> myChannel = new ArrayList();
myChannel.add(channel);
ResourceConfig resourceConfig = ResourceConfig.builder()
.instanceType(TrainingInstanceType.ML_M5_2_XLARGE) // ml.c5.2xlarge
.instanceCount(10)
.volumeSizeInGB(1)
.build();
CheckpointConfig checkpointConfig = CheckpointConfig.builder()
.s3Uri(s3Uri)
.build();
OutputDataConfig outputDataConfig = OutputDataConfig.builder()
.s3OutputPath(s3OutputPath)
.build();
StoppingCondition stoppingCondition = StoppingCondition.builder()
.maxRuntimeInSeconds(1200)
.build();
AlgorithmSpecification algorithmSpecification = AlgorithmSpecification.builder()
.trainingImage(trainingImage)
.trainingInputMode(TrainingInputMode.FILE)
.build();
// Set hyper parameters
Map<String,String> hyperParameters = new HashMap<String, String>();
hyperParameters.put("num_round", "100");
hyperParameters.put("eta", "0.2");
hyperParameters.put("gamma", "4");
hyperParameters.put("max_depth", "5");
hyperParameters.put("min_child_weight", "6");
hyperParameters.put("objective", "binary:logistic");
hyperParameters.put("silent", "0");
hyperParameters.put("subsample", "0.8");
CreateTrainingJobRequest trainingJobRequest = CreateTrainingJobRequest.builder()
.trainingJobName(trainingJobName)
.algorithmSpecification(algorithmSpecification)
.roleArn(roleArn)
.resourceConfig(resourceConfig)
.checkpointConfig(checkpointConfig)
.inputDataConfig(myChannel)
.outputDataConfig(outputDataConfig)
.stoppingCondition(stoppingCondition)
.hyperParameters(hyperParameters)
.build();
CreateTrainingJobResponse jobResponse = sageMakerClient.createTrainingJob(trainingJobRequest);
System.out.println("The Amazon Resource Name (ARN) of the training job is "+ jobResponse.trainingJobArn());
} catch (SageMakerException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
//snippet-end:[sagemaker.java2.train_job.main]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.