There is indeed a way of submitting jobs to Cloud ML Engine from a Python script. You can use the Google Python API Client Library for that purpose, and in the link I shared you can have a look at a close explanation of how the API calls. There's a command-by-command explanation, and at the end an example of how to put everything together. In order to work with the library, you will have to install it first, as explained in this other page.
Then, the method you are interested in (for submitting jobs) is cloudml.projects.jobs.create()
, and you can find detailed information on how to call it in the developers page. I think you might be interested in playing around with the REST API first, in order to get familiar with how it works; you can do so through the APIs Explorer. Below there's an example of a body used to make the API call:
training_inputs = {'scaleTier': 'CUSTOM',
'masterType': 'complex_model_m',
'workerType': 'complex_model_m',
'parameterServerType': 'large_model',
'workerCount': 9,
'parameterServerCount': 3,
'packageUris': ['gs://<YOUR_TRAINER_PATH>/package-0.0.0.tar.gz'],
'pythonModule': 'trainer.task',
'args': ['--arg1', 'value1', '--arg2', 'value2'],
'region': '<REGION>',
'jobDir': 'gs://<YOUR_TRAINING_PATH>',
'runtimeVersion': '1.4'}
job_spec = {'jobId': my_job_name, 'trainingInput': training_inputs}
You should, adapt it to the specifications of your model. Once it is ready, you can have a look at this page explaining how to submit a training job using Python, but in short, it should be something like this:
from oauth2client.client import GoogleCredentials
from googleapiclient import discovery
from googleapiclient import errors
project_name = 'my_project_name'
project_id = 'projects/{}'.format(project_name)
credentials = GoogleCredentials.get_application_default()
cloudml = discovery.build('ml', 'v1', credentials=credentials)
request = cloudml.projects().jobs().create(body=job_spec, parent=project_id)
try:
response = request.execute()
# Handle a successful request
except errors.HttpError, err:
logging.error('There was an error creating the training job.'
' Check the details:')
logging.error(err._get_reason())
You should be able to run this code in order to submit a Cloud ML Engine job through a Python script.
I hope this could be of help and lighten a bit the opacity of the documentation you mentioned.
request.execute()
twice. The second one will always fail because after executing the first a job withmy_job_name
will already exist. – Unison