簡體   English   中英

gcloud ml-engine API

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM