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,445 @@
|
|
|
1
|
+
# -*- encoding:utf-8 -*-
|
|
2
|
+
# Copyright (c) Alibaba, Inc. and its affiliates.
|
|
3
|
+
import logging
|
|
4
|
+
|
|
5
|
+
import tensorflow as tf
|
|
6
|
+
|
|
7
|
+
from easy_rec.python.compat import regularizers
|
|
8
|
+
from easy_rec.python.layers import dnn
|
|
9
|
+
from easy_rec.python.layers.capsule_layer import CapsuleLayer
|
|
10
|
+
from easy_rec.python.model.match_model import MatchModel
|
|
11
|
+
from easy_rec.python.protos.loss_pb2 import LossType
|
|
12
|
+
from easy_rec.python.protos.mind_pb2 import MIND as MINDConfig
|
|
13
|
+
from easy_rec.python.protos.simi_pb2 import Similarity
|
|
14
|
+
from easy_rec.python.utils.proto_util import copy_obj
|
|
15
|
+
|
|
16
|
+
if tf.__version__ >= '2.0':
|
|
17
|
+
tf = tf.compat.v1
|
|
18
|
+
losses = tf.losses
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class MIND(MatchModel):
|
|
22
|
+
|
|
23
|
+
def __init__(self,
|
|
24
|
+
model_config,
|
|
25
|
+
feature_configs,
|
|
26
|
+
features,
|
|
27
|
+
labels=None,
|
|
28
|
+
is_training=False):
|
|
29
|
+
super(MIND, self).__init__(model_config, feature_configs, features, labels,
|
|
30
|
+
is_training)
|
|
31
|
+
assert self._model_config.WhichOneof('model') == 'mind', \
|
|
32
|
+
'invalid model config: %s' % self._model_config.WhichOneof('model')
|
|
33
|
+
self._model_config = self._model_config.mind
|
|
34
|
+
|
|
35
|
+
self._hist_seq_features, _, _ = self._input_layer(
|
|
36
|
+
self._feature_dict, 'hist', is_combine=False)
|
|
37
|
+
self._user_features, _ = self._input_layer(self._feature_dict, 'user')
|
|
38
|
+
self._item_features, _ = self._input_layer(self._feature_dict, 'item')
|
|
39
|
+
|
|
40
|
+
# copy_obj so that any modification will not affect original config
|
|
41
|
+
self.user_dnn = copy_obj(self._model_config.user_dnn)
|
|
42
|
+
# copy_obj so that any modification will not affect original config
|
|
43
|
+
self.item_dnn = copy_obj(self._model_config.item_dnn)
|
|
44
|
+
# copy obj so that any modification will not affect original config
|
|
45
|
+
self.concat_dnn = copy_obj(self._model_config.concat_dnn)
|
|
46
|
+
|
|
47
|
+
self._l2_reg = regularizers.l2_regularizer(
|
|
48
|
+
self._model_config.l2_regularization)
|
|
49
|
+
|
|
50
|
+
def build_predict_graph(self):
|
|
51
|
+
capsule_layer = CapsuleLayer(self._model_config.capsule_config,
|
|
52
|
+
self._is_training)
|
|
53
|
+
|
|
54
|
+
if self._model_config.time_id_fea:
|
|
55
|
+
time_id_fea = [
|
|
56
|
+
x[0]
|
|
57
|
+
for x in self._hist_seq_features
|
|
58
|
+
if self._model_config.time_id_fea in x[0].name
|
|
59
|
+
]
|
|
60
|
+
logging.info('time_id_fea is set(%s), find num: %d' %
|
|
61
|
+
(self._model_config.time_id_fea, len(time_id_fea)))
|
|
62
|
+
else:
|
|
63
|
+
time_id_fea = []
|
|
64
|
+
time_id_fea = time_id_fea[0] if len(time_id_fea) > 0 else None
|
|
65
|
+
|
|
66
|
+
if time_id_fea is not None:
|
|
67
|
+
hist_seq_feas = [
|
|
68
|
+
x[0]
|
|
69
|
+
for x in self._hist_seq_features
|
|
70
|
+
if self._model_config.time_id_fea not in x[0].name
|
|
71
|
+
]
|
|
72
|
+
else:
|
|
73
|
+
hist_seq_feas = [x[0] for x in self._hist_seq_features]
|
|
74
|
+
|
|
75
|
+
# it is assumed that all hist have the same length
|
|
76
|
+
hist_seq_len = self._hist_seq_features[0][1]
|
|
77
|
+
|
|
78
|
+
if self._model_config.user_seq_combine == MINDConfig.SUM:
|
|
79
|
+
# sum pooling over the features
|
|
80
|
+
hist_embed_dims = [x.get_shape()[-1] for x in hist_seq_feas]
|
|
81
|
+
for i in range(1, len(hist_embed_dims)):
|
|
82
|
+
assert hist_embed_dims[i] == hist_embed_dims[0], \
|
|
83
|
+
'all hist seq must have the same embedding shape, but: %s' \
|
|
84
|
+
% str(hist_embed_dims)
|
|
85
|
+
hist_seq_feas = tf.add_n(hist_seq_feas) / len(hist_seq_feas)
|
|
86
|
+
else:
|
|
87
|
+
hist_seq_feas = tf.concat(hist_seq_feas, axis=2)
|
|
88
|
+
|
|
89
|
+
if self._model_config.HasField('pre_capsule_dnn') and \
|
|
90
|
+
len(self._model_config.pre_capsule_dnn.hidden_units) > 0:
|
|
91
|
+
pre_dnn_layer = dnn.DNN(self._model_config.pre_capsule_dnn, self._l2_reg,
|
|
92
|
+
'pre_capsule_dnn', self._is_training)
|
|
93
|
+
hist_seq_feas = pre_dnn_layer(hist_seq_feas)
|
|
94
|
+
|
|
95
|
+
if time_id_fea is not None:
|
|
96
|
+
assert time_id_fea.get_shape(
|
|
97
|
+
)[-1] == 1, 'time_id must have only embedding_size of 1'
|
|
98
|
+
time_id_mask = tf.sequence_mask(hist_seq_len, tf.shape(time_id_fea)[1])
|
|
99
|
+
time_id_mask = (tf.cast(time_id_mask, tf.float32) * 2 - 1) * 1e32
|
|
100
|
+
time_id_fea = tf.minimum(time_id_fea, time_id_mask[:, :, None])
|
|
101
|
+
hist_seq_feas = hist_seq_feas * tf.nn.softmax(time_id_fea, axis=1)
|
|
102
|
+
|
|
103
|
+
tf.summary.histogram('hist_seq_len', hist_seq_len)
|
|
104
|
+
|
|
105
|
+
# batch_size x max_k x high_capsule_dim
|
|
106
|
+
high_capsules, num_high_capsules = capsule_layer(hist_seq_feas,
|
|
107
|
+
hist_seq_len)
|
|
108
|
+
|
|
109
|
+
tf.summary.histogram('num_high_capsules', num_high_capsules)
|
|
110
|
+
|
|
111
|
+
# high_capsules = tf.layers.batch_normalization(
|
|
112
|
+
# high_capsules, training=self._is_training,
|
|
113
|
+
# trainable=True, name='capsule_bn')
|
|
114
|
+
# high_capsules = high_capsules * 0.1
|
|
115
|
+
|
|
116
|
+
tf.summary.scalar('high_capsules_norm',
|
|
117
|
+
tf.reduce_mean(tf.norm(high_capsules, axis=-1)))
|
|
118
|
+
tf.summary.scalar('num_high_capsules',
|
|
119
|
+
tf.reduce_mean(tf.to_float(num_high_capsules)))
|
|
120
|
+
|
|
121
|
+
user_features = tf.layers.batch_normalization(
|
|
122
|
+
self._user_features,
|
|
123
|
+
training=self._is_training,
|
|
124
|
+
trainable=True,
|
|
125
|
+
name='user_fea_bn')
|
|
126
|
+
user_dnn = dnn.DNN(self.user_dnn, self._l2_reg, 'user_dnn',
|
|
127
|
+
self._is_training)
|
|
128
|
+
user_features = user_dnn(user_features)
|
|
129
|
+
|
|
130
|
+
tf.summary.scalar('user_features_norm',
|
|
131
|
+
tf.reduce_mean(tf.norm(self._user_features, axis=-1)))
|
|
132
|
+
|
|
133
|
+
# concatenate with user features
|
|
134
|
+
user_features_tile = tf.tile(user_features[:, None, :],
|
|
135
|
+
[1, tf.shape(high_capsules)[1], 1])
|
|
136
|
+
user_interests = tf.concat([high_capsules, user_features_tile], axis=2)
|
|
137
|
+
|
|
138
|
+
num_concat_dnn_layer = len(self.concat_dnn.hidden_units)
|
|
139
|
+
last_hidden = self.concat_dnn.hidden_units.pop()
|
|
140
|
+
concat_dnn = dnn.DNN(self.concat_dnn, self._l2_reg, 'concat_dnn',
|
|
141
|
+
self._is_training)
|
|
142
|
+
user_interests = concat_dnn(user_interests)
|
|
143
|
+
user_interests = tf.layers.dense(
|
|
144
|
+
inputs=user_interests,
|
|
145
|
+
units=last_hidden,
|
|
146
|
+
kernel_regularizer=self._l2_reg,
|
|
147
|
+
name='concat_dnn/dnn_%d' % (num_concat_dnn_layer - 1))
|
|
148
|
+
|
|
149
|
+
num_item_dnn_layer = len(self.item_dnn.hidden_units)
|
|
150
|
+
last_item_hidden = self.item_dnn.hidden_units.pop()
|
|
151
|
+
item_dnn = dnn.DNN(self.item_dnn, self._l2_reg, 'item_dnn',
|
|
152
|
+
self._is_training)
|
|
153
|
+
item_tower_emb = item_dnn(self._item_features)
|
|
154
|
+
item_tower_emb = tf.layers.dense(
|
|
155
|
+
inputs=item_tower_emb,
|
|
156
|
+
units=last_item_hidden,
|
|
157
|
+
kernel_regularizer=self._l2_reg,
|
|
158
|
+
name='item_dnn/dnn_%d' % (num_item_dnn_layer - 1))
|
|
159
|
+
|
|
160
|
+
assert self._model_config.simi_func in [
|
|
161
|
+
Similarity.COSINE, Similarity.INNER_PRODUCT
|
|
162
|
+
]
|
|
163
|
+
|
|
164
|
+
if self._model_config.simi_func == Similarity.COSINE:
|
|
165
|
+
item_tower_emb = self.norm(item_tower_emb)
|
|
166
|
+
user_interests = self.norm(user_interests)
|
|
167
|
+
|
|
168
|
+
# label guided attention
|
|
169
|
+
# attention item features on high capsules vector
|
|
170
|
+
batch_size = tf.shape(user_interests)[0]
|
|
171
|
+
pos_item_fea = item_tower_emb[:batch_size]
|
|
172
|
+
simi = tf.einsum('bhe,be->bh', user_interests, pos_item_fea)
|
|
173
|
+
tf.summary.histogram('interest_item_simi/pre_scale',
|
|
174
|
+
tf.reduce_max(simi, axis=1))
|
|
175
|
+
# simi = tf.Print(simi, [tf.reduce_max(simi, axis=1), tf.reduce_min(simi, axis=1)], message='simi_max_0')
|
|
176
|
+
# simi = tf.pow(simi, self._model_config.simi_pow)
|
|
177
|
+
simi = simi * self._model_config.simi_pow
|
|
178
|
+
tf.summary.histogram('interest_item_simi/scaled',
|
|
179
|
+
tf.reduce_max(simi, axis=1))
|
|
180
|
+
# simi = tf.Print(simi, [tf.reduce_max(simi, axis=1), tf.reduce_min(simi, axis=1)], message='simi_max')
|
|
181
|
+
simi_mask = tf.sequence_mask(num_high_capsules,
|
|
182
|
+
self._model_config.capsule_config.max_k)
|
|
183
|
+
|
|
184
|
+
user_interests = user_interests * tf.to_float(simi_mask[:, :, None])
|
|
185
|
+
self._prediction_dict['user_interests'] = user_interests
|
|
186
|
+
|
|
187
|
+
max_thresh = (tf.cast(simi_mask, tf.float32) * 2 - 1) * 1e32
|
|
188
|
+
simi = tf.minimum(simi, max_thresh)
|
|
189
|
+
simi = tf.nn.softmax(simi, axis=1)
|
|
190
|
+
tf.summary.histogram('interest_item_simi/softmax',
|
|
191
|
+
tf.reduce_max(simi, axis=1))
|
|
192
|
+
|
|
193
|
+
if self._model_config.simi_pow >= 100:
|
|
194
|
+
logging.info(
|
|
195
|
+
'simi_pow=%d, will change to argmax, only use the most similar interests for calculate loss.'
|
|
196
|
+
% self._model_config.simi_pow)
|
|
197
|
+
simi_max_id = tf.argmax(simi, axis=1)
|
|
198
|
+
simi = tf.one_hot(simi_max_id, tf.shape(simi)[1], dtype=tf.float32)
|
|
199
|
+
|
|
200
|
+
user_tower_emb = tf.einsum('bhe,bh->be', user_interests, simi)
|
|
201
|
+
|
|
202
|
+
# calculate similarity between user_tower_emb and item_tower_emb
|
|
203
|
+
user_item_sim = self.sim(user_tower_emb, item_tower_emb)
|
|
204
|
+
if self._model_config.scale_simi:
|
|
205
|
+
sim_w = tf.get_variable(
|
|
206
|
+
'sim_w',
|
|
207
|
+
dtype=tf.float32,
|
|
208
|
+
shape=(1),
|
|
209
|
+
initializer=tf.ones_initializer())
|
|
210
|
+
sim_b = tf.get_variable(
|
|
211
|
+
'sim_b',
|
|
212
|
+
dtype=tf.float32,
|
|
213
|
+
shape=(1),
|
|
214
|
+
initializer=tf.zeros_initializer())
|
|
215
|
+
y_pred = user_item_sim * tf.abs(sim_w) + sim_b
|
|
216
|
+
else:
|
|
217
|
+
y_pred = user_item_sim
|
|
218
|
+
|
|
219
|
+
if self._is_point_wise:
|
|
220
|
+
y_pred = tf.reshape(y_pred, [-1])
|
|
221
|
+
|
|
222
|
+
if self._loss_type == LossType.CLASSIFICATION:
|
|
223
|
+
self._prediction_dict['logits'] = y_pred
|
|
224
|
+
self._prediction_dict['probs'] = tf.nn.sigmoid(y_pred)
|
|
225
|
+
elif self._loss_type == LossType.SOFTMAX_CROSS_ENTROPY:
|
|
226
|
+
y_pred = self._mask_in_batch(y_pred)
|
|
227
|
+
self._prediction_dict['logits'] = y_pred
|
|
228
|
+
self._prediction_dict['probs'] = tf.nn.softmax(y_pred)
|
|
229
|
+
else:
|
|
230
|
+
self._prediction_dict['y'] = y_pred
|
|
231
|
+
|
|
232
|
+
self._prediction_dict['high_capsules'] = high_capsules
|
|
233
|
+
self._prediction_dict['user_interests'] = user_interests
|
|
234
|
+
self._prediction_dict['user_tower_emb'] = user_tower_emb
|
|
235
|
+
self._prediction_dict['item_tower_emb'] = item_tower_emb
|
|
236
|
+
self._prediction_dict['user_emb'] = tf.reduce_join(
|
|
237
|
+
tf.reduce_join(tf.as_string(user_interests), axis=-1, separator=','),
|
|
238
|
+
axis=-1,
|
|
239
|
+
separator='|')
|
|
240
|
+
self._prediction_dict['user_emb_num'] = num_high_capsules
|
|
241
|
+
self._prediction_dict['item_emb'] = tf.reduce_join(
|
|
242
|
+
tf.as_string(item_tower_emb), axis=-1, separator=',')
|
|
243
|
+
|
|
244
|
+
if self._labels is not None:
|
|
245
|
+
# for summary purpose
|
|
246
|
+
batch_simi, batch_capsule_simi = self._build_interest_simi()
|
|
247
|
+
# self._prediction_dict['probs'] = tf.Print(self._prediction_dict['probs'],
|
|
248
|
+
# [batch_simi, batch_capsule_simi], message='batch_simi')
|
|
249
|
+
self._prediction_dict['interests_simi'] = batch_simi
|
|
250
|
+
return self._prediction_dict
|
|
251
|
+
|
|
252
|
+
def build_loss_graph(self):
|
|
253
|
+
loss_dict = super(MIND, self).build_loss_graph()
|
|
254
|
+
if self._model_config.max_interests_simi < 1.0:
|
|
255
|
+
loss_dict['reg_interest_simi'] = tf.nn.relu(
|
|
256
|
+
self._prediction_dict['interests_simi'] -
|
|
257
|
+
self._model_config.max_interests_simi)
|
|
258
|
+
return loss_dict
|
|
259
|
+
|
|
260
|
+
def _build_interest_simi(self):
|
|
261
|
+
user_emb_num = self._prediction_dict['user_emb_num']
|
|
262
|
+
high_capsule_mask = tf.sequence_mask(
|
|
263
|
+
user_emb_num, self._model_config.capsule_config.max_k)
|
|
264
|
+
|
|
265
|
+
user_interests = self._prediction_dict['user_interests']
|
|
266
|
+
high_capsule_mask = tf.to_float(high_capsule_mask[:, :, None])
|
|
267
|
+
user_interests = self.norm(user_interests) * high_capsule_mask
|
|
268
|
+
|
|
269
|
+
user_feature_sum_sqr = tf.square(tf.reduce_sum(user_interests, axis=1))
|
|
270
|
+
user_feature_sqr_sum = tf.reduce_sum(tf.square(user_interests), axis=1)
|
|
271
|
+
interest_simi = user_feature_sum_sqr - user_feature_sqr_sum
|
|
272
|
+
|
|
273
|
+
high_capsules = self._prediction_dict['high_capsules']
|
|
274
|
+
high_capsules = self.norm(high_capsules) * high_capsule_mask
|
|
275
|
+
high_capsule_sum_sqr = tf.square(tf.reduce_sum(high_capsules, axis=1))
|
|
276
|
+
high_capsule_sqr_sum = tf.reduce_sum(tf.square(high_capsules), axis=1)
|
|
277
|
+
high_capsule_simi = high_capsule_sum_sqr - high_capsule_sqr_sum
|
|
278
|
+
|
|
279
|
+
# normalize by interest number
|
|
280
|
+
interest_div = tf.maximum(
|
|
281
|
+
tf.to_float(user_emb_num * (user_emb_num - 1)), 1.0)
|
|
282
|
+
interest_simi = tf.reduce_sum(interest_simi, axis=1) / interest_div
|
|
283
|
+
|
|
284
|
+
high_capsule_simi = tf.reduce_sum(high_capsule_simi, axis=1) / interest_div
|
|
285
|
+
|
|
286
|
+
# normalize by batch_size
|
|
287
|
+
multi_interest = tf.to_float(user_emb_num > 1)
|
|
288
|
+
sum_interest_simi = tf.reduce_sum(
|
|
289
|
+
(interest_simi + 1) * multi_interest) / 2.0
|
|
290
|
+
sum_div = tf.maximum(tf.reduce_sum(multi_interest), 1.0)
|
|
291
|
+
avg_interest_simi = sum_interest_simi / sum_div
|
|
292
|
+
|
|
293
|
+
sum_capsule_simi = tf.reduce_sum(
|
|
294
|
+
(high_capsule_simi + 1) * multi_interest) / 2.0
|
|
295
|
+
avg_capsule_simi = sum_capsule_simi / sum_div
|
|
296
|
+
|
|
297
|
+
tf.summary.scalar('interest_similarity', avg_interest_simi)
|
|
298
|
+
tf.summary.scalar('capsule_similarity', avg_capsule_simi)
|
|
299
|
+
return avg_interest_simi, avg_capsule_simi
|
|
300
|
+
|
|
301
|
+
def build_metric_graph(self, eval_config):
|
|
302
|
+
from easy_rec.python.core.easyrec_metrics import metrics_tf as metrics
|
|
303
|
+
# build interest metric
|
|
304
|
+
interest_simi, capsule_simi = self._build_interest_simi()
|
|
305
|
+
metric_dict = {
|
|
306
|
+
'interest_similarity': metrics.mean(interest_simi),
|
|
307
|
+
'capsule_similarity': metrics.mean(capsule_simi)
|
|
308
|
+
}
|
|
309
|
+
if self._is_point_wise:
|
|
310
|
+
metric_dict.update(self._build_point_wise_metric_graph(eval_config))
|
|
311
|
+
return metric_dict
|
|
312
|
+
|
|
313
|
+
recall_at_topks = []
|
|
314
|
+
for metric in eval_config.metrics_set:
|
|
315
|
+
if metric.WhichOneof('metric') == 'recall_at_topk':
|
|
316
|
+
assert self._loss_type in [
|
|
317
|
+
LossType.CLASSIFICATION, LossType.SOFTMAX_CROSS_ENTROPY
|
|
318
|
+
]
|
|
319
|
+
if metric.recall_at_topk.topk not in recall_at_topks:
|
|
320
|
+
recall_at_topks.append(metric.recall_at_topk.topk)
|
|
321
|
+
|
|
322
|
+
# compute interest recall
|
|
323
|
+
# [batch_size, num_interests, embed_dim]
|
|
324
|
+
user_interests = self._prediction_dict['user_interests']
|
|
325
|
+
# [?, embed_dim]
|
|
326
|
+
item_tower_emb = self._prediction_dict['item_tower_emb']
|
|
327
|
+
batch_size = tf.shape(user_interests)[0]
|
|
328
|
+
# [?, 2] first dimension is the sample_id in batch
|
|
329
|
+
# second dimension is the neg_id with respect to the sample
|
|
330
|
+
hard_neg_indices = self._feature_dict.get('hard_neg_indices', None)
|
|
331
|
+
|
|
332
|
+
if hard_neg_indices is not None:
|
|
333
|
+
logging.info('With hard negative examples')
|
|
334
|
+
noclk_size = tf.shape(hard_neg_indices)[0]
|
|
335
|
+
simple_item_emb, hard_neg_item_emb = tf.split(
|
|
336
|
+
item_tower_emb, [-1, noclk_size], axis=0)
|
|
337
|
+
else:
|
|
338
|
+
simple_item_emb = item_tower_emb
|
|
339
|
+
hard_neg_item_emb = None
|
|
340
|
+
|
|
341
|
+
# batch_size num_interest sample_neg_num
|
|
342
|
+
simple_item_sim = tf.einsum('bhe,ne->bhn', user_interests, simple_item_emb)
|
|
343
|
+
# batch_size sample_neg_num
|
|
344
|
+
simple_item_sim = tf.reduce_max(simple_item_sim, axis=1)
|
|
345
|
+
simple_lbls = tf.cast(tf.range(tf.shape(user_interests)[0]), tf.int64)
|
|
346
|
+
|
|
347
|
+
# labels = tf.zeros_like(logits[:, :1], dtype=tf.int64)
|
|
348
|
+
pos_indices = tf.range(batch_size)
|
|
349
|
+
pos_indices = tf.concat([pos_indices[:, None], pos_indices[:, None]],
|
|
350
|
+
axis=1)
|
|
351
|
+
pos_item_sim = tf.gather_nd(simple_item_sim[:batch_size, :batch_size],
|
|
352
|
+
pos_indices)
|
|
353
|
+
|
|
354
|
+
simple_item_sim_v2 = tf.concat(
|
|
355
|
+
[pos_item_sim[:, None], simple_item_sim[:, batch_size:]], axis=1)
|
|
356
|
+
simple_lbls_v2 = tf.zeros_like(simple_item_sim_v2[:, :1], dtype=tf.int64)
|
|
357
|
+
|
|
358
|
+
for topk in recall_at_topks:
|
|
359
|
+
metric_dict['interests_recall@%d' % topk] = metrics.recall_at_k(
|
|
360
|
+
labels=simple_lbls,
|
|
361
|
+
predictions=simple_item_sim,
|
|
362
|
+
k=topk,
|
|
363
|
+
name='interests_recall_at_%d' % topk)
|
|
364
|
+
metric_dict['interests_neg_sam_recall@%d' % topk] = metrics.recall_at_k(
|
|
365
|
+
labels=simple_lbls_v2,
|
|
366
|
+
predictions=simple_item_sim_v2,
|
|
367
|
+
k=topk,
|
|
368
|
+
name='interests_recall_neg_sam_at_%d' % topk)
|
|
369
|
+
|
|
370
|
+
logits = self._prediction_dict['logits']
|
|
371
|
+
pos_item_logits = tf.gather_nd(logits[:batch_size, :batch_size],
|
|
372
|
+
pos_indices)
|
|
373
|
+
logits_v2 = tf.concat([pos_item_logits[:, None], logits[:, batch_size:]],
|
|
374
|
+
axis=1)
|
|
375
|
+
labels_v2 = tf.zeros_like(logits_v2[:, :1], dtype=tf.int64)
|
|
376
|
+
|
|
377
|
+
for topk in recall_at_topks:
|
|
378
|
+
metric_dict['recall@%d' % topk] = metrics.recall_at_k(
|
|
379
|
+
labels=simple_lbls,
|
|
380
|
+
predictions=logits,
|
|
381
|
+
k=topk,
|
|
382
|
+
name='recall_at_%d' % topk)
|
|
383
|
+
metric_dict['recall_neg_sam@%d' % topk] = metrics.recall_at_k(
|
|
384
|
+
labels=labels_v2,
|
|
385
|
+
predictions=logits_v2,
|
|
386
|
+
k=topk,
|
|
387
|
+
name='recall_neg_sam_at_%d' % topk)
|
|
388
|
+
eval_logits = logits[:, :batch_size]
|
|
389
|
+
eval_logits = tf.cond(
|
|
390
|
+
batch_size < topk, lambda: tf.pad(
|
|
391
|
+
eval_logits, [[0, 0], [0, topk - batch_size]],
|
|
392
|
+
mode='CONSTANT',
|
|
393
|
+
constant_values=-1e32,
|
|
394
|
+
name='pad_eval_logits'), lambda: eval_logits)
|
|
395
|
+
metric_dict['recall_in_batch@%d' % topk] = metrics.recall_at_k(
|
|
396
|
+
labels=simple_lbls,
|
|
397
|
+
predictions=eval_logits,
|
|
398
|
+
k=topk,
|
|
399
|
+
name='recall_in_batch_at_%d' % topk)
|
|
400
|
+
|
|
401
|
+
# batch_size num_interest
|
|
402
|
+
if hard_neg_indices is not None:
|
|
403
|
+
hard_neg_user_emb = tf.gather(user_interests, hard_neg_indices[:, 0])
|
|
404
|
+
hard_neg_sim = tf.einsum('nhe,ne->nh', hard_neg_user_emb,
|
|
405
|
+
hard_neg_item_emb)
|
|
406
|
+
hard_neg_sim = tf.reduce_max(hard_neg_sim, axis=1)
|
|
407
|
+
max_num_neg = tf.reduce_max(hard_neg_indices[:, 1]) + 1
|
|
408
|
+
hard_neg_shape = tf.stack([tf.to_int64(batch_size), max_num_neg])
|
|
409
|
+
hard_neg_mask = tf.scatter_nd(
|
|
410
|
+
hard_neg_indices,
|
|
411
|
+
tf.ones_like(hard_neg_sim, dtype=tf.float32),
|
|
412
|
+
shape=hard_neg_shape)
|
|
413
|
+
hard_neg_sim = tf.scatter_nd(hard_neg_indices, hard_neg_sim,
|
|
414
|
+
hard_neg_shape)
|
|
415
|
+
hard_neg_sim = hard_neg_sim - (1 - hard_neg_mask) * 1e32
|
|
416
|
+
|
|
417
|
+
hard_logits = tf.concat([pos_item_logits[:, None], hard_neg_sim], axis=1)
|
|
418
|
+
hard_lbls = tf.zeros_like(hard_logits[:, :1], dtype=tf.int64)
|
|
419
|
+
metric_dict['hard_neg_acc'] = metrics.accuracy(
|
|
420
|
+
hard_lbls, tf.argmax(hard_logits, axis=1))
|
|
421
|
+
|
|
422
|
+
return metric_dict
|
|
423
|
+
|
|
424
|
+
def get_outputs(self):
|
|
425
|
+
if self._loss_type == LossType.CLASSIFICATION:
|
|
426
|
+
return [
|
|
427
|
+
'logits', 'probs', 'user_emb', 'item_emb', 'user_emb_num',
|
|
428
|
+
'user_interests', 'item_tower_emb'
|
|
429
|
+
]
|
|
430
|
+
elif self._loss_type == LossType.SOFTMAX_CROSS_ENTROPY:
|
|
431
|
+
self._prediction_dict['logits'] = tf.squeeze(
|
|
432
|
+
self._prediction_dict['logits'], axis=-1)
|
|
433
|
+
self._prediction_dict['probs'] = tf.nn.sigmoid(
|
|
434
|
+
self._prediction_dict['logits'])
|
|
435
|
+
return [
|
|
436
|
+
'logits', 'probs', 'user_emb', 'item_emb', 'user_emb_num',
|
|
437
|
+
'user_interests', 'item_tower_emb'
|
|
438
|
+
]
|
|
439
|
+
elif self._loss_type == LossType.L2_LOSS:
|
|
440
|
+
return [
|
|
441
|
+
'y', 'user_emb', 'item_emb', 'user_emb_num', 'user_interests',
|
|
442
|
+
'item_tower_emb'
|
|
443
|
+
]
|
|
444
|
+
else:
|
|
445
|
+
raise ValueError('invalid loss type: %s' % str(self._loss_type))
|
|
@@ -0,0 +1,70 @@
|
|
|
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.layers import mmoe
|
|
7
|
+
from easy_rec.python.model.multi_task_model import MultiTaskModel
|
|
8
|
+
from easy_rec.python.protos.mmoe_pb2 import MMoE as MMoEConfig
|
|
9
|
+
|
|
10
|
+
if tf.__version__ >= '2.0':
|
|
11
|
+
tf = tf.compat.v1
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class MMoE(MultiTaskModel):
|
|
15
|
+
|
|
16
|
+
def __init__(self,
|
|
17
|
+
model_config,
|
|
18
|
+
feature_configs,
|
|
19
|
+
features,
|
|
20
|
+
labels=None,
|
|
21
|
+
is_training=False):
|
|
22
|
+
super(MMoE, self).__init__(model_config, feature_configs, features, labels,
|
|
23
|
+
is_training)
|
|
24
|
+
assert self._model_config.WhichOneof('model') == 'mmoe', \
|
|
25
|
+
'invalid model config: %s' % self._model_config.WhichOneof('model')
|
|
26
|
+
self._model_config = self._model_config.mmoe
|
|
27
|
+
assert isinstance(self._model_config, MMoEConfig)
|
|
28
|
+
|
|
29
|
+
if self.has_backbone:
|
|
30
|
+
self._features = self.backbone
|
|
31
|
+
else:
|
|
32
|
+
self._features, _ = self._input_layer(self._feature_dict, 'all')
|
|
33
|
+
self._init_towers(self._model_config.task_towers)
|
|
34
|
+
|
|
35
|
+
def build_predict_graph(self):
|
|
36
|
+
if self._model_config.HasField('expert_dnn'):
|
|
37
|
+
mmoe_layer = mmoe.MMOE(
|
|
38
|
+
self._model_config.expert_dnn,
|
|
39
|
+
l2_reg=self._l2_reg,
|
|
40
|
+
num_task=self._task_num,
|
|
41
|
+
num_expert=self._model_config.num_expert)
|
|
42
|
+
else:
|
|
43
|
+
# For backward compatibility with original mmoe layer config
|
|
44
|
+
mmoe_layer = mmoe.MMOE([x.dnn for x in self._model_config.experts],
|
|
45
|
+
l2_reg=self._l2_reg,
|
|
46
|
+
num_task=self._task_num)
|
|
47
|
+
task_input_list = mmoe_layer(self._features)
|
|
48
|
+
|
|
49
|
+
tower_outputs = {}
|
|
50
|
+
for i, task_tower_cfg in enumerate(self._model_config.task_towers):
|
|
51
|
+
tower_name = task_tower_cfg.tower_name
|
|
52
|
+
|
|
53
|
+
if task_tower_cfg.HasField('dnn'):
|
|
54
|
+
tower_dnn = dnn.DNN(
|
|
55
|
+
task_tower_cfg.dnn,
|
|
56
|
+
self._l2_reg,
|
|
57
|
+
name=tower_name,
|
|
58
|
+
is_training=self._is_training)
|
|
59
|
+
tower_output = tower_dnn(task_input_list[i])
|
|
60
|
+
else:
|
|
61
|
+
tower_output = task_input_list[i]
|
|
62
|
+
tower_output = tf.layers.dense(
|
|
63
|
+
inputs=tower_output,
|
|
64
|
+
units=task_tower_cfg.num_class,
|
|
65
|
+
kernel_regularizer=self._l2_reg,
|
|
66
|
+
name='dnn_output_%d' % i)
|
|
67
|
+
|
|
68
|
+
tower_outputs[tower_name] = tower_output
|
|
69
|
+
self._add_to_prediction_dict(tower_outputs)
|
|
70
|
+
return self._prediction_dict
|