【问题标题】:How do I get online predictions in C# for my model on Cloud Machine Learning Engine?如何在 C# 中为 Cloud Machine Learning Engine 上的模型获取在线预测?
【发布时间】:2017-07-21 16:11:41
【问题描述】:

我已成功在 Cloud ML Engine 上部署模型,并通过关注 the instructions 验证它与 gcloud ml-engine models predict 一起使用,现在我想从我的 C# 应用程序向它发送预测。我该怎么做?

【问题讨论】:

    标签: c# google-cloud-ml-engine


    【解决方案1】:

    在线预测 API 是一个 REST API,因此您可以使用任何库来发送 HTTPS 请求,但您需要使用 Google's OAuth 库来获取您的凭据。

    请求的格式是JSON,如docs中所述。

    为了举例说明,请考虑Census 示例。其客户端可能如下所示:

    using System;
    using System.Collections.Generic;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Text;
    using System.Threading.Tasks;
    using Google.Apis.Auth.OAuth2;
    using Newtonsoft.Json;
    
    namespace prediction_client
    {
        class Person
        {
            public int age { get; set; }
            public String workclass { get; set; }
            public String education { get; set; }
            public int education_num { get; set; }
            public string marital_status { get; set; }
            public string occupation { get; set; }
            public string relationship { get; set; }
            public string race { get; set; }
            public string gender { get; set; }
            public int capital_gain { get; set; }
            public int capital_loss { get; set; }
            public int hours_per_week { get; set; }
            public string native_country { get; set; }
        }
    
        class Prediction
        {
            public List<Double> probabilities { get; set; }
            public List<Double> logits { get; set; }
            public Int32 classes { get; set; }
            public List<Double> logistic { get; set; }
    
            public override string ToString()
            {
                return JsonConvert.SerializeObject(this);
            }
        }
    
        class MainClass
        {
            static PredictClient client = new PredictClient();
            static String project = "MY_PROJECT";
            static String model = "census";  // Whatever you deployed your model as
    
            public static void Main(string[] args)
            {
                RunAsync().Wait();
            }
    
            static async Task RunAsync()
            {
                try
                {
                    Person person = new Person
                    {
                        age = 25,
                        workclass = " Private",
                        education = " 11th",
                        education_num = 7,
                        marital_status = " Never - married",
                        occupation = " Machine - op - inspct",
                        relationship = " Own - child",
                        race = " Black",
                        gender = " Male",
                        capital_gain = 0,
                        capital_loss = 0,
                        hours_per_week = 40,
                        native_country = " United - Stats"
                    };
                    var instances = new List<Person> { person };
    
                    List<Prediction> predictions = await client.Predict<Person, Prediction>(project, model, instances);
                    Console.WriteLine(String.Join("\n", predictions));
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }
    
        class PredictClient {
    
            private HttpClient client;
    
            public PredictClient() 
            {
                this.client = new HttpClient();
                client.BaseAddress = new Uri("https://ml.googleapis.com/v1/");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            }        
    
            public async Task<List<O>> Predict<I, O>(String project, String model, List<I> instances, String version = null)
            {
                var version_suffix = version == null ? "" : $"/version/{version}";
                var model_uri = $"projects/{project}/models/{model}{version_suffix}";
                var predict_uri = $"{model_uri}:predict";
    
                GoogleCredential credential = await GoogleCredential.GetApplicationDefaultAsync();
                var bearer_token = await credential.UnderlyingCredential.GetAccessTokenForRequestAsync();
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearer_token);
    
                var request = new { instances = instances };
                var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
    
                var responseMessage = await client.PostAsync(predict_uri, content);
                responseMessage.EnsureSuccessStatusCode();
    
                var responseBody = await responseMessage.Content.ReadAsStringAsync();
                dynamic response = JsonConvert.DeserializeObject(responseBody);
    
                return response.predictions.ToObject<List<O>>();
            }
        }
    }
    

    如果您还没有在本地运行之前,您可能需要运行 gcloud auth login 来初始化您的凭据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-22
      • 2018-06-23
      相关资源
      最近更新 更多