简体   繁体   中英

How do I create a Cloud Machine Learning Engine Job on GCP in Java?

I am unable to find any source code that illustrates the basic setup for a GCP Cloud ML engine job in Java. All I am able to find is Python examples. Where can I get started? Thanks

What's your goal? Creating a training job or a prediction job?

I can't give you the full answer as I also couldn't find the resources you need, but perhaps it's enough to get you started.

All Google Cloud REST APIs have autogenerated client libraries for all of their methods. That's what @Guoqing Xu linked for you. They are very similar in how you interact with them.

  1. For compute engine, you can have a look here, at the very bottom of the page is an example (also in Java) on how to perform a request using such library:

https://cloud.google.com/compute/docs/reference/rest/v1/instances/get#examples

  1. Here is a similar reference for ML Engine:

https://cloud.google.com/ml-engine/reference/rest/v1/projects.jobs/get

Sadly, here you don't have the examples at the bottom.

  1. Javadoc: https://developers.google.com/resources/api-libraries/documentation/ml/v1/java/latest/

Library: https://developers.google.com/api-client-library/java/apis/ml/v1

You could glue these together. I would start from the compute engine example, leave the authentication/credentials the same, add some imports, and replace the builder and the method, see bellow.

The sample most definitely won't work. But I hope it sets you in the right direction :)

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.ml.v1.CloudMachineLearningEngine;  // perhaps without v1? not sure
import com.google.api.services.ml.v1.model.GoogleCloudMlV1Job; // perhaps without v1? not sure
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Arrays;

public class MLEngineExample {
  public static void main(String args[]) throws IOException, GeneralSecurityException {
    // Job Name for this request.
    String name = "job-name"; // TODO: Update placeholder value.

    CloudMachineLearningEngine mlEngineService = createMLEngineService();
    CloudMachineLearningEngine.Projects.Jobs request = mlEngineService.projects().jobs().get(name);

    GoogleCloudMlV1Job response = request.execute();

    // TODO: Change code below to process the `response` object:
    System.out.println(response);
  }

  public static CloudMachineLearningEngine createMLEngineService() throws IOException, GeneralSecurityException {
    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();

    GoogleCredential credential = GoogleCredential.getApplicationDefault();
    if (credential.createScopedRequired()) {
      credential =
          credential.createScoped(Arrays.asList("https://www.googleapis.com/auth/cloud-platform"));
    }

    return new CloudMachineLearningEngine.Builder(httpTransport, jsonFactory, credential)
        .setApplicationName("Google-MlEngineSample/0.1")
        .build();
  }
}

So, one thing is to interact with the Cloud Machine Learning Engine APIs and one is to build a model that can be trained with Cloud Machine Learning Engine.

The first can be done in Java, as it is simply an API you can use, the latter require you to provide a TensorFlow model to train. It is true that Tensorflow offers Java bindings , but these are mostly to use a pre-existing TensorFlow graph within your application, not to develop one (albeit possible). If you want to construct a graph, you essentially have to use Python as the number of available features is much greater than Java's.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM