【问题标题】:How to call Sagemaker training model endpoint API in C#如何在 C# 中调用 Sagemaker 训练模型端点 API
【发布时间】:2018-06-30 03:45:24
【问题描述】:

我已经通过 sagemaker 实现了机器学习算法。

我已经为 .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);

通过执行此操作,我收到验证错误为“1 validation error detected: Value at 'body' failed to satisfy constraint: Member must not be null

谁能指导我如何以及需要传递哪些输入数据来调用给定的 API?

编辑

我进一步尝试通过提供包含由“.gz”或“.pkl”文件写入的 MemoryStream 的主体参数来尝试,它给我的错误是:“从 AWS 解组响应时出错,HTTP 内容长度超过 5246976字节。”

编辑 2018 年 1 月 23 日

进一步我想出了错误消息

错误 - 模型服务器 - 'TypeError' 对象没有属性 'message'

谢谢

【问题讨论】:

  • 为什么这是一个sparkr 的问题??
  • 我已经使用 SparkR 训练模型洞察 Sagemaker notebook。
  • 我不是 .NET 专家,但您缺少应该保存推理输入的 Body 属性:docs.aws.amazon.com/sdkfornet/v3/apidocs/Index.html
  • 我进一步尝试通过提供包含由“.gz”文件写入的 MemoryStream 的主体参数来尝试,它给我的错误是:“错误从 AWS 解组响应”

标签: c# amazon-web-services amazon-s3 sparkr amazon-sagemaker


【解决方案1】:

后来通过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());

【讨论】:

    【解决方案2】:

    据我所知,您的请求缺少 Guy 所建议的 Body 属性和 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());
    

    关于 5246976 字节的限制,即 API 在单个请求的上下文中达到允许的最大响应正文长度。 避免这种情况的一种方法是执行多次调用,而不是传递大量项目进行预测。

    如果您使用的是 Amazon SageMaker 内置算法,您可以在以下地址查看允许的输入和输出数据格式:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-29
      • 2019-05-24
      • 2020-02-08
      • 2018-12-20
      • 2020-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多