Skip to content

Commit 66d173e

Browse files
authored
v 0.8.0
- Support `Tensorflow Estimator` for large scale data and distributed training. - Support different initializers for different embedding weights by settting `embeddings_initializer` of `SparseFeat` - Support using pretrained weights to initialize embedding weights and frozen embedding weights. - Add new model `FwFM` - API changes
2 parents 9e6be3f + 7c8ad48 commit 66d173e

File tree

114 files changed

+2914
-568
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+2914
-568
lines changed

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ Steps to reproduce the behavior:
1818
4. See error
1919

2020
**Operating environment(运行环境):**
21-
- python version [e.g. 3.4, 3.6]
22-
- tensorflow version [e.g. 1.4.0, 1.12.0]
23-
- deepctr version [e.g. 0.7.1,]
21+
- python version [e.g. 3.5, 3.7]
22+
- tensorflow version [e.g. 1.4.0, 1.15.0, 2.2.0]
23+
- deepctr version [e.g. 0.8.0,]
2424

2525
**Additional context**
2626
Add any other context about the problem here.

.github/ISSUE_TEMPLATE/question.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ Add any other context about the problem here.
1616

1717
**Operating environment(运行环境):**
1818
- python version [e.g. 3.6]
19-
- tensorflow version [e.g. 1.4.0,]
20-
- deepctr version [e.g. 0.7.1,]
19+
- tensorflow version [e.g. 1.4.0, 1.5.0, 2.2.0]
20+
- deepctr version [e.g. 0.8.0,]

CONTRIBUTING.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
This project is under development and we need developers to participate in.
2+
3+
If you
4+
5+
- familiar with and interested in ctr prediction algorithms
6+
- familiar with tensorflow
7+
- have spare time to learn and develop
8+
- familiar with git
9+
10+
please send a brief introduction of your background and experience to [email protected], welcome to join us!

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@
1818
<!-- [![Gitter](https://badges.gitter.im/DeepCTR/community.svg)](https://gitter.im/DeepCTR/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) -->
1919

2020

21-
DeepCTR is a **Easy-to-use**,**Modular** and **Extendible** package of deep-learning based CTR models along with lots of core components layers which can be used to easily build custom models.It is compatible with **tensorflow 1.4+ and 2.0+**.You can use any complex model with `model.fit()`and `model.predict()` .
21+
DeepCTR is a **Easy-to-use**,**Modular** and **Extendible** package of deep-learning based CTR models along with lots of core components layers which can be used to easily build custom models.You can use any complex model with `model.fit()`and `model.predict()` .
2222

23-
Let's [**Get Started!**](https://deepctr-doc.readthedocs.io/en/latest/Quick-Start.html)([Chinese Introduction](https://zhuanlan.zhihu.com/p/53231955))
23+
- Provide `tf.keras.Model` like interface for **quick experiment**. [example](https://deepctr-doc.readthedocs.io/en/latest/Quick-Start.html#getting-started-4-steps-to-deepctr)
24+
- Provide `tensorflow estimator` interface for **large scale data** and **distributed training**. [example](https://deepctr-doc.readthedocs.io/en/latest/Quick-Start.html#getting-started-4-steps-to-deepctr-estimator-with-tfrecord)
25+
- It is compatible with both `tf 1.x` and `tf 2.x`.
2426

2527

28+
29+
30+
Let's [**Get Started!**](https://deepctr-doc.readthedocs.io/en/latest/Quick-Start.html)([Chinese Introduction](https://zhuanlan.zhihu.com/p/53231955)) and [welcome to join us!](./CONTRIBUTING.md)
31+
2632
## Models List
2733

2834
| Model | Paper |
@@ -40,6 +46,7 @@ Let's [**Get Started!**](https://deepctr-doc.readthedocs.io/en/latest/Quick-Star
4046
| AutoInt | [arxiv 2018][AutoInt: Automatic Feature Interaction Learning via Self-Attentive Neural Networks](https://arxiv.org/abs/1810.11921) |
4147
| Deep Interest Network | [KDD 2018][Deep Interest Network for Click-Through Rate Prediction](https://arxiv.org/pdf/1706.06978.pdf) |
4248
| Deep Interest Evolution Network | [AAAI 2019][Deep Interest Evolution Network for Click-Through Rate Prediction](https://arxiv.org/pdf/1809.03672.pdf) |
49+
| FwFM | [WWW 2018][Field-weighted Factorization Machines for Click-Through Rate Prediction in Display Advertising](https://arxiv.org/pdf/1806.03514.pdf) |
4350
| ONN | [arxiv 2019][Operation-aware Neural Networks for User Response Prediction](https://arxiv.org/pdf/1904.12579.pdf) |
4451
| FGCNN | [WWW 2019][Feature Generation by Convolutional Neural Network for Click-Through Rate Prediction ](https://arxiv.org/pdf/1904.04447) |
4552
| Deep Session Interest Network | [IJCAI 2019][Deep Session Interest Network for Click-Through Rate Prediction ](https://arxiv.org/abs/1905.06482) |

deepctr/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from .utils import check_version
22

3-
__version__ = '0.7.5'
3+
__version__ = '0.8.0'
44
check_version(__version__)

deepctr/estimator/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .models import *
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import tensorflow as tf
2+
from tensorflow.python.feature_column.feature_column import _EmbeddingColumn
3+
4+
from .utils import LINEAR_SCOPE_NAME, variable_scope, get_collection, get_GraphKeys, input_layer, get_losses
5+
6+
7+
def linear_model(features, linear_feature_columns):
8+
if tf.__version__ >= '2.0.0':
9+
linear_logits = tf.compat.v1.feature_column.linear_model(features, linear_feature_columns)
10+
else:
11+
linear_logits = tf.feature_column.linear_model(features, linear_feature_columns)
12+
return linear_logits
13+
14+
15+
def get_linear_logit(features, linear_feature_columns, l2_reg_linear=0):
16+
with variable_scope(LINEAR_SCOPE_NAME):
17+
if not linear_feature_columns:
18+
linear_logits = tf.Variable([[0.0]], name='bias_weights')
19+
else:
20+
21+
linear_logits = linear_model(features, linear_feature_columns)
22+
23+
if l2_reg_linear > 0:
24+
for var in get_collection(get_GraphKeys().TRAINABLE_VARIABLES, LINEAR_SCOPE_NAME)[:-1]:
25+
get_losses().add_loss(l2_reg_linear * tf.nn.l2_loss(var, name=var.name.split(":")[0] + "_l2loss"),
26+
get_GraphKeys().REGULARIZATION_LOSSES)
27+
return linear_logits
28+
29+
30+
def input_from_feature_columns(features, feature_columns, l2_reg_embedding=0.0):
31+
dense_value_list = []
32+
sparse_emb_list = []
33+
for feat in feature_columns:
34+
if is_embedding(feat):
35+
sparse_emb = tf.expand_dims(input_layer(features, [feat]), axis=1)
36+
sparse_emb_list.append(sparse_emb)
37+
if l2_reg_embedding > 0:
38+
get_losses().add_loss(l2_reg_embedding * tf.nn.l2_loss(sparse_emb, name=feat.name + "_l2loss"),
39+
get_GraphKeys().REGULARIZATION_LOSSES)
40+
41+
else:
42+
dense_value_list.append(input_layer(features, [feat]))
43+
44+
return sparse_emb_list, dense_value_list
45+
46+
47+
def is_embedding(feature_column):
48+
try:
49+
from tensorflow.python.feature_column.feature_column_v2 import EmbeddingColumn
50+
except:
51+
EmbeddingColumn = _EmbeddingColumn
52+
return isinstance(feature_column, (_EmbeddingColumn, EmbeddingColumn))

deepctr/estimator/inputs.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import tensorflow as tf
2+
3+
4+
def input_fn_pandas(df, features, label=None, batch_size=256, num_epochs=1, shuffle=False, queue_capacity_factor=10,
5+
num_threads=1):
6+
if label is not None:
7+
y = df[label]
8+
else:
9+
y = None
10+
if tf.__version__ >= "2.0.0":
11+
return tf.compat.v1.estimator.inputs.pandas_input_fn(df[features], y, batch_size=batch_size,
12+
num_epochs=num_epochs,
13+
shuffle=shuffle,
14+
queue_capacity=batch_size * queue_capacity_factor,
15+
num_threads=num_threads)
16+
17+
return tf.estimator.inputs.pandas_input_fn(df[features], y, batch_size=batch_size, num_epochs=num_epochs,
18+
shuffle=shuffle, queue_capacity=batch_size * queue_capacity_factor,
19+
num_threads=num_threads)
20+
21+
22+
def input_fn_tfrecord(filenames, feature_description, label=None, batch_size=256, num_epochs=1, num_parallel_calls=8,
23+
shuffle_factor=10, prefetch_factor=1,
24+
):
25+
def _parse_examples(serial_exmp):
26+
features = tf.parse_single_example(serial_exmp, features=feature_description)
27+
if label is not None:
28+
labels = features.pop(label)
29+
return features, labels
30+
return features
31+
32+
def input_fn():
33+
dataset = tf.data.TFRecordDataset(filenames)
34+
dataset = dataset.map(_parse_examples, num_parallel_calls=num_parallel_calls)
35+
if shuffle_factor > 0:
36+
dataset = dataset.shuffle(buffer_size=batch_size * shuffle_factor)
37+
38+
dataset = dataset.repeat(num_epochs).batch(batch_size)
39+
40+
if prefetch_factor > 0:
41+
dataset = dataset.prefetch(buffer_size=batch_size * prefetch_factor)
42+
43+
iterator = dataset.make_one_shot_iterator()
44+
45+
return iterator.get_next()
46+
47+
return input_fn
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from .afm import AFMEstimator
2+
from .autoint import AutoIntEstimator
3+
from .ccpm import CCPMEstimator
4+
from .dcn import DCNEstimator
5+
from .deepfm import DeepFMEstimator
6+
from .fwfm import FwFMEstimator
7+
from .fibinet import FiBiNETEstimator
8+
from .fnn import FNNEstimator
9+
from .nfm import NFMEstimator
10+
from .pnn import PNNEstimator
11+
from .wdl import WDLEstimator
12+
from .xdeepfm import xDeepFMEstimator

deepctr/estimator/models/afm.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# -*- coding:utf-8 -*-
2+
"""
3+
4+
Author:
5+
Weichen Shen,[email protected]
6+
7+
Reference:
8+
[1] Xiao J, Ye H, He X, et al. Attentional factorization machines: Learning the weight of feature interactions via attention networks[J]. arXiv preprint arXiv:1708.04617, 2017.
9+
(https://arxiv.org/abs/1708.04617)
10+
11+
"""
12+
import tensorflow as tf
13+
14+
from ..feature_column import get_linear_logit, input_from_feature_columns
15+
from ..utils import deepctr_model_fn, DNN_SCOPE_NAME, variable_scope
16+
from ...layers.interaction import AFMLayer, FM
17+
from ...layers.utils import concat_func
18+
19+
20+
def AFMEstimator(linear_feature_columns, dnn_feature_columns, use_attention=True, attention_factor=8,
21+
l2_reg_linear=1e-5, l2_reg_embedding=1e-5, l2_reg_att=1e-5, afm_dropout=0, seed=1024,
22+
task='binary', model_dir=None, config=None, linear_optimizer='Ftrl',
23+
dnn_optimizer='Adagrad', training_chief_hooks=None):
24+
"""Instantiates the Attentional Factorization Machine architecture.
25+
26+
:param linear_feature_columns: An iterable containing all the features used by linear part of the model.
27+
:param dnn_feature_columns: An iterable containing all the features used by deep part of the model.
28+
:param use_attention: bool,whether use attention or not,if set to ``False``.it is the same as **standard Factorization Machine**
29+
:param attention_factor: positive integer,units in attention net
30+
:param l2_reg_linear: float. L2 regularizer strength applied to linear part
31+
:param l2_reg_embedding: float. L2 regularizer strength applied to embedding vector
32+
:param l2_reg_att: float. L2 regularizer strength applied to attention net
33+
:param afm_dropout: float in [0,1), Fraction of the attention net output units to dropout.
34+
:param seed: integer ,to use as random seed.
35+
:param task: str, ``"binary"`` for binary logloss or ``"regression"`` for regression loss
36+
:param model_dir: Directory to save model parameters, graph and etc. This can
37+
also be used to load checkpoints from the directory into a estimator
38+
to continue training a previously saved model.
39+
:param config: tf.RunConfig object to configure the runtime settings.
40+
:param linear_optimizer: An instance of `tf.Optimizer` used to apply gradients to
41+
the linear part of the model. Defaults to FTRL optimizer.
42+
:param dnn_optimizer: An instance of `tf.Optimizer` used to apply gradients to
43+
the deep part of the model. Defaults to Adagrad optimizer.
44+
:param training_chief_hooks: Iterable of `tf.train.SessionRunHook` objects to
45+
run on the chief worker during training.
46+
:return: A Tensorflow Estimator instance.
47+
48+
"""
49+
50+
def _model_fn(features, labels, mode, config):
51+
train_flag = (mode == tf.estimator.ModeKeys.TRAIN)
52+
53+
linear_logits = get_linear_logit(features, linear_feature_columns, l2_reg_linear=l2_reg_linear)
54+
55+
with variable_scope(DNN_SCOPE_NAME):
56+
sparse_embedding_list, dense_value_list = input_from_feature_columns(features, dnn_feature_columns,
57+
l2_reg_embedding=l2_reg_embedding)
58+
if use_attention:
59+
60+
fm_logit = AFMLayer(attention_factor, l2_reg_att, afm_dropout,
61+
seed)(sparse_embedding_list, training=train_flag)
62+
else:
63+
fm_logit = FM()(concat_func(sparse_embedding_list, axis=1))
64+
65+
logits = linear_logits + fm_logit
66+
67+
return deepctr_model_fn(features, mode, logits, labels, task, linear_optimizer, dnn_optimizer,
68+
training_chief_hooks=training_chief_hooks)
69+
70+
return tf.estimator.Estimator(_model_fn, model_dir=model_dir, config=config)

0 commit comments

Comments
 (0)