Skip to content

Commit 3da641f

Browse files
committed
refactor&update docs
Use a more space efficient implementation in CrossNet Add custom_objects in utils which will be used in load_model function Add FAQ in docs
1 parent d15a956 commit 3da641f

File tree

7 files changed

+57
-18
lines changed

7 files changed

+57
-18
lines changed

deepctr/layers.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ def call(self, inputs,**kwargs):
122122
for j in range(i + 1, num_inputs):
123123
row.append(i)
124124
col.append(j)
125-
p = concatenate([embeds_vec_list[idx] for idx in row],axis=1)# batch num_pairs k
126-
q = concatenate([embeds_vec_list[idx] for idx in col],axis=1) # Reshape([num_pairs, self.embedding_size])
125+
p = tf.concat([embeds_vec_list[idx] for idx in row],axis=1)
126+
q = tf.concat([embeds_vec_list[idx] for idx in col],axis=1)
127127
inner_product = p * q
128128

129129
bi_interaction = inner_product
@@ -233,16 +233,13 @@ def call(self, inputs,**kwargs):
233233
if K.ndim(inputs) !=2 :
234234
raise ValueError("Unexpected inputs dimensions %d, expect to be 2 dimensions"% (K.ndim(inputs)))
235235

236-
x = inputs
237-
dim = x.get_shape()[-1]
238-
x_0 = K.reshape(x,[-1,dim, 1])
236+
x_0 = tf.expand_dims(inputs,axis=2)
239237
x_l = x_0
240238
for i in range(self.layer_num):
241-
dot_ = tf.matmul(x_0, tf.transpose(x_l, [0, 2, 1])) # K.dot(x_0,K.transpose(x_l))
242-
dot_ = K.dot(dot_, self.kernels[i])
243-
#x_l = K.bias_add(dot_+ x_l,self.bias[i]) # K.bias_add(dot_, self.bias)
244-
x_l = dot_ + x_l + self.bias[i]#K.reshape(self.bias[i],[1,dim,1])
245-
x_l = K.reshape(x_l, [-1, dim])
239+
xl_w = tf.tensordot(tf.transpose(x_l,[0,2,1]),self.kernels[i],axes=(-1,0))
240+
dot_ = tf.matmul(x_0,xl_w)
241+
x_l = dot_ + x_l + self.bias[i]
242+
x_l = tf.squeeze(x_l,axis=2)
246243
return x_l
247244

248245
def get_config(self,):
@@ -259,7 +256,7 @@ class MLP(Layer):
259256
- nD tensor with shape: ``(batch_size, ..., input_dim)``. The most common situation would be a 2D input with shape ``(batch_size, input_dim)``.
260257
261258
Output shape
262-
- nD tensor with shape: ``(batch_size, ..., hidden_size[-1])``. For instance, for a 2D input with shape `(batch_size, input_dim)`, the output would have shape ``(batch_size, hidden_size[-1])``.
259+
- nD tensor with shape: ``(batch_size, ..., hidden_size[-1])``. For instance, for a 2D input with shape ``(batch_size, input_dim)``, the output would have shape ``(batch_size, hidden_size[-1])``.
263260
264261
Arguments
265262
- **hidden_size**:list of positive integer, the layer number and units in each layer.

deepctr/sequence.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class SequencePoolingLayer(Layer):
1515
- seq_len is a 2D tensor with shape : ``(batch_size, 1)``,indicate valid length of each sequence.
1616
1717
Output shape
18-
- 3D tensor with shape: `(batch_size, 1, embedding_size)`.
18+
- 3D tensor with shape: ``(batch_size, 1, embedding_size)``.
1919
2020
Arguments
2121
- **seq_len_max**:Positive integer indicates that the max length of all the sequence feature,usually same as T.
@@ -78,7 +78,7 @@ class AttentionSequencePoolingLayer(Layer):
7878
- keys_length is a 2D tensor with shape: ``(batch_size, 1)``
7979
8080
Output shape
81-
-3D tensor with shape: ``(batch_size, 1, embedding_size)``.
81+
- 3D tensor with shape: ``(batch_size, 1, embedding_size)``.
8282
8383
Arguments
8484
- **hidden_size**:list of positive integer, the attention net layer number and units in each layer.

deepctr/utils.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
21
from tensorflow.python.keras.layers import Input
3-
4-
2+
from .activations import *
3+
from .layers import *
4+
from .sequence import *
55

66

77
def get_input(feature_dim_dict, bias_feature_dim_dict=None):
@@ -18,3 +18,16 @@ def get_input(feature_dim_dict, bias_feature_dim_dict=None):
1818
enumerate(bias_feature_dim_dict["dense"])]
1919
return sparse_input, dense_input, bias_sparse_input, bias_dense_input
2020

21+
22+
custom_objects = {'InnerProductLayer': InnerProductLayer,
23+
'OutterProductLayer': OutterProductLayer,
24+
'MLP': MLP,
25+
'PredictionLayer': PredictionLayer,
26+
'FM': FM,
27+
'AFMLayer': AFMLayer,
28+
'CrossNet': CrossNet,
29+
'BiInteractionPooling': BiInteractionPooling,
30+
'LocalActivationUnit': LocalActivationUnit,
31+
'Dice': Dice,
32+
'SequencePoolingLayer': SequencePoolingLayer,
33+
'AttentionSequencePoolingLayer': AttentionSequencePoolingLayer, }

docs/source/FAQ.rst

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
FAQ
2+
==========
3+
1. How to save or load weights/models?
4+
5+
To save/load weights,you can write codes just like any other keras models.
6+
7+
.. code-block:: python
8+
9+
model = DeepFM()
10+
model.save_weights('DeepFM_w.h5')
11+
model.load_weights('DeepFM_w.h5')
12+
13+
14+
To save/load models,just a little different.
15+
16+
.. code-block:: python
17+
18+
from tensorflow.python.keras.models import save_model,load_model
19+
model = DeepFM()
20+
save_model(model, 'DeepFM.h5')# save_model, same as before
21+
22+
from deepctr.utils import custom_objects
23+
model = load_model('DeepFM.h5',custom_objects)# load_model,just add a parameter
24+
25+
2. Does the models support multi-value input?
26+
27+
Now only the `DIN <Features.html#din-deep-interest-network>`_ model support multi-value input,you can use layers in `sequence <deepctr.sequence.html>`_ to build your own models!
28+
And I will add the feature soon~

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.1.3'
29+
release = '0.1.4'
3030

3131

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

docs/source/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ You can read the source code at https://github.com/shenweichen/DeepCTR
1919
Quick-Start
2020
Features
2121
Demo
22+
FAQ
2223

2324
.. toctree::
2425
:maxdepth: 3

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="deepctr",
8-
version="0.1.3",
8+
version="0.1.4",
99
author="Weichen Shen",
1010
author_email="[email protected]",
1111
description="DeepCTR is a Easy-to-use,Modular and Extendible package of deep-learning based CTR models ,including serval DNN-based CTR models and lots of core components layer of the models which can be used to build your own custom model.",

0 commit comments

Comments
 (0)