# The CREATE MODEL statement for Deep Neural Network (DNN) models

## CREATE MODEL statement for DNN models

To create a Deep Neural Network model in BigQuery, use the BigQuery ML CREATE MODEL statement with the DNN_CLASSIFIER or DNN_REGRESSOR model types. These models are built using TensorFlow estimators.

## CREATE MODEL syntax

{CREATE MODEL | CREATE MODEL IF NOT EXISTS | CREATE OR REPLACE MODEL} model_name
[OPTIONS(MODEL_TYPE = { 'DNN_CLASSIFIER' | 'DNN_REGRESSOR' },
ACTIVATION_FN = { 'RELU' | 'RELU6' | 'CRELU' | 'ELU' | 'SELU' | 'SIGMOID' | 'TANH' },
AUTO_CLASS_WEIGHTS = { TRUE | FALSE },
BATCH_SIZE = int64_value,
CLASS_WEIGHTS = struct_array,
DROPOUT = float64_value,
EARLY_STOP = { TRUE | FALSE },
HIDDEN_UNITS = int_array,
LEARN_RATE = float64_value,
INPUT_LABEL_COLS = string_array,
MAX_ITERATIONS = int64_value,
MIN_REL_PROGRESS = float64_value,
)];


### CREATE MODEL

Creates a new BigQuery ML model in the specified dataset. If the model name exists, CREATE MODEL returns an error.

### CREATE MODEL IF NOT EXISTS

Creates a new BigQuery ML model only if the model does not currently exist in the specified dataset.

### CREATE OR REPLACE MODEL

Creates a new BigQuery ML model and replaces any existing model with the same name in the specified dataset.

### Model Options

BigQuery ML currently supports the following options. model_name and model_type are required; others are optional.

#### model_name

model_name is the name of the BigQuery ML model you're creating or replacing. The model name must be unique per dataset: no other model or table can have the same name. The model name must follow the same naming rules as a BigQuery table. A model name can contain the following:

• Up to 1,024 characters
• Letters of either case, numbers, and underscores

model_name is not case-sensitive.

If you do not have a default project configured, prepend the project ID to the model name in following format, including backticks:

[PROJECT_ID].[DATASET].[MODEL]

For example:

myproject.mydataset.mymodel

#### MODEL_TYPE

Syntax

MODEL_TYPE = { 'DNN_CLASSIFIER' | 'DNN_REGRESSOR' }


Description

Specifies the model type. This option is required.

#### ACTIVATION_FN

Syntax

ACTIVATION_FN =  { 'RELU' | 'RELU6' | 'CRELU' | 'ELU' | 'SELU' | 'SIGMOID' | 'TANH' }


Description

For DNN model types, specifies the activation function of the neural network.

Arguments

The following options are available:

The default value is 'RELU'.

#### AUTO_CLASS_WEIGHTS

Syntax

AUTO_CLASS_WEIGHTS = { TRUE | FALSE }


Description

Whether to balance class labels using weights for each class in inverse proportion to the frequency of that class.

Use only with the DNN_CLASSIFIER model.

By default, the training data that is used to create a multiclass DNN Classifier model is unweighted. If the labels in the training data are imbalanced, the model might learn to predict the most popular class of labels more heavily, which might not be desired.

To balance every class, use the following formula:

total_input_rows / (input_rows_for_class_n * number_of_unique_classes)


Arguments

Accepts a BOOL. The default value is FALSE.

#### BATCH_SIZE

Syntax

BATCH_SIZE = int64_value


Description

For DNN model types, specifies the mini batch size of samples that are fed to the neural network.

Arguments

A positive number that is no greater than 8192.

The default value is the smaller value of 1024 and the number of samples.

#### CLASS_WEIGHTS

Syntax

CLASS_WEIGHTS = struct_array

Description

The weights to use for each class label. This option cannot be specified if AUTO_CLASS_WEIGHTS is TRUE.

Arguments

struct_array is an ARRAY of STRUCTs; each STRUCT contains a STRING that contains the class label and a FLOAT64 containing the weight for that class label. A weight must be present for every class label. The weights are not required to add up to one. For example:

CLASS_WEIGHTS = [STRUCT('example_label', .2)]


#### DROPOUT

Syntax

DROPOUT = float64_value


Description

For DNN model types, specifies the dropout rate of units in the neural network.

Arguments

A valid value must be non-negative and no greater than 1.0. The default value is 0.0.

#### EARLY_STOP

Syntax

EARLY_STOP = { TRUE | FALSE }


Description

Whether training should stop after the first iteration in which the relative loss improvement is less than the value specified for MIN_REL_PROGRESS.

Arguments

The value is a BOOL. The default value is TRUE.

#### HIDDEN_UNITS

Syntax

HIDDEN_UNITS = int_array


Description

For DNN model types, specifies the hidden layers of the neural network.

Arguments

An array of integers that represents the architecture of the hidden layers. If not specified, BigQuery ML applies a single hidden layer that contains no more than 128 units. The number of units is calculated based on a variety of aspects, such as feature columns and number of categorical values.

Example

HIDDEN_UNITS = [256, 128, 64]


This example represents an architecture of 3 hidden layers with 256, 128, and 64 nodes respectively.

#### INPUT_LABEL_COLS

Syntax

 INPUT_LABEL_COLS = string_array 

Description

The label column name(s) in the training data.

Arguments

string_array is an ARRAY of STRINGs. The DNN model types only support string_array values that contain one element.

#### LEARN_RATE

Syntax

 LEARN_RATE = float64_value 

Description

The initial learn rate for training.

Arguments

float64_value is a FLOAT64. The default value is 0.01.

#### MAX_ITERATIONS

Syntax

 MAX_ITERATIONS = int64_value 

Description

The maximum number of training iterations or steps.

Arguments

int64_value is an INT64. The default value is 20.

#### MIN_REL_PROGRESS

Syntax

MIN_REL_PROGRESS = float64_value

Description

The minimum relative loss improvement that is necessary to continue training when EARLY_STOP is set to true. For example, a value of 0.01 specifies that each iteration must reduce the loss by 1% for training to continue.

Arguments

float64_value is a FLOAT64. The default value is 0.01.

#### OPTIMIZER

Syntax

OPTIMIZER =  { 'ADAGRAD' | 'ADAM' | 'FTRL' | 'RMSPROP' | 'SGD' }


Description

For DNN model types, specifies the optimizer for training the model.

Arguments

The following options are available:

The default value is 'ADAM'.

#### INTERNAL DEFAULT OPTIONS

BigQuery ML also uses the following default values when building DNN models internally.

loss_reduction = losses_utils.ReductionV2.SUM_OVER_BATCH_SIZE

batch_norm = False


## Example

The following example trains a DNN classifier model against 'mytable' with 'mylabel' as the label column.

CREATE MODEL project_id:mydataset.mymodel
OPTIONS(MODEL_TYPE='DNN_CLASSIFIER',
ACTIVATION_FN = 'RELU',
BATCH_SIZE = 2048,
DROPOUT = 0.1,
EARLY_STOP = FALSE,
HIDDEN_UNITS = [128, 128, 128],
INPUT_LABEL_COLS = ['mylabel'],
LEARN_RATE=0.001,
MAX_ITERATIONS = 50,