Model

Model

Model objects are returned by methods such as Dataset#model and Dataset#getModels.

Constructor

new Model(dataset, id)

Parameters:
Name Type Description
dataset Dataset

Dataset instance.

id string

The ID of the model.

Example
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('my-dataset');

const model = dataset.model('my-model');

Methods

createExtractJob(destination, optionsopt, callbackopt)

Export model to Cloud Storage.

Parameters:
Name Type Attributes Description
destination string | File

Where the model should be exported to. A string or File object.

options object <optional>

The configuration object.

Properties
Name Type Attributes Description
format string <optional>

The format to export the data in. Allowed options are "ML_TF_SAVED_MODEL" or "ML_XGBOOST_BOOSTER". Default: "ML_TF_SAVED_MODEL".

jobId string <optional>

Custom job id.

jobPrefix string <optional>

Prefix to apply to the job id.

callback function <optional>

The callback function.

Properties
Name Type Attributes Description
err error <nullable>

An error returned while making this request.

job Job

The job used to export the model.

apiResponse object

The full API response.

See:
Throws:

If a destination isn't a string or File object.

Type
Error
Example
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('my-dataset');
const model = dataset.model('my-model');

const extractedModel = 'gs://my-bucket/extracted-model';

function callback(err, job, apiResponse) {
  // `job` is a Job object that can be used to check the status of the
  // request.
}

//-
// To use the default options, just pass a string or a File
object.
//
// Note: The default format is 'ML_TF_SAVED_MODEL'.
//-
model.createExtractJob(extractedModel, callback);

//-
// If you need more customization, pass an `options` object.
//-
const options = {
  format: 'ML_TF_SAVED_MODEL',
  jobId: '123abc'
};

model.createExtractJob(extractedModel, options, callback);

//-
// If the callback is omitted, we'll return a Promise.
//-
model.createExtractJob(extractedModel, options).then((data) => {
  const job = data[0];
  const apiResponse = data[1];
});

delete(callbackopt) → {Promise}

Delete the model.

Parameters:
Name Type Attributes Description
callback function <optional>

The callback function.

Properties
Name Type Attributes Description
err error <nullable>

An error returned while making this request.

apiResponse object

The full API response.

Returns:
Type Description
Promise
See:
Examples
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('my-dataset');
const model = dataset.model('my-model');

model.delete((err, apiResponse) => {});

If the callback is omitted we'll return a Promise.

const [apiResponse] = await model.delete();

exists(callbackopt) → {Promise}

Check if the model exists.

Parameters:
Name Type Attributes Description
callback function <optional>

The callback function.

Properties
Name Type Attributes Description
err error <nullable>

An error returned while making this request.

exists boolean

Whether the model exists or not.

Returns:
Type Description
Promise
Examples
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('my-dataset');
const model = dataset.model('my-model');

model.exists((err, exists) => {});

If the callback is omitted we'll return a Promise.

const [exists] = await model.exists();

extract(destination, optionsopt, callbackopt) → {Promise}

Export model to Cloud Storage.

Parameters:
Name Type Attributes Description
destination string | File

Where the model should be exported to. A string or File object.

options object <optional>

The configuration object.

Properties
Name Type Attributes Description
format string <optional>

The format to export the data in. Allowed options are "ML_TF_SAVED_MODEL" or "ML_XGBOOST_BOOSTER". Default: "ML_TF_SAVED_MODEL".

jobId string <optional>

Custom id for the underlying job.

jobPrefix string <optional>

Prefix to apply to the underlying job id.

callback function <optional>

The callback function.

Properties
Name Type Attributes Description
err error <nullable>

An error returned while making this request

apiResponse object

The full API response.

Returns:
Type Description
Promise
Throws:

If destination isn't a string or File object.

Type
Error
Example
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('my-dataset');
const model = dataset.model('my-model');

const extractedModel = 'gs://my-bucket/extracted-model';


//-
function callback(err, job, apiResponse) {
  // `job` is a Job object that can be used to check the status of the
  // request.
}

//-
// To use the default options, just pass a string or a File
object.
//
// Note: The default format is 'ML_TF_SAVED_MODEL'.
//-
model.createExtractJob(extractedModel, callback);

//-
// If you need more customization, pass an `options` object.
//-
const options = {
  format: 'ML_TF_SAVED_MODEL',
  jobId: '123abc'
};

model.createExtractJob(extractedModel, options, callback);

//-
// If the callback is omitted, we'll return a Promise.
//-
model.createExtractJob(extractedModel, options).then((data) => {
  const job = data[0];
  const apiResponse = data[1];
});

get:(callbackopt) → {Promise}

Get a model if it exists.

Parameters:
Name Type Attributes Description
callback function <optional>

The callback function.

Properties
Name Type Attributes Description
err error <nullable>

An error returned while making this request.

model Model

The Model.

apiResponse object

The full API response.

Returns:
Type Description
Promise
See:
Examples
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('my-dataset');
const model = dataset.model('my-model');

model.get(err => {
  if (!err) {
    // `model.metadata` has been populated.
  }
});

If the callback is omitted we'll return a Promise.

await model.get();

getMetadata(callbackopt) → {Promise}

Return the metadata associated with the model.

Parameters:
Name Type Attributes Description
callback function <optional>

The callback function.

Properties
Name Type Attributes Description
err error <nullable>

An error returned while making this request.

metadata object

The metadata of the model.

apiResponse object

The full API response.

Returns:
Type Description
Promise
See:
Examples
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('my-dataset');
const model = dataset.model('my-model');

model.getMetadata((err, metadata, apiResponse) => {});

If the callback is omitted we'll return a Promise.

const [metadata, apiResponse] = await model.getMetadata();

setMetadata(metadata, callbackopt) → {Promise}

Parameters:
Name Type Attributes Description
metadata object

The metadata key/value object to set.

callback function <optional>

The callback function.

Properties
Name Type Attributes Description
err error <nullable>

An error returned while making this request.

metadata object

The updated metadata of the model.

apiResponse object

The full API response.

Returns:
Type Description
Promise
See:
Examples
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('my-dataset');
const model = dataset.model('my-model');

const metadata = {
  friendlyName: 'TheBestModelEver'
};

model.setMetadata(metadata, (err, metadata, apiResponse) => {});

If the callback is omitted we'll return a Promise.

const [metadata, apiResponse] = await model.setMetadata(metadata);