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,63 @@
|
|
|
1
|
+
# -*- encoding:utf-8 -*-
|
|
2
|
+
# Copyright (c) Alibaba, Inc. and its affiliates.
|
|
3
|
+
from __future__ import print_function
|
|
4
|
+
|
|
5
|
+
import tensorflow as tf
|
|
6
|
+
|
|
7
|
+
from easy_rec.python.layers import fm
|
|
8
|
+
from easy_rec.python.model.rank_model import RankModel
|
|
9
|
+
from easy_rec.python.protos.fm_pb2 import FM as FMConfig
|
|
10
|
+
|
|
11
|
+
if tf.__version__ >= '2.0':
|
|
12
|
+
tf = tf.compat.v1
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class FM(RankModel):
|
|
16
|
+
|
|
17
|
+
def __init__(self,
|
|
18
|
+
model_config,
|
|
19
|
+
feature_configs,
|
|
20
|
+
features,
|
|
21
|
+
labels=None,
|
|
22
|
+
is_training=False):
|
|
23
|
+
super(FM, self).__init__(model_config, feature_configs, features, labels,
|
|
24
|
+
is_training)
|
|
25
|
+
assert self._model_config.WhichOneof('model') == 'fm', \
|
|
26
|
+
'invalid model config: %s' % self._model_config.WhichOneof('model')
|
|
27
|
+
self._model_config = self._model_config.fm
|
|
28
|
+
assert isinstance(self._model_config, FMConfig)
|
|
29
|
+
|
|
30
|
+
self._wide_features, _ = self._input_layer(self._feature_dict, 'wide')
|
|
31
|
+
self._deep_features, self._fm_features = self._input_layer(
|
|
32
|
+
self._feature_dict, 'deep')
|
|
33
|
+
|
|
34
|
+
def build_input_layer(self, model_config, feature_configs):
|
|
35
|
+
# overwrite create input_layer to support wide_output_dim
|
|
36
|
+
self._wide_output_dim = model_config.num_class
|
|
37
|
+
super(FM, self).build_input_layer(model_config, feature_configs)
|
|
38
|
+
|
|
39
|
+
def build_predict_graph(self):
|
|
40
|
+
wide_fea = tf.reduce_sum(
|
|
41
|
+
self._wide_features, axis=1, keepdims=True, name='wide_feature')
|
|
42
|
+
|
|
43
|
+
fm_fea = fm.FM(name='fm_feature')(self._fm_features)
|
|
44
|
+
|
|
45
|
+
if self._num_class > 1:
|
|
46
|
+
fm_fea = tf.layers.dense(
|
|
47
|
+
fm_fea,
|
|
48
|
+
self._num_class,
|
|
49
|
+
kernel_regularizer=self._l2_reg,
|
|
50
|
+
name='fm_logits')
|
|
51
|
+
else:
|
|
52
|
+
fm_fea = tf.reduce_sum(fm_fea, 1, keepdims=True)
|
|
53
|
+
|
|
54
|
+
bias = tf.get_variable(
|
|
55
|
+
'fm_bias', [self._num_class],
|
|
56
|
+
initializer=tf.zeros_initializer(),
|
|
57
|
+
trainable=True)
|
|
58
|
+
|
|
59
|
+
output = wide_fea + fm_fea
|
|
60
|
+
output = tf.nn.bias_add(output, bias)
|
|
61
|
+
|
|
62
|
+
self._add_to_prediction_dict(output)
|
|
63
|
+
return self._prediction_dict
|
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
# -*- encoding:utf-8 -*-
|
|
2
|
+
# Copyright (c) Alibaba, Inc. and its affiliates.
|
|
3
|
+
import logging
|
|
4
|
+
import os
|
|
5
|
+
|
|
6
|
+
import tensorflow as tf
|
|
7
|
+
|
|
8
|
+
from easy_rec.python.builders import loss_builder
|
|
9
|
+
from easy_rec.python.model.easy_rec_model import EasyRecModel
|
|
10
|
+
from easy_rec.python.protos.loss_pb2 import LossType
|
|
11
|
+
from easy_rec.python.protos.simi_pb2 import Similarity
|
|
12
|
+
|
|
13
|
+
if tf.__version__ >= '2.0':
|
|
14
|
+
tf = tf.compat.v1
|
|
15
|
+
losses = tf.losses
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class MatchModel(EasyRecModel):
|
|
19
|
+
|
|
20
|
+
def __init__(self,
|
|
21
|
+
model_config,
|
|
22
|
+
feature_configs,
|
|
23
|
+
features,
|
|
24
|
+
labels=None,
|
|
25
|
+
is_training=False):
|
|
26
|
+
super(MatchModel, self).__init__(model_config, feature_configs, features,
|
|
27
|
+
labels, is_training)
|
|
28
|
+
self._loss_type = self._model_config.loss_type
|
|
29
|
+
self._num_class = self._model_config.num_class
|
|
30
|
+
|
|
31
|
+
if self._loss_type == LossType.CLASSIFICATION:
|
|
32
|
+
assert self._num_class == 1
|
|
33
|
+
|
|
34
|
+
if self._loss_type in [LossType.CLASSIFICATION, LossType.L2_LOSS]:
|
|
35
|
+
self._is_point_wise = True
|
|
36
|
+
logging.info('Use point wise dssm.')
|
|
37
|
+
else:
|
|
38
|
+
self._is_point_wise = False
|
|
39
|
+
logging.info('Use list wise dssm.')
|
|
40
|
+
|
|
41
|
+
cls_mem = self._model_config.WhichOneof('model')
|
|
42
|
+
sub_model_config = getattr(self._model_config, cls_mem)
|
|
43
|
+
|
|
44
|
+
self._item_ids = None
|
|
45
|
+
assert sub_model_config is not None, 'sub_model_config undefined: model_cls = %s' % cls_mem
|
|
46
|
+
if getattr(sub_model_config, 'item_id', '') != '':
|
|
47
|
+
logging.info('item_id feature is: %s' % sub_model_config.item_id)
|
|
48
|
+
self._item_ids = features[sub_model_config.item_id]
|
|
49
|
+
|
|
50
|
+
def _mask_in_batch(self, logits):
|
|
51
|
+
batch_size = tf.shape(logits)[0]
|
|
52
|
+
if getattr(self._model_config, 'ignore_in_batch_neg_sam', False):
|
|
53
|
+
in_batch = logits[:, :batch_size] - (
|
|
54
|
+
1 - tf.diag(tf.ones([batch_size], dtype=tf.float32))) * 1e32
|
|
55
|
+
return tf.concat([in_batch, logits[:, batch_size:]], axis=1)
|
|
56
|
+
else:
|
|
57
|
+
if self._item_ids is not None:
|
|
58
|
+
mask_in_batch_neg = tf.to_float(
|
|
59
|
+
tf.equal(self._item_ids[None, :batch_size],
|
|
60
|
+
self._item_ids[:batch_size, None])) - tf.diag(
|
|
61
|
+
tf.ones([batch_size], dtype=tf.float32))
|
|
62
|
+
tf.summary.scalar('in_batch_neg_conflict',
|
|
63
|
+
tf.reduce_sum(mask_in_batch_neg))
|
|
64
|
+
return tf.concat([
|
|
65
|
+
logits[:, :batch_size] - mask_in_batch_neg * 1e32,
|
|
66
|
+
logits[:, batch_size:]],
|
|
67
|
+
axis=1) # yapf: disable
|
|
68
|
+
else:
|
|
69
|
+
return logits
|
|
70
|
+
|
|
71
|
+
def _list_wise_sim(self, user_emb, item_emb):
|
|
72
|
+
batch_size = tf.shape(user_emb)[0]
|
|
73
|
+
hard_neg_indices = self._feature_dict.get('hard_neg_indices', None)
|
|
74
|
+
|
|
75
|
+
if hard_neg_indices is not None:
|
|
76
|
+
logging.info('With hard negative examples')
|
|
77
|
+
noclk_size = tf.shape(hard_neg_indices)[0]
|
|
78
|
+
# pos_item_emb, neg_item_emb, hard_neg_item_emb = tf.split(
|
|
79
|
+
# item_emb, [batch_size, -1, noclk_size], axis=0)
|
|
80
|
+
simple_item_emb, hard_neg_item_emb = tf.split(
|
|
81
|
+
item_emb, [-1, noclk_size], axis=0)
|
|
82
|
+
else:
|
|
83
|
+
# pos_item_emb = item_emb[:batch_size]
|
|
84
|
+
# neg_item_emb = item_emb[batch_size:]
|
|
85
|
+
simple_item_emb = item_emb
|
|
86
|
+
|
|
87
|
+
# pos_user_item_sim = tf.reduce_sum(
|
|
88
|
+
# tf.multiply(user_emb, pos_item_emb), axis=1, keep_dims=True)
|
|
89
|
+
# neg_user_item_sim = tf.matmul(user_emb, tf.transpose(neg_item_emb))
|
|
90
|
+
# simple_user_item_sim = tf.matmul(user_emb, tf.transpose(simple_item_emb))
|
|
91
|
+
|
|
92
|
+
_mode = os.environ['tf.estimator.mode']
|
|
93
|
+
if _mode == tf.estimator.ModeKeys.PREDICT:
|
|
94
|
+
simple_user_item_sim = tf.reduce_sum(
|
|
95
|
+
tf.multiply(user_emb, simple_item_emb), axis=1, keep_dims=True)
|
|
96
|
+
else:
|
|
97
|
+
simple_user_item_sim = tf.matmul(user_emb, tf.transpose(simple_item_emb))
|
|
98
|
+
|
|
99
|
+
if hard_neg_indices is None:
|
|
100
|
+
return simple_user_item_sim
|
|
101
|
+
else:
|
|
102
|
+
user_emb_expand = tf.gather(user_emb, hard_neg_indices[:, 0])
|
|
103
|
+
hard_neg_user_item_sim = tf.reduce_sum(
|
|
104
|
+
tf.multiply(user_emb_expand, hard_neg_item_emb), axis=1)
|
|
105
|
+
max_num_neg = tf.reduce_max(hard_neg_indices[:, 1]) + 1
|
|
106
|
+
hard_neg_shape = tf.stack([tf.to_int64(batch_size), max_num_neg])
|
|
107
|
+
hard_neg_sim = tf.scatter_nd(hard_neg_indices, hard_neg_user_item_sim,
|
|
108
|
+
hard_neg_shape)
|
|
109
|
+
hard_neg_mask = tf.scatter_nd(
|
|
110
|
+
hard_neg_indices,
|
|
111
|
+
tf.ones_like(hard_neg_user_item_sim, dtype=tf.float32),
|
|
112
|
+
shape=hard_neg_shape)
|
|
113
|
+
# set tail positions to -1e32, so that after exp(x), will be zero
|
|
114
|
+
hard_neg_user_item_sim = hard_neg_sim - (1 - hard_neg_mask) * 1e32
|
|
115
|
+
|
|
116
|
+
# user_item_sim = [pos_user_item_sim, neg_user_item_sim]
|
|
117
|
+
# if hard_neg_indices is not None:
|
|
118
|
+
# user_item_sim.append(hard_neg_user_item_sim)
|
|
119
|
+
# return tf.concat(user_item_sim, axis=1)
|
|
120
|
+
|
|
121
|
+
return tf.concat([simple_user_item_sim, hard_neg_user_item_sim], axis=1)
|
|
122
|
+
|
|
123
|
+
def _point_wise_sim(self, user_emb, item_emb):
|
|
124
|
+
user_item_sim = tf.reduce_sum(
|
|
125
|
+
tf.multiply(user_emb, item_emb), axis=1, keep_dims=True)
|
|
126
|
+
return user_item_sim
|
|
127
|
+
|
|
128
|
+
def sim(self, user_emb, item_emb):
|
|
129
|
+
# Name the outputs of the user tower and the item tower, i.e. the inputs of the
|
|
130
|
+
# simularity operation.
|
|
131
|
+
# Explicit names of these nodes are necessary for some online recall systems like
|
|
132
|
+
# BasicEngine to split up the predicting graph into different clusters.
|
|
133
|
+
user_emb = tf.identity(user_emb, 'user_tower_emb')
|
|
134
|
+
item_emb = tf.identity(item_emb, 'item_tower_emb')
|
|
135
|
+
|
|
136
|
+
if self._is_point_wise:
|
|
137
|
+
return self._point_wise_sim(user_emb, item_emb)
|
|
138
|
+
else:
|
|
139
|
+
return self._list_wise_sim(user_emb, item_emb)
|
|
140
|
+
|
|
141
|
+
def norm(self, fea):
|
|
142
|
+
fea_norm = tf.nn.l2_normalize(fea, axis=-1)
|
|
143
|
+
return fea_norm
|
|
144
|
+
|
|
145
|
+
def build_predict_graph(self):
|
|
146
|
+
if not self.has_backbone:
|
|
147
|
+
raise NotImplementedError(
|
|
148
|
+
'method `build_predict_graph` must be implemented when you donot use backbone network'
|
|
149
|
+
)
|
|
150
|
+
model = self._model_config.WhichOneof('model')
|
|
151
|
+
assert model == 'model_params', '`model_params` must be configured'
|
|
152
|
+
model_params = self._model_config.model_params
|
|
153
|
+
for out in model_params.outputs:
|
|
154
|
+
self._outputs.append(out)
|
|
155
|
+
|
|
156
|
+
output = self.backbone
|
|
157
|
+
|
|
158
|
+
user_tower_emb = output[model_params.user_tower_idx_in_output]
|
|
159
|
+
item_tower_emb = output[model_params.item_tower_idx_in_output]
|
|
160
|
+
|
|
161
|
+
if model_params.simi_func == Similarity.COSINE:
|
|
162
|
+
user_tower_emb = self.norm(user_tower_emb)
|
|
163
|
+
item_tower_emb = self.norm(item_tower_emb)
|
|
164
|
+
temperature = model_params.temperature
|
|
165
|
+
else:
|
|
166
|
+
temperature = 1.0
|
|
167
|
+
|
|
168
|
+
user_item_sim = self.sim(user_tower_emb, item_tower_emb) / temperature
|
|
169
|
+
|
|
170
|
+
if model_params.scale_simi:
|
|
171
|
+
sim_w = tf.get_variable(
|
|
172
|
+
'sim_w',
|
|
173
|
+
dtype=tf.float32,
|
|
174
|
+
shape=(1),
|
|
175
|
+
initializer=tf.ones_initializer())
|
|
176
|
+
sim_b = tf.get_variable(
|
|
177
|
+
'sim_b',
|
|
178
|
+
dtype=tf.float32,
|
|
179
|
+
shape=(1),
|
|
180
|
+
initializer=tf.zeros_initializer())
|
|
181
|
+
y_pred = user_item_sim * tf.abs(sim_w) + sim_b
|
|
182
|
+
else:
|
|
183
|
+
y_pred = user_item_sim
|
|
184
|
+
|
|
185
|
+
if self._is_point_wise:
|
|
186
|
+
y_pred = tf.reshape(y_pred, [-1])
|
|
187
|
+
|
|
188
|
+
if self._loss_type == LossType.CLASSIFICATION:
|
|
189
|
+
self._prediction_dict['logits'] = y_pred
|
|
190
|
+
self._prediction_dict['probs'] = tf.nn.sigmoid(y_pred)
|
|
191
|
+
elif self._loss_type == LossType.SOFTMAX_CROSS_ENTROPY:
|
|
192
|
+
y_pred = self._mask_in_batch(y_pred)
|
|
193
|
+
self._prediction_dict['logits'] = y_pred
|
|
194
|
+
self._prediction_dict['probs'] = tf.nn.softmax(y_pred)
|
|
195
|
+
else:
|
|
196
|
+
self._prediction_dict['y'] = y_pred
|
|
197
|
+
|
|
198
|
+
self._prediction_dict['user_tower_emb'] = user_tower_emb
|
|
199
|
+
self._prediction_dict['item_tower_emb'] = item_tower_emb
|
|
200
|
+
self._prediction_dict['user_emb'] = tf.reduce_join(
|
|
201
|
+
tf.as_string(user_tower_emb), axis=-1, separator=',')
|
|
202
|
+
self._prediction_dict['item_emb'] = tf.reduce_join(
|
|
203
|
+
tf.as_string(item_tower_emb), axis=-1, separator=',')
|
|
204
|
+
|
|
205
|
+
return self._prediction_dict
|
|
206
|
+
|
|
207
|
+
def build_loss_graph(self):
|
|
208
|
+
if self._is_point_wise:
|
|
209
|
+
return self._build_point_wise_loss_graph()
|
|
210
|
+
else:
|
|
211
|
+
return self._build_list_wise_loss_graph()
|
|
212
|
+
|
|
213
|
+
def _build_list_wise_loss_graph(self):
|
|
214
|
+
if self._loss_type == LossType.SOFTMAX_CROSS_ENTROPY:
|
|
215
|
+
batch_size = tf.shape(self._prediction_dict['probs'])[0]
|
|
216
|
+
indices = tf.range(batch_size)
|
|
217
|
+
indices = tf.concat([indices[:, None], indices[:, None]], axis=1)
|
|
218
|
+
hit_prob = tf.gather_nd(
|
|
219
|
+
self._prediction_dict['probs'][:batch_size, :batch_size], indices)
|
|
220
|
+
|
|
221
|
+
sample_weights = tf.cast(tf.squeeze(self._sample_weight), tf.float32)
|
|
222
|
+
self._loss_dict['cross_entropy_loss'] = -tf.reduce_mean(
|
|
223
|
+
tf.log(hit_prob + 1e-12) *
|
|
224
|
+
sample_weights) / tf.reduce_mean(sample_weights)
|
|
225
|
+
|
|
226
|
+
logging.info('softmax cross entropy loss is used')
|
|
227
|
+
|
|
228
|
+
user_features = self._prediction_dict['user_tower_emb']
|
|
229
|
+
pos_item_emb = self._prediction_dict['item_tower_emb'][:batch_size]
|
|
230
|
+
pos_simi = tf.reduce_sum(user_features * pos_item_emb, axis=1)
|
|
231
|
+
# if pos_simi < 0, produce loss
|
|
232
|
+
reg_pos_loss = tf.nn.relu(-pos_simi)
|
|
233
|
+
self._loss_dict['reg_pos_loss'] = tf.reduce_mean(
|
|
234
|
+
reg_pos_loss * sample_weights) / tf.reduce_mean(sample_weights)
|
|
235
|
+
|
|
236
|
+
# the AMM loss for DAT model
|
|
237
|
+
if all([
|
|
238
|
+
k in self._prediction_dict.keys() for k in
|
|
239
|
+
['augmented_p_u', 'augmented_p_i', 'augmented_a_u', 'augmented_a_i']
|
|
240
|
+
]):
|
|
241
|
+
self._loss_dict[
|
|
242
|
+
'amm_loss_u'] = self._model_config.amm_u_weight * tf.reduce_mean(
|
|
243
|
+
tf.square(self._prediction_dict['augmented_a_u'] -
|
|
244
|
+
self._prediction_dict['augmented_p_i'][:batch_size]) *
|
|
245
|
+
sample_weights) / tf.reduce_mean(sample_weights)
|
|
246
|
+
self._loss_dict[
|
|
247
|
+
'amm_loss_i'] = self._model_config.amm_i_weight * tf.reduce_mean(
|
|
248
|
+
tf.square(self._prediction_dict['augmented_a_i'][:batch_size] -
|
|
249
|
+
self._prediction_dict['augmented_p_u']) *
|
|
250
|
+
sample_weights) / tf.reduce_mean(sample_weights)
|
|
251
|
+
|
|
252
|
+
else:
|
|
253
|
+
raise ValueError('invalid loss type: %s' % str(self._loss_type))
|
|
254
|
+
return self._loss_dict
|
|
255
|
+
|
|
256
|
+
def _build_point_wise_loss_graph(self):
|
|
257
|
+
label = list(self._labels.values())[0]
|
|
258
|
+
if self._loss_type == LossType.CLASSIFICATION:
|
|
259
|
+
pred = self._prediction_dict['logits']
|
|
260
|
+
loss_name = 'cross_entropy_loss'
|
|
261
|
+
elif self._loss_type == LossType.L2_LOSS:
|
|
262
|
+
pred = self._prediction_dict['y']
|
|
263
|
+
loss_name = 'l2_loss'
|
|
264
|
+
else:
|
|
265
|
+
raise ValueError('invalid loss type: %s' % str(self._loss_type))
|
|
266
|
+
|
|
267
|
+
kwargs = {'loss_name': loss_name}
|
|
268
|
+
self._loss_dict[loss_name] = loss_builder.build(
|
|
269
|
+
self._loss_type,
|
|
270
|
+
label=label,
|
|
271
|
+
pred=pred,
|
|
272
|
+
loss_weight=self._sample_weight,
|
|
273
|
+
**kwargs)
|
|
274
|
+
|
|
275
|
+
# build kd loss
|
|
276
|
+
kd_loss_dict = loss_builder.build_kd_loss(self.kd, self._prediction_dict,
|
|
277
|
+
self._labels, self._feature_dict)
|
|
278
|
+
self._loss_dict.update(kd_loss_dict)
|
|
279
|
+
return self._loss_dict
|
|
280
|
+
|
|
281
|
+
def build_metric_graph(self, eval_config):
|
|
282
|
+
if self._is_point_wise:
|
|
283
|
+
return self._build_point_wise_metric_graph(eval_config)
|
|
284
|
+
else:
|
|
285
|
+
return self._build_list_wise_metric_graph(eval_config)
|
|
286
|
+
|
|
287
|
+
def _build_list_wise_metric_graph(self, eval_config):
|
|
288
|
+
from easy_rec.python.core.easyrec_metrics import metrics_tf
|
|
289
|
+
logits = self._prediction_dict['logits']
|
|
290
|
+
# label = tf.zeros_like(logits[:, :1], dtype=tf.int64)
|
|
291
|
+
batch_size = tf.shape(logits)[0]
|
|
292
|
+
label = tf.cast(tf.range(batch_size), tf.int64)
|
|
293
|
+
|
|
294
|
+
indices = tf.range(batch_size)
|
|
295
|
+
indices = tf.concat([indices[:, None], indices[:, None]], axis=1)
|
|
296
|
+
pos_item_sim = tf.gather_nd(logits[:batch_size, :batch_size], indices)
|
|
297
|
+
metric_dict = {}
|
|
298
|
+
for metric in eval_config.metrics_set:
|
|
299
|
+
if metric.WhichOneof('metric') == 'recall_at_topk':
|
|
300
|
+
metric_dict['recall@%d' %
|
|
301
|
+
metric.recall_at_topk.topk] = metrics_tf.recall_at_k(
|
|
302
|
+
label, logits, metric.recall_at_topk.topk)
|
|
303
|
+
|
|
304
|
+
logits_v2 = tf.concat([pos_item_sim[:, None], logits[:, batch_size:]],
|
|
305
|
+
axis=1)
|
|
306
|
+
labels_v2 = tf.zeros_like(logits_v2[:, :1], dtype=tf.int64)
|
|
307
|
+
metric_dict['recall_neg_sam@%d' %
|
|
308
|
+
metric.recall_at_topk.topk] = metrics_tf.recall_at_k(
|
|
309
|
+
labels_v2, logits_v2, metric.recall_at_topk.topk)
|
|
310
|
+
|
|
311
|
+
metric_dict['recall_in_batch@%d' %
|
|
312
|
+
metric.recall_at_topk.topk] = metrics_tf.recall_at_k(
|
|
313
|
+
label, logits[:, :batch_size],
|
|
314
|
+
metric.recall_at_topk.topk)
|
|
315
|
+
else:
|
|
316
|
+
raise ValueError('invalid metric type: %s' % str(metric))
|
|
317
|
+
return metric_dict
|
|
318
|
+
|
|
319
|
+
def _build_point_wise_metric_graph(self, eval_config):
|
|
320
|
+
from easy_rec.python.core.easyrec_metrics import metrics_tf
|
|
321
|
+
metric_dict = {}
|
|
322
|
+
label = list(self._labels.values())[0]
|
|
323
|
+
for metric in eval_config.metrics_set:
|
|
324
|
+
if metric.WhichOneof('metric') == 'auc':
|
|
325
|
+
assert self._loss_type == LossType.CLASSIFICATION
|
|
326
|
+
metric_dict['auc'] = metrics_tf.auc(label,
|
|
327
|
+
self._prediction_dict['probs'])
|
|
328
|
+
elif metric.WhichOneof('metric') == 'mean_absolute_error':
|
|
329
|
+
assert self._loss_type == LossType.L2_LOSS
|
|
330
|
+
metric_dict['mean_absolute_error'] = metrics_tf.mean_absolute_error(
|
|
331
|
+
tf.to_float(label), self._prediction_dict['y'])
|
|
332
|
+
else:
|
|
333
|
+
raise ValueError('invalid metric type: %s' % str(metric))
|
|
334
|
+
return metric_dict
|
|
335
|
+
|
|
336
|
+
def get_outputs(self):
|
|
337
|
+
if not self.has_backbone:
|
|
338
|
+
raise NotImplementedError(
|
|
339
|
+
'could not call get_outputs on abstract class MatchModel')
|
|
340
|
+
if self._loss_type == LossType.CLASSIFICATION:
|
|
341
|
+
return [
|
|
342
|
+
'logits', 'probs', 'user_emb', 'item_emb', 'user_tower_emb',
|
|
343
|
+
'item_tower_emb'
|
|
344
|
+
]
|
|
345
|
+
elif self._loss_type == LossType.SOFTMAX_CROSS_ENTROPY:
|
|
346
|
+
self._prediction_dict['logits'] = tf.squeeze(
|
|
347
|
+
self._prediction_dict['logits'], axis=-1)
|
|
348
|
+
self._prediction_dict['probs'] = tf.nn.sigmoid(
|
|
349
|
+
self._prediction_dict['logits'])
|
|
350
|
+
return [
|
|
351
|
+
'logits', 'probs', 'user_emb', 'item_emb', 'user_tower_emb',
|
|
352
|
+
'item_tower_emb'
|
|
353
|
+
]
|
|
354
|
+
elif self._loss_type == LossType.L2_LOSS:
|
|
355
|
+
return ['y', 'user_emb', 'item_emb', 'user_tower_emb', 'item_tower_emb']
|
|
356
|
+
else:
|
|
357
|
+
raise ValueError('invalid loss type: %s' % str(self._loss_type))
|