Search in sources :

Example 1 with HyperparameterTuningJob

use of com.google.cloud.aiplatform.v1.HyperparameterTuningJob in project java-aiplatform by googleapis.

the class JobServiceClientTest method listHyperparameterTuningJobsTest.

@Test
public void listHyperparameterTuningJobsTest() throws Exception {
    HyperparameterTuningJob responsesElement = HyperparameterTuningJob.newBuilder().build();
    ListHyperparameterTuningJobsResponse expectedResponse = ListHyperparameterTuningJobsResponse.newBuilder().setNextPageToken("").addAllHyperparameterTuningJobs(Arrays.asList(responsesElement)).build();
    mockJobService.addResponse(expectedResponse);
    LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
    ListHyperparameterTuningJobsPagedResponse pagedListResponse = client.listHyperparameterTuningJobs(parent);
    List<HyperparameterTuningJob> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getHyperparameterTuningJobsList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockJobService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListHyperparameterTuningJobsRequest actualRequest = ((ListHyperparameterTuningJobsRequest) actualRequests.get(0));
    Assert.assertEquals(parent.toString(), actualRequest.getParent());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : ListHyperparameterTuningJobsPagedResponse(com.google.cloud.aiplatform.v1.JobServiceClient.ListHyperparameterTuningJobsPagedResponse) AbstractMessage(com.google.protobuf.AbstractMessage) Test(org.junit.Test)

Example 2 with HyperparameterTuningJob

use of com.google.cloud.aiplatform.v1.HyperparameterTuningJob in project java-aiplatform by googleapis.

the class GetHyperparameterTuningJobSample method getHyperparameterTuningJobSample.

static void getHyperparameterTuningJobSample(String project, String hyperparameterTuningJobId) throws IOException {
    JobServiceSettings settings = JobServiceSettings.newBuilder().setEndpoint("us-central1-aiplatform.googleapis.com:443").build();
    String location = "us-central1";
    // the "close" method on the client to safely clean up any remaining background resources.
    try (JobServiceClient client = JobServiceClient.create(settings)) {
        HyperparameterTuningJobName name = HyperparameterTuningJobName.of(project, location, hyperparameterTuningJobId);
        HyperparameterTuningJob response = client.getHyperparameterTuningJob(name);
        System.out.format("response: %s\n", response);
    }
}
Also used : JobServiceSettings(com.google.cloud.aiplatform.v1.JobServiceSettings) HyperparameterTuningJob(com.google.cloud.aiplatform.v1.HyperparameterTuningJob) HyperparameterTuningJobName(com.google.cloud.aiplatform.v1.HyperparameterTuningJobName) JobServiceClient(com.google.cloud.aiplatform.v1.JobServiceClient)

Example 3 with HyperparameterTuningJob

use of com.google.cloud.aiplatform.v1.HyperparameterTuningJob in project java-aiplatform by googleapis.

the class CreateHyperparameterTuningJobSample method createHyperparameterTuningJobSample.

static void createHyperparameterTuningJobSample(String project, String displayName, String containerImageUri) throws IOException {
    JobServiceSettings settings = JobServiceSettings.newBuilder().setEndpoint("us-central1-aiplatform.googleapis.com:443").build();
    String location = "us-central1";
    // the "close" method on the client to safely clean up any remaining background resources.
    try (JobServiceClient client = JobServiceClient.create(settings)) {
        StudySpec.MetricSpec metric0 = StudySpec.MetricSpec.newBuilder().setMetricId("accuracy").setGoal(StudySpec.MetricSpec.GoalType.MAXIMIZE).build();
        StudySpec.ParameterSpec.DoubleValueSpec doubleValueSpec = StudySpec.ParameterSpec.DoubleValueSpec.newBuilder().setMinValue(0.001).setMaxValue(0.1).build();
        StudySpec.ParameterSpec parameter0 = StudySpec.ParameterSpec.newBuilder().setParameterId("lr").setDoubleValueSpec(doubleValueSpec).build();
        StudySpec studySpec = StudySpec.newBuilder().addMetrics(metric0).addParameters(parameter0).build();
        MachineSpec machineSpec = MachineSpec.newBuilder().setMachineType("n1-standard-4").setAcceleratorType(AcceleratorType.NVIDIA_TESLA_K80).setAcceleratorCount(1).build();
        ContainerSpec containerSpec = ContainerSpec.newBuilder().setImageUri(containerImageUri).build();
        WorkerPoolSpec workerPoolSpec0 = WorkerPoolSpec.newBuilder().setMachineSpec(machineSpec).setReplicaCount(1).setContainerSpec(containerSpec).build();
        CustomJobSpec trialJobSpec = CustomJobSpec.newBuilder().addWorkerPoolSpecs(workerPoolSpec0).build();
        HyperparameterTuningJob hyperparameterTuningJob = HyperparameterTuningJob.newBuilder().setDisplayName(displayName).setMaxTrialCount(2).setParallelTrialCount(1).setMaxFailedTrialCount(1).setStudySpec(studySpec).setTrialJobSpec(trialJobSpec).build();
        LocationName parent = LocationName.of(project, location);
        HyperparameterTuningJob response = client.createHyperparameterTuningJob(parent, hyperparameterTuningJob);
        System.out.format("response: %s\n", response);
        System.out.format("Name: %s\n", response.getName());
    }
}
Also used : JobServiceSettings(com.google.cloud.aiplatform.v1.JobServiceSettings) StudySpec(com.google.cloud.aiplatform.v1.StudySpec) ContainerSpec(com.google.cloud.aiplatform.v1.ContainerSpec) JobServiceClient(com.google.cloud.aiplatform.v1.JobServiceClient) CustomJobSpec(com.google.cloud.aiplatform.v1.CustomJobSpec) MachineSpec(com.google.cloud.aiplatform.v1.MachineSpec) LocationName(com.google.cloud.aiplatform.v1.LocationName) HyperparameterTuningJob(com.google.cloud.aiplatform.v1.HyperparameterTuningJob) WorkerPoolSpec(com.google.cloud.aiplatform.v1.WorkerPoolSpec)

Example 4 with HyperparameterTuningJob

use of com.google.cloud.aiplatform.v1.HyperparameterTuningJob in project java-aiplatform by googleapis.

the class JobServiceClientTest method listHyperparameterTuningJobsTest2.

@Test
public void listHyperparameterTuningJobsTest2() throws Exception {
    HyperparameterTuningJob responsesElement = HyperparameterTuningJob.newBuilder().build();
    ListHyperparameterTuningJobsResponse expectedResponse = ListHyperparameterTuningJobsResponse.newBuilder().setNextPageToken("").addAllHyperparameterTuningJobs(Arrays.asList(responsesElement)).build();
    mockJobService.addResponse(expectedResponse);
    String parent = "parent-995424086";
    ListHyperparameterTuningJobsPagedResponse pagedListResponse = client.listHyperparameterTuningJobs(parent);
    List<HyperparameterTuningJob> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getHyperparameterTuningJobsList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockJobService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListHyperparameterTuningJobsRequest actualRequest = ((ListHyperparameterTuningJobsRequest) actualRequests.get(0));
    Assert.assertEquals(parent, actualRequest.getParent());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : ListHyperparameterTuningJobsPagedResponse(com.google.cloud.aiplatform.v1.JobServiceClient.ListHyperparameterTuningJobsPagedResponse) AbstractMessage(com.google.protobuf.AbstractMessage) Test(org.junit.Test)

Example 5 with HyperparameterTuningJob

use of com.google.cloud.aiplatform.v1.HyperparameterTuningJob in project java-aiplatform by googleapis.

the class CreateHyperparameterTuningJobPythonPackageSample method createHyperparameterTuningJobPythonPackageSample.

static void createHyperparameterTuningJobPythonPackageSample(String project, String displayName, String executorImageUri, String packageUri, String pythonModule) throws IOException {
    JobServiceSettings settings = JobServiceSettings.newBuilder().setEndpoint("us-central1-aiplatform.googleapis.com:443").build();
    String location = "us-central1";
    // the "close" method on the client to safely clean up any remaining background resources.
    try (JobServiceClient client = JobServiceClient.create(settings)) {
        // study spec
        MetricSpec metric = MetricSpec.newBuilder().setMetricId("val_rmse").setGoal(GoalType.MINIMIZE).build();
        // decay
        DoubleValueSpec doubleValueSpec = DoubleValueSpec.newBuilder().setMinValue(1e-07).setMaxValue(1).build();
        ParameterSpec parameterDecaySpec = ParameterSpec.newBuilder().setParameterId("decay").setDoubleValueSpec(doubleValueSpec).setScaleType(ScaleType.UNIT_LINEAR_SCALE).build();
        Double[] decayValues = { 32.0, 64.0 };
        DiscreteValueCondition discreteValueDecay = DiscreteValueCondition.newBuilder().addAllValues(Arrays.asList(decayValues)).build();
        ConditionalParameterSpec conditionalParameterDecay = ConditionalParameterSpec.newBuilder().setParameterSpec(parameterDecaySpec).setParentDiscreteValues(discreteValueDecay).build();
        // learning rate
        ParameterSpec parameterLearningSpec = ParameterSpec.newBuilder().setParameterId("learning_rate").setDoubleValueSpec(// Use the same min/max as for decay
        doubleValueSpec).setScaleType(ScaleType.UNIT_LINEAR_SCALE).build();
        Double[] learningRateValues = { 4.0, 8.0, 16.0 };
        DiscreteValueCondition discreteValueLearning = DiscreteValueCondition.newBuilder().addAllValues(Arrays.asList(learningRateValues)).build();
        ConditionalParameterSpec conditionalParameterLearning = ConditionalParameterSpec.newBuilder().setParameterSpec(parameterLearningSpec).setParentDiscreteValues(discreteValueLearning).build();
        // batch size
        Double[] batchSizeValues = { 4.0, 8.0, 16.0, 32.0, 64.0, 128.0 };
        DiscreteValueSpec discreteValueSpec = DiscreteValueSpec.newBuilder().addAllValues(Arrays.asList(batchSizeValues)).build();
        ParameterSpec parameter = ParameterSpec.newBuilder().setParameterId("batch_size").setDiscreteValueSpec(discreteValueSpec).setScaleType(ScaleType.UNIT_LINEAR_SCALE).addConditionalParameterSpecs(conditionalParameterDecay).addConditionalParameterSpecs(conditionalParameterLearning).build();
        // trial_job_spec
        MachineSpec machineSpec = MachineSpec.newBuilder().setMachineType("n1-standard-4").setAcceleratorType(AcceleratorType.NVIDIA_TESLA_K80).setAcceleratorCount(1).build();
        PythonPackageSpec pythonPackageSpec = PythonPackageSpec.newBuilder().setExecutorImageUri(executorImageUri).addPackageUris(packageUri).setPythonModule(pythonModule).build();
        WorkerPoolSpec workerPoolSpec = WorkerPoolSpec.newBuilder().setMachineSpec(machineSpec).setReplicaCount(1).setPythonPackageSpec(pythonPackageSpec).build();
        StudySpec studySpec = StudySpec.newBuilder().addMetrics(metric).addParameters(parameter).setAlgorithm(StudySpec.Algorithm.RANDOM_SEARCH).build();
        CustomJobSpec trialJobSpec = CustomJobSpec.newBuilder().addWorkerPoolSpecs(workerPoolSpec).build();
        // hyperparameter_tuning_job
        HyperparameterTuningJob hyperparameterTuningJob = HyperparameterTuningJob.newBuilder().setDisplayName(displayName).setMaxTrialCount(4).setParallelTrialCount(2).setStudySpec(studySpec).setTrialJobSpec(trialJobSpec).build();
        LocationName parent = LocationName.of(project, location);
        HyperparameterTuningJob response = client.createHyperparameterTuningJob(parent, hyperparameterTuningJob);
        System.out.format("response: %s\n", response);
        System.out.format("Name: %s\n", response.getName());
    }
}
Also used : JobServiceSettings(com.google.cloud.aiplatform.v1.JobServiceSettings) StudySpec(com.google.cloud.aiplatform.v1.StudySpec) DoubleValueSpec(com.google.cloud.aiplatform.v1.StudySpec.ParameterSpec.DoubleValueSpec) ParameterSpec(com.google.cloud.aiplatform.v1.StudySpec.ParameterSpec) ConditionalParameterSpec(com.google.cloud.aiplatform.v1.StudySpec.ParameterSpec.ConditionalParameterSpec) MetricSpec(com.google.cloud.aiplatform.v1.StudySpec.MetricSpec) JobServiceClient(com.google.cloud.aiplatform.v1.JobServiceClient) CustomJobSpec(com.google.cloud.aiplatform.v1.CustomJobSpec) MachineSpec(com.google.cloud.aiplatform.v1.MachineSpec) ConditionalParameterSpec(com.google.cloud.aiplatform.v1.StudySpec.ParameterSpec.ConditionalParameterSpec) PythonPackageSpec(com.google.cloud.aiplatform.v1.PythonPackageSpec) LocationName(com.google.cloud.aiplatform.v1.LocationName) DiscreteValueCondition(com.google.cloud.aiplatform.v1.StudySpec.ParameterSpec.ConditionalParameterSpec.DiscreteValueCondition) HyperparameterTuningJob(com.google.cloud.aiplatform.v1.HyperparameterTuningJob) DiscreteValueSpec(com.google.cloud.aiplatform.v1.StudySpec.ParameterSpec.DiscreteValueSpec) WorkerPoolSpec(com.google.cloud.aiplatform.v1.WorkerPoolSpec)

Aggregations

HyperparameterTuningJob (com.google.cloud.aiplatform.v1.HyperparameterTuningJob)3 JobServiceClient (com.google.cloud.aiplatform.v1.JobServiceClient)3 JobServiceSettings (com.google.cloud.aiplatform.v1.JobServiceSettings)3 CustomJobSpec (com.google.cloud.aiplatform.v1.CustomJobSpec)2 ListHyperparameterTuningJobsPagedResponse (com.google.cloud.aiplatform.v1.JobServiceClient.ListHyperparameterTuningJobsPagedResponse)2 LocationName (com.google.cloud.aiplatform.v1.LocationName)2 MachineSpec (com.google.cloud.aiplatform.v1.MachineSpec)2 StudySpec (com.google.cloud.aiplatform.v1.StudySpec)2 WorkerPoolSpec (com.google.cloud.aiplatform.v1.WorkerPoolSpec)2 AbstractMessage (com.google.protobuf.AbstractMessage)2 Test (org.junit.Test)2 ContainerSpec (com.google.cloud.aiplatform.v1.ContainerSpec)1 HyperparameterTuningJobName (com.google.cloud.aiplatform.v1.HyperparameterTuningJobName)1 PythonPackageSpec (com.google.cloud.aiplatform.v1.PythonPackageSpec)1 MetricSpec (com.google.cloud.aiplatform.v1.StudySpec.MetricSpec)1 ParameterSpec (com.google.cloud.aiplatform.v1.StudySpec.ParameterSpec)1 ConditionalParameterSpec (com.google.cloud.aiplatform.v1.StudySpec.ParameterSpec.ConditionalParameterSpec)1 DiscreteValueCondition (com.google.cloud.aiplatform.v1.StudySpec.ParameterSpec.ConditionalParameterSpec.DiscreteValueCondition)1 DiscreteValueSpec (com.google.cloud.aiplatform.v1.StudySpec.ParameterSpec.DiscreteValueSpec)1 DoubleValueSpec (com.google.cloud.aiplatform.v1.StudySpec.ParameterSpec.DoubleValueSpec)1