Skip to content

Commit f89ca59

Browse files
authored
samples: adds missing Node.js samples (#128)
* samples: adds missing Node.js samples
1 parent d5d8366 commit f89ca59

17 files changed

Lines changed: 1305 additions & 0 deletions
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
function main(
20+
batchPredictionDisplayName,
21+
modelId,
22+
gcsSourceUri,
23+
gcsDestinationOutputUriPrefix,
24+
project,
25+
location = 'us-central1'
26+
) {
27+
// [START aiplatform_create_batch_prediction_job_video_action_recognition]
28+
/**
29+
* TODO(developer): Uncomment these variables before running the sample.\
30+
* (Not necessary if passing values as arguments)
31+
*/
32+
33+
// const batchPredictionDisplayName = 'YOUR_BATCH_PREDICTION_DISPLAY_NAME';
34+
// const modelId = 'YOUR_MODEL_ID';
35+
// const gcsSourceUri = 'YOUR_GCS_SOURCE_URI';
36+
// const gcsDestinationOutputUriPrefix = 'YOUR_GCS_DEST_OUTPUT_URI_PREFIX';
37+
// eg. "gs://<your-gcs-bucket>/destination_path"
38+
// const project = 'YOUR_PROJECT_ID';
39+
// const location = 'YOUR_PROJECT_LOCATION';
40+
const aiplatform = require('@google-cloud/aiplatform');
41+
const {params} = aiplatform.protos.google.cloud.aiplatform.v1.schema.predict;
42+
43+
// Imports the Google Cloud Job Service Client library
44+
const {JobServiceClient} = require('@google-cloud/aiplatform').v1;
45+
46+
// Specifies the location of the api endpoint
47+
const clientOptions = {
48+
apiEndpoint: 'us-central1-aiplatform.googleapis.com',
49+
};
50+
51+
// Instantiates a client
52+
const jobServiceClient = new JobServiceClient(clientOptions);
53+
54+
async function createBatchPredictionJobVideoActionRecognition() {
55+
// Configure the parent resource
56+
const parent = `projects/${project}/locations/${location}`;
57+
const modelName = `projects/${project}/locations/${location}/models/${modelId}`;
58+
59+
// For more information on how to configure the model parameters object, see
60+
// https://cloud.google.com/ai-platform-unified/docs/predictions/batch-predictions
61+
const modelParamsObj = new params.VideoActionRecognitionPredictionParams({
62+
confidenceThreshold: 0.5,
63+
});
64+
65+
const modelParameters = modelParamsObj.toValue();
66+
67+
const inputConfig = {
68+
instancesFormat: 'jsonl',
69+
gcsSource: {uris: [gcsSourceUri]},
70+
};
71+
const outputConfig = {
72+
predictionsFormat: 'jsonl',
73+
gcsDestination: {outputUriPrefix: gcsDestinationOutputUriPrefix},
74+
};
75+
const batchPredictionJob = {
76+
displayName: batchPredictionDisplayName,
77+
model: modelName,
78+
modelParameters,
79+
inputConfig,
80+
outputConfig,
81+
};
82+
const request = {
83+
parent,
84+
batchPredictionJob,
85+
};
86+
87+
// Create batch prediction job request
88+
const [response] = await jobServiceClient.createBatchPredictionJob(request);
89+
90+
console.log(
91+
'Create batch prediction job video action recognition response'
92+
);
93+
console.log(`Name : ${response.name}`);
94+
console.log('Raw response:');
95+
console.log(JSON.stringify(response, null, 2));
96+
}
97+
createBatchPredictionJobVideoActionRecognition();
98+
// [END aiplatform_create_batch_prediction_job_video_action_recognition]
99+
}
100+
101+
process.on('unhandledRejection', err => {
102+
console.error(err.message);
103+
process.exitCode = 1;
104+
});
105+
106+
main(...process.argv.slice(2));
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
function main(
20+
displayName,
21+
containerImageUri,
22+
project,
23+
location = 'us-central1'
24+
) {
25+
// [START aiplatform_create_hyperparameter_tuning_job_sample]
26+
/**
27+
* TODO(developer): Uncomment these variables before running the sample.
28+
* (Not necessary if passing values as arguments)
29+
*/
30+
/*
31+
const displayName = 'YOUR HYPERPARAMETER TUNING JOB;
32+
const containerImageUri = 'TUNING JOB CONTAINER URI;
33+
const project = 'YOUR PROJECT ID';
34+
const location = 'us-central1';
35+
*/
36+
// Imports the Google Cloud Pipeline Service Client library
37+
const {JobServiceClient} = require('@google-cloud/aiplatform');
38+
39+
// Specifies the location of the api endpoint
40+
const clientOptions = {
41+
apiEndpoint: 'us-central1-aiplatform.googleapis.com',
42+
};
43+
44+
// Instantiates a client
45+
const jobServiceClient = new JobServiceClient(clientOptions);
46+
47+
async function createHyperParameterTuningJob() {
48+
// Configure the parent resource
49+
const parent = `projects/${project}/locations/${location}`;
50+
51+
// Create the hyperparameter tuning job configuration
52+
const hyperparameterTuningJob = {
53+
displayName,
54+
maxTrialCount: 2,
55+
parallelTrialCount: 1,
56+
maxFailedTrialCount: 1,
57+
studySpec: {
58+
metrics: [
59+
{
60+
metricId: 'accuracy',
61+
goal: 'MAXIMIZE',
62+
},
63+
],
64+
parameters: [
65+
{
66+
parameterId: 'lr',
67+
doubleValueSpec: {
68+
minValue: 0.001,
69+
maxValue: 0.1,
70+
},
71+
},
72+
],
73+
},
74+
trialJobSpec: {
75+
workerPoolSpecs: [
76+
{
77+
machineSpec: {
78+
machineType: 'n1-standard-4',
79+
acceleratorType: 'NVIDIA_TESLA_K80',
80+
acceleratorCount: 1,
81+
},
82+
replicaCount: 1,
83+
containerSpec: {
84+
imageUri: containerImageUri,
85+
command: [],
86+
args: [],
87+
},
88+
},
89+
],
90+
},
91+
};
92+
93+
const [response] = await jobServiceClient.createHyperparameterTuningJob({
94+
parent,
95+
hyperparameterTuningJob,
96+
});
97+
98+
console.log('Create hyperparameter tuning job response:');
99+
console.log(`\tDisplay name: ${response.displayName}`);
100+
console.log(`\tTuning job resource name: ${response.name}`);
101+
console.log(`\tJob status: ${response.state}`);
102+
}
103+
104+
createHyperParameterTuningJob();
105+
// [END aiplatform_create_hyperparameter_tuning_job_sample]
106+
}
107+
108+
process.on('unhandledRejection', err => {
109+
console.error(err.message);
110+
process.exitCode = 1;
111+
});
112+
113+
main(...process.argv.slice(2));
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
function main(
20+
displayName,
21+
containerImageUri,
22+
project,
23+
location = 'us-central1'
24+
) {
25+
// [START aiplatform_create_hyperparameter_tuning_job_sample]
26+
/**
27+
* TODO(developer): Uncomment these variables before running the sample.
28+
* (Not necessary if passing values as arguments)
29+
*/
30+
/*
31+
const displayName = 'YOUR HYPERPARAMETER TUNING JOB;
32+
const containerImageUri = 'TUNING JOB CONTAINER URI;
33+
const project = 'YOUR PROJECT ID';
34+
const location = 'us-central1';
35+
*/
36+
// Imports the Google Cloud Pipeline Service Client library
37+
const {JobServiceClient} = require('@google-cloud/aiplatform');
38+
39+
// Specifies the location of the api endpoint
40+
const clientOptions = {
41+
apiEndpoint: 'us-central1-aiplatform.googleapis.com',
42+
};
43+
44+
// Instantiates a client
45+
const jobServiceClient = new JobServiceClient(clientOptions);
46+
47+
async function createHyperParameterTuningJob() {
48+
// Configure the parent resource
49+
const parent = `projects/${project}/locations/${location}`;
50+
51+
// Create the hyperparameter tuning job configuration
52+
const hyperparameterTuningJob = {
53+
displayName,
54+
maxTrialCount: 2,
55+
parallelTrialCount: 1,
56+
maxFailedTrialCount: 1,
57+
studySpec: {
58+
metrics: [
59+
{
60+
metricId: 'accuracy',
61+
goal: 'MAXIMIZE',
62+
},
63+
],
64+
parameters: [
65+
{
66+
parameterId: 'lr',
67+
doubleValueSpec: {
68+
minValue: 0.001,
69+
maxValue: 0.1,
70+
},
71+
},
72+
],
73+
},
74+
trialJobSpec: {
75+
workerPoolSpecs: [
76+
{
77+
machineSpec: {
78+
machineType: 'n1-standard-4',
79+
acceleratorType: 'NVIDIA_TESLA_K80',
80+
acceleratorCount: 1,
81+
},
82+
replicaCount: 1,
83+
containerSpec: {
84+
imageUri: containerImageUri,
85+
command: [],
86+
args: [],
87+
},
88+
},
89+
],
90+
},
91+
};
92+
93+
const [response] = await jobServiceClient.createHyperparameterTuningJob({
94+
parent,
95+
hyperparameterTuningJob,
96+
});
97+
98+
console.log('Create hyperparameter tuning job response:');
99+
console.log(`\tDisplay name: ${response.displayName}`);
100+
console.log(`\tTuning job resource name: ${response.name}`);
101+
console.log(`\tJob status: ${response.state}`);
102+
}
103+
104+
createHyperParameterTuningJob();
105+
// [END aiplatform_create_hyperparameter_tuning_job_sample]
106+
}
107+
108+
process.on('unhandledRejection', err => {
109+
console.error(err.message);
110+
process.exitCode = 1;
111+
});
112+
113+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)