easy_rec.python.model

easy_rec.python.model.easy_rec_model

class easy_rec.python.model.easy_rec_model.EasyRecModel(model_config, feature_configs, features, labels=None, is_training=False)[source]

Bases: object

__init__(model_config, feature_configs, features, labels=None, is_training=False)[source]
build_backbone_network()[source]
property has_backbone
property backbone
property embedding_regularization
property kd
property feature_groups
property l2_regularization
build_input_layer(model_config, feature_configs)[source]
abstract build_predict_graph()[source]
abstract build_loss_graph()[source]
build_metric_graph(eval_config)[source]
abstract get_outputs()[source]
build_output_dict()[source]

For exporting: get standard output nodes.

build_feature_output_dict()[source]

For exporting: get output feature nodes.

classmethod create_class(name)
build_rtp_output_dict()[source]

For exporting: get output nodes for RTP infering.

restore(ckpt_path, include_global_step=False, ckpt_var_map_path='', force_restore_shape_compatible=False)[source]

Restore variables from ckpt_path.

steps:
  1. list the variables in graph that need to be restored

  2. inspect checkpoint and find the variables that could restore from checkpoint substitute scope names in case necessary

  3. call tf.train.init_from_checkpoint to restore the variables

Parameters:
  • ckpt_path – checkpoint path to restore from

  • include_global_step – whether to restore global_step variable

  • ckpt_var_map_path – variable map from graph variables to variables in a checkpoint each line consists of: variable name in graph variable name in ckpt

  • force_restore_shape_compatible – if variable shape is incompatible, clip or pad variables in checkpoint, and then restore

Returns:

IncompatibleShapeRestoreHook if force_shape_compatible else None

get_restore_filter()[source]

Get restore variable filter.

Returns:

type of Filter in restore_filter.py scope_drop: type of ScopeDrop in restore_filter.py

Return type:

filter

get_grouped_vars(opt_num)[source]

Group the vars into different optimization groups.

Each group will be optimized by a separate optimizer.

Parameters:

opt_num – number of optimizers from easyrec config.

Returns:

list of list of variables.

easy_rec.python.model.rank_model

class easy_rec.python.model.rank_model.RankModel(model_config, feature_configs, features, labels=None, is_training=False)[source]

Bases: EasyRecModel

__init__(model_config, feature_configs, features, labels=None, is_training=False)[source]
build_predict_graph()[source]
build_rtp_output_dict()[source]

Forward tensor as rank_predict, which is a special node for RTP.

build_loss_graph()[source]
build_metric_graph(eval_config)[source]
classmethod create_class(name)
get_outputs()[source]

easy_rec.python.model.fm

class easy_rec.python.model.fm.FM(model_config, feature_configs, features, labels=None, is_training=False)[source]

Bases: RankModel

__init__(model_config, feature_configs, features, labels=None, is_training=False)[source]
build_input_layer(model_config, feature_configs)[source]
build_predict_graph()[source]
classmethod create_class(name)

easy_rec.python.model.wide_and_deep

class easy_rec.python.model.wide_and_deep.WideAndDeep(model_config, feature_configs, features, labels=None, is_training=False)[source]

Bases: RankModel

__init__(model_config, feature_configs, features, labels=None, is_training=False)[source]
build_input_layer(model_config, feature_configs)[source]
build_predict_graph()[source]
classmethod create_class(name)
get_grouped_vars(opt_num)[source]

Group the vars into different optimization groups.

Each group will be optimized by a separate optimizer.

Parameters:

opt_num – number of optimizers from easyrec config.

Returns:

list of list of variables.

easy_rec.python.model.deepfm

class easy_rec.python.model.deepfm.DeepFM(model_config, feature_configs, features, labels=None, is_training=False)[source]

Bases: RankModel

__init__(model_config, feature_configs, features, labels=None, is_training=False)[source]
build_input_layer(model_config, feature_configs)[source]
build_predict_graph()[source]
classmethod create_class(name)
build_feature_output_dict()[source]

For exporting: get output feature nodes.

easy_rec.python.model.multi_tower

class easy_rec.python.model.multi_tower.MultiTower(model_config, feature_configs, features, labels=None, is_training=False)[source]

Bases: RankModel

__init__(model_config, feature_configs, features, labels=None, is_training=False)[source]
build_predict_graph()[source]
classmethod create_class(name)

easy_rec.python.model.dcn

class easy_rec.python.model.dcn.DCN(model_config, feature_configs, features, labels=None, is_training=False)[source]

Bases: RankModel

__init__(model_config, feature_configs, features, labels=None, is_training=False)[source]
build_predict_graph()[source]
classmethod create_class(name)

easy_rec.python.model.autoint

class easy_rec.python.model.autoint.AutoInt(model_config, feature_configs, features, labels=None, is_training=False)[source]

Bases: RankModel

__init__(model_config, feature_configs, features, labels=None, is_training=False)[source]
build_predict_graph()[source]
classmethod create_class(name)

easy_rec.python.model.dbmtl

class easy_rec.python.model.dbmtl.DBMTL(model_config, feature_configs, features, labels=None, is_training=False)[source]

Bases: MultiTaskModel

__init__(model_config, feature_configs, features, labels=None, is_training=False)[source]
build_predict_graph()[source]
classmethod create_class(name)

easy_rec.python.model.multi_tower_bst

class easy_rec.python.model.multi_tower_bst.MultiTowerBST(model_config, feature_configs, features, labels=None, is_training=False)[source]

Bases: RankModel

__init__(model_config, feature_configs, features, labels=None, is_training=False)[source]
dnn_net(net, dnn_units, name)[source]
attention_net(net, dim, cur_seq_len, seq_size, name)[source]
multi_head_att_net(id_cols, head_count, emb_dim, seq_len, seq_size)[source]
add_and_norm(net_1, net_2, emb_dim, name)[source]
bst(bst_fea, seq_size, head_count, name)[source]
build_predict_graph()[source]
classmethod create_class(name)

easy_rec.python.model.multi_tower_din

class easy_rec.python.model.multi_tower_din.MultiTowerDIN(model_config, feature_configs, features, labels=None, is_training=False)[source]

Bases: RankModel

__init__(model_config, feature_configs, features, labels=None, is_training=False)[source]
din(dnn_config, deep_fea, name)[source]
build_predict_graph()[source]
classmethod create_class(name)

easy_rec.python.model.dssm

class easy_rec.python.model.dssm.DSSM(model_config, feature_configs, features, labels=None, is_training=False)[source]

Bases: MatchModel

__init__(model_config, feature_configs, features, labels=None, is_training=False)[source]
build_predict_graph()[source]
get_outputs()[source]
build_output_dict()[source]

For exporting: get standard output nodes.

build_rtp_output_dict()[source]

For exporting: get output nodes for RTP infering.

classmethod create_class(name)

easy_rec.python.model.mind

class easy_rec.python.model.mind.MIND(model_config, feature_configs, features, labels=None, is_training=False)[source]

Bases: MatchModel

__init__(model_config, feature_configs, features, labels=None, is_training=False)[source]
build_predict_graph()[source]
build_loss_graph()[source]
build_metric_graph(eval_config)[source]
classmethod create_class(name)
get_outputs()[source]

easy_rec.python.model.multi_task_model

class easy_rec.python.model.multi_task_model.MultiTaskModel(model_config, feature_configs, features, labels=None, is_training=False)[source]

Bases: RankModel

__init__(model_config, feature_configs, features, labels=None, is_training=False)[source]
build_predict_graph()[source]
build_metric_graph(eval_config)[source]

Build metric graph for multi task model.

build_loss_weight()[source]
get_learnt_loss(loss_type, name, value)[source]
build_loss_graph()[source]

Build loss graph for multi task model.

classmethod create_class(name)
get_outputs()[source]

easy_rec.python.model.mmoe

class easy_rec.python.model.mmoe.MMoE(model_config, feature_configs, features, labels=None, is_training=False)[source]

Bases: MultiTaskModel

__init__(model_config, feature_configs, features, labels=None, is_training=False)[source]
classmethod create_class(name)
build_predict_graph()[source]

easy_rec.python.model.esmm

class easy_rec.python.model.esmm.ESMM(model_config, feature_configs, features, labels=None, is_training=False)[source]

Bases: MultiTaskModel

__init__(model_config, feature_configs, features, labels=None, is_training=False)[source]
build_loss_graph()[source]

Build loss graph.

Returns:

Weighted loss of ctr and cvr.

Return type:

self._loss_dict

build_metric_graph(eval_config)[source]

Build metric graph.

Parameters:

eval_config – Evaluation configuration.

Returns:

Calculate AUC of ctr, cvr and ctrvr.

Return type:

metric_dict

build_predict_graph()[source]

Forward function.

Returns:

Prediction result of two tasks.

Return type:

self._prediction_dict

classmethod create_class(name)
get_outputs()[source]

Get model outputs.

Returns:

The list of tensor names output by the model.

Return type:

outputs

easy_rec.python.model.easy_rec_estimator

class easy_rec.python.model.simple_multi_task.SimpleMultiTask(model_config, feature_configs, features, labels=None, is_training=False)[source]

Bases: MultiTaskModel

__init__(model_config, feature_configs, features, labels=None, is_training=False)[source]
classmethod create_class(name)
build_predict_graph()[source]

easy_rec.python.model.easy_rec_estimator

class easy_rec.python.model.easy_rec_estimator.EasyRecEstimator(pipeline_config, model_cls, run_config, params)[source]

Bases: Estimator

__init__(pipeline_config, model_cls, run_config, params)[source]

Constructs an Estimator instance.

Parameters:
  • model_fn – Model function. Follows the signature: * features – This is the first item returned from the input_fn passed to train, evaluate, and predict. This should be a single tf.Tensor or dict of same. * labels – This is the second item returned from the input_fn passed to train, evaluate, and predict. This should be a single tf.Tensor or dict of same (for multi-head models). If mode is tf.estimator.ModeKeys.PREDICT, labels=None will be passed. If the model_fn’s signature does not accept mode, the model_fn must still be able to handle labels=None. * mode – Optional. Specifies if this is training, evaluation or prediction. See tf.estimator.ModeKeys. params – Optional dict of hyperparameters. Will receive what is passed to Estimator in params parameter. This allows to configure Estimators from hyper parameter tuning. * config – Optional estimator.RunConfig object. Will receive what is passed to Estimator as its config parameter, or a default value. Allows setting up things in your model_fn based on configuration such as num_ps_replicas, or model_dir. * Returns – tf.estimator.EstimatorSpec

  • model_dir – Directory to save model parameters, graph and etc. This can also be used to load checkpoints from the directory into an estimator to continue training a previously saved model. If PathLike object, the path will be resolved. If None, the model_dir in config will be used if set. If both are set, they must be same. If both are None, a temporary directory will be used.

  • configestimator.RunConfig configuration object.

  • paramsdict of hyper parameters that will be passed into model_fn. Keys are names of parameters, values are basic python types.

  • warm_start_from – Optional string filepath to a checkpoint or SavedModel to warm-start from, or a tf.estimator.WarmStartSettings object to fully configure warm-starting. If None, only TRAINABLE variables are warm-started. If the string filepath is provided instead of a tf.estimator.WarmStartSettings, then all variables are warm-started, and it is assumed that vocabularies and tf.Tensor names are unchanged.

Raises:
  • ValueError – parameters of model_fn don’t match params.

  • ValueError – if this is called via a subclass and if that class overrides a member of Estimator.

evaluate(input_fn, steps=None, hooks=None, checkpoint_path=None, name=None)[source]

Evaluates the model given evaluation data input_fn.

For each step, calls input_fn, which returns one batch of data. Evaluates until: - steps batches are processed, or - input_fn raises an end-of-input exception (tf.errors.OutOfRangeError or StopIteration).

Parameters:
  • input_fn

    A function that constructs the input data for evaluation. See [Premade Estimators]( https://tensorflow.org/guide/premade_estimators#create_input_functions) for more information. The function should construct and return one of the following: * A tf.data.Dataset object: Outputs of Dataset object must be a

    tuple (features, labels) with same constraints as below.

    • A tuple (features, labels): Where features is a tf.Tensor or a dictionary of string feature name to Tensor and labels is a Tensor or a dictionary of string label name to Tensor. Both features and labels are consumed by model_fn. They should satisfy the expectation of model_fn from inputs.

  • steps – Number of steps for which to evaluate model. If None, evaluates until input_fn raises an end-of-input exception.

  • hooks – List of tf.train.SessionRunHook subclass instances. Used for callbacks inside the evaluation call.

  • checkpoint_path – Path of a specific checkpoint to evaluate. If None, the latest checkpoint in model_dir is used. If there are no checkpoints in model_dir, evaluation is run with newly initialized Variables instead of ones restored from checkpoint.

  • name – Name of the evaluation if user needs to run multiple evaluations on different data sets, such as on training data vs test data. Metrics for different evaluations are saved in separate folders, and appear separately in tensorboard.

Returns:

A dict containing the evaluation metrics specified in model_fn keyed by name, as well as an entry global_step which contains the value of the global step for which this evaluation was performed. For canned estimators, the dict contains the loss (mean loss per mini-batch) and the average_loss (mean loss per sample). Canned classifiers also return the accuracy. Canned regressors also return the label/mean and the prediction/mean.

Raises:

ValueError – If steps <= 0.

train(input_fn, hooks=None, steps=None, max_steps=None, saving_listeners=None)[source]

Trains a model given training data input_fn.

Parameters:
  • input_fn

    A function that provides input data for training as minibatches. See [Premade Estimators]( https://tensorflow.org/guide/premade_estimators#create_input_functions)

    for more information. The function should construct and return one of

    the following:
    • A tf.data.Dataset object: Outputs of Dataset object must be a tuple (features, labels) with same constraints as below.

    • A tuple (features, labels): Where features is a tf.Tensor or a dictionary of string feature name to Tensor and labels is a Tensor or a dictionary of string label name to Tensor. Both features and labels are consumed by model_fn. They should satisfy the expectation of model_fn from inputs.

  • hooks – List of tf.train.SessionRunHook subclass instances. Used for callbacks inside the training loop.

  • steps – Number of steps for which to train the model. If None, train forever or train until input_fn generates the tf.errors.OutOfRange error or StopIteration exception. steps works incrementally. If you call two times train(steps=10) then training occurs in total 20 steps. If OutOfRange or StopIteration occurs in the middle, training stops before 20 steps. If you don’t want to have incremental behavior please set max_steps instead. If set, max_steps must be None.

  • max_steps – Number of total steps for which to train model. If None, train forever or train until input_fn generates the tf.errors.OutOfRange error or StopIteration exception. If set, steps must be None. If OutOfRange or StopIteration occurs in the middle, training stops before max_steps steps. Two calls to train(steps=100) means 200 training iterations. On the other hand, two calls to train(max_steps=100) means that the second call will not do any iteration since first call did all 100 steps.

  • saving_listeners – list of CheckpointSaverListener objects. Used for callbacks that run immediately before or after checkpoint savings.

Returns:

self, for chaining.

Raises:
  • ValueError – If both steps and max_steps are not None.

  • ValueError – If either steps or max_steps <= 0.

property feature_configs
property model_config
property eval_config
property train_config
property incr_save_config
property export_config
property embedding_parallel
property saver_cls
export_checkpoint(export_path=None, serving_input_receiver_fn=None, checkpoint_path=None, mode='infer')[source]