Skip to content

Commit

Permalink
improve compatibility
Browse files Browse the repository at this point in the history
improve compatibility with tensorflow 2.x
  • Loading branch information
shenweichen committed Jun 11, 2022
1 parent cd9643c commit 9564e05
Show file tree
Hide file tree
Showing 69 changed files with 411 additions and 333 deletions.
31 changes: 24 additions & 7 deletions .github/workflows/ci.yml
Expand Up @@ -17,28 +17,45 @@ jobs:
timeout-minutes: 180
strategy:
matrix:
python-version: [3.6,3.7]
tf-version: [1.4.0,1.15.0,2.2.0,2.5.0]
python-version: [3.6,3.7,3.8]
tf-version: [1.4.0,1.15.0,2.5.0,2.6.0,2.7.0,2.8.0,2.9.0]

exclude:
- python-version: 3.7
tf-version: 1.4.0
- python-version: 3.7
tf-version: 1.15.0

- python-version: 3.8
tf-version: 1.4.0
- python-version: 3.8
tf-version: 1.14.0
- python-version: 3.8
tf-version: 1.15.0
- python-version: 3.6
tf-version: 2.7.0
- python-version: 3.6
tf-version: 2.8.0
- python-version: 3.6
tf-version: 2.9.0
- python-version: 3.9
tf-version: 1.4.0
- python-version: 3.9
tf-version: 1.15.0
- python-version: 3.9
tf-version: 2.2.0
steps:

- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Setup python environment
uses: actions/setup-python@v2.2.2
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
pip3 install -q tensorflow==${{ matrix.tf-version }}
pip install -q numpy==1.19.5
pip install -q protobuf==3.19.0
pip install -q requests
pip install -e .
- name: Test with pytest
Expand All @@ -49,7 +66,7 @@ jobs:
pip install -q python-coveralls
pytest --cov=deepctr --cov-report=xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v2.0.3
uses: codecov/codecov-action@v3.1.0
with:
token: ${{secrets.CODECOV_TOKEN}}
file: ./coverage.xml
Expand Down
13 changes: 7 additions & 6 deletions README.md
Expand Up @@ -89,11 +89,12 @@ If you find this code useful in your research, please cite it using the followin

## DisscussionGroup

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

![wechat](./docs/pics/code.png)
|公众号:浅梦学习笔记|微信:deepctrbot|学习小组 [加入](https://t.zsxq.com/026UJEuzv) [主题集合](https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MjM5MzY4NzE3MA==&action=getalbum&album_id=1361647041096843265&scene=126#wechat_redirect)|
|:--:|:--:|:--:|
| [![公众号](./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)|

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

Expand All @@ -119,12 +120,12 @@ If you find this code useful in your research, please cite it using the followin
<td>
​ <a href="https://github.com/morningsky"><img width="70" height="70" src="https://github.com/morningsky.png?s=40" alt="pic"></a><br>
​ <a href="https://github.com/morningsky">Lai Mincai</a>
<p> ShanghaiTech University </p>​
<p> ByteDance </p>​
</td>
<td>
​ <a href="https://github.com/codewithzichao"><img width="70" height="70" src="https://github.com/codewithzichao.png?s=40" alt="pic"></a><br>
​ <a href="https://github.com/codewithzichao">Li Zichao</a>
<p> Peking University </p>​
<p> ByteDance </p>​
</td>
<td>
​ <a href="https://github.com/TanTingyi"><img width="70" height="70" src="https://github.com/TanTingyi.png?s=40" alt="pic"></a><br>
Expand Down
2 changes: 1 addition & 1 deletion deepctr/__init__.py
@@ -1,4 +1,4 @@
from .utils import check_version

__version__ = '0.9.0'
__version__ = '0.9.1'
check_version(__version__)
4 changes: 2 additions & 2 deletions deepctr/layers/__init__.py
Expand Up @@ -11,7 +11,7 @@
KMaxPooling, SequencePoolingLayer, WeightedSequenceLayer,
Transformer, DynamicGRU,PositionEncoding)

from .utils import NoMask, Hash, Linear, Add, combined_dnn_input, softmax, reduce_sum
from .utils import NoMask, Hash, Linear, _Add, combined_dnn_input, softmax, reduce_sum

custom_objects = {'tf': tf,
'InnerProductLayer': InnerProductLayer,
Expand Down Expand Up @@ -42,7 +42,7 @@
'SENETLayer': SENETLayer,
'BilinearInteraction': BilinearInteraction,
'WeightedSequenceLayer': WeightedSequenceLayer,
'Add': Add,
'_Add': _Add,
'FieldWiseBiInteraction': FieldWiseBiInteraction,
'FwFMLayer': FwFMLayer,
'softmax': softmax,
Expand Down
17 changes: 13 additions & 4 deletions deepctr/layers/activation.py
Expand Up @@ -7,8 +7,17 @@
"""

import tensorflow as tf
from tensorflow.python.keras.initializers import Zeros
from tensorflow.python.keras.layers import Layer

try:
from tensorflow.python.ops.init_ops import Zeros
except ImportError:
from tensorflow.python.ops.init_ops_v2 import Zeros
from tensorflow.python.keras.layers import Layer, Activation

try:
from tensorflow.python.keras.layers import BatchNormalization
except ImportError:
BatchNormalization = tf.keras.layers.BatchNormalization

try:
unicode
Expand Down Expand Up @@ -40,7 +49,7 @@ def __init__(self, axis=-1, epsilon=1e-9, **kwargs):
super(Dice, self).__init__(**kwargs)

def build(self, input_shape):
self.bn = tf.keras.layers.BatchNormalization(
self.bn = BatchNormalization(
axis=self.axis, epsilon=self.epsilon, center=False, scale=False)
self.alphas = self.add_weight(shape=(input_shape[-1],), initializer=Zeros(
), dtype=tf.float32, name='dice_alpha') # name='alpha_'+self.name
Expand All @@ -67,7 +76,7 @@ def activation_layer(activation):
if activation in ("dice", "Dice"):
act_layer = Dice()
elif isinstance(activation, (str, unicode)):
act_layer = tf.keras.layers.Activation(activation)
act_layer = Activation(activation)
elif issubclass(activation, Layer):
act_layer = activation()
else:
Expand Down
22 changes: 16 additions & 6 deletions deepctr/layers/core.py
Expand Up @@ -8,8 +8,18 @@

import tensorflow as tf
from tensorflow.python.keras import backend as K
from tensorflow.python.keras.initializers import Zeros, glorot_normal
from tensorflow.python.keras.layers import Layer

try:
from tensorflow.python.ops.init_ops_v2 import Zeros, glorot_normal
except ImportError:
from tensorflow.python.ops.init_ops import Zeros, glorot_normal_initializer as glorot_normal

from tensorflow.python.keras.layers import Layer, Dropout

try:
from tensorflow.python.keras.layers import BatchNormalization
except ImportError:
BatchNormalization = tf.keras.layers.BatchNormalization
from tensorflow.python.keras.regularizers import l2

from .activation import activation_layer
Expand Down Expand Up @@ -68,8 +78,8 @@ def build(self, input_shape):
'inputs of a two inputs with shape (None,1,embedding_size) and (None,T,embedding_size)'
'Got different shapes: %s,%s' % (input_shape[0], input_shape[1]))
size = 4 * \
int(input_shape[0][-1]
) if len(self.hidden_units) == 0 else self.hidden_units[-1]
int(input_shape[0][-1]
) if len(self.hidden_units) == 0 else self.hidden_units[-1]
self.kernel = self.add_weight(shape=(size, 1),
initializer=glorot_normal(
seed=self.seed),
Expand Down Expand Up @@ -164,9 +174,9 @@ def build(self, input_shape):
initializer=Zeros(),
trainable=True) for i in range(len(self.hidden_units))]
if self.use_bn:
self.bn_layers = [tf.keras.layers.BatchNormalization() for _ in range(len(self.hidden_units))]
self.bn_layers = [BatchNormalization() for _ in range(len(self.hidden_units))]

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

self.activation_layers = [activation_layer(self.activation) for _ in range(len(self.hidden_units))]
Expand Down
48 changes: 27 additions & 21 deletions deepctr/layers/interaction.py
Expand Up @@ -12,9 +12,15 @@
import tensorflow as tf
from tensorflow.python.keras import backend as K
from tensorflow.python.keras.backend import batch_dot
from tensorflow.python.keras.initializers import (Zeros, glorot_normal,
glorot_uniform, TruncatedNormal)
from tensorflow.python.keras.layers import Layer

try:
from tensorflow.python.ops.init_ops import Zeros, Ones, Constant, TruncatedNormal, \
glorot_normal_initializer as glorot_normal, \
glorot_uniform_initializer as glorot_uniform
except ImportError:
from tensorflow.python.ops.init_ops_v2 import Zeros, Ones, Constant, TruncatedNormal, glorot_normal, glorot_uniform

from tensorflow.python.keras.layers import Layer, MaxPooling2D, Conv2D, Dropout, Lambda, Dense, Flatten
from tensorflow.python.keras.regularizers import l2
from tensorflow.python.layers import utils

Expand Down Expand Up @@ -90,10 +96,10 @@ def build(self, input_shape):
initializer=glorot_normal(seed=self.seed), name="projection_h")
self.projection_p = self.add_weight(shape=(
embedding_size, 1), initializer=glorot_normal(seed=self.seed), name="projection_p")
self.dropout = tf.keras.layers.Dropout(
self.dropout = Dropout(
self.dropout_rate, seed=self.seed)

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

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

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

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

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

self.bias = [self.add_weight(name='bias' + str(i),
shape=(dim, 1),
Expand Down Expand Up @@ -717,17 +723,17 @@ def build(self, input_shape):
embedding_size = int(input_shape[-1])
self.W_Query = self.add_weight(name='query', shape=[embedding_size, self.att_embedding_size * self.head_num],
dtype=tf.float32,
initializer=tf.keras.initializers.TruncatedNormal(seed=self.seed))
initializer=TruncatedNormal(seed=self.seed))
self.W_key = self.add_weight(name='key', shape=[embedding_size, self.att_embedding_size * self.head_num],
dtype=tf.float32,
initializer=tf.keras.initializers.TruncatedNormal(seed=self.seed + 1))
initializer=TruncatedNormal(seed=self.seed + 1))
self.W_Value = self.add_weight(name='value', shape=[embedding_size, self.att_embedding_size * self.head_num],
dtype=tf.float32,
initializer=tf.keras.initializers.TruncatedNormal(seed=self.seed + 2))
initializer=TruncatedNormal(seed=self.seed + 2))
if self.use_res:
self.W_Res = self.add_weight(name='res', shape=[embedding_size, self.att_embedding_size * self.head_num],
dtype=tf.float32,
initializer=tf.keras.initializers.TruncatedNormal(seed=self.seed))
initializer=TruncatedNormal(seed=self.seed))

# Be sure to call this somewhere!
super(InteractingLayer, self).build(input_shape)
Expand Down Expand Up @@ -964,15 +970,15 @@ def build(self, input_shape):
pooling_shape, (width, 1))
pooling_shape = self._pooling_output_shape(
conv_output_shape, (pooling_width, 1))
self.conv_layers.append(tf.keras.layers.Conv2D(filters=filters, kernel_size=(width, 1), strides=(1, 1),
padding='same',
activation='tanh', use_bias=True, ))
self.conv_layers.append(Conv2D(filters=filters, kernel_size=(width, 1), strides=(1, 1),
padding='same',
activation='tanh', use_bias=True, ))
self.pooling_layers.append(
tf.keras.layers.MaxPooling2D(pool_size=(pooling_width, 1)))
self.dense_layers.append(tf.keras.layers.Dense(pooling_shape[1] * embedding_size * new_filters,
activation='tanh', use_bias=True))
MaxPooling2D(pool_size=(pooling_width, 1)))
self.dense_layers.append(Dense(pooling_shape[1] * embedding_size * new_filters,
activation='tanh', use_bias=True))

self.flatten = tf.keras.layers.Flatten()
self.flatten = Flatten()

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

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

# Be sure to call this somewhere!
Expand Down Expand Up @@ -1245,14 +1251,14 @@ def build(self, input_shape):
self.kernel_mf = self.add_weight(
name='kernel_mf',
shape=(int(self.num_fields * (self.num_fields - 1) / 2), 1),
initializer=tf.keras.initializers.Ones(),
initializer=Ones(),
regularizer=None,
trainable=True)

self.kernel_fm = self.add_weight(
name='kernel_fm',
shape=(self.num_fields, 1),
initializer=tf.keras.initializers.Constant(value=0.5),
initializer=Constant(value=0.5),
regularizer=None,
trainable=True)
if self.use_bias:
Expand Down
6 changes: 5 additions & 1 deletion deepctr/layers/normalization.py
Expand Up @@ -7,9 +7,13 @@
"""

from tensorflow.python.keras import backend as K
from tensorflow.python.keras.initializers import Ones, Zeros
from tensorflow.python.keras.layers import Layer

try:
from tensorflow.python.ops.init_ops import Zeros, Ones
except ImportError:
from tensorflow.python.ops.init_ops_v2 import Zeros, Ones


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

0 comments on commit 9564e05

Please sign in to comment.