gcloud ml-engine调用是否包含在python的google-cloud客户端库中?我目前找不到任何文档(虽然我看到了自然语言API)。我试图通过API在jupyter笔记本中复制以下命令,但没有取得任何成功:
gcloud ml-engine local predict --json-instances=XXX --model-dir=YYY
更新 w /解决方案
with open('test.json') as data_file:
json_request = json.load(data_file)
response = predict_json(project = PROJECT_ID,
model= 'test_model',
instances = [json_request],
version = 'v1')
答案 0 :(得分:1)
我相信您所寻找的内容可以在“请求预测”部分的official documentation中找到(请务必点击Python标签页)。
为方便起见:
def predict_json(project, model, instances, version=None):
"""Send json data to a deployed model for prediction.
Args:
project (str): project where the Cloud ML Engine Model is deployed.
model (str): model name.
instances ([Mapping[str: Any]]): Keys should be the names of Tensors
your deployed model expects as inputs. Values should be datatypes
convertible to Tensors, or (potentially nested) lists of datatypes
convertible to tensors.
version: str, version of the model to target.
Returns:
Mapping[str: any]: dictionary of prediction results defined by the
model.
"""
# Create the ML Engine service object.
# To authenticate set the environment variable
# GOOGLE_APPLICATION_CREDENTIALS=<path_to_service_account_file>
service = googleapiclient.discovery.build('ml', 'v1')
name = 'projects/{}/models/{}'.format(project, model)
if version is not None:
name += '/versions/{}'.format(version)
response = service.projects().predict(
name=name,
body={'instances': instances}
).execute()
if 'error' in response:
raise RuntimeError(response['error'])
return response['predictions']