Skip to content

Commit 9e6be3f

Browse files
authored
v 0.7.5
- Fix numerical instability in `LayerNormalization` - update `PNN` api
1 parent df06f25 commit 9e6be3f

15 files changed

+67
-47
lines changed

.github/workflows/ci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ jobs:
1717
timeout-minutes: 120
1818
strategy:
1919
matrix:
20-
python-version: [3.5,3.6,3.7]
21-
tf-version: [1.4.0,1.15.0,2.0.0]
20+
python-version: [3.6,3.7]
21+
tf-version: [1.4.0,1.15.0,2.1.0,2.2.0]
2222

2323
exclude:
2424
- python-version: 3.7

deepctr/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from .utils import check_version
22

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

deepctr/layers/normalization.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212

1313

1414
class LayerNormalization(Layer):
15-
def __init__(self, axis=-1, eps=1e-9, **kwargs):
15+
def __init__(self, axis=-1, eps=1e-9, center=True,
16+
scale=True, **kwargs):
1617
self.axis = axis
1718
self.eps = eps
19+
self.center = center
20+
self.scale = scale
1821
super(LayerNormalization, self).__init__(**kwargs)
1922

2023
def build(self, input_shape):
@@ -24,15 +27,21 @@ def build(self, input_shape):
2427
initializer=Zeros(), trainable=True)
2528
super(LayerNormalization, self).build(input_shape)
2629

27-
def call(self, x):
28-
mean = K.mean(x, axis=self.axis, keepdims=True)
29-
std = K.std(x, axis=self.axis, keepdims=True)
30-
return self.gamma * (x - mean) / (std + self.eps) + self.beta
30+
def call(self, inputs):
31+
mean = K.mean(inputs, axis=self.axis, keepdims=True)
32+
variance = K.mean(K.square(inputs - mean), axis=-1, keepdims=True)
33+
std = K.sqrt(variance + self.eps)
34+
outputs = (inputs - mean) / std
35+
if self.scale:
36+
outputs *= self.gamma
37+
if self.center:
38+
outputs += self.beta
39+
return outputs
3140

3241
def compute_output_shape(self, input_shape):
3342
return input_shape
3443

3544
def get_config(self, ):
36-
config = {'axis': self.axis, 'eps': self.eps}
45+
config = {'axis': self.axis, 'eps': self.eps, 'center': self.center, 'scale': self.scale}
3746
base_config = super(LayerNormalization, self).get_config()
3847
return dict(list(base_config.items()) + list(config.items()))

deepctr/models/pnn.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@
1515
from ..layers.utils import concat_func
1616

1717

18-
def PNN(dnn_feature_columns, embedding_size=8, dnn_hidden_units=(128, 128), l2_reg_embedding=1e-5, l2_reg_dnn=0,
19-
init_std=0.0001, seed=1024, dnn_dropout=0, dnn_activation='relu', use_inner=True, use_outter=False,
20-
kernel_type='mat', task='binary'):
18+
def PNN(dnn_feature_columns, dnn_hidden_units=(128, 128), l2_reg_embedding=1e-5, l2_reg_dnn=0, init_std=0.0001,
19+
seed=1024, dnn_dropout=0, dnn_activation='relu', use_inner=True, use_outter=False, kernel_type='mat',
20+
task='binary'):
2121
"""Instantiates the Product-based Neural Network architecture.
2222
2323
:param dnn_feature_columns: An iterable containing all the features used by deep part of the model.
24-
:param embedding_size: positive integer,sparse feature embedding_size
2524
:param dnn_hidden_units: list,list of positive integer or empty list, the layer number and units in each layer of deep net
2625
:param l2_reg_embedding: float . L2 regularizer strength applied to embedding vector
2726
:param l2_reg_dnn: float. L2 regularizer strength applied to DNN
@@ -51,7 +50,7 @@ def PNN(dnn_feature_columns, embedding_size=8, dnn_hidden_units=(128, 128), l2_r
5150

5251
# ipnn deep input
5352
linear_signal = tf.keras.layers.Reshape(
54-
[len(sparse_embedding_list) * embedding_size])(concat_func(sparse_embedding_list))
53+
[sum(map(lambda x:int(x.shape[-1]) ,sparse_embedding_list))])(concat_func(sparse_embedding_list))
5554

5655
if use_inner and use_outter:
5756
deep_input = tf.keras.layers.Concatenate()(

docs/source/History.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# History
2+
3+
- 05/17/2020 : [v0.7.5](https://github.com/shenweichen/DeepCTR/releases/tag/v0.7.5) released.Fix numerical instability in `LayerNormalization`.
24
- 03/15/2020 : [v0.7.4](https://github.com/shenweichen/DeepCTR/releases/tag/v0.7.4) released.Add [FLEN](./Features.html#flen-field-leveraged-embedding-network) and `FieldWiseBiInteraction`.
35
- 03/04/2020 : [v0.7.3](https://github.com/shenweichen/DeepCTR/releases/tag/v0.7.3) released.Fix the inconsistency of prediction results when the model is loaded with trained weights.
46
- 02/08/2020 : [v0.7.2](https://github.com/shenweichen/DeepCTR/releases/tag/v0.7.2) released.Fix some bugs.

docs/source/Quick-Start.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ fixlen_feature_columns = [SparseFeat(feat, vocabulary_size=1e6,embedding_dim=4,
9292
```
9393
- generate feature columns
9494
```python
95-
dnn_feature_columns = sparse_feature_columns + dense_feature_columns
96-
linear_feature_columns = sparse_feature_columns + dense_feature_columns
95+
dnn_feature_columns = fixlen_feature_columns
96+
linear_feature_columns = fixlen_feature_columns
9797

9898
feature_names = get_feature_names(linear_feature_columns + dnn_feature_columns)
9999

docs/source/conf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
# The short X.Y version
2727
version = ''
2828
# The full version, including alpha/beta/rc tags
29-
release = '0.7.4'
29+
release = '0.7.5'
3030

3131

3232
# -- General configuration ---------------------------------------------------

docs/source/index.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ You can read the latest code at https://github.com/shenweichen/DeepCTR
3535
News
3636
-----
3737

38+
05/17/2020 : Fix numerical instability in ``LayerNormalization``. `Changelog <https://github.com/shenweichen/DeepCTR/releases/tag/v0.7.5>`_
39+
3840
03/15/2020 : Add `FLEN <./Features.html#flen-field-leveraged-embedding-network>`_ (`中文介绍 <https://zhuanlan.zhihu.com/p/92787577>`_) and ``FieldWiseBiInteraction``. `Changelog <https://github.com/shenweichen/DeepCTR/releases/tag/v0.7.4>`_
3941

4042
03/04/2020 : Fix the inconsistency of prediction results when the model is loaded with trained weights. `Changelog <https://github.com/shenweichen/DeepCTR/releases/tag/v0.7.3>`_
4143

42-
02/08/2020 : Fix some bugs. `Changelog <https://github.com/shenweichen/DeepCTR/releases/tag/v0.7.2>`_
43-
4444
DisscussionGroup
4545
-----------------------
4646

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
setuptools.setup(
1111
name="deepctr",
12-
version="0.7.4",
12+
version="0.7.5",
1313
author="Weichen Shen",
1414
author_email="[email protected]",
1515
description="Easy-to-use,Modular and Extendible package of deep learning based CTR(Click Through Rate) prediction models with tensorflow 1.x and 2.x .",

tests/layers/activations_test.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
from deepctr.layers import activation
2-
from tensorflow.python.keras.utils import CustomObjectScope
2+
try:
3+
from tensorflow.python.keras.utils import CustomObjectScope
4+
except:
5+
from tensorflow.keras.utils import CustomObjectScope
36
from tests.utils import layer_test
47

58

tests/layers/core_test.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import pytest
22
import tensorflow as tf
33
from tensorflow.python.keras.layers import PReLU
4-
from tensorflow.python.keras.utils import CustomObjectScope
5-
4+
try:
5+
from tensorflow.python.keras.utils import CustomObjectScope
6+
except:
7+
from tensorflow.keras.utils import CustomObjectScope
68
from deepctr import layers
79
from deepctr.layers import Dice
810
from tests.layers.interaction_test import BATCH_SIZE, EMBEDDING_SIZE, SEQ_LENGTH

tests/layers/interaction_test.py

+20-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import pytest
2-
#from tensorflow.python.keras.layers import PReLU
3-
from tensorflow.python.keras.utils import CustomObjectScope
42

3+
try:
4+
from tensorflow.python.keras.utils import CustomObjectScope
5+
except:
6+
from tensorflow.keras.utils import CustomObjectScope
57
from deepctr import layers
68

79
from tests.utils import layer_test
@@ -16,13 +18,13 @@
1618
1719
'layer_num',
1820
19-
[0,1]
21+
[0, 1]
2022
2123
)
22-
def test_CrossNet(layer_num,):
24+
def test_CrossNet(layer_num, ):
2325
with CustomObjectScope({'CrossNet': layers.CrossNet}):
2426
layer_test(layers.CrossNet, kwargs={
25-
'layer_num': layer_num, }, input_shape=(2, 3))
27+
'layer_num': layer_num, }, input_shape=(2, 3))
2628

2729

2830
# def test_CrossNet_invalid():
@@ -41,7 +43,7 @@ def test_CrossNet(layer_num,):
4143
def test_InnerProductLayer(reduce_sum):
4244
with CustomObjectScope({'InnerProductLayer': layers.InnerProductLayer}):
4345
layer_test(layers.InnerProductLayer, kwargs={
44-
'reduce_sum': reduce_sum}, input_shape=[(BATCH_SIZE, 1, EMBEDDING_SIZE)]*FIELD_SIZE)
46+
'reduce_sum': reduce_sum}, input_shape=[(BATCH_SIZE, 1, EMBEDDING_SIZE)] * FIELD_SIZE)
4547

4648

4749
@pytest.mark.parametrize(
@@ -53,7 +55,7 @@ def test_InnerProductLayer(reduce_sum):
5355
def test_OutterProductLayer(kernel_type):
5456
with CustomObjectScope({'OutterProductLayer': layers.OutterProductLayer}):
5557
layer_test(layers.OutterProductLayer, kwargs={
56-
'kernel_type': kernel_type}, input_shape=[(BATCH_SIZE, 1, EMBEDDING_SIZE)]*FIELD_SIZE)
58+
'kernel_type': kernel_type}, input_shape=[(BATCH_SIZE, 1, EMBEDDING_SIZE)] * FIELD_SIZE)
5759

5860

5961
def test_BiInteractionPooling():
@@ -70,13 +72,13 @@ def test_FM():
7072

7173
def test_AFMLayer():
7274
with CustomObjectScope({'AFMLayer': layers.AFMLayer}):
73-
layer_test(layers.AFMLayer, kwargs={'dropout_rate':0.5}, input_shape=[(
74-
BATCH_SIZE, 1, EMBEDDING_SIZE)]*FIELD_SIZE)
75+
layer_test(layers.AFMLayer, kwargs={'dropout_rate': 0.5}, input_shape=[(
76+
BATCH_SIZE, 1, EMBEDDING_SIZE)] * FIELD_SIZE)
7577

7678

7779
@pytest.mark.parametrize(
7880
'layer_size,split_half',
79-
[((10,),False),((10,8),True)
81+
[((10,), False), ((10, 8), True)
8082
]
8183
)
8284
def test_CIN(layer_size, split_half):
@@ -99,17 +101,18 @@ def test_CIN(layer_size, split_half):
99101

100102
@pytest.mark.parametrize(
101103
'head_num,use_res',
102-
[(1,True),(2,False,)]
104+
[(1, True), (2, False,)]
103105
)
104-
def test_InteractingLayer(head_num, use_res,):
106+
def test_InteractingLayer(head_num, use_res, ):
105107
with CustomObjectScope({'InteractingLayer': layers.InteractingLayer}):
106108
layer_test(layers.InteractingLayer, kwargs={"head_num": head_num, "use_res":
107-
use_res, }, input_shape=(
109+
use_res, }, input_shape=(
108110
BATCH_SIZE, FIELD_SIZE, EMBEDDING_SIZE))
109111

112+
110113
def test_FGCNNLayer():
111114
with CustomObjectScope({'FGCNNLayer': layers.FGCNNLayer}):
112-
layer_test(layers.FGCNNLayer, kwargs={'filters':(4, 6,),'kernel_width':(7, 7,)}, input_shape=(
115+
layer_test(layers.FGCNNLayer, kwargs={'filters': (4, 6,), 'kernel_width': (7, 7,)}, input_shape=(
113116
BATCH_SIZE, FIELD_SIZE, EMBEDDING_SIZE))
114117

115118

@@ -121,12 +124,10 @@ def test_FGCNNLayer():
121124

122125
@pytest.mark.parametrize(
123126
'bilinear_type',
124-
['all','each','interaction'
127+
['all', 'each', 'interaction'
125128
]
126129
)
127130
def test_BilinearInteraction(bilinear_type):
128131
with CustomObjectScope({'BilinearInteraction': layers.BilinearInteraction}):
129-
layer_test(layers.BilinearInteraction, kwargs={'bilinear_type':bilinear_type}, input_shape=[(
130-
BATCH_SIZE, 1, EMBEDDING_SIZE)]*FIELD_SIZE)
131-
132-
132+
layer_test(layers.BilinearInteraction, kwargs={'bilinear_type': bilinear_type}, input_shape=[(
133+
BATCH_SIZE, 1, EMBEDDING_SIZE)] * FIELD_SIZE)

tests/layers/normalization_test.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import pytest
2-
from tensorflow.python.keras.utils import CustomObjectScope
3-
2+
try:
3+
from tensorflow.python.keras.utils import CustomObjectScope
4+
except:
5+
from tensorflow.keras.utils import CustomObjectScope
46
from deepctr import layers
57
from tests.layers.interaction_test import BATCH_SIZE, FIELD_SIZE, EMBEDDING_SIZE
68
from tests.utils import layer_test

tests/layers/sequence_test.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import pytest
22
from packaging import version
3-
from tensorflow.python.keras.utils import CustomObjectScope
3+
try:
4+
from tensorflow.python.keras.utils import CustomObjectScope
5+
except:
6+
from tensorflow.keras.utils import CustomObjectScope
47
import tensorflow as tf
58
from deepctr.layers import sequence
69

tests/models/PNN_test.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ def test_PNN(use_inner, use_outter, sparse_feature_num):
1414
sample_size = SAMPLE_SIZE
1515
x, y, feature_columns = get_test_data(sample_size, sparse_feature_num=sparse_feature_num,
1616
dense_feature_num=sparse_feature_num)
17-
model = PNN(feature_columns, embedding_size=4, dnn_hidden_units=[4, 4], dnn_dropout=0.5, use_inner=use_inner,
18-
use_outter=use_outter)
17+
model = PNN(feature_columns, dnn_hidden_units=[4, 4], dnn_dropout=0.5, use_inner=use_inner, use_outter=use_outter)
1918
check_model(model, model_name, x, y)
2019

2120

0 commit comments

Comments
 (0)