简体   繁体   English

如何在C#中调用Sagemaker培训模型端点API

[英]How to call Sagemaker training model endpoint API in C#

I have implemented machine learning algorithms through sagemaker. 我通过sagemaker实现了机器学习算法。

I have installed SDK for .net, and tried by executing below code. 我已经为.net安装了SDK,并尝试执行下面的代码。

Uri sagemakerEndPointURI = new Uri("https://runtime.sagemaker.us-east-2.amazonaws.com/endpoints/MyEndpointName/invocations");
Amazon.SageMakerRuntime.Model.InvokeEndpointRequest request = new Amazon.SageMakerRuntime.Model.InvokeEndpointRequest();
request.EndpointName = "MyEndpointName";
AmazonSageMakerRuntimeClient aawsClient = new AmazonSageMakerRuntimeClient(myAwsAccessKey,myAwsSecreteKey);            
Amazon.SageMakerRuntime.Model.InvokeEndpointResponse resposnse= aawsClient.InvokeEndpoint(request);

By executing this, I am getting validation error as " 1 validation error detected: Value at 'body' failed to satisfy constraint: Member must not be null " 通过执行此操作,我收到验证错误“ 1 validation error detected: Value at 'body' failed to satisfy constraint: Member must not be null

Can anyone guide me on how and what more input data I need to pass to call the given API? 任何人都可以指导我如何以及需要传递多少输入数据来调用给定的API?

EDIT 编辑

Further I'd tried by provinding body parameter which contains a MemoryStream written by a '.gz' or '.pkl' file, and it giving me error as : "Error unmarshalling response back from AWS, HTTP content length exceeded 5246976 bytes." 此外,我尝试通过provinding body参数,其中包含由'.gz'或'.pkl'文件编写的MemoryStream,并且它给出了错误:“错误解组来自AWS的响应,HTTP内容长度超过5246976字节。”

EDIT 1/23/2018 编辑1/23/2018

Further I came up with the error message as 此外,我想出了错误消息

ERROR - model server - 'TypeError' object has no attribute 'message' 错误 - 模型服务器 - 'TypeError'对象没有属性'message'

Thanks 谢谢

As far as I can see, your request is missing both the Body property, as suggested by Guy and the ContentType which must refer to the type of input data you are passing to Amazon SageMaker (see the code below; my input CSV file contains a single example). 据我所知,您的请求缺少Body属性,如Guy和ContentType所示,它必须引用您传递给Amazon SageMaker的输入数据类型(请参阅下面的代码;我的输入CSV文件包含单个例子)。

byte[] content = File.ReadAllBytes("input.csv");
Amazon.SageMakerRuntime.Model.InvokeEndpointRequest request = new Amazon.SageMakerRuntime.Model.InvokeEndpointRequest();
request.EndpointName = "linear-learner-xxxxxxxx-xxxx";
request.ContentType = "text/csv";
request.Body = new MemoryStream(content);

AmazonSageMakerRuntimeClient awsClient = new AmazonSageMakerRuntimeClient(accessKey, secretKey);
Amazon.SageMakerRuntime.Model.InvokeEndpointResponse response = awsClient.InvokeEndpoint(request);

string predictions = Encoding.UTF8.GetString(response.Body.ToArray());

With regards to the 5246976 bytes limit, that is the API reaching the maximum allowed response body length, in the context of a single request. 关于5246976字节限制,即在单个请求的上下文中API达到允许的最大响应主体长度。 A way to avoid that is to execute multiple calls, rather than passing large batches of items for prediction. 避免这种情况的一种方法是执行多个调用,而不是传递大批项目进行预测。

If you are using Amazon SageMaker built-in algorithms, you can check the allowed data format for inputs and outputs at the following address: 如果您使用的是Amazon SageMaker内置算法,则可以在以下地址检查输入和输出的允许数据格式:

https://docs.aws.amazon.com/sagemaker/latest/dg/common-info-all-im-models.html https://docs.aws.amazon.com/sagemaker/latest/dg/common-info-all-im-models.html

Later solved it by Encoding.ASCII.GetBytes as in below code. 后来通过Encoding.ASCII.GetBytes解决了它,如下面的代码所示。

 byte[] bytes = System.IO.File.ReadAllBytes(@"EXCEL_FILE_PATH");
    string listA = "";
    while (!reader.EndOfStream)
        {
            var line = reader.ReadLine();
            listA = listA + line + "\n";
        }
    byte[] bytes = Encoding.ASCII.GetBytes(listA);
    request.Body = new MemoryStream(bytes);
    InvokeEndpointResponse response = sagemakerRunTimeClient.InvokeEndpoint(request);
    string predictions = Encoding.UTF8.GetString(response.Body.ToArray());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM