Skip to content

Commit

Permalink
samples: adds missing Node.js samples (#128)
Browse files Browse the repository at this point in the history
* samples: adds missing Node.js samples
  • Loading branch information
telpirion authored May 20, 2021
1 parent d5d8366 commit f89ca59
Show file tree
Hide file tree
Showing 17 changed files with 1,305 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

function main(
batchPredictionDisplayName,
modelId,
gcsSourceUri,
gcsDestinationOutputUriPrefix,
project,
location = 'us-central1'
) {
// [START aiplatform_create_batch_prediction_job_video_action_recognition]
/**
* TODO(developer): Uncomment these variables before running the sample.\
* (Not necessary if passing values as arguments)
*/

// const batchPredictionDisplayName = 'YOUR_BATCH_PREDICTION_DISPLAY_NAME';
// const modelId = 'YOUR_MODEL_ID';
// const gcsSourceUri = 'YOUR_GCS_SOURCE_URI';
// const gcsDestinationOutputUriPrefix = 'YOUR_GCS_DEST_OUTPUT_URI_PREFIX';
// eg. "gs://<your-gcs-bucket>/destination_path"
// const project = 'YOUR_PROJECT_ID';
// const location = 'YOUR_PROJECT_LOCATION';
const aiplatform = require('@google-cloud/aiplatform');
const {params} = aiplatform.protos.google.cloud.aiplatform.v1.schema.predict;

// Imports the Google Cloud Job Service Client library
const {JobServiceClient} = require('@google-cloud/aiplatform').v1;

// Specifies the location of the api endpoint
const clientOptions = {
apiEndpoint: 'us-central1-aiplatform.googleapis.com',
};

// Instantiates a client
const jobServiceClient = new JobServiceClient(clientOptions);

async function createBatchPredictionJobVideoActionRecognition() {
// Configure the parent resource
const parent = `projects/${project}/locations/${location}`;
const modelName = `projects/${project}/locations/${location}/models/${modelId}`;

// For more information on how to configure the model parameters object, see
// https://cloud.google.com/ai-platform-unified/docs/predictions/batch-predictions
const modelParamsObj = new params.VideoActionRecognitionPredictionParams({
confidenceThreshold: 0.5,
});

const modelParameters = modelParamsObj.toValue();

const inputConfig = {
instancesFormat: 'jsonl',
gcsSource: {uris: [gcsSourceUri]},
};
const outputConfig = {
predictionsFormat: 'jsonl',
gcsDestination: {outputUriPrefix: gcsDestinationOutputUriPrefix},
};
const batchPredictionJob = {
displayName: batchPredictionDisplayName,
model: modelName,
modelParameters,
inputConfig,
outputConfig,
};
const request = {
parent,
batchPredictionJob,
};

// Create batch prediction job request
const [response] = await jobServiceClient.createBatchPredictionJob(request);

console.log(
'Create batch prediction job video action recognition response'
);
console.log(`Name : ${response.name}`);
console.log('Raw response:');
console.log(JSON.stringify(response, null, 2));
}
createBatchPredictionJobVideoActionRecognition();
// [END aiplatform_create_batch_prediction_job_video_action_recognition]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));
113 changes: 113 additions & 0 deletions ai-platform/snippets/create-hyperparameter-tuning-job-sample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

function main(
displayName,
containerImageUri,
project,
location = 'us-central1'
) {
// [START aiplatform_create_hyperparameter_tuning_job_sample]
/**
* TODO(developer): Uncomment these variables before running the sample.
* (Not necessary if passing values as arguments)
*/
/*
const displayName = 'YOUR HYPERPARAMETER TUNING JOB;
const containerImageUri = 'TUNING JOB CONTAINER URI;
const project = 'YOUR PROJECT ID';
const location = 'us-central1';
*/
// Imports the Google Cloud Pipeline Service Client library
const {JobServiceClient} = require('@google-cloud/aiplatform');

// Specifies the location of the api endpoint
const clientOptions = {
apiEndpoint: 'us-central1-aiplatform.googleapis.com',
};

// Instantiates a client
const jobServiceClient = new JobServiceClient(clientOptions);

async function createHyperParameterTuningJob() {
// Configure the parent resource
const parent = `projects/${project}/locations/${location}`;

// Create the hyperparameter tuning job configuration
const hyperparameterTuningJob = {
displayName,
maxTrialCount: 2,
parallelTrialCount: 1,
maxFailedTrialCount: 1,
studySpec: {
metrics: [
{
metricId: 'accuracy',
goal: 'MAXIMIZE',
},
],
parameters: [
{
parameterId: 'lr',
doubleValueSpec: {
minValue: 0.001,
maxValue: 0.1,
},
},
],
},
trialJobSpec: {
workerPoolSpecs: [
{
machineSpec: {
machineType: 'n1-standard-4',
acceleratorType: 'NVIDIA_TESLA_K80',
acceleratorCount: 1,
},
replicaCount: 1,
containerSpec: {
imageUri: containerImageUri,
command: [],
args: [],
},
},
],
},
};

const [response] = await jobServiceClient.createHyperparameterTuningJob({
parent,
hyperparameterTuningJob,
});

console.log('Create hyperparameter tuning job response:');
console.log(`\tDisplay name: ${response.displayName}`);
console.log(`\tTuning job resource name: ${response.name}`);
console.log(`\tJob status: ${response.state}`);
}

createHyperParameterTuningJob();
// [END aiplatform_create_hyperparameter_tuning_job_sample]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));
113 changes: 113 additions & 0 deletions ai-platform/snippets/create-hyperparameter-tuning-job.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

function main(
displayName,
containerImageUri,
project,
location = 'us-central1'
) {
// [START aiplatform_create_hyperparameter_tuning_job_sample]
/**
* TODO(developer): Uncomment these variables before running the sample.
* (Not necessary if passing values as arguments)
*/
/*
const displayName = 'YOUR HYPERPARAMETER TUNING JOB;
const containerImageUri = 'TUNING JOB CONTAINER URI;
const project = 'YOUR PROJECT ID';
const location = 'us-central1';
*/
// Imports the Google Cloud Pipeline Service Client library
const {JobServiceClient} = require('@google-cloud/aiplatform');

// Specifies the location of the api endpoint
const clientOptions = {
apiEndpoint: 'us-central1-aiplatform.googleapis.com',
};

// Instantiates a client
const jobServiceClient = new JobServiceClient(clientOptions);

async function createHyperParameterTuningJob() {
// Configure the parent resource
const parent = `projects/${project}/locations/${location}`;

// Create the hyperparameter tuning job configuration
const hyperparameterTuningJob = {
displayName,
maxTrialCount: 2,
parallelTrialCount: 1,
maxFailedTrialCount: 1,
studySpec: {
metrics: [
{
metricId: 'accuracy',
goal: 'MAXIMIZE',
},
],
parameters: [
{
parameterId: 'lr',
doubleValueSpec: {
minValue: 0.001,
maxValue: 0.1,
},
},
],
},
trialJobSpec: {
workerPoolSpecs: [
{
machineSpec: {
machineType: 'n1-standard-4',
acceleratorType: 'NVIDIA_TESLA_K80',
acceleratorCount: 1,
},
replicaCount: 1,
containerSpec: {
imageUri: containerImageUri,
command: [],
args: [],
},
},
],
},
};

const [response] = await jobServiceClient.createHyperparameterTuningJob({
parent,
hyperparameterTuningJob,
});

console.log('Create hyperparameter tuning job response:');
console.log(`\tDisplay name: ${response.displayName}`);
console.log(`\tTuning job resource name: ${response.name}`);
console.log(`\tJob status: ${response.state}`);
}

createHyperParameterTuningJob();
// [END aiplatform_create_hyperparameter_tuning_job_sample]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));
Loading

0 comments on commit f89ca59

Please sign in to comment.