Skip to content

Commit

Permalink
refactor&update docs
Browse files Browse the repository at this point in the history
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
  • Loading branch information
shenweichen committed Nov 29, 2018
1 parent d15a956 commit 3da641f
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 18 deletions.
19 changes: 8 additions & 11 deletions deepctr/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ def call(self, inputs,**kwargs):
for j in range(i + 1, num_inputs):
row.append(i)
col.append(j)
p = concatenate([embeds_vec_list[idx] for idx in row],axis=1)# batch num_pairs k
q = concatenate([embeds_vec_list[idx] for idx in col],axis=1) # Reshape([num_pairs, self.embedding_size])
p = tf.concat([embeds_vec_list[idx] for idx in row],axis=1)
q = tf.concat([embeds_vec_list[idx] for idx in col],axis=1)
inner_product = p * q

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

x = inputs
dim = x.get_shape()[-1]
x_0 = K.reshape(x,[-1,dim, 1])
x_0 = tf.expand_dims(inputs,axis=2)
x_l = x_0
for i in range(self.layer_num):
dot_ = tf.matmul(x_0, tf.transpose(x_l, [0, 2, 1])) # K.dot(x_0,K.transpose(x_l))
dot_ = K.dot(dot_, self.kernels[i])
#x_l = K.bias_add(dot_+ x_l,self.bias[i]) # K.bias_add(dot_, self.bias)
x_l = dot_ + x_l + self.bias[i]#K.reshape(self.bias[i],[1,dim,1])
x_l = K.reshape(x_l, [-1, dim])
xl_w = tf.tensordot(tf.transpose(x_l,[0,2,1]),self.kernels[i],axes=(-1,0))
dot_ = tf.matmul(x_0,xl_w)
x_l = dot_ + x_l + self.bias[i]
x_l = tf.squeeze(x_l,axis=2)
return x_l

def get_config(self,):
Expand All @@ -259,7 +256,7 @@ class MLP(Layer):
- nD tensor with shape: ``(batch_size, ..., input_dim)``. The most common situation would be a 2D input with shape ``(batch_size, input_dim)``.
Output shape
- 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])``.
- 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])``.
Arguments
- **hidden_size**:list of positive integer, the layer number and units in each layer.
Expand Down
4 changes: 2 additions & 2 deletions deepctr/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class SequencePoolingLayer(Layer):
- seq_len is a 2D tensor with shape : ``(batch_size, 1)``,indicate valid length of each sequence.
Output shape
- 3D tensor with shape: `(batch_size, 1, embedding_size)`.
- 3D tensor with shape: ``(batch_size, 1, embedding_size)``.
Arguments
- **seq_len_max**:Positive integer indicates that the max length of all the sequence feature,usually same as T.
Expand Down Expand Up @@ -78,7 +78,7 @@ class AttentionSequencePoolingLayer(Layer):
- keys_length is a 2D tensor with shape: ``(batch_size, 1)``
Output shape
-3D tensor with shape: ``(batch_size, 1, embedding_size)``.
- 3D tensor with shape: ``(batch_size, 1, embedding_size)``.
Arguments
- **hidden_size**:list of positive integer, the attention net layer number and units in each layer.
Expand Down
19 changes: 16 additions & 3 deletions deepctr/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

from tensorflow.python.keras.layers import Input


from .activations import *
from .layers import *
from .sequence import *


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


custom_objects = {'InnerProductLayer': InnerProductLayer,
'OutterProductLayer': OutterProductLayer,
'MLP': MLP,
'PredictionLayer': PredictionLayer,
'FM': FM,
'AFMLayer': AFMLayer,
'CrossNet': CrossNet,
'BiInteractionPooling': BiInteractionPooling,
'LocalActivationUnit': LocalActivationUnit,
'Dice': Dice,
'SequencePoolingLayer': SequencePoolingLayer,
'AttentionSequencePoolingLayer': AttentionSequencePoolingLayer, }
28 changes: 28 additions & 0 deletions docs/source/FAQ.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FAQ
==========
1. How to save or load weights/models?

To save/load weights,you can write codes just like any other keras models.

.. code-block:: python
model = DeepFM()
model.save_weights('DeepFM_w.h5')
model.load_weights('DeepFM_w.h5')
To save/load models,just a little different.

.. code-block:: python
from tensorflow.python.keras.models import save_model,load_model
model = DeepFM()
save_model(model, 'DeepFM.h5')# save_model, same as before
from deepctr.utils import custom_objects
model = load_model('DeepFM.h5',custom_objects)# load_model,just add a parameter
2. Does the models support multi-value input?

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!
And I will add the feature soon~
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# The short X.Y version
version = ''
# The full version, including alpha/beta/rc tags
release = '0.1.3'
release = '0.1.4'


# -- General configuration ---------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ You can read the source code at https://github.com/shenweichen/DeepCTR
Quick-Start
Features
Demo
FAQ

.. toctree::
:maxdepth: 3
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="deepctr",
version="0.1.3",
version="0.1.4",
author="Weichen Shen",
author_email="[email protected]",
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.",
Expand Down

0 comments on commit 3da641f

Please sign in to comment.