[英]gcloud ml-engine API
python的google-cloud客户端库中是否包含gcloud ml-engine调用? 我目前找不到任何文档(尽管我看到了自然语言API)。 我正在尝试通过API在jupyter笔记本中复制以下命令,但没有成功:
gcloud ml-engine local predict --json-instances=XXX --model-dir=YYY
带有解决方案的更新
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')
我相信您要查找的内容可以在官方文档的“请求预测”部分中找到(一定要单击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']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.