easy-cs-rec-custommodel 0.8.6__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of easy-cs-rec-custommodel might be problematic. Click here for more details.
- easy_cs_rec_custommodel-0.8.6.dist-info/LICENSE +203 -0
- easy_cs_rec_custommodel-0.8.6.dist-info/METADATA +48 -0
- easy_cs_rec_custommodel-0.8.6.dist-info/RECORD +336 -0
- easy_cs_rec_custommodel-0.8.6.dist-info/WHEEL +6 -0
- easy_cs_rec_custommodel-0.8.6.dist-info/top_level.txt +2 -0
- easy_rec/__init__.py +114 -0
- easy_rec/python/__init__.py +0 -0
- easy_rec/python/builders/__init__.py +0 -0
- easy_rec/python/builders/hyperparams_builder.py +78 -0
- easy_rec/python/builders/loss_builder.py +333 -0
- easy_rec/python/builders/optimizer_builder.py +211 -0
- easy_rec/python/builders/strategy_builder.py +44 -0
- easy_rec/python/compat/__init__.py +0 -0
- easy_rec/python/compat/adam_s.py +245 -0
- easy_rec/python/compat/array_ops.py +229 -0
- easy_rec/python/compat/dynamic_variable.py +542 -0
- easy_rec/python/compat/early_stopping.py +653 -0
- easy_rec/python/compat/embedding_ops.py +162 -0
- easy_rec/python/compat/embedding_parallel_saver.py +316 -0
- easy_rec/python/compat/estimator_train.py +116 -0
- easy_rec/python/compat/exporter.py +473 -0
- easy_rec/python/compat/feature_column/__init__.py +0 -0
- easy_rec/python/compat/feature_column/feature_column.py +3675 -0
- easy_rec/python/compat/feature_column/feature_column_v2.py +5233 -0
- easy_rec/python/compat/feature_column/sequence_feature_column.py +648 -0
- easy_rec/python/compat/feature_column/utils.py +154 -0
- easy_rec/python/compat/layers.py +329 -0
- easy_rec/python/compat/ops.py +14 -0
- easy_rec/python/compat/optimizers.py +619 -0
- easy_rec/python/compat/queues.py +311 -0
- easy_rec/python/compat/regularizers.py +208 -0
- easy_rec/python/compat/sok_optimizer.py +440 -0
- easy_rec/python/compat/sync_replicas_optimizer.py +528 -0
- easy_rec/python/compat/weight_decay_optimizers.py +475 -0
- easy_rec/python/core/__init__.py +0 -0
- easy_rec/python/core/easyrec_metrics/__init__.py +24 -0
- easy_rec/python/core/easyrec_metrics/distribute_metrics_impl_pai.py +3702 -0
- easy_rec/python/core/easyrec_metrics/distribute_metrics_impl_tf.py +3768 -0
- easy_rec/python/core/learning_schedules.py +228 -0
- easy_rec/python/core/metrics.py +402 -0
- easy_rec/python/core/sampler.py +844 -0
- easy_rec/python/eval.py +102 -0
- easy_rec/python/export.py +150 -0
- easy_rec/python/feature_column/__init__.py +0 -0
- easy_rec/python/feature_column/feature_column.py +664 -0
- easy_rec/python/feature_column/feature_group.py +89 -0
- easy_rec/python/hpo/__init__.py +0 -0
- easy_rec/python/hpo/emr_hpo.py +140 -0
- easy_rec/python/hpo/generate_hpo_sql.py +71 -0
- easy_rec/python/hpo/pai_hpo.py +297 -0
- easy_rec/python/inference/__init__.py +0 -0
- easy_rec/python/inference/csv_predictor.py +189 -0
- easy_rec/python/inference/hive_parquet_predictor.py +200 -0
- easy_rec/python/inference/hive_predictor.py +166 -0
- easy_rec/python/inference/odps_predictor.py +70 -0
- easy_rec/python/inference/parquet_predictor.py +147 -0
- easy_rec/python/inference/parquet_predictor_v2.py +147 -0
- easy_rec/python/inference/predictor.py +621 -0
- easy_rec/python/inference/processor/__init__.py +0 -0
- easy_rec/python/inference/processor/test.py +170 -0
- easy_rec/python/inference/vector_retrieve.py +124 -0
- easy_rec/python/input/__init__.py +0 -0
- easy_rec/python/input/batch_tfrecord_input.py +117 -0
- easy_rec/python/input/criteo_binary_reader.py +259 -0
- easy_rec/python/input/criteo_input.py +107 -0
- easy_rec/python/input/csv_input.py +175 -0
- easy_rec/python/input/csv_input_ex.py +72 -0
- easy_rec/python/input/csv_input_v2.py +68 -0
- easy_rec/python/input/datahub_input.py +320 -0
- easy_rec/python/input/dummy_input.py +58 -0
- easy_rec/python/input/hive_input.py +123 -0
- easy_rec/python/input/hive_parquet_input.py +140 -0
- easy_rec/python/input/hive_rtp_input.py +174 -0
- easy_rec/python/input/input.py +1064 -0
- easy_rec/python/input/kafka_dataset.py +144 -0
- easy_rec/python/input/kafka_input.py +235 -0
- easy_rec/python/input/load_parquet.py +317 -0
- easy_rec/python/input/odps_input.py +101 -0
- easy_rec/python/input/odps_input_v2.py +110 -0
- easy_rec/python/input/odps_input_v3.py +132 -0
- easy_rec/python/input/odps_rtp_input.py +187 -0
- easy_rec/python/input/odps_rtp_input_v2.py +104 -0
- easy_rec/python/input/parquet_input.py +397 -0
- easy_rec/python/input/parquet_input_v2.py +180 -0
- easy_rec/python/input/parquet_input_v3.py +203 -0
- easy_rec/python/input/rtp_input.py +225 -0
- easy_rec/python/input/rtp_input_v2.py +145 -0
- easy_rec/python/input/tfrecord_input.py +100 -0
- easy_rec/python/layers/__init__.py +0 -0
- easy_rec/python/layers/backbone.py +571 -0
- easy_rec/python/layers/capsule_layer.py +176 -0
- easy_rec/python/layers/cmbf.py +390 -0
- easy_rec/python/layers/common_layers.py +192 -0
- easy_rec/python/layers/dnn.py +87 -0
- easy_rec/python/layers/embed_input_layer.py +25 -0
- easy_rec/python/layers/fm.py +26 -0
- easy_rec/python/layers/input_layer.py +396 -0
- easy_rec/python/layers/keras/__init__.py +34 -0
- easy_rec/python/layers/keras/activation.py +114 -0
- easy_rec/python/layers/keras/attention.py +267 -0
- easy_rec/python/layers/keras/auxiliary_loss.py +47 -0
- easy_rec/python/layers/keras/blocks.py +262 -0
- easy_rec/python/layers/keras/bst.py +119 -0
- easy_rec/python/layers/keras/custom_ops.py +250 -0
- easy_rec/python/layers/keras/data_augment.py +133 -0
- easy_rec/python/layers/keras/din.py +67 -0
- easy_rec/python/layers/keras/einsum_dense.py +598 -0
- easy_rec/python/layers/keras/embedding.py +81 -0
- easy_rec/python/layers/keras/fibinet.py +251 -0
- easy_rec/python/layers/keras/interaction.py +416 -0
- easy_rec/python/layers/keras/layer_norm.py +364 -0
- easy_rec/python/layers/keras/mask_net.py +166 -0
- easy_rec/python/layers/keras/multi_head_attention.py +717 -0
- easy_rec/python/layers/keras/multi_task.py +125 -0
- easy_rec/python/layers/keras/numerical_embedding.py +376 -0
- easy_rec/python/layers/keras/ppnet.py +194 -0
- easy_rec/python/layers/keras/transformer.py +192 -0
- easy_rec/python/layers/layer_norm.py +51 -0
- easy_rec/python/layers/mmoe.py +83 -0
- easy_rec/python/layers/multihead_attention.py +162 -0
- easy_rec/python/layers/multihead_cross_attention.py +749 -0
- easy_rec/python/layers/senet.py +73 -0
- easy_rec/python/layers/seq_input_layer.py +134 -0
- easy_rec/python/layers/sequence_feature_layer.py +249 -0
- easy_rec/python/layers/uniter.py +301 -0
- easy_rec/python/layers/utils.py +248 -0
- easy_rec/python/layers/variational_dropout_layer.py +130 -0
- easy_rec/python/loss/__init__.py +0 -0
- easy_rec/python/loss/circle_loss.py +82 -0
- easy_rec/python/loss/contrastive_loss.py +79 -0
- easy_rec/python/loss/f1_reweight_loss.py +38 -0
- easy_rec/python/loss/focal_loss.py +93 -0
- easy_rec/python/loss/jrc_loss.py +128 -0
- easy_rec/python/loss/listwise_loss.py +161 -0
- easy_rec/python/loss/multi_similarity.py +68 -0
- easy_rec/python/loss/pairwise_loss.py +307 -0
- easy_rec/python/loss/softmax_loss_with_negative_mining.py +110 -0
- easy_rec/python/loss/zero_inflated_lognormal.py +76 -0
- easy_rec/python/main.py +878 -0
- easy_rec/python/model/__init__.py +0 -0
- easy_rec/python/model/autoint.py +73 -0
- easy_rec/python/model/cmbf.py +47 -0
- easy_rec/python/model/collaborative_metric_learning.py +182 -0
- easy_rec/python/model/custom_model.py +323 -0
- easy_rec/python/model/dat.py +138 -0
- easy_rec/python/model/dbmtl.py +116 -0
- easy_rec/python/model/dcn.py +70 -0
- easy_rec/python/model/deepfm.py +106 -0
- easy_rec/python/model/dlrm.py +73 -0
- easy_rec/python/model/dropoutnet.py +207 -0
- easy_rec/python/model/dssm.py +154 -0
- easy_rec/python/model/dssm_senet.py +143 -0
- easy_rec/python/model/dummy_model.py +48 -0
- easy_rec/python/model/easy_rec_estimator.py +739 -0
- easy_rec/python/model/easy_rec_model.py +467 -0
- easy_rec/python/model/esmm.py +242 -0
- easy_rec/python/model/fm.py +63 -0
- easy_rec/python/model/match_model.py +357 -0
- easy_rec/python/model/mind.py +445 -0
- easy_rec/python/model/mmoe.py +70 -0
- easy_rec/python/model/multi_task_model.py +303 -0
- easy_rec/python/model/multi_tower.py +62 -0
- easy_rec/python/model/multi_tower_bst.py +190 -0
- easy_rec/python/model/multi_tower_din.py +130 -0
- easy_rec/python/model/multi_tower_recall.py +68 -0
- easy_rec/python/model/pdn.py +203 -0
- easy_rec/python/model/ple.py +120 -0
- easy_rec/python/model/rank_model.py +485 -0
- easy_rec/python/model/rocket_launching.py +203 -0
- easy_rec/python/model/simple_multi_task.py +54 -0
- easy_rec/python/model/uniter.py +46 -0
- easy_rec/python/model/wide_and_deep.py +121 -0
- easy_rec/python/ops/1.12/incr_record.so +0 -0
- easy_rec/python/ops/1.12/kafka.so +0 -0
- easy_rec/python/ops/1.12/libcustom_ops.so +0 -0
- easy_rec/python/ops/1.12/libembed_op.so +0 -0
- easy_rec/python/ops/1.12/libhiredis.so.1.0.0 +0 -0
- easy_rec/python/ops/1.12/librdkafka++.so.1 +0 -0
- easy_rec/python/ops/1.12/librdkafka.so.1 +0 -0
- easy_rec/python/ops/1.12/libredis++.so +0 -0
- easy_rec/python/ops/1.12/libredis++.so.1 +0 -0
- easy_rec/python/ops/1.12/libredis++.so.1.2.3 +0 -0
- easy_rec/python/ops/1.12/libstr_avx_op.so +0 -0
- easy_rec/python/ops/1.12/libwrite_sparse_kv.so +0 -0
- easy_rec/python/ops/1.15/incr_record.so +0 -0
- easy_rec/python/ops/1.15/kafka.so +0 -0
- easy_rec/python/ops/1.15/libcustom_ops.so +0 -0
- easy_rec/python/ops/1.15/libembed_op.so +0 -0
- easy_rec/python/ops/1.15/libhiredis.so.1.0.0 +0 -0
- easy_rec/python/ops/1.15/librdkafka++.so +0 -0
- easy_rec/python/ops/1.15/librdkafka++.so.1 +0 -0
- easy_rec/python/ops/1.15/librdkafka.so +0 -0
- easy_rec/python/ops/1.15/librdkafka.so.1 +0 -0
- easy_rec/python/ops/1.15/libredis++.so.1 +0 -0
- easy_rec/python/ops/1.15/libstr_avx_op.so +0 -0
- easy_rec/python/ops/2.12/libcustom_ops.so +0 -0
- easy_rec/python/ops/2.12/libload_embed.so +0 -0
- easy_rec/python/ops/2.12/libstr_avx_op.so +0 -0
- easy_rec/python/ops/__init__.py +0 -0
- easy_rec/python/ops/gen_kafka_ops.py +193 -0
- easy_rec/python/ops/gen_str_avx_op.py +28 -0
- easy_rec/python/ops/incr_record.py +30 -0
- easy_rec/python/predict.py +170 -0
- easy_rec/python/protos/__init__.py +0 -0
- easy_rec/python/protos/autoint_pb2.py +122 -0
- easy_rec/python/protos/backbone_pb2.py +1416 -0
- easy_rec/python/protos/cmbf_pb2.py +435 -0
- easy_rec/python/protos/collaborative_metric_learning_pb2.py +252 -0
- easy_rec/python/protos/custom_model_pb2.py +57 -0
- easy_rec/python/protos/dat_pb2.py +262 -0
- easy_rec/python/protos/data_source_pb2.py +422 -0
- easy_rec/python/protos/dataset_pb2.py +1920 -0
- easy_rec/python/protos/dbmtl_pb2.py +191 -0
- easy_rec/python/protos/dcn_pb2.py +197 -0
- easy_rec/python/protos/deepfm_pb2.py +163 -0
- easy_rec/python/protos/dlrm_pb2.py +163 -0
- easy_rec/python/protos/dnn_pb2.py +329 -0
- easy_rec/python/protos/dropoutnet_pb2.py +239 -0
- easy_rec/python/protos/dssm_pb2.py +262 -0
- easy_rec/python/protos/dssm_senet_pb2.py +282 -0
- easy_rec/python/protos/easy_rec_model_pb2.py +1672 -0
- easy_rec/python/protos/esmm_pb2.py +133 -0
- easy_rec/python/protos/eval_pb2.py +930 -0
- easy_rec/python/protos/export_pb2.py +379 -0
- easy_rec/python/protos/feature_config_pb2.py +1359 -0
- easy_rec/python/protos/fm_pb2.py +90 -0
- easy_rec/python/protos/hive_config_pb2.py +138 -0
- easy_rec/python/protos/hyperparams_pb2.py +624 -0
- easy_rec/python/protos/keras_layer_pb2.py +692 -0
- easy_rec/python/protos/layer_pb2.py +1936 -0
- easy_rec/python/protos/loss_pb2.py +1713 -0
- easy_rec/python/protos/mind_pb2.py +497 -0
- easy_rec/python/protos/mmoe_pb2.py +215 -0
- easy_rec/python/protos/multi_tower_pb2.py +295 -0
- easy_rec/python/protos/multi_tower_recall_pb2.py +198 -0
- easy_rec/python/protos/optimizer_pb2.py +2017 -0
- easy_rec/python/protos/pdn_pb2.py +293 -0
- easy_rec/python/protos/pipeline_pb2.py +516 -0
- easy_rec/python/protos/ple_pb2.py +231 -0
- easy_rec/python/protos/predict_pb2.py +1140 -0
- easy_rec/python/protos/rocket_launching_pb2.py +169 -0
- easy_rec/python/protos/seq_encoder_pb2.py +1084 -0
- easy_rec/python/protos/simi_pb2.py +54 -0
- easy_rec/python/protos/simple_multi_task_pb2.py +97 -0
- easy_rec/python/protos/tf_predict_pb2.py +630 -0
- easy_rec/python/protos/tower_pb2.py +661 -0
- easy_rec/python/protos/train_pb2.py +1197 -0
- easy_rec/python/protos/uniter_pb2.py +307 -0
- easy_rec/python/protos/variational_dropout_pb2.py +91 -0
- easy_rec/python/protos/wide_and_deep_pb2.py +131 -0
- easy_rec/python/test/__init__.py +0 -0
- easy_rec/python/test/csv_input_test.py +340 -0
- easy_rec/python/test/custom_early_stop_func.py +19 -0
- easy_rec/python/test/dh_local_run.py +104 -0
- easy_rec/python/test/embed_test.py +155 -0
- easy_rec/python/test/emr_run.py +119 -0
- easy_rec/python/test/eval_metric_test.py +107 -0
- easy_rec/python/test/excel_convert_test.py +64 -0
- easy_rec/python/test/export_test.py +513 -0
- easy_rec/python/test/fg_test.py +70 -0
- easy_rec/python/test/hive_input_test.py +311 -0
- easy_rec/python/test/hpo_test.py +235 -0
- easy_rec/python/test/kafka_test.py +373 -0
- easy_rec/python/test/local_incr_test.py +122 -0
- easy_rec/python/test/loss_test.py +110 -0
- easy_rec/python/test/odps_command.py +61 -0
- easy_rec/python/test/odps_local_run.py +86 -0
- easy_rec/python/test/odps_run.py +254 -0
- easy_rec/python/test/odps_test_cls.py +39 -0
- easy_rec/python/test/odps_test_prepare.py +198 -0
- easy_rec/python/test/odps_test_util.py +237 -0
- easy_rec/python/test/pre_check_test.py +54 -0
- easy_rec/python/test/predictor_test.py +394 -0
- easy_rec/python/test/rtp_convert_test.py +133 -0
- easy_rec/python/test/run.py +138 -0
- easy_rec/python/test/train_eval_test.py +1299 -0
- easy_rec/python/test/util_test.py +85 -0
- easy_rec/python/test/zero_inflated_lognormal_test.py +53 -0
- easy_rec/python/tools/__init__.py +0 -0
- easy_rec/python/tools/add_boundaries_to_config.py +67 -0
- easy_rec/python/tools/add_feature_info_to_config.py +145 -0
- easy_rec/python/tools/convert_config_format.py +48 -0
- easy_rec/python/tools/convert_rtp_data.py +79 -0
- easy_rec/python/tools/convert_rtp_fg.py +106 -0
- easy_rec/python/tools/create_config_from_excel.py +427 -0
- easy_rec/python/tools/criteo/__init__.py +0 -0
- easy_rec/python/tools/criteo/convert_data.py +157 -0
- easy_rec/python/tools/edit_lookup_graph.py +134 -0
- easy_rec/python/tools/faiss_index_pai.py +116 -0
- easy_rec/python/tools/feature_selection.py +316 -0
- easy_rec/python/tools/hit_rate_ds.py +223 -0
- easy_rec/python/tools/hit_rate_pai.py +138 -0
- easy_rec/python/tools/pre_check.py +120 -0
- easy_rec/python/tools/predict_and_chk.py +111 -0
- easy_rec/python/tools/read_kafka.py +55 -0
- easy_rec/python/tools/split_model_pai.py +286 -0
- easy_rec/python/tools/split_pdn_model_pai.py +272 -0
- easy_rec/python/tools/test_saved_model.py +80 -0
- easy_rec/python/tools/view_saved_model.py +39 -0
- easy_rec/python/tools/write_kafka.py +65 -0
- easy_rec/python/train_eval.py +325 -0
- easy_rec/python/utils/__init__.py +15 -0
- easy_rec/python/utils/activation.py +120 -0
- easy_rec/python/utils/check_utils.py +87 -0
- easy_rec/python/utils/compat.py +14 -0
- easy_rec/python/utils/config_util.py +652 -0
- easy_rec/python/utils/constant.py +43 -0
- easy_rec/python/utils/convert_rtp_fg.py +616 -0
- easy_rec/python/utils/dag.py +192 -0
- easy_rec/python/utils/distribution_utils.py +268 -0
- easy_rec/python/utils/ds_util.py +65 -0
- easy_rec/python/utils/embedding_utils.py +73 -0
- easy_rec/python/utils/estimator_utils.py +1036 -0
- easy_rec/python/utils/export_big_model.py +630 -0
- easy_rec/python/utils/expr_util.py +118 -0
- easy_rec/python/utils/fg_util.py +53 -0
- easy_rec/python/utils/hit_rate_utils.py +220 -0
- easy_rec/python/utils/hive_utils.py +183 -0
- easy_rec/python/utils/hpo_util.py +137 -0
- easy_rec/python/utils/hvd_utils.py +56 -0
- easy_rec/python/utils/input_utils.py +108 -0
- easy_rec/python/utils/io_util.py +282 -0
- easy_rec/python/utils/load_class.py +249 -0
- easy_rec/python/utils/meta_graph_editor.py +941 -0
- easy_rec/python/utils/multi_optimizer.py +62 -0
- easy_rec/python/utils/numpy_utils.py +18 -0
- easy_rec/python/utils/odps_util.py +79 -0
- easy_rec/python/utils/pai_util.py +86 -0
- easy_rec/python/utils/proto_util.py +90 -0
- easy_rec/python/utils/restore_filter.py +89 -0
- easy_rec/python/utils/shape_utils.py +432 -0
- easy_rec/python/utils/static_shape.py +71 -0
- easy_rec/python/utils/test_utils.py +866 -0
- easy_rec/python/utils/tf_utils.py +56 -0
- easy_rec/version.py +4 -0
- test/__init__.py +0 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# -*- encoding:utf-8 -*-
|
|
2
|
+
# Copyright (c) Alibaba, Inc. and its affiliates.
|
|
3
|
+
|
|
4
|
+
import tensorflow as tf
|
|
5
|
+
|
|
6
|
+
from easy_rec.python.layers import dnn
|
|
7
|
+
from easy_rec.python.model.rank_model import RankModel
|
|
8
|
+
|
|
9
|
+
from easy_rec.python.protos.multi_tower_recall_pb2 import MultiTowerRecall as MultiTowerRecallConfig # NOQA
|
|
10
|
+
|
|
11
|
+
if tf.__version__ >= '2.0':
|
|
12
|
+
tf = tf.compat.v1
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class MultiTowerRecall(RankModel):
|
|
16
|
+
|
|
17
|
+
def __init__(self,
|
|
18
|
+
model_config,
|
|
19
|
+
feature_configs,
|
|
20
|
+
features,
|
|
21
|
+
labels=None,
|
|
22
|
+
is_training=False):
|
|
23
|
+
super(MultiTowerRecall, self).__init__(model_config, feature_configs,
|
|
24
|
+
features, labels, is_training)
|
|
25
|
+
assert self._model_config.WhichOneof('model') == 'multi_tower_recall', (
|
|
26
|
+
'invalid model config: %s' % self._model_config.WhichOneof('model'))
|
|
27
|
+
self._model_config = self._model_config.multi_tower_recall
|
|
28
|
+
assert isinstance(self._model_config, MultiTowerRecallConfig)
|
|
29
|
+
|
|
30
|
+
self.user_tower_feature, _ = self._input_layer(self._feature_dict, 'user')
|
|
31
|
+
self.item_tower_feature, _ = self._input_layer(self._feature_dict, 'item')
|
|
32
|
+
|
|
33
|
+
def build_predict_graph(self):
|
|
34
|
+
|
|
35
|
+
user_tower_feature = self.user_tower_feature
|
|
36
|
+
batch_size = tf.shape(user_tower_feature)[0]
|
|
37
|
+
pos_item_feature = self.item_tower_feature[:batch_size]
|
|
38
|
+
neg_item_feature = self.item_tower_feature[batch_size:]
|
|
39
|
+
item_tower_feature = tf.concat([
|
|
40
|
+
pos_item_feature[:, tf.newaxis, :],
|
|
41
|
+
tf.tile(
|
|
42
|
+
neg_item_feature[tf.newaxis, :, :], multiples=[batch_size, 1, 1])
|
|
43
|
+
],
|
|
44
|
+
axis=1) # noqa: E126
|
|
45
|
+
|
|
46
|
+
user_dnn = dnn.DNN(self._model_config.user_tower.dnn, self._l2_reg,
|
|
47
|
+
'user_dnn', self._is_training)
|
|
48
|
+
user_tower_emb = user_dnn(user_tower_feature)
|
|
49
|
+
|
|
50
|
+
item_dnn = dnn.DNN(self._model_config.item_tower.dnn, self._l2_reg,
|
|
51
|
+
'item_dnn', self._is_training)
|
|
52
|
+
item_tower_emb = item_dnn(item_tower_feature)
|
|
53
|
+
item_tower_emb = tf.reshape(item_tower_emb, tf.shape(user_tower_emb))
|
|
54
|
+
|
|
55
|
+
tower_fea_arr = []
|
|
56
|
+
tower_fea_arr.append(user_tower_emb)
|
|
57
|
+
tower_fea_arr.append(item_tower_emb)
|
|
58
|
+
|
|
59
|
+
all_fea = tf.concat(tower_fea_arr, axis=-1)
|
|
60
|
+
final_dnn_layer = dnn.DNN(self._model_config.final_dnn, self._l2_reg,
|
|
61
|
+
'final_dnn', self._is_training)
|
|
62
|
+
all_fea = final_dnn_layer(all_fea)
|
|
63
|
+
output = tf.layers.dense(all_fea, 1, name='output')
|
|
64
|
+
output = output[:, 0]
|
|
65
|
+
|
|
66
|
+
self._add_to_prediction_dict(output)
|
|
67
|
+
|
|
68
|
+
return self._prediction_dict
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
# -*- encoding:utf-8 -*-
|
|
2
|
+
# Copyright (c) Alibaba, Inc. and its affiliates.
|
|
3
|
+
|
|
4
|
+
import tensorflow as tf
|
|
5
|
+
|
|
6
|
+
from easy_rec.python.layers import dnn
|
|
7
|
+
from easy_rec.python.model.match_model import MatchModel
|
|
8
|
+
from easy_rec.python.protos.simi_pb2 import Similarity
|
|
9
|
+
|
|
10
|
+
if tf.__version__ >= '2.0':
|
|
11
|
+
tf = tf.compat.v1
|
|
12
|
+
losses = tf.losses
|
|
13
|
+
metrics = tf.metrics
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class PDN(MatchModel):
|
|
17
|
+
|
|
18
|
+
def __init__(self,
|
|
19
|
+
model_config,
|
|
20
|
+
feature_configs,
|
|
21
|
+
features,
|
|
22
|
+
labels=None,
|
|
23
|
+
is_training=False):
|
|
24
|
+
super(PDN, self).__init__(model_config, feature_configs, features, labels,
|
|
25
|
+
is_training)
|
|
26
|
+
assert self._model_config.WhichOneof('model') == 'pdn', \
|
|
27
|
+
'invalid model config: %s' % self._model_config.WhichOneof('model')
|
|
28
|
+
self._model_config = self._model_config.pdn
|
|
29
|
+
|
|
30
|
+
self._user_features, _ = self._input_layer(self._feature_dict, 'user')
|
|
31
|
+
self._item_features, _ = self._input_layer(self._feature_dict, 'item')
|
|
32
|
+
|
|
33
|
+
if self._input_layer.has_group('bias'):
|
|
34
|
+
self._bias_features, _ = self._input_layer(self._feature_dict, 'bias')
|
|
35
|
+
else:
|
|
36
|
+
self._bias_features = None
|
|
37
|
+
|
|
38
|
+
self._u2i_seq, self._seq_len = self._get_seq_features('u2i_seq')
|
|
39
|
+
self._i_seq, _ = self._get_seq_features('i_seq')
|
|
40
|
+
self._i2i_seq, _ = self._get_seq_features('i2i_seq')
|
|
41
|
+
|
|
42
|
+
def build_predict_graph(self):
|
|
43
|
+
trigger_out = self._build_trigger_net()
|
|
44
|
+
sim_out = self._build_similarity_net()
|
|
45
|
+
logits = tf.multiply(sim_out, trigger_out)
|
|
46
|
+
|
|
47
|
+
seq_mask = tf.to_float(
|
|
48
|
+
tf.sequence_mask(self._seq_len,
|
|
49
|
+
tf.shape(sim_out)[1]))
|
|
50
|
+
logits = tf.reduce_sum(logits * seq_mask[:, :, None], axis=1)
|
|
51
|
+
|
|
52
|
+
direct_logits = self._build_direct_net()
|
|
53
|
+
if direct_logits is not None:
|
|
54
|
+
logits += direct_logits
|
|
55
|
+
|
|
56
|
+
bias_logits = self._build_bias_net()
|
|
57
|
+
if bias_logits is not None:
|
|
58
|
+
logits += bias_logits
|
|
59
|
+
|
|
60
|
+
logits = tf.squeeze(logits, axis=1)
|
|
61
|
+
probs = 1 - tf.exp(-logits) # map [0, inf) to [0, 1)
|
|
62
|
+
|
|
63
|
+
self._prediction_dict['probs'] = probs
|
|
64
|
+
self._prediction_dict['logits'] = tf.log(
|
|
65
|
+
tf.clip_by_value(probs, 1e-8, 1 - 1e-8))
|
|
66
|
+
return self._prediction_dict
|
|
67
|
+
|
|
68
|
+
def _get_seq_features(self, name):
|
|
69
|
+
seqs, _, _ = self._input_layer(self._feature_dict, name, is_combine=False)
|
|
70
|
+
seq_len = seqs[0][1]
|
|
71
|
+
seq = tf.concat([x[0] for x in seqs], axis=2)
|
|
72
|
+
return seq, seq_len
|
|
73
|
+
|
|
74
|
+
def _build_trigger_net(self):
|
|
75
|
+
user_dnn_layer = dnn.DNN(self._model_config.user_dnn, self._l2_reg,
|
|
76
|
+
'user_dnn', self._is_training)
|
|
77
|
+
user_fea = user_dnn_layer(self._user_features)
|
|
78
|
+
|
|
79
|
+
trigger_seq = tf.concat([self._u2i_seq, self._i_seq], axis=2)
|
|
80
|
+
u2i_dnn_layer = dnn.DNN(self._model_config.u2i_dnn, self._l2_reg, 'u2i_dnn',
|
|
81
|
+
self._is_training)
|
|
82
|
+
trigger_seq_fea = u2i_dnn_layer(trigger_seq)
|
|
83
|
+
|
|
84
|
+
trigger_merge_fea = trigger_seq_fea + user_fea[:, None, :]
|
|
85
|
+
trigger_dnn_layer = dnn.DNN(
|
|
86
|
+
self._model_config.trigger_dnn,
|
|
87
|
+
self._l2_reg,
|
|
88
|
+
'trigger_dnn',
|
|
89
|
+
self._is_training,
|
|
90
|
+
last_layer_no_activation=True,
|
|
91
|
+
last_layer_no_batch_norm=True)
|
|
92
|
+
|
|
93
|
+
# output: N x seq_len x d, d is usually set to 1
|
|
94
|
+
trigger_out = trigger_dnn_layer(trigger_merge_fea)
|
|
95
|
+
# exp(x): map (-inf, inf) to (0, inf)
|
|
96
|
+
trigger_out = tf.exp(trigger_out)
|
|
97
|
+
|
|
98
|
+
self._prediction_dict['trigger_out'] = tf.reduce_join(
|
|
99
|
+
tf.reduce_join(
|
|
100
|
+
tf.as_string(trigger_out, precision=4, shortest=True),
|
|
101
|
+
axis=2,
|
|
102
|
+
separator=','),
|
|
103
|
+
axis=1,
|
|
104
|
+
separator=';')
|
|
105
|
+
return trigger_out
|
|
106
|
+
|
|
107
|
+
def _build_similarity_net(self):
|
|
108
|
+
item_dnn_layer = dnn.DNN(self._model_config.item_dnn, self._l2_reg,
|
|
109
|
+
'item_dnn', self._is_training)
|
|
110
|
+
item_fea = item_dnn_layer(self._item_features)
|
|
111
|
+
|
|
112
|
+
sim_side_dnn_layer = dnn.DNN(self._model_config.i2i_dnn, self._l2_reg,
|
|
113
|
+
'i2i_dnn', self._is_training)
|
|
114
|
+
sim_seq_fea = sim_side_dnn_layer(self._i_seq)
|
|
115
|
+
|
|
116
|
+
sim_seq_cross = sim_seq_fea * item_fea[:, None, :]
|
|
117
|
+
|
|
118
|
+
item_fea_tile = tf.tile(item_fea[:, None, :],
|
|
119
|
+
[1, tf.shape(sim_seq_fea)[1], 1])
|
|
120
|
+
|
|
121
|
+
sim_seq_concat = tf.concat(
|
|
122
|
+
[sim_seq_cross, sim_seq_cross, self._i2i_seq, item_fea_tile], axis=2)
|
|
123
|
+
sim_dnn_layer = dnn.DNN(
|
|
124
|
+
self._model_config.sim_dnn,
|
|
125
|
+
self._l2_reg,
|
|
126
|
+
'sim_dnn',
|
|
127
|
+
self._is_training,
|
|
128
|
+
last_layer_no_activation=True,
|
|
129
|
+
last_layer_no_batch_norm=True)
|
|
130
|
+
# output: N x seq_len x 1
|
|
131
|
+
sim_out = sim_dnn_layer(sim_seq_concat)
|
|
132
|
+
# exp(x): map (-inf, inf) to (0, inf)
|
|
133
|
+
sim_out = tf.exp(sim_out)
|
|
134
|
+
|
|
135
|
+
self._prediction_dict['sim_out'] = tf.reduce_join(
|
|
136
|
+
tf.reduce_join(
|
|
137
|
+
tf.as_string(sim_out, precision=4, shortest=True),
|
|
138
|
+
axis=2,
|
|
139
|
+
separator=','),
|
|
140
|
+
axis=1,
|
|
141
|
+
separator=';')
|
|
142
|
+
return sim_out
|
|
143
|
+
|
|
144
|
+
def _build_direct_net(self):
|
|
145
|
+
if self._model_config.HasField('direct_user_dnn') and \
|
|
146
|
+
self._model_config.HasField('direct_item_dnn'):
|
|
147
|
+
direct_user_layer = dnn.DNN(
|
|
148
|
+
self._model_config.direct_user_dnn,
|
|
149
|
+
'direct_user_dnn',
|
|
150
|
+
self._is_training,
|
|
151
|
+
last_layer_no_activation=True,
|
|
152
|
+
last_layer_no_batch_norm=True)
|
|
153
|
+
direct_user_out = direct_user_layer(self._user_features)
|
|
154
|
+
direct_item_layer = dnn.DNN(
|
|
155
|
+
self._model_config.direct_item_dnn,
|
|
156
|
+
'direct_item_dnn',
|
|
157
|
+
self._is_training,
|
|
158
|
+
last_layer_no_activation=True,
|
|
159
|
+
last_layer_no_batch_norm=True)
|
|
160
|
+
direct_item_out = direct_item_layer(self._item_features)
|
|
161
|
+
|
|
162
|
+
if self._model_config.simi_func == Similarity.COSINE:
|
|
163
|
+
direct_user_out = self.norm(direct_user_out)
|
|
164
|
+
direct_item_out = self.norm(direct_item_out)
|
|
165
|
+
|
|
166
|
+
self._prediction_dict['direct_user_embedding'] = direct_user_out
|
|
167
|
+
self._prediction_dict['direct_item_embedding'] = direct_item_out
|
|
168
|
+
direct_logits = tf.reduce_sum(direct_user_out * direct_item_out, axis=1)
|
|
169
|
+
|
|
170
|
+
if self._model_config.scale_simi:
|
|
171
|
+
sim_w = tf.get_variable(
|
|
172
|
+
'direct_net/sim_w',
|
|
173
|
+
dtype=tf.float32,
|
|
174
|
+
shape=(1),
|
|
175
|
+
initializer=tf.ones_initializer())
|
|
176
|
+
sim_b = tf.get_variable(
|
|
177
|
+
'direct_net/sim_b',
|
|
178
|
+
dtype=tf.float32,
|
|
179
|
+
shape=(1),
|
|
180
|
+
initializer=tf.zeros_initializer())
|
|
181
|
+
direct_logits = direct_logits * tf.abs(sim_w) + sim_b
|
|
182
|
+
|
|
183
|
+
return tf.nn.softplus(direct_logits)
|
|
184
|
+
else:
|
|
185
|
+
return None
|
|
186
|
+
|
|
187
|
+
def _build_bias_net(self):
|
|
188
|
+
if self._model_config.HasField('bias_dnn'):
|
|
189
|
+
assert self._bias_features is not None, 'bias group must be defined'
|
|
190
|
+
bias_dnn_layer = dnn.DNN(
|
|
191
|
+
self._model_config.bias_dnn,
|
|
192
|
+
self._l2_reg,
|
|
193
|
+
'bias_dnn',
|
|
194
|
+
self._is_training,
|
|
195
|
+
last_layer_no_activation=True,
|
|
196
|
+
last_layer_no_batch_norm=True)
|
|
197
|
+
bias_logits = bias_dnn_layer(self._bias_features)
|
|
198
|
+
return tf.nn.softplus(bias_logits)
|
|
199
|
+
else:
|
|
200
|
+
return None
|
|
201
|
+
|
|
202
|
+
def get_outputs(self):
|
|
203
|
+
return ['logits', 'probs', 'trigger_out', 'sim_out']
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# -*- encoding:utf-8 -*-
|
|
2
|
+
# Copyright (c) Alibaba, Inc. and its affiliates.
|
|
3
|
+
import tensorflow as tf
|
|
4
|
+
|
|
5
|
+
from easy_rec.python.layers import dnn
|
|
6
|
+
from easy_rec.python.model.multi_task_model import MultiTaskModel
|
|
7
|
+
from easy_rec.python.protos.ple_pb2 import PLE as PLEConfig
|
|
8
|
+
|
|
9
|
+
if tf.__version__ >= '2.0':
|
|
10
|
+
tf = tf.compat.v1
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class PLE(MultiTaskModel):
|
|
14
|
+
|
|
15
|
+
def __init__(self,
|
|
16
|
+
model_config,
|
|
17
|
+
feature_configs,
|
|
18
|
+
features,
|
|
19
|
+
labels=None,
|
|
20
|
+
is_training=False):
|
|
21
|
+
super(PLE, self).__init__(model_config, feature_configs, features, labels,
|
|
22
|
+
is_training)
|
|
23
|
+
assert self._model_config.WhichOneof('model') == 'ple', \
|
|
24
|
+
'invalid model config: %s' % self._model_config.WhichOneof('model')
|
|
25
|
+
self._model_config = self._model_config.ple
|
|
26
|
+
assert isinstance(self._model_config, PLEConfig)
|
|
27
|
+
|
|
28
|
+
self._layer_nums = len(self._model_config.extraction_networks)
|
|
29
|
+
self._task_nums = len(self._model_config.task_towers)
|
|
30
|
+
if self.has_backbone:
|
|
31
|
+
self._features = self.backbone
|
|
32
|
+
else:
|
|
33
|
+
self._features, _ = self._input_layer(self._feature_dict, 'all')
|
|
34
|
+
self._init_towers(self._model_config.task_towers)
|
|
35
|
+
|
|
36
|
+
def gate(self, selector_fea, vec_feas, name):
|
|
37
|
+
vec = tf.stack(vec_feas, axis=1)
|
|
38
|
+
gate = tf.layers.dense(
|
|
39
|
+
inputs=selector_fea,
|
|
40
|
+
units=len(vec_feas),
|
|
41
|
+
kernel_regularizer=self._l2_reg,
|
|
42
|
+
activation=None,
|
|
43
|
+
name=name + '_gate/dnn')
|
|
44
|
+
gate = tf.nn.softmax(gate, axis=1)
|
|
45
|
+
gate = tf.expand_dims(gate, -1)
|
|
46
|
+
task_input = tf.multiply(vec, gate)
|
|
47
|
+
task_input = tf.reduce_sum(task_input, axis=1)
|
|
48
|
+
return task_input
|
|
49
|
+
|
|
50
|
+
def experts_layer(self, deep_fea, expert_num, experts_cfg, name):
|
|
51
|
+
tower_outputs = []
|
|
52
|
+
for expert_id in range(expert_num):
|
|
53
|
+
tower_dnn = dnn.DNN(
|
|
54
|
+
experts_cfg,
|
|
55
|
+
self._l2_reg,
|
|
56
|
+
name=name + '_expert_%d/dnn' % expert_id,
|
|
57
|
+
is_training=self._is_training)
|
|
58
|
+
tower_output = tower_dnn(deep_fea)
|
|
59
|
+
tower_outputs.append(tower_output)
|
|
60
|
+
return tower_outputs
|
|
61
|
+
|
|
62
|
+
def CGC_layer(self, extraction_networks_cfg, extraction_network_fea,
|
|
63
|
+
shared_expert_fea, final_flag):
|
|
64
|
+
layer_name = extraction_networks_cfg.network_name
|
|
65
|
+
expert_shared_out = self.experts_layer(
|
|
66
|
+
shared_expert_fea, extraction_networks_cfg.share_num,
|
|
67
|
+
extraction_networks_cfg.share_expert_net, layer_name + '_share/dnn')
|
|
68
|
+
|
|
69
|
+
experts_outs = []
|
|
70
|
+
cgc_layer_outs = []
|
|
71
|
+
for task_idx in range(self._task_nums):
|
|
72
|
+
name = layer_name + '_task_%d' % task_idx
|
|
73
|
+
experts_out = self.experts_layer(
|
|
74
|
+
extraction_network_fea[task_idx],
|
|
75
|
+
extraction_networks_cfg.expert_num_per_task,
|
|
76
|
+
extraction_networks_cfg.task_expert_net, name)
|
|
77
|
+
cgc_layer_out = self.gate(extraction_network_fea[task_idx],
|
|
78
|
+
experts_out + expert_shared_out, name)
|
|
79
|
+
experts_outs.extend(experts_out)
|
|
80
|
+
cgc_layer_outs.append(cgc_layer_out)
|
|
81
|
+
|
|
82
|
+
if final_flag:
|
|
83
|
+
shared_layer_out = None
|
|
84
|
+
else:
|
|
85
|
+
shared_layer_out = self.gate(shared_expert_fea,
|
|
86
|
+
experts_outs + expert_shared_out,
|
|
87
|
+
layer_name + '_share')
|
|
88
|
+
return cgc_layer_outs, shared_layer_out
|
|
89
|
+
|
|
90
|
+
def build_predict_graph(self):
|
|
91
|
+
extraction_network_fea = [self._features] * self._task_nums
|
|
92
|
+
shared_expert_fea = self._features
|
|
93
|
+
final_flag = False
|
|
94
|
+
for idx in range(len(self._model_config.extraction_networks)):
|
|
95
|
+
extraction_network = self._model_config.extraction_networks[idx]
|
|
96
|
+
if idx == len(self._model_config.extraction_networks) - 1:
|
|
97
|
+
final_flag = True
|
|
98
|
+
extraction_network_fea, shared_expert_fea = self.CGC_layer(
|
|
99
|
+
extraction_network, extraction_network_fea, shared_expert_fea,
|
|
100
|
+
final_flag)
|
|
101
|
+
|
|
102
|
+
tower_outputs = {}
|
|
103
|
+
for i, task_tower_cfg in enumerate(self._model_config.task_towers):
|
|
104
|
+
tower_name = task_tower_cfg.tower_name
|
|
105
|
+
tower_dnn = dnn.DNN(
|
|
106
|
+
task_tower_cfg.dnn,
|
|
107
|
+
self._l2_reg,
|
|
108
|
+
name=tower_name,
|
|
109
|
+
is_training=self._is_training)
|
|
110
|
+
tower_output = tower_dnn(extraction_network_fea[i])
|
|
111
|
+
|
|
112
|
+
tower_output = tf.layers.dense(
|
|
113
|
+
inputs=tower_output,
|
|
114
|
+
units=task_tower_cfg.num_class,
|
|
115
|
+
kernel_regularizer=self._l2_reg,
|
|
116
|
+
name='dnn_output_%d' % i)
|
|
117
|
+
|
|
118
|
+
tower_outputs[tower_name] = tower_output
|
|
119
|
+
self._add_to_prediction_dict(tower_outputs)
|
|
120
|
+
return self._prediction_dict
|