.NET-Core GoogleCloudMlV1PredictRequest 执行方法 Returns 空响应
.NET-Core GoogleCloudMlV1PredictRequest Execture Method Returns null Response
在 .NET-Core C# 中,我使用 Googles ml API 与引擎交互。我的预测方法代码在这里
string credPath = @".\appkey.json";
var json = File.ReadAllText(credPath);
PersonalServiceAccountCred cr = JsonConvert.DeserializeObject(json);
// Create an explicit ServiceAccountCredential credential
var xCred = new ServiceAccountCredential(new
ServiceAccountCredential.Initializer(cr.ClientEmail)
{
Scopes = new [] {
CloudMachineLearningEngineService.Scope.CloudPlatform
}
}.FromPrivateKey(cr.PrivateKey));
var service = new CloudMachineLearningEngineService(new BaseClientService.Initializer {
HttpClientInitializer = xCred
});
ProjectsResource.PredictRequest req = new ProjectsResource.PredictRequest(service, new GoogleCloudMlV1PredictRequest {
HttpBody = new GoogleApiHttpBody {
Data = "{\"instances\": [{\"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 - States\"}]}"
}, "projects/{project_name}/models/census/versions/v1");
GoogleApiHttpBody body = req.Execute();
但是,我在 GoogleApiHttpBody 对象上收到此响应:
有人知道这是怎么回事吗?
检查 Google API 并看到请求通过后,我认为库可能有问题。我已经在我自己的博客上发布了详细信息:.NET-Core GoogleCloudMlV1PredictRequest Execture Method Returns null Response
发生的事情是 GoogleApiHttpBody 没有序列化 :predict 端点期望的 "instances" 对象。当我阅读流并看到此响应时,我发现了这一点:
{"error": "<strong>Missing \"instances\" field in request body</strong>: {\"httpBody\":{\"data\":\"{\\"instances\\":[{\\"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 - States\\"}]}\"}}"}
所以,我简单地改变了我的代码如下,现在我得到了正确的预测结果
string credPath = @".\appkey.json";
var json = File.ReadAllText(credPath);
PersonalServiceAccountCred cr = JsonConvert.DeserializeObject(json);
// Create an explicit ServiceAccountCredential credential
var xCred = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(cr.ClientEmail) {
Scopes = new [] {
CloudMachineLearningEngineService.Scope.CloudPlatform
}
}.FromPrivateKey(cr.PrivateKey));
var service = new CloudMachineLearningEngineService(new BaseClientService.Initializer {
HttpClientInitializer = xCred
});
ProjectsResource.PredictRequest req = new ProjectsResource.PredictRequest(service, new GoogleCloudMlV1PredictRequest(), "projects/{project-name}/models/census/versions/v1");
string requestPath = req.Service.BaseUri +
CloudMachineLearningEngineService.Version + "/" + req.Name + ":" + req.MethodName;
Task result = service.HttpClient.PostAsync(requestPath, new StringContent("{\"instances\": [{\"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 - States\"}]}"));
Task.WaitAll(result);
HttpResponseMessage responseMessage = result.Result;
Task responseStreamTask = responseMessage.Content.ReadAsStringAsync();
Task.WaitAll(responseStreamTask);
string responseText = responseStreamTask.Result;
在 .NET-Core C# 中,我使用 Googles ml API 与引擎交互。我的预测方法代码在这里
string credPath = @".\appkey.json";
var json = File.ReadAllText(credPath);
PersonalServiceAccountCred cr = JsonConvert.DeserializeObject(json);
// Create an explicit ServiceAccountCredential credential
var xCred = new ServiceAccountCredential(new
ServiceAccountCredential.Initializer(cr.ClientEmail)
{
Scopes = new [] {
CloudMachineLearningEngineService.Scope.CloudPlatform
}
}.FromPrivateKey(cr.PrivateKey));
var service = new CloudMachineLearningEngineService(new BaseClientService.Initializer {
HttpClientInitializer = xCred
});
ProjectsResource.PredictRequest req = new ProjectsResource.PredictRequest(service, new GoogleCloudMlV1PredictRequest {
HttpBody = new GoogleApiHttpBody {
Data = "{\"instances\": [{\"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 - States\"}]}"
}, "projects/{project_name}/models/census/versions/v1");
GoogleApiHttpBody body = req.Execute();
但是,我在 GoogleApiHttpBody 对象上收到此响应:
有人知道这是怎么回事吗?
检查 Google API 并看到请求通过后,我认为库可能有问题。我已经在我自己的博客上发布了详细信息:.NET-Core GoogleCloudMlV1PredictRequest Execture Method Returns null Response
发生的事情是 GoogleApiHttpBody 没有序列化 :predict 端点期望的 "instances" 对象。当我阅读流并看到此响应时,我发现了这一点:
{"error": "<strong>Missing \"instances\" field in request body</strong>: {\"httpBody\":{\"data\":\"{\\"instances\\":[{\\"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 - States\\"}]}\"}}"}
所以,我简单地改变了我的代码如下,现在我得到了正确的预测结果
string credPath = @".\appkey.json";
var json = File.ReadAllText(credPath);
PersonalServiceAccountCred cr = JsonConvert.DeserializeObject(json);
// Create an explicit ServiceAccountCredential credential
var xCred = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(cr.ClientEmail) {
Scopes = new [] {
CloudMachineLearningEngineService.Scope.CloudPlatform
}
}.FromPrivateKey(cr.PrivateKey));
var service = new CloudMachineLearningEngineService(new BaseClientService.Initializer {
HttpClientInitializer = xCred
});
ProjectsResource.PredictRequest req = new ProjectsResource.PredictRequest(service, new GoogleCloudMlV1PredictRequest(), "projects/{project-name}/models/census/versions/v1");
string requestPath = req.Service.BaseUri +
CloudMachineLearningEngineService.Version + "/" + req.Name + ":" + req.MethodName;
Task result = service.HttpClient.PostAsync(requestPath, new StringContent("{\"instances\": [{\"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 - States\"}]}"));
Task.WaitAll(result);
HttpResponseMessage responseMessage = result.Result;
Task responseStreamTask = responseMessage.Content.ReadAsStringAsync();
Task.WaitAll(responseStreamTask);
string responseText = responseStreamTask.Result;