【问题标题】:gcloud ml-engine APIgcloud ml-engine API
【发布时间】:2017-08-25 17:56:19
【问题描述】:

gcloud ml-engine 调用是否包含在 Python 的 google-cloud 客户端库中?我目前找不到任何文档(尽管我看到了自然语言 API)。我正在尝试通过 API 在 jupyter notebook 中复制以下命令,但没有成功:

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')

【问题讨论】:

    标签: python google-cloud-platform google-cloud-ml-engine


    【解决方案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']
    

    【讨论】:

    • 我正在尝试实施您建议的解决方案,但遇到了错误。你能建议我做错了什么吗?
    • 啊,看来我只需要在我的 json_request 周围添加 [ ]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-02
    • 2018-12-06
    • 2018-09-18
    • 1970-01-01
    • 1970-01-01
    • 2018-07-27
    相关资源
    最近更新 更多