2017-08-25 11 views
0

クライアントライブラリにpython用のgcloud ml-engine呼び出しが含まれていますか?私は現在、それについてのドキュメントを見つけることができません(自然言語APIを参照していますが)。私は、APIを介してjupyterのノートブックで次のコマンドを複製しようとしていますし、任意の成功を持っていなかった。gcloud ml-engine API

gcloud ml-engine local predict --json-instances=XXX --model-dir=YYY 

UPDATEソリューション

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

答えて

1

/wが、私はあなたが缶探しているものと考えています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'] 
+0

私はあなたが示唆したが、エラーに遭遇したソリューションを実装しようとしています。私が間違っていることを示唆することはできますか? – reese0106

+0

json_requestの周りに[]を追加しなければならなかったようです – reese0106

関連する問題