Example #1
0
def build_estimator(model_dir, model_type):
    """Build an estimator using official tf.estimator API.
    Args:
        model_dir: model save base directory
        model_type: one of {`wide_deep`, `deep`, `wide_deep`}
    Returns:
        model instance of tf.estimator.Estimator class
    """
    wide_columns, deep_columns = _build_model_columns()
    _build_distribution()
    # Create a tf.estimator.RunConfig to ensure the model is run on CPU, which
    # trains faster than GPU for this model.
    run_config = tf.estimator.RunConfig(**CONF.runconfig).replace(
        session_config=tf.ConfigProto(device_count={'GPU': 0}))

    if model_type == 'wide_deep':
        return tf.estimator.LinearClassifier(
            model_dir=model_dir,
            feature_columns=wide_columns,
            weight_column=weight_column,
            optimizer=tf.train.FtrlOptimizer(  # can not read from conf
                learning_rate=0.1,
                l1_regularization_strength=0.5,
                l2_regularization_strength=1),
            partitioner=None,
            config=run_config)
    elif model_type == 'deep':
        return tf.estimator.DNNClassifier(
            model_dir=model_dir,
            feature_columns=deep_columns,
            hidden_units=CONF.model["dnn_hidden_units"],
            optimizer=tf.train.ProximalAdagradOptimizer(
                learning_rate=0.1,
                l1_regularization_strength=0.1,
                l2_regularization_strength=0.1
            ),  # {'Adagrad', 'Adam', 'Ftrl', 'RMSProp', 'SGD'}
            activation_fn=activation_fn(CONF.model["dnn_activation_function"]
                                        ),  # tf.nn.relu vs 'tf.nn.relu'
            dropout=CONF.model["dnn_dropout"],
            weight_column=weight_column,
            input_layer_partitioner=None,
            config=run_config)
    else:
        return tf.estimator.DNNLinearCombinedClassifier(
            model_dir=
            model_dir,  # self._model_dir = model_dir or self._config.model_dir
            linear_feature_columns=wide_columns,
            linear_optimizer=tf.train.FtrlOptimizer(
                learning_rate=0.1,
                l1_regularization_strength=0.5,
                l2_regularization_strength=1),
            dnn_feature_columns=deep_columns,
            dnn_optimizer=tf.train.ProximalAdagradOptimizer(
                learning_rate=0.1,
                l1_regularization_strength=0.1,
                l2_regularization_strength=0.1),
            dnn_hidden_units=CONF.model["dnn_hidden_units"],
            dnn_activation_fn=activation_fn(
                CONF.model["dnn_activation_function"]),
            dnn_dropout=CONF.model["dnn_dropout"],
            n_classes=2,
            weight_column=weight_column,
            label_vocabulary=None,
            input_layer_partitioner=None,
            config=run_config)
from __future__ import print_function

import six
import tensorflow as tf
from tensorflow.python.estimator.canned import head as head_lib

import os
import sys
PACKAGE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, PACKAGE_DIR)

from lib.read_conf import Config
from lib.utils.model_util import add_layer_summary, get_optimizer_instance, activation_fn

CONF = Config().model
ACTIVATION_FN = activation_fn(CONF['dnn_activation_function'])
DROPOUT = CONF['dnn_dropout']
BATCH_NORM = CONF['dnn_batch_normalization']
DNN_L1 = CONF['dnn_l1']
DNN_L2 = CONF['dnn_l2']
regularizer_list = []

# WENQI
if DNN_L1 and DNN_L2:
    REG = tf.keras.regularizers.L1L2(l1=DNN_L1, l2=DNN_L2)
elif DNN_L1:
    REG = tf.keras.regularizers.l1(DNN_L1)
elif DNN_L2:
    REG = tf.keras.regularizers.l2(0.5 * (DNN_L2))
else:
    REG = None