Skip to content

Commit 9564e05

Browse files
authored
improve compatibility
improve compatibility with tensorflow 2.x
1 parent cd9643c commit 9564e05

Some content is hidden

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

69 files changed

+411
-333
lines changed

.github/workflows/ci.yml

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,45 @@ jobs:
1717
timeout-minutes: 180
1818
strategy:
1919
matrix:
20-
python-version: [3.6,3.7]
21-
tf-version: [1.4.0,1.15.0,2.2.0,2.5.0]
20+
python-version: [3.6,3.7,3.8]
21+
tf-version: [1.4.0,1.15.0,2.5.0,2.6.0,2.7.0,2.8.0,2.9.0]
2222

2323
exclude:
2424
- python-version: 3.7
2525
tf-version: 1.4.0
2626
- python-version: 3.7
2727
tf-version: 1.15.0
28-
28+
- python-version: 3.8
29+
tf-version: 1.4.0
30+
- python-version: 3.8
31+
tf-version: 1.14.0
32+
- python-version: 3.8
33+
tf-version: 1.15.0
34+
- python-version: 3.6
35+
tf-version: 2.7.0
36+
- python-version: 3.6
37+
tf-version: 2.8.0
38+
- python-version: 3.6
39+
tf-version: 2.9.0
40+
- python-version: 3.9
41+
tf-version: 1.4.0
42+
- python-version: 3.9
43+
tf-version: 1.15.0
44+
- python-version: 3.9
45+
tf-version: 2.2.0
2946
steps:
3047

31-
- uses: actions/checkout@v2
48+
- uses: actions/checkout@v3
3249

3350
- name: Setup python environment
34-
uses: actions/setup-python@v2.2.2
51+
uses: actions/setup-python@v4
3552
with:
3653
python-version: ${{ matrix.python-version }}
3754

3855
- name: Install dependencies
3956
run: |
4057
pip3 install -q tensorflow==${{ matrix.tf-version }}
41-
pip install -q numpy==1.19.5
58+
pip install -q protobuf==3.19.0
4259
pip install -q requests
4360
pip install -e .
4461
- name: Test with pytest
@@ -49,7 +66,7 @@ jobs:
4966
pip install -q python-coveralls
5067
pytest --cov=deepctr --cov-report=xml
5168
- name: Upload coverage to Codecov
52-
uses: codecov/codecov-action@v2.0.3
69+
uses: codecov/codecov-action@v3.1.0
5370
with:
5471
token: ${{secrets.CODECOV_TOKEN}}
5572
file: ./coverage.xml

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,12 @@ If you find this code useful in your research, please cite it using the followin
8989

9090
## DisscussionGroup
9191

92-
- [Discussions](https://github.com/shenweichen/DeepCTR/discussions)
93-
- 公众号:**浅梦学习笔记**
94-
- wechat ID: **deepctrbot**
92+
- [Github Discussions](https://github.com/shenweichen/DeepCTR/discussions)
93+
- Wechat Discussions
9594

96-
![wechat](./docs/pics/code.png)
95+
|公众号:浅梦学习笔记|微信:deepctrbot|学习小组 [加入](https://t.zsxq.com/026UJEuzv) [主题集合](https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MjM5MzY4NzE3MA==&action=getalbum&album_id=1361647041096843265&scene=126#wechat_redirect)|
96+
|:--:|:--:|:--:|
97+
| [![公众号](./docs/pics/code.png)](https://github.com/shenweichen/AlgoNotes)| [![微信](./docs/pics/deepctrbot.png)](https://github.com/shenweichen/AlgoNotes)|[![学习小组](./docs/pics/planet_github.png)](https://t.zsxq.com/026UJEuzv)|
9798

9899
## Main contributors([welcome to join us!](./CONTRIBUTING.md))
99100

@@ -119,12 +120,12 @@ If you find this code useful in your research, please cite it using the followin
119120
<td>
120121
​ <a href="https://github.com/morningsky"><img width="70" height="70" src="https://github.com/morningsky.png?s=40" alt="pic"></a><br>
121122
​ <a href="https://github.com/morningsky">Lai Mincai</a>
122-
<p> ShanghaiTech University </p>​
123+
<p> ByteDance </p>​
123124
</td>
124125
<td>
125126
​ <a href="https://github.com/codewithzichao"><img width="70" height="70" src="https://github.com/codewithzichao.png?s=40" alt="pic"></a><br>
126127
​ <a href="https://github.com/codewithzichao">Li Zichao</a>
127-
<p> Peking University </p>​
128+
<p> ByteDance </p>​
128129
</td>
129130
<td>
130131
​ <a href="https://github.com/TanTingyi"><img width="70" height="70" src="https://github.com/TanTingyi.png?s=40" alt="pic"></a><br>

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.9.0'
3+
__version__ = '0.9.1'
44
check_version(__version__)

deepctr/layers/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
KMaxPooling, SequencePoolingLayer, WeightedSequenceLayer,
1212
Transformer, DynamicGRU,PositionEncoding)
1313

14-
from .utils import NoMask, Hash, Linear, Add, combined_dnn_input, softmax, reduce_sum
14+
from .utils import NoMask, Hash, Linear, _Add, combined_dnn_input, softmax, reduce_sum
1515

1616
custom_objects = {'tf': tf,
1717
'InnerProductLayer': InnerProductLayer,
@@ -42,7 +42,7 @@
4242
'SENETLayer': SENETLayer,
4343
'BilinearInteraction': BilinearInteraction,
4444
'WeightedSequenceLayer': WeightedSequenceLayer,
45-
'Add': Add,
45+
'_Add': _Add,
4646
'FieldWiseBiInteraction': FieldWiseBiInteraction,
4747
'FwFMLayer': FwFMLayer,
4848
'softmax': softmax,

deepctr/layers/activation.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,17 @@
77
"""
88

99
import tensorflow as tf
10-
from tensorflow.python.keras.initializers import Zeros
11-
from tensorflow.python.keras.layers import Layer
10+
11+
try:
12+
from tensorflow.python.ops.init_ops import Zeros
13+
except ImportError:
14+
from tensorflow.python.ops.init_ops_v2 import Zeros
15+
from tensorflow.python.keras.layers import Layer, Activation
16+
17+
try:
18+
from tensorflow.python.keras.layers import BatchNormalization
19+
except ImportError:
20+
BatchNormalization = tf.keras.layers.BatchNormalization
1221

1322
try:
1423
unicode
@@ -40,7 +49,7 @@ def __init__(self, axis=-1, epsilon=1e-9, **kwargs):
4049
super(Dice, self).__init__(**kwargs)
4150

4251
def build(self, input_shape):
43-
self.bn = tf.keras.layers.BatchNormalization(
52+
self.bn = BatchNormalization(
4453
axis=self.axis, epsilon=self.epsilon, center=False, scale=False)
4554
self.alphas = self.add_weight(shape=(input_shape[-1],), initializer=Zeros(
4655
), dtype=tf.float32, name='dice_alpha') # name='alpha_'+self.name
@@ -67,7 +76,7 @@ def activation_layer(activation):
6776
if activation in ("dice", "Dice"):
6877
act_layer = Dice()
6978
elif isinstance(activation, (str, unicode)):
70-
act_layer = tf.keras.layers.Activation(activation)
79+
act_layer = Activation(activation)
7180
elif issubclass(activation, Layer):
7281
act_layer = activation()
7382
else:

deepctr/layers/core.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,18 @@
88

99
import tensorflow as tf
1010
from tensorflow.python.keras import backend as K
11-
from tensorflow.python.keras.initializers import Zeros, glorot_normal
12-
from tensorflow.python.keras.layers import Layer
11+
12+
try:
13+
from tensorflow.python.ops.init_ops_v2 import Zeros, glorot_normal
14+
except ImportError:
15+
from tensorflow.python.ops.init_ops import Zeros, glorot_normal_initializer as glorot_normal
16+
17+
from tensorflow.python.keras.layers import Layer, Dropout
18+
19+
try:
20+
from tensorflow.python.keras.layers import BatchNormalization
21+
except ImportError:
22+
BatchNormalization = tf.keras.layers.BatchNormalization
1323
from tensorflow.python.keras.regularizers import l2
1424

1525
from .activation import activation_layer
@@ -68,8 +78,8 @@ def build(self, input_shape):
6878
'inputs of a two inputs with shape (None,1,embedding_size) and (None,T,embedding_size)'
6979
'Got different shapes: %s,%s' % (input_shape[0], input_shape[1]))
7080
size = 4 * \
71-
int(input_shape[0][-1]
72-
) if len(self.hidden_units) == 0 else self.hidden_units[-1]
81+
int(input_shape[0][-1]
82+
) if len(self.hidden_units) == 0 else self.hidden_units[-1]
7383
self.kernel = self.add_weight(shape=(size, 1),
7484
initializer=glorot_normal(
7585
seed=self.seed),
@@ -164,9 +174,9 @@ def build(self, input_shape):
164174
initializer=Zeros(),
165175
trainable=True) for i in range(len(self.hidden_units))]
166176
if self.use_bn:
167-
self.bn_layers = [tf.keras.layers.BatchNormalization() for _ in range(len(self.hidden_units))]
177+
self.bn_layers = [BatchNormalization() for _ in range(len(self.hidden_units))]
168178

169-
self.dropout_layers = [tf.keras.layers.Dropout(self.dropout_rate, seed=self.seed + i) for i in
179+
self.dropout_layers = [Dropout(self.dropout_rate, seed=self.seed + i) for i in
170180
range(len(self.hidden_units))]
171181

172182
self.activation_layers = [activation_layer(self.activation) for _ in range(len(self.hidden_units))]

deepctr/layers/interaction.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@
1212
import tensorflow as tf
1313
from tensorflow.python.keras import backend as K
1414
from tensorflow.python.keras.backend import batch_dot
15-
from tensorflow.python.keras.initializers import (Zeros, glorot_normal,
16-
glorot_uniform, TruncatedNormal)
17-
from tensorflow.python.keras.layers import Layer
15+
16+
try:
17+
from tensorflow.python.ops.init_ops import Zeros, Ones, Constant, TruncatedNormal, \
18+
glorot_normal_initializer as glorot_normal, \
19+
glorot_uniform_initializer as glorot_uniform
20+
except ImportError:
21+
from tensorflow.python.ops.init_ops_v2 import Zeros, Ones, Constant, TruncatedNormal, glorot_normal, glorot_uniform
22+
23+
from tensorflow.python.keras.layers import Layer, MaxPooling2D, Conv2D, Dropout, Lambda, Dense, Flatten
1824
from tensorflow.python.keras.regularizers import l2
1925
from tensorflow.python.layers import utils
2026

@@ -90,10 +96,10 @@ def build(self, input_shape):
9096
initializer=glorot_normal(seed=self.seed), name="projection_h")
9197
self.projection_p = self.add_weight(shape=(
9298
embedding_size, 1), initializer=glorot_normal(seed=self.seed), name="projection_p")
93-
self.dropout = tf.keras.layers.Dropout(
99+
self.dropout = Dropout(
94100
self.dropout_rate, seed=self.seed)
95101

96-
self.tensordot = tf.keras.layers.Lambda(
102+
self.tensordot = Lambda(
97103
lambda x: tf.tensordot(x[0], x[1], axes=(-1, 0)))
98104

99105
# Be sure to call this somewhere!
@@ -244,7 +250,7 @@ def build(self, input_shape):
244250
regularizer=l2(self.l2_reg)))
245251

246252
self.bias.append(self.add_weight(name='bias' + str(i), shape=[size], dtype=tf.float32,
247-
initializer=tf.keras.initializers.Zeros()))
253+
initializer=Zeros()))
248254

249255
if self.split_half:
250256
if i != len(self.layer_size) - 1 and size % 2 > 0:
@@ -485,7 +491,7 @@ def build(self, input_shape):
485491
regularizer=l2(self.l2_reg),
486492
trainable=True) for i in range(self.layer_num)]
487493

488-
self.gating = [tf.keras.layers.Dense(1, use_bias=False) for i in range(self.num_experts)]
494+
self.gating = [Dense(1, use_bias=False) for i in range(self.num_experts)]
489495

490496
self.bias = [self.add_weight(name='bias' + str(i),
491497
shape=(dim, 1),
@@ -717,17 +723,17 @@ def build(self, input_shape):
717723
embedding_size = int(input_shape[-1])
718724
self.W_Query = self.add_weight(name='query', shape=[embedding_size, self.att_embedding_size * self.head_num],
719725
dtype=tf.float32,
720-
initializer=tf.keras.initializers.TruncatedNormal(seed=self.seed))
726+
initializer=TruncatedNormal(seed=self.seed))
721727
self.W_key = self.add_weight(name='key', shape=[embedding_size, self.att_embedding_size * self.head_num],
722728
dtype=tf.float32,
723-
initializer=tf.keras.initializers.TruncatedNormal(seed=self.seed + 1))
729+
initializer=TruncatedNormal(seed=self.seed + 1))
724730
self.W_Value = self.add_weight(name='value', shape=[embedding_size, self.att_embedding_size * self.head_num],
725731
dtype=tf.float32,
726-
initializer=tf.keras.initializers.TruncatedNormal(seed=self.seed + 2))
732+
initializer=TruncatedNormal(seed=self.seed + 2))
727733
if self.use_res:
728734
self.W_Res = self.add_weight(name='res', shape=[embedding_size, self.att_embedding_size * self.head_num],
729735
dtype=tf.float32,
730-
initializer=tf.keras.initializers.TruncatedNormal(seed=self.seed))
736+
initializer=TruncatedNormal(seed=self.seed))
731737

732738
# Be sure to call this somewhere!
733739
super(InteractingLayer, self).build(input_shape)
@@ -964,15 +970,15 @@ def build(self, input_shape):
964970
pooling_shape, (width, 1))
965971
pooling_shape = self._pooling_output_shape(
966972
conv_output_shape, (pooling_width, 1))
967-
self.conv_layers.append(tf.keras.layers.Conv2D(filters=filters, kernel_size=(width, 1), strides=(1, 1),
968-
padding='same',
969-
activation='tanh', use_bias=True, ))
973+
self.conv_layers.append(Conv2D(filters=filters, kernel_size=(width, 1), strides=(1, 1),
974+
padding='same',
975+
activation='tanh', use_bias=True, ))
970976
self.pooling_layers.append(
971-
tf.keras.layers.MaxPooling2D(pool_size=(pooling_width, 1)))
972-
self.dense_layers.append(tf.keras.layers.Dense(pooling_shape[1] * embedding_size * new_filters,
973-
activation='tanh', use_bias=True))
977+
MaxPooling2D(pool_size=(pooling_width, 1)))
978+
self.dense_layers.append(Dense(pooling_shape[1] * embedding_size * new_filters,
979+
activation='tanh', use_bias=True))
974980

975-
self.flatten = tf.keras.layers.Flatten()
981+
self.flatten = Flatten()
976982

977983
super(FGCNNLayer, self).build(
978984
input_shape) # Be sure to call this somewhere!
@@ -1090,7 +1096,7 @@ def build(self, input_shape):
10901096
self.W_2 = self.add_weight(shape=(
10911097
reduction_size, self.filed_size), initializer=glorot_normal(seed=self.seed), name="W_2")
10921098

1093-
self.tensordot = tf.keras.layers.Lambda(
1099+
self.tensordot = Lambda(
10941100
lambda x: tf.tensordot(x[0], x[1], axes=(-1, 0)))
10951101

10961102
# Be sure to call this somewhere!
@@ -1245,14 +1251,14 @@ def build(self, input_shape):
12451251
self.kernel_mf = self.add_weight(
12461252
name='kernel_mf',
12471253
shape=(int(self.num_fields * (self.num_fields - 1) / 2), 1),
1248-
initializer=tf.keras.initializers.Ones(),
1254+
initializer=Ones(),
12491255
regularizer=None,
12501256
trainable=True)
12511257

12521258
self.kernel_fm = self.add_weight(
12531259
name='kernel_fm',
12541260
shape=(self.num_fields, 1),
1255-
initializer=tf.keras.initializers.Constant(value=0.5),
1261+
initializer=Constant(value=0.5),
12561262
regularizer=None,
12571263
trainable=True)
12581264
if self.use_bias:

deepctr/layers/normalization.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77
"""
88

99
from tensorflow.python.keras import backend as K
10-
from tensorflow.python.keras.initializers import Ones, Zeros
1110
from tensorflow.python.keras.layers import Layer
1211

12+
try:
13+
from tensorflow.python.ops.init_ops import Zeros, Ones
14+
except ImportError:
15+
from tensorflow.python.ops.init_ops_v2 import Zeros, Ones
16+
1317

1418
class LayerNormalization(Layer):
1519
def __init__(self, axis=-1, eps=1e-9, center=True,

0 commit comments

Comments
 (0)