diff --git a/README.md b/README.md
new file mode 100644
index 0000000..f35f62c
--- /dev/null
+++ b/README.md
@@ -0,0 +1,67 @@
+# DeepMatch
+
+[](https://pypi.org/project/deepctr)
+[](https://pypi.org/project/deepmatch)
+[](https://pypi.org/project/deepmatch)
+[](https://github.com/shenweichen/deepmatch/issues)
+
+
+
+[](https://deepctrmatch.readthedocs.io/)
+[](./README.md#disscussiongroup)
+[](https://github.com/shenweichen/deepmatch/blob/master/LICENSE)
+
+
+DeepMatch is a **Easy-to-use** deep matching model library for recommendations, advertising, and search. It's easy to train models and to **export representation vectors** for user and item which can be used for **ANN search**.You can use any complex model with `model.fit()`and `model.predict()` .
+
+Let's [**Get Started!**](https://deepmatch.readthedocs.io/en/latest/Quick-Start.html)
+
+
+## Models List
+
+| Model | Paper |
+| :------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------- |
+| FM | [ICDM 2010][Factorization Machines](https://www.researchgate.net/publication/220766482_Factorization_Machines) |
+| DSSM | [CIKM 2013][Deep Structured Semantic Models for Web Search using Clickthrough Data](https://www.microsoft.com/en-us/research/publication/learning-deep-structured-semantic-models-for-web-search-using-clickthrough-data/) |
+| YoutubeDNN | [RecSys 2016][Deep Neural Networks for YouTube Recommendations](https://www.researchgate.net/publication/307573656_Deep_Neural_Networks_for_YouTube_Recommendations) |
+| NCF | [WWW 2017][Neural Collaborative Filtering](https://arxiv.org/abs/1708.05031) |
+| MIND | [CIKM 2019][Multi-interest network with dynamic routing for recommendation at Tmall](https://arxiv.org/pdf/1904.08030) |
+
+## Contributors([welcome to join us!](./CONTRIBUTING.md))
+
+
+
+## DisscussionGroup
+
+Please follow our wechat to join group:
+- 公众号:**浅梦的学习笔记**
+- wechat ID: **deepctrbot**
+
+ 
diff --git a/deepmatch/__init__.py b/deepmatch/__init__.py
new file mode 100644
index 0000000..6a1db79
--- /dev/null
+++ b/deepmatch/__init__.py
@@ -0,0 +1,4 @@
+from .utils import check_version
+
+__version__ = '0.0.0'
+check_version(__version__)
diff --git a/deepmatch/inputs.py b/deepmatch/inputs.py
new file mode 100644
index 0000000..b37357a
--- /dev/null
+++ b/deepmatch/inputs.py
@@ -0,0 +1,25 @@
+from itertools import chain
+from deepctr.inputs import SparseFeat,VarLenSparseFeat,create_embedding_matrix,embedding_lookup,get_dense_input,varlen_embedding_lookup,get_varlen_pooling_list,mergeDict
+
+def input_from_feature_columns(features, feature_columns, l2_reg, init_std, seed, prefix='', seq_mask_zero=True,
+ support_dense=True, support_group=False,embedding_matrix_dict=None):
+ sparse_feature_columns = list(
+ filter(lambda x: isinstance(x, SparseFeat), feature_columns)) if feature_columns else []
+ varlen_sparse_feature_columns = list(
+ filter(lambda x: isinstance(x, VarLenSparseFeat), feature_columns)) if feature_columns else []
+ if embedding_matrix_dict is None:
+ embedding_matrix_dict = create_embedding_matrix(feature_columns, l2_reg, init_std, seed, prefix=prefix,
+ seq_mask_zero=seq_mask_zero)
+
+ group_sparse_embedding_dict = embedding_lookup(embedding_matrix_dict, features, sparse_feature_columns)
+ dense_value_list = get_dense_input(features, feature_columns)
+ if not support_dense and len(dense_value_list) > 0:
+ raise ValueError("DenseFeat is not supported in dnn_feature_columns")
+
+ sequence_embed_dict = varlen_embedding_lookup(embedding_matrix_dict, features, varlen_sparse_feature_columns)
+ group_varlen_sparse_embedding_dict = get_varlen_pooling_list(sequence_embed_dict, features,
+ varlen_sparse_feature_columns)
+ group_embedding_dict = mergeDict(group_sparse_embedding_dict, group_varlen_sparse_embedding_dict)
+ if not support_group:
+ group_embedding_dict = list(chain.from_iterable(group_embedding_dict.values()))
+ return group_embedding_dict, dense_value_list
\ No newline at end of file
diff --git a/deepmatch/layers/__init__.py b/deepmatch/layers/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/deepmatch/layers/core.py b/deepmatch/layers/core.py
new file mode 100644
index 0000000..a8d8ad9
--- /dev/null
+++ b/deepmatch/layers/core.py
@@ -0,0 +1,125 @@
+import tensorflow as tf
+from deepctr.layers.utils import reduce_max, reduce_mean, reduce_sum, concat_func, div, softmax
+from tensorflow.python.keras.initializers import RandomNormal, Zeros
+from tensorflow.python.keras.layers import Layer
+
+
+class PoolingLayer(Layer):
+
+ def __init__(self, mode='mean', supports_masking=False, **kwargs):
+
+ if mode not in ['sum', 'mean', 'max']:
+ raise ValueError("mode must be sum or mean")
+ self.mode = mode
+ self.eps = tf.constant(1e-8, tf.float32)
+ super(PoolingLayer, self).__init__(**kwargs)
+
+ self.supports_masking = supports_masking
+
+ def build(self, input_shape):
+
+ super(PoolingLayer, self).build(
+ input_shape) # Be sure to call this somewhere!
+
+ def call(self, seq_value_len_list, mask=None, **kwargs):
+ if not isinstance(seq_value_len_list, list):
+ seq_value_len_list = [seq_value_len_list]
+ if len(seq_value_len_list) == 1:
+ return seq_value_len_list[0]
+ expand_seq_value_len_list = list(map(lambda x: tf.expand_dims(x, axis=-1), seq_value_len_list))
+ a = concat_func(expand_seq_value_len_list)
+ if self.mode == "mean":
+ hist = reduce_mean(a, axis=-1, )
+ if self.mode == "sum":
+ hist = reduce_sum(a, axis=-1, )
+ if self.mode == "max":
+ hist = reduce_max(a, axis=-1, )
+ return hist
+
+
+class SampledSoftmaxLayer(Layer):
+ def __init__(self, item_embedding, num_sampled=5, **kwargs):
+ self.num_sampled = num_sampled
+ self.target_song_size = item_embedding.input_dim
+ self.item_embedding = item_embedding
+ super(SampledSoftmaxLayer, self).__init__(**kwargs)
+
+ def build(self, input_shape):
+ self.zero_bias = self.add_weight(shape=[self.target_song_size],
+ initializer=Zeros,
+ dtype=tf.float32,
+ trainable=False,
+ name="bias")
+ if not self.item_embedding.built:
+ self.item_embedding.build([])
+ self.trainable_weights.append(self.item_embedding.embeddings)
+ super(SampledSoftmaxLayer, self).build(input_shape)
+
+ def call(self, inputs_with_label_idx, training=None, **kwargs):
+ """
+ The first input should be the model as it were, and the second the
+ target (i.e., a repeat of the training data) to compute the labels
+ argument
+ """
+ inputs, label_idx = inputs_with_label_idx
+
+ loss = tf.nn.sampled_softmax_loss(weights=self.item_embedding.embeddings,
+ biases=self.zero_bias,
+ labels=label_idx,
+ inputs=inputs,
+ num_sampled=self.num_sampled,
+ num_classes=self.target_song_size
+ )
+ return tf.expand_dims(loss, axis=1)
+
+ def compute_output_shape(self, input_shape):
+ return (None, 1)
+
+ def get_config(self, ):
+ config = {'item_embedding': self.item_embedding, 'num_sampled': self.num_sampled}
+ base_config = super(SampledSoftmaxLayer, self).get_config()
+ return dict(list(base_config.items()) + list(config.items()))
+
+
+class LabelAwareAttention(Layer):
+ def __init__(self, k_max, pow_p=1, **kwargs):
+ self.k_max = k_max
+ self.pow_p = pow_p
+ super(LabelAwareAttention, self).__init__(**kwargs)
+
+ def build(self, input_shape):
+ # Be sure to call this somewhere!
+
+ self.embedding_size = input_shape[0][-1]
+ super(LabelAwareAttention, self).build(input_shape)
+
+ def call(self, inputs, training=None, **kwargs):
+ keys = inputs[0]
+ query = inputs[1]
+ weight = tf.reduce_sum(keys * query, axis=-1, keep_dims=True)
+ weight = tf.pow(weight, self.pow_p) # [x,k_max,1]
+
+ if len(inputs) == 3:
+ k_user = tf.cast(tf.maximum(
+ 1.,
+ tf.minimum(
+ tf.cast(self.k_max, dtype="float32"), # k_max
+ tf.log1p(tf.cast(inputs[2], dtype="float32")) / tf.log(2.) # hist_len
+ )
+ ), dtype="int64")
+ seq_mask = tf.transpose(tf.sequence_mask(k_user, self.k_max), [0, 2, 1])
+ padding = tf.ones_like(seq_mask, dtype=tf.float32) * (-2 ** 32 + 1) # [x,k_max,1]
+ weight = tf.where(seq_mask, weight, padding)
+
+ weight = softmax(weight, dim=1, name="weight")
+ output = tf.reduce_sum(keys * weight, axis=1)
+
+ return output
+
+ def compute_output_shape(self, input_shape):
+ return (None, self.embedding_size)
+
+ def get_config(self, ):
+ config = {'k_max': self.k_max, 'pow_p': self.pow_p}
+ base_config = super(LabelAwareAttention, self).get_config()
+ return dict(list(base_config.items()) + list(config.items()))
diff --git a/deepmatch/models/__init__.py b/deepmatch/models/__init__.py
new file mode 100644
index 0000000..2950bd1
--- /dev/null
+++ b/deepmatch/models/__init__.py
@@ -0,0 +1,5 @@
+from .fm import FM
+from .dssm import DSSM
+from .youtubednn import YoutubeDNN
+from .ncf import NCF
+from .mind import MIND
diff --git a/deepmatch/models/fm.py b/deepmatch/models/fm.py
new file mode 100644
index 0000000..3b5fa91
--- /dev/null
+++ b/deepmatch/models/fm.py
@@ -0,0 +1,62 @@
+from deepctr.inputs import build_input_features
+from deepctr.layers.core import PredictionLayer
+from deepctr.layers.utils import concat_func, reduce_sum
+from tensorflow.python.keras.layers import Lambda
+from tensorflow.python.keras.models import Model
+
+from ..inputs import create_embedding_matrix, input_from_feature_columns
+from ..layers.core import Similarity
+
+
+def FM(user_feature_columns, item_feature_columns, l2_reg_embedding=1e-6, init_std=0.0001, seed=1024, metric='cos'):
+ """Instantiates the FM architecture.
+
+ :param user_feature_columns: An iterable containing user's features used by the model.
+ :param item_feature_columns: An iterable containing item's features used by the model.
+ :param l2_reg_embedding: float. L2 regularizer strength applied to embedding vector
+ :param init_std: float,to use as the initialize std of embedding vector
+ :param seed: integer ,to use as random seed.
+ :param metric: str, ``"cos"`` for cosine or ``"ip"`` for inner product
+ :return: A Keras model instance.
+
+ """
+
+ embedding_matrix_dict = create_embedding_matrix(user_feature_columns + item_feature_columns, l2_reg_embedding,
+ init_std, seed,
+ seq_mask_zero=True)
+
+ user_features = build_input_features(user_feature_columns)
+ user_inputs_list = list(user_features.values())
+ user_sparse_embedding_list, user_dense_value_list = input_from_feature_columns(user_features,
+ user_feature_columns,
+ l2_reg_embedding, init_std, seed,
+ support_dense=False,
+ embedding_matrix_dict=embedding_matrix_dict)
+
+ item_features = build_input_features(item_feature_columns)
+ item_inputs_list = list(item_features.values())
+ item_sparse_embedding_list, item_dense_value_list = input_from_feature_columns(item_features,
+ item_feature_columns,
+ l2_reg_embedding, init_std, seed,
+ support_dense=False,
+ embedding_matrix_dict=embedding_matrix_dict)
+
+ user_dnn_input = concat_func(user_sparse_embedding_list, axis=1)
+ user_vector_sum = Lambda(lambda x: reduce_sum(x, axis=1, keep_dims=False))(user_dnn_input)
+
+ item_dnn_input = concat_func(item_sparse_embedding_list, axis=1)
+ item_vector_sum = Lambda(lambda x: reduce_sum(x, axis=1, keep_dims=False))(item_dnn_input)
+
+ score = Similarity(type=metric)([user_vector_sum, item_vector_sum])
+
+ output = PredictionLayer("binary", False)(score)
+
+ model = Model(inputs=user_inputs_list + item_inputs_list, outputs=output)
+
+ model.__setattr__("user_input", user_inputs_list)
+ model.__setattr__("user_embedding", user_vector_sum)
+
+ model.__setattr__("item_input", item_inputs_list)
+ model.__setattr__("item_embedding", item_vector_sum)
+
+ return model
diff --git a/deepmatch/models/youtubednn.py b/deepmatch/models/youtubednn.py
new file mode 100644
index 0000000..2c1fe29
--- /dev/null
+++ b/deepmatch/models/youtubednn.py
@@ -0,0 +1,70 @@
+"""
+Author:
+ Weichen Shen, wcshen1994@163.com
+Reference:
+Deep Neural Networks for YouTube Recommendations
+"""
+
+from deepctr.inputs import input_from_feature_columns, build_input_features, combined_dnn_input, create_embedding_matrix
+from deepctr.layers.core import DNN
+from tensorflow.python.keras.models import Model
+
+from deepmatch.utils import get_item_embedding
+from ..inputs import input_from_feature_columns
+from ..layers.core import SampledSoftmaxLayer
+
+
+def YoutubeDNN(user_feature_columns, item_feature_columns, num_sampled=5,
+ user_dnn_hidden_units=(64, 16),
+ dnn_activation='relu', dnn_use_bn=False,
+ l2_reg_dnn=0, l2_reg_embedding=1e-6, dnn_dropout=0, init_std=0.0001, seed=1024, ):
+ """Instantiates the YoutubeDNN Model architecture.
+
+ :param user_feature_columns: An iterable containing user's features used by the model.
+ :param item_feature_columns: An iterable containing item's features used by the model.
+ :param num_sampled: int, the number of classes to randomly sample per batch.
+ :param user_dnn_hidden_units: list,list of positive integer or empty list, the layer number and units in each layer of user tower
+ :param dnn_activation: Activation function to use in deep net
+ :param dnn_use_bn: bool. Whether use BatchNormalization before activation or not in deep net
+ :param l2_reg_dnn: float. L2 regularizer strength applied to DNN
+ :param l2_reg_embedding: float. L2 regularizer strength applied to embedding vector
+ :param dnn_dropout: float in [0,1), the probability we will drop out a given DNN coordinate.
+ :param init_std: float,to use as the initialize std of embedding vector
+ :param seed: integer ,to use as random seed.
+ :return: A Keras model instance.
+
+ """
+
+ if len(item_feature_columns) > 1:
+ raise ValueError("Now YoutubeNN only support 1 item feature like item_id")
+ item_feature_name = item_feature_columns[0].name
+
+ embedding_matrix_dict = create_embedding_matrix(user_feature_columns + item_feature_columns, l2_reg_embedding,
+ init_std, seed, prefix="")
+
+ user_features = build_input_features(user_feature_columns)
+ user_inputs_list = list(user_features.values())
+ user_sparse_embedding_list, user_dense_value_list = input_from_feature_columns(user_features,
+ user_feature_columns,
+ l2_reg_embedding, init_std, seed,
+ embedding_matrix_dict=embedding_matrix_dict)
+ user_dnn_input = combined_dnn_input(user_sparse_embedding_list, user_dense_value_list)
+
+ item_features = build_input_features(item_feature_columns)
+ item_inputs_list = list(item_features.values())
+ user_dnn_out = DNN(user_dnn_hidden_units, dnn_activation, l2_reg_dnn, dnn_dropout,
+ dnn_use_bn, seed, )(user_dnn_input)
+
+ item_embedding = embedding_matrix_dict[item_feature_name]
+
+ output = SampledSoftmaxLayer(item_embedding, num_sampled=num_sampled)(
+ inputs=(user_dnn_out, item_features[item_feature_name]))
+ model = Model(inputs=user_inputs_list + item_inputs_list, outputs=output)
+
+ model.__setattr__("user_input", user_inputs_list)
+ model.__setattr__("user_embedding", user_dnn_out)
+
+ model.__setattr__("item_input", item_inputs_list)
+ model.__setattr__("item_embedding", get_item_embedding(item_embedding, item_features[item_feature_name]))
+
+ return model
diff --git a/deepmatch/utils.py b/deepmatch/utils.py
new file mode 100644
index 0000000..721b226
--- /dev/null
+++ b/deepmatch/utils.py
@@ -0,0 +1,64 @@
+# -*- coding:utf-8 -*-
+"""
+
+Author:
+ Weichen Shen,wcshen1994@163.com
+
+"""
+
+import json
+import logging
+from threading import Thread
+
+import requests
+
+try:
+ from packaging.version import parse
+except ImportError:
+ from pip._vendor.packaging.version import parse
+
+
+import tensorflow as tf
+from tensorflow.python.keras import backend as K
+from tensorflow.python.keras._impl.keras.layers import Lambda
+
+def recall_N(y_true, y_pred, N=50):
+ return len(set(y_pred[:N]) & set(y_true)) * 1.0 / len(y_true)
+
+
+def sampledsoftmaxloss(y_true, y_pred):
+ return K.mean(y_pred)
+
+
+def get_item_embedding(item_embedding_layer, item_input_layer):
+ return Lambda(lambda x: tf.squeeze(tf.gather(item_embedding_layer.embeddings, x), axis=1))(
+ item_input_layer)
+
+
+
+def check_version(version):
+ """Return version of package on pypi.python.org using json."""
+
+ def check(version):
+ try:
+ url_pattern = 'https://pypi.python.org/pypi/deepmatch/json'
+ req = requests.get(url_pattern)
+ latest_version = parse('0')
+ version = parse(version)
+ if req.status_code == requests.codes.ok:
+ j = json.loads(req.text.encode('utf-8'))
+ releases = j.get('releases', [])
+ for release in releases:
+ ver = parse(release)
+ if ver.is_prerelease or ver.is_postrelease:
+ continue
+ latest_version = max(latest_version, ver)
+ if latest_version > version:
+ logging.warning(
+ '\nDeepMatch version {0} detected. Your version is {1}.\nUse `pip install -U deepmatch` to upgrade.Changelog: https://github.com/shenweichen/DeepMatch/releases/tag/v{0}'.format(
+ latest_version, version))
+ except:
+ print("Please check the latest version manually on https://pypi.org/project/deepmatch/#history")
+ return
+
+ Thread(target=check, args=(version,)).start()
diff --git a/docs/Makefile b/docs/Makefile
new file mode 100644
index 0000000..4fb32d8
--- /dev/null
+++ b/docs/Makefile
@@ -0,0 +1,20 @@
+# Minimal makefile for Sphinx documentation
+#
+
+# You can set these variables from the command line.
+SPHINXOPTS =
+SPHINXBUILD = sphinx-build
+SPHINXPROJ = DeepMatch
+SOURCEDIR = source
+BUILDDIR = build
+
+# Put it first so that "make" without argument is like "make help".
+help:
+ @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
+
+.PHONY: help Makefile
+
+# Catch-all target: route all unknown targets to Sphinx using the new
+# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
+%: Makefile
+ @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
\ No newline at end of file
diff --git a/docs/make.bat b/docs/make.bat
new file mode 100644
index 0000000..22e7990
--- /dev/null
+++ b/docs/make.bat
@@ -0,0 +1,36 @@
+@ECHO OFF
+
+pushd %~dp0
+
+REM Command file for Sphinx documentation
+
+if "%SPHINXBUILD%" == "" (
+ set SPHINXBUILD=sphinx-build
+)
+set SOURCEDIR=source
+set BUILDDIR=build
+set SPHINXPROJ=DeepMatch
+
+if "%1" == "" goto help
+
+%SPHINXBUILD% >NUL 2>NUL
+if errorlevel 9009 (
+ echo.
+ echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
+ echo.installed, then set the SPHINXBUILD environment variable to point
+ echo.to the full path of the 'sphinx-build' executable. Alternatively you
+ echo.may add the Sphinx directory to PATH.
+ echo.
+ echo.If you don't have Sphinx installed, grab it from
+ echo.http://sphinx-doc.org/
+ exit /b 1
+)
+
+%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS%
+goto end
+
+:help
+%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS%
+
+:end
+popd
diff --git a/docs/pics/deepctrbot.png b/docs/pics/deepctrbot.png
new file mode 100644
index 0000000..780e614
Binary files /dev/null and b/docs/pics/deepctrbot.png differ
diff --git a/docs/pics/dssm.jpg b/docs/pics/dssm.jpg
new file mode 100644
index 0000000..6635828
Binary files /dev/null and b/docs/pics/dssm.jpg differ
diff --git a/docs/pics/mind.jpg b/docs/pics/mind.jpg
new file mode 100644
index 0000000..4dd1388
Binary files /dev/null and b/docs/pics/mind.jpg differ
diff --git a/docs/pics/movielens_sample.png b/docs/pics/movielens_sample.png
new file mode 100644
index 0000000..b74e333
Binary files /dev/null and b/docs/pics/movielens_sample.png differ
diff --git a/docs/pics/ncf.jpg b/docs/pics/ncf.jpg
new file mode 100644
index 0000000..a198408
Binary files /dev/null and b/docs/pics/ncf.jpg differ
diff --git a/docs/pics/neucf.jpg b/docs/pics/neucf.jpg
new file mode 100644
index 0000000..f2630e0
Binary files /dev/null and b/docs/pics/neucf.jpg differ
diff --git a/docs/pics/weichennote.png b/docs/pics/weichennote.png
new file mode 100644
index 0000000..fec7b11
Binary files /dev/null and b/docs/pics/weichennote.png differ
diff --git a/docs/pics/youtubednn.jpg b/docs/pics/youtubednn.jpg
new file mode 100644
index 0000000..57b6864
Binary files /dev/null and b/docs/pics/youtubednn.jpg differ
diff --git a/docs/source/Examples.md b/docs/source/Examples.md
new file mode 100644
index 0000000..81cc023
--- /dev/null
+++ b/docs/source/Examples.md
@@ -0,0 +1,247 @@
+# Examples
+
+## DSSM with negative sampling
+
+The MovieLens data has been used for personalized tag recommendation,which
+contains 668, 953 tag applications of users on movies.
+Here is a small fraction of data include only sparse field.
+
+
+
+
+This example shows how to use ``DSSM`` to solve a matching task. You can get the demo data
+[movielens_sample.txt](https://github.com/shenweichen/DeepMatch/tree/master/examples/movielens_sample.txt) and run the following codes.
+
+```python
+import pandas as pd
+from deepctr.inputs import SparseFeat, VarLenSparseFeat
+from preprocess import gen_data_set, gen_model_input
+from sklearn.preprocessing import LabelEncoder
+from tensorflow.python.keras.models import Model
+
+from deepmatch.models import *
+
+if __name__ == "__main__":
+
+ data = pd.read_csvdata = pd.read_csv("./movielens_sample.txt")
+ sparse_features = ["movie_id", "user_id",
+ "gender", "age", "occupation", "zip", ]
+ SEQ_LEN = 50
+ negsample = 3
+
+ # 1.Label Encoding for sparse features,and process sequence features with `gen_date_set` and `gen_model_input`
+
+ features = ['user_id', 'movie_id', 'gender', 'age', 'occupation', 'zip']
+ feature_max_idx = {}
+ for feature in features:
+ lbe = LabelEncoder()
+ data[feature] = lbe.fit_transform(data[feature]) + 1
+ feature_max_idx[feature] = data[feature].max() + 1
+
+ user_profile = data[["user_id", "gender", "age", "occupation", "zip"]].drop_duplicates('user_id')
+
+ item_profile = data[["movie_id"]].drop_duplicates('movie_id')
+
+ user_profile.set_index("user_id", inplace=True)
+
+ user_item_list = data.groupby("user_id")['movie_id'].apply(list)
+
+ train_set, test_set = gen_data_set(data, negsample)
+
+ train_model_input, train_label = gen_model_input(train_set, user_profile, SEQ_LEN)
+ test_model_input, test_label = gen_model_input(test_set, user_profile, SEQ_LEN)
+
+ # 2.count #unique features for each sparse field and generate feature config for sequence feature
+
+ embedding_dim = 16
+
+ user_feature_columns = [SparseFeat('user_id', feature_max_idx['user_id'], embedding_dim),
+ SparseFeat("gender", feature_max_idx['gender'], embedding_dim),
+ SparseFeat("age", feature_max_idx['age'], embedding_dim),
+ SparseFeat("occupation", feature_max_idx['occupation'], embedding_dim),
+ SparseFeat("zip", feature_max_idx['zip'], embedding_dim),
+ VarLenSparseFeat(SparseFeat('hist_movie_id', feature_max_idx['movie_id'], embedding_dim,
+ embedding_name="movie_id"), SEQ_LEN, 'mean', 'hist_len'),
+ ]
+
+ item_feature_columns = [SparseFeat('movie_id', feature_max_idx['movie_id'], embedding_dim)]
+
+ # 3.Define Model and train
+
+ model = DSSM(user_feature_columns, item_feature_columns) # FM(user_feature_columns,item_feature_columns)
+
+ model.compile(optimizer='adagrad', loss="binary_crossentropy")
+
+ history = model.fit(train_model_input, train_label, # train_label,
+ batch_size=256, epochs=1, verbose=1, validation_split=0.0, )
+
+ # 4. Generate user features for testing and full item features for retrieval
+ test_user_model_input = test_model_input
+ all_item_model_input = {"movie_id": item_profile['movie_id'].values, "movie_idx": item_profile['movie_id'].values}
+
+ user_embedding_model = Model(inputs=model.user_input, outputs=model.user_embedding)
+ item_embedding_model = Model(inputs=model.item_input, outputs=model.item_embedding)
+
+ user_embs = user_embedding_model.predict(test_user_model_input, batch_size=2 ** 12)
+ item_embs = item_embedding_model.predict(all_item_model_input, batch_size=2 ** 12)
+
+ print(user_embs.shape)
+ print(item_embs.shape)
+
+ # 5. [Optional] ANN search by faiss and evaluate the result
+
+ # test_true_label = {line[0]:[line[2]] for line in test_set}
+ #
+ # import numpy as np
+ # import faiss
+ # from tqdm import tqdm
+ # from deepmatch.utils import recall_N
+ #
+ # index = faiss.IndexFlatIP(embedding_dim)
+ # # faiss.normalize_L2(item_embs)
+ # index.add(item_embs)
+ # # faiss.normalize_L2(user_embs)
+ # D, I = index.search(user_embs, 50)
+ # s = []
+ # hit = 0
+ # for i, uid in tqdm(enumerate(test_user_model_input['user_id'])):
+ # try:
+ # pred = [item_profile['movie_id'].values[x] for x in I[i]]
+ # filter_item = None
+ # recall_score = recall_N(test_true_label[uid], pred, N=50)
+ # s.append(recall_score)
+ # if test_true_label[uid] in pred:
+ # hit += 1
+ # except:
+ # print(i)
+ # print("recall", np.mean(s))
+ # print("hr", hit / len(test_user_model_input['user_id']))
+
+
+```
+
+## YoutubeDNN with sampled softmax
+
+The MovieLens data has been used for personalized tag recommendation,which
+contains 668, 953 tag applications of users on movies.
+Here is a small fraction of data include only sparse field.
+
+
+
+
+This example shows how to use ``YoutubeDNN`` to solve a matching task. You can get the demo data
+[movielens_sample.txt](https://github.com/shenweichen/DeepMatch/tree/master/examples/movielens_sample.txt) and run the following codes.
+
+```python
+import pandas as pd
+from deepctr.inputs import SparseFeat, VarLenSparseFeat
+from preprocess import gen_data_set, gen_model_input
+from sklearn.preprocessing import LabelEncoder
+from tensorflow.python.keras import backend as K
+from tensorflow.python.keras.models import Model
+
+from deepmatch.models import *
+from deepmatch.utils import sampledsoftmaxloss
+
+if __name__ == "__main__":
+
+ data = pd.read_csvdata = pd.read_csv("./movielens_sample.txt")
+ sparse_features = ["movie_id", "user_id",
+ "gender", "age", "occupation", "zip", ]
+ SEQ_LEN = 50
+ negsample = 0
+
+ # 1.Label Encoding for sparse features,and process sequence features with `gen_date_set` and `gen_model_input`
+
+ features = ['user_id', 'movie_id', 'gender', 'age', 'occupation', 'zip']
+ feature_max_idx = {}
+ for feature in features:
+ lbe = LabelEncoder()
+ data[feature] = lbe.fit_transform(data[feature]) + 1
+ feature_max_idx[feature] = data[feature].max() + 1
+
+ user_profile = data[["user_id", "gender", "age", "occupation", "zip"]].drop_duplicates('user_id')
+
+ item_profile = data[["movie_id"]].drop_duplicates('movie_id')
+
+ user_profile.set_index("user_id", inplace=True)
+
+ user_item_list = data.groupby("user_id")['movie_id'].apply(list)
+
+ train_set, test_set = gen_data_set(data, negsample)
+
+ train_model_input, train_label = gen_model_input(train_set, user_profile, SEQ_LEN)
+ test_model_input, test_label = gen_model_input(test_set, user_profile, SEQ_LEN)
+
+ # 2.count #unique features for each sparse field and generate feature config for sequence feature
+
+ embedding_dim = 16
+
+ user_feature_columns = [SparseFeat('user_id', feature_max_idx['user_id'], embedding_dim),
+ SparseFeat("gender", feature_max_idx['gender'], embedding_dim),
+ SparseFeat("age", feature_max_idx['age'], embedding_dim),
+ SparseFeat("occupation", feature_max_idx['occupation'], embedding_dim),
+ SparseFeat("zip", feature_max_idx['zip'], embedding_dim),
+ VarLenSparseFeat(SparseFeat('hist_movie_id', feature_max_idx['movie_id'], embedding_dim,
+ embedding_name="movie_id"), SEQ_LEN, 'mean', 'hist_len'),
+ ]
+
+ item_feature_columns = [SparseFeat('movie_id', feature_max_idx['movie_id'], embedding_dim)]
+
+ # 3.Define Model and train
+
+ K.set_learning_phase(True)
+
+ model = YoutubeDNN(user_feature_columns, item_feature_columns, num_sampled=5, user_dnn_hidden_units=(64, 16))
+ # model = MIND(user_feature_columns,item_feature_columns,dynamic_k=True,p=1,k_max=2,num_sampled=5,user_dnn_hidden_units=(64,16),init_std=0.001)
+
+ model.compile(optimizer="adagrad", loss=sampledsoftmaxloss) # "binary_crossentropy")
+
+ history = model.fit(train_model_input, train_label, # train_label,
+ batch_size=256, epochs=1, verbose=1, validation_split=0.0, )
+
+ # 4. Generate user features for testing and full item features for retrieval
+ test_user_model_input = test_model_input
+ all_item_model_input = {"movie_id": item_profile['movie_id'].values, "movie_idx": item_profile['movie_id'].values}
+
+ user_embedding_model = Model(inputs=model.user_input, outputs=model.user_embedding)
+ item_embedding_model = Model(inputs=model.item_input, outputs=model.item_embedding)
+
+ user_embs = user_embedding_model.predict(test_user_model_input, batch_size=2 ** 12)
+ # user_embs = user_embs[:, i, :] i in [0,k_max) if MIND
+ item_embs = item_embedding_model.predict(all_item_model_input, batch_size=2 ** 12)
+
+ print(user_embs.shape)
+ print(item_embs.shape)
+
+ # 5. [Optional] ANN search by faiss and evaluate the result
+
+ # test_true_label = {line[0]:[line[2]] for line in test_set}
+ #
+ # import numpy as np
+ # import faiss
+ # from tqdm import tqdm
+ # from deepmatch.utils import recall_N
+ #
+ # index = faiss.IndexFlatIP(embedding_dim)
+ # # faiss.normalize_L2(item_embs)
+ # index.add(item_embs)
+ # # faiss.normalize_L2(user_embs)
+ # D, I = index.search(user_embs, 50)
+ # s = []
+ # hit = 0
+ # for i, uid in tqdm(enumerate(test_user_model_input['user_id'])):
+ # try:
+ # pred = [item_profile['movie_id'].values[x] for x in I[i]]
+ # filter_item = None
+ # recall_score = recall_N(test_true_label[uid], pred, N=50)
+ # s.append(recall_score)
+ # if test_true_label[uid] in pred:
+ # hit += 1
+ # except:
+ # print(i)
+ # print("recall", np.mean(s))
+ # print("hr", hit / len(test_user_model_input['user_id']))
+
+
+```
diff --git a/docs/source/FAQ.md b/docs/source/FAQ.md
new file mode 100644
index 0000000..49f8a8e
--- /dev/null
+++ b/docs/source/FAQ.md
@@ -0,0 +1,18 @@
+# FAQ
+
+## 1. Set learning rate and use earlystopping
+---------------------------------------------------
+You can use any models in DeepCTR like a keras model object.
+Here is a example of how to set learning rate and earlystopping:
+
+```python
+import deepmatch
+from tensorflow.python.keras.optimizers import Adam,Adagrad
+from tensorflow.python.keras.callbacks import EarlyStopping
+
+model = deepmatch.models.FM(user_feature_columns,item_feature_columns)
+model.compile(Adagrad(0.01),'binary_crossentropy',metrics=['binary_crossentropy'])
+
+es = EarlyStopping(monitor='val_binary_crossentropy')
+history = model.fit(model_input, data[target].values,batch_size=256, epochs=10, verbose=2, validation_split=0.2,callbacks=[es] )
+```
diff --git a/docs/source/Features.md b/docs/source/Features.md
new file mode 100644
index 0000000..904854c
--- /dev/null
+++ b/docs/source/Features.md
@@ -0,0 +1,82 @@
+# Features
+
+## Feature Columns
+### SparseFeat
+``SparseFeat`` is a namedtuple with signature ``SparseFeat(name, vocabulary_size, embedding_dim, use_hash, dtype,embedding_name, group_name)``
+
+- name : feature name
+- vocabulary_size : number of unique feature values for sprase feature or hashing space when `use_hash=True`
+- embedding_dim : embedding dimension
+- use_hash : defualt `False`.If `True` the input will be hashed to space of size `vocabulary_size`.
+- dtype : default `float32`.dtype of input tensor.
+- embedding_name : default `None`. If None, the embedding_name will be same as `name`.
+- group_name : feature group of this feature.
+
+### DenseFeat
+``DenseFeat`` is a namedtuple with signature ``DenseFeat(name, dimension, dtype)``
+
+- name : feature name
+- dimension : dimension of dense feature vector.
+- dtype : default `float32`.dtype of input tensor.
+
+### VarLenSparseFeat
+
+``VarLenSparseFeat`` is a namedtuple with signature ``VarLenSparseFeat(sparsefeat, maxlen, combiner, length_name, weight_name,weight_norm)``
+
+- sparsefeat : a instance of `SparseFeat`
+- maxlen : maximum length of this feature for all samples
+- combiner : pooling method,can be ``sum``,``mean`` or ``max``
+- length_name : feature length name,if `None`, value 0 in feature is for padding.
+- weight_name : default `None`. If not None, the sequence feature will be multiplyed by the feature whose name is `weight_name`.
+- weight_norm : default `True`. Whether normalize the weight score or not.
+
+## Models
+
+
+### FM (Convolutional Click Prediction Model)
+
+
+[**FM Model API**](./deepmatch.models.fm.html)
+
+
+[Factorization Machines](https://www.researchgate.net/publication/220766482_Factorization_Machines)
+
+
+### DSSM (Deep Structured Semantic Model)
+
+
+[**DSSM Model API**](./deepmatch.models.dssm.html)
+
+
+
+
+[Deep Structured Semantic Models for Web Search using Clickthrough Data](https://www.microsoft.com/en-us/research/publication/learning-deep-structured-semantic-models-for-web-search-using-clickthrough-data/)
+
+### YoutubeDNN
+
+
+[**YoutubeDNN Model API**](./deepmatch.models.youtubednn.html)
+
+
+
+[Deep Neural Networks for YouTube Recommendations](https://www.researchgate.net/publication/307573656_Deep_Neural_Networks_for_YouTube_Recommendations)
+
+### NCF (Neural Collaborative Filtering)
+
+[**NCF Model API**](./deepmatch.models.ncf.html)
+
+
+
+[Neural Collaborative Filtering](https://arxiv.org/abs/1708.05031)
+
+
+### MIND (Multi-Interest Network with Dynamic routing)
+
+
+
+[**MIND Model API**](./deepmatch.models.mind.html)
+
+
+
+[Multi-interest network with dynamic routing for recommendation at Tmall](https://arxiv.org/pdf/1904.08030)
+
diff --git a/docs/source/History.md b/docs/source/History.md
new file mode 100644
index 0000000..b2d0d09
--- /dev/null
+++ b/docs/source/History.md
@@ -0,0 +1,2 @@
+# History
+- 04/06/2020 : DeepMatch first version v0.0.0 is released on [PyPi](https://pypi.org/project/deepmatch/)
\ No newline at end of file
diff --git a/docs/source/Model_Methods.md b/docs/source/Model_Methods.md
new file mode 100644
index 0000000..789c021
--- /dev/null
+++ b/docs/source/Model_Methods.md
@@ -0,0 +1,260 @@
+# Methods
+## compile
+```python
+compile(optimizer, loss=None, metrics=None, loss_weights=None, sample_weight_mode=None, weighted_metrics=None, target_tensors=None)
+```
+
+Configures the model for training.
+
+**Arguments**
+- **optimizer**: String (name of optimizer) or optimizer instance. See [optimizers](https://www.tensorflow.org/versions/r1.12/api_docs/python/tf/keras/optimizers/).
+- **loss**: String (name of objective function) or objective function. See [losses](https://www.tensorflow.org/versions/r1.12/api_docs/python/tf/keras/losses). If the model has multiple outputs, you can use a different loss on each output by passing a dictionary or a list of losses. The loss value that will be minimized by the model will then be the sum of all individual losses.
+- **metrics**: List of metrics to be evaluated by the model during training and testing. Typically you will use `metrics=['accuracy']`. To specify different metrics for different outputs of a multi-output model, you could also pass a dictionary, such as `metrics={'output_a': 'accuracy'}`.
+- **loss_weights**: Optional list or dictionary specifying scalar coefficients (Python floats) to weight the loss contributions of different model outputs. The loss value that will be minimized by the model will then be the weighted sum of all individual losses, weighted by the `loss_weights` coefficients. If a list, it is expected to have a 1:1 mapping to the model's outputs. If a tensor, it is expected to map output names (strings) to scalar coefficients.
+- **sample_weight_mode**: If you need to do timestep-wise sample weighting (2D weights), set this to `"temporal"`. `None` defaults to sample-wise weights (1D). If the model has multiple outputs, you can use a different `sample_weight_mode` on each output by passing a dictionary or a list of modes.
+- **weighted_metrics**: List of metrics to be evaluated and weighted by sample_weight or class_weight during training and testing.
+- **target_tensors**: By default, Keras will create placeholders for the model's target, which will be fed with the target data during training. If instead you would like to use your own target tensors (in turn, Keras will not expect external Numpy data for these targets at training time), you can specify them via the `target_tensors` argument. It can be a single tensor (for a single-output model), a list of tensors, or a dict mapping output names to target tensors.
+
+**Raises**
+
+- **ValueError**: In case of invalid arguments for `optimizer`, `loss`, `metrics` or `sample_weight_mode`.
+
+## fit
+```python
+fit(x=None, y=None, batch_size=None, epochs=1, verbose=1, callbacks=None, validation_split=0.0, validation_data=None, shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0, steps_per_epoch=None, validation_steps=None, validation_freq=1)
+```
+Trains the model for a given number of epochs (iterations on a dataset).
+
+**Arguments**
+- **x**: Numpy array of training data (if the model has a single input), or list of Numpy arrays (if the model has multiple inputs). If input layers in the model are named, you can also pass a dictionary mapping input names to Numpy arrays. `x` can be `None` (default) if feeding from framework-native tensors (e.g. TensorFlow data tensors).
+- **y**: Numpy array of target (label) data (if the model has a single output), or list of Numpy arrays (if the model has multiple outputs). If output layers in the model are named, you can also pass a dictionary mapping output names to Numpy arrays. `y` can be `None` (default) if feeding from framework-native tensors (e.g. TensorFlow data tensors).
+- **batch_size**: Integer or `None`. Number of samples per gradient update. If unspecified, `batch_size` will default to 32.
+- **epochs**: Integer. Number of epochs to train the model. An epoch is an iteration over the entire `x` and `y` data provided. Note that in conjunction with `initial_epoch`, `epochs` is to be understood as "final epoch". The model is not trained for a number of iterations given by `epochs`, but merely until the epoch of index `epochs` is reached.
+- **verbose**: Integer. 0, 1, or 2. Verbosity mode. 0 = silent, 1 = progress bar, 2 = one line per epoch.
+- **callbacks**: List of `tf.keras.callbacks.Callback` instances. List of callbacks to apply during training and validation (if ). See [callbacks](https://www.tensorflow.org/versions/r1.12/api_docs/python/tf/keras/callbacks).
+- **validation_split**: Float between 0 and 1. Fraction of the training data to be used as validation data. The model will set apart this fraction of the training data, will not train on it, and will evaluate the loss and any model metrics on this data at the end of each epoch. The validation data is selected from the last samples in the `x` and `y` data provided, before shuffling.
+- **validation_data**: tuple `(x_val, y_val)` or tuple `(x_val, y_val, val_sample_weights)` on which to evaluate the loss and any model metrics at the end of each epoch. The model will not be trained on this data. `validation_data` will override `validation_split`.
+- **shuffle**: Boolean (whether to shuffle the training data before each epoch) or str (for 'batch'). 'batch' is a special option for dealing with the limitations of HDF5 data; it shuffles in batch-sized chunks. Has no effect when `steps_per_epoch` is not `None`.
+- **class_weight**: Optional dictionary mapping class indices (integers) to a weight (float) value, used for weighting the loss function (during training only). This can be useful to tell the model to "pay more attention" to samples from an under-represented class.
+- **sample_weight**: Optional Numpy array of weights for the training samples, used for weighting the loss function (during training only). You can either pass a flat (1D) Numpy array with the same length as the input samples (1:1 mapping between weights and samples), or in the case of temporal data, you can pass a 2D array with shape `(samples, sequence_length)`, to apply a different weight to every timestep of every sample. In this case you should make sure to specify `sample_weight_mode="temporal"` in `compile()`.
+- **initial_epoch**: Integer. Epoch at which to start training (useful for resuming a previous training run).
+- **steps_per_epoch**: Integer or `None`. Total number of steps (batches of samples) before declaring one epoch finished and starting the next epoch. When training with input tensors such as TensorFlow data tensors, the default `None` is equal to the number of samples in your dataset divided by the batch size, or 1 if that cannot be determined.
+validation_steps: Only relevant if `steps_per_epoch` is specified. Total number of steps (batches of samples) to validate before stopping.
+- **validation_freq**: Only relevant if validation data is provided. Integer or list/tuple/set. If an integer, specifies how many training epochs to run before a new validation run is performed, e.g. `validation_freq=2` runs validation every 2 epochs. If a list, tuple, or set, specifies the epochs on which to run validation, e.g. `validation_freq=[1, 2, 10]` runs validation at the end of the 1st, 2nd, and 10th epochs.
+
+**Returns**
+
+- A `History` object. Its `History.history` attribute is a record of training loss values and metrics values at successive epochs, as well as validation loss values and validation metrics values (if applicable).
+
+**Raises**
+- **RuntimeError**: If the model was never compiled.
+ValueError: In case of mismatch between the provided input data and what the model expects.
+
+## evaluate
+```python
+evaluate(x=None, y=None, batch_size=None, verbose=1, sample_weight=None, steps=None, callbacks=None)
+```
+Returns the loss value & metrics values for the model in test mode.
+Computation is done in batches.
+
+**Arguments**
+
+- **x**: Numpy array of test data (if the model has a single input), or list of Numpy arrays (if the model has multiple inputs). If input layers in the model are named, you can also pass a dictionary mapping input names to Numpy arrays. `x` can be `None` (default) if feeding from framework-native tensors (e.g. TensorFlow data tensors).
+- **y**: Numpy array of target (label) data (if the model has a single output), or list of Numpy arrays (if the model has multiple outputs). If output layers in the model are named, you can also pass a dictionary mapping output names to Numpy arrays. `y` can be `None` (default) if feeding from framework-native tensors (e.g. TensorFlow data tensors).
+- **batch_size**: Integer or `None`. Number of samples per evaluation step. If unspecified, `batch_size` will default to 32.
+- **verbose**: 0 or 1. Verbosity mode. 0 = silent, 1 = progress bar.
+- **sample_weight**: Optional Numpy array of weights for the test samples, used for weighting the loss function. You can either pass a flat (1D) Numpy array with the same length as the input samples (1:1 mapping between weights and samples), or in the case of temporal data, you can pass a 2D array with shape `(samples, sequence_length)`, to apply a different weight to every timestep of every sample. In this case you should make sure to specify `sample_weight_mode="temporal"` in `compile()`.
+- **steps**: Integer or `None`. Total number of steps (batches of samples) before declaring the evaluation round finished. Ignored with the default value of `None`.
+- **callbacks**: List of `tf.keras.callbacks.Callback` instances. List of callbacks to apply during evaluation. See [callbacks](https://www.tensorflow.org/versions/r1.12/api_docs/python/tf/keras/callbacks).
+
+**Returns**
+- Scalar test loss (if the model has a single output and no metrics) or list of scalars (if the model has multiple outputs and/or metrics). The attribute `model.metrics_names` will give you the display labels for the scalar outputs.
+
+## predict
+```python
+predict(x, batch_size=None, verbose=0, steps=None, callbacks=None)
+```
+Generates output predictions for the input samples.
+
+Computation is done in batches.
+
+**Arguments**
+
+- **x**: The input data, as a Numpy array (or list of Numpy arrays if the model has multiple inputs).
+batch_size: Integer. If unspecified, it will default to 32.
+- **verbose**: Verbosity mode, 0 or 1.
+- **steps**: Total number of steps (batches of samples) before declaring the prediction round finished. Ignored with the default value of None.
+- **callbacks**: List of `tf.keras.callbacks.Callback` instances. List of callbacks to apply during prediction. See [callbacks](https://www.tensorflow.org/versions/r1.12/api_docs/python/tf/keras/callbacks).
+
+**Returns**
+
+- Numpy array(s) of predictions.
+
+**Raises**
+
+- **ValueError**: In case of mismatch between the provided input data and the model's expectations, or in case a stateful model receives a number of samples that is not a multiple of the batch size.
+
+## train_on_batch
+```python
+train_on_batch(x, y, sample_weight=None, class_weight=None)
+```
+Runs a single gradient update on a single batch of data.
+
+**Arguments**
+
+- **x**: Numpy array of training data, or list of Numpy arrays if the model has multiple inputs. If all inputs in the model are named, you can also pass a dictionary mapping input names to Numpy arrays.
+- **y**: Numpy array of target data, or list of Numpy arrays if the model has multiple outputs. If all outputs in the model are named, you can also pass a dictionary mapping output names to Numpy arrays.
+- **sample_weight**: Optional array of the same length as x, containing weights to apply to the model's loss for each sample. In the case of temporal data, you can pass a 2D array with shape (samples, sequence_length), to apply a different weight to every timestep of every sample. In this case you should make sure to specify sample_weight_mode="temporal" in compile().
+- **class_weight**: Optional dictionary mapping class indices (integers) to a weight (float) to apply to the model's loss for the samples from this class during training. This can be useful to tell the model to "pay more attention" to samples from an under-represented class.
+
+**Returns**
+
+- Scalar training loss (if the model has a single output and no metrics) or list of scalars (if the model has multiple outputs and/or metrics). The attribute `model.metrics_names` will give you the display labels for the scalar outputs.
+
+## test_on_batch
+```python
+test_on_batch(x, y, sample_weight=None)
+```
+Test the model on a single batch of samples.
+
+**Arguments**
+
+- **x**: Numpy array of test data, or list of Numpy arrays if the model has multiple inputs. If all inputs in the model are named, you can also pass a dictionary mapping input names to Numpy arrays.
+- **y**: Numpy array of target data, or list of Numpy arrays if the model has multiple outputs. If all outputs in the model are named, you can also pass a dictionary mapping output names to Numpy arrays.
+- **sample_weight**: Optional array of the same length as x, containing weights to apply to the model's loss for each sample. In the case of temporal data, you can pass a 2D array with shape (samples, sequence_length), to apply a different weight to every timestep of every sample. In this case you should make sure to specify `sample_weight_mode="temporal"` in `compile()`.
+
+**Returns**
+
+- Scalar test loss (if the model has a single output and no metrics) or list of scalars (if the model has multiple outputs and/or metrics). The attribute `model.metrics_names` will give you the display labels for the scalar outputs.
+
+## predict_on_batch
+```python
+predict_on_batch(x)
+```
+Returns predictions for a single batch of samples.
+
+**Arguments**
+- **x**: Input samples, as a Numpy array.
+
+**Returns**
+- Numpy array(s) of predictions.
+
+## fit_generator
+```python
+fit_generator(generator, steps_per_epoch=None, epochs=1, verbose=1, callbacks=None, validation_data=None, validation_steps=None, validation_freq=1, class_weight=None, max_queue_size=10, workers=1, use_multiprocessing=False, shuffle=True, initial_epoch=0)
+```
+Trains the model on data generated batch-by-batch by a Python generator (or an instance of `Sequence`).
+The generator is run in parallel to the model, for efficiency. For instance, this allows you to do real-time data augmentation on images on CPU in parallel to training your model on GPU.
+The use of `tf.keras.utils.Sequence` guarantees the ordering and guarantees the single use of every input per epoch when using `use_multiprocessing=True`.
+
+**Arguments**
+
+- **generator**: A generator or an instance of `Sequence` (`tf.keras.utils.Sequence`) object in order to avoid duplicate data when using multiprocessing. The output of the generator must be either
+
+ a tuple `(inputs, targets)` or
+ a tuple `(inputs, targets, sample_weights)`.
+ This tuple (a single output of the generator) makes a single batch. Therefore, all arrays in this tuple must have the same length (equal to the size of this batch). Different batches may have different sizes. For example, the last batch of the epoch is commonly smaller than the others, if the size of the dataset is not divisible by the batch size. The generator is expected to loop over its data indefinitely. An epoch finishes when `steps_per_epoch` batches have been seen by the model.
+- **steps_per_epoch**: Integer. Total number of steps (batches of samples) to yield from `generator` before declaring one epoch finished and starting the next epoch. It should typically be equal to `ceil(num_samples / batch_size)` Optional for `Sequence`: if unspecified, will use the `len(generator)` as a number of steps.
+- **epochs**: Integer. Number of epochs to train the model. An epoch is an iteration over the entire data provided, as defined by `steps_per_epoch`. Note that in conjunction with `initial_epoch`, `epochs` is to be understood as "final epoch". The model is not trained for a number of iterations given by `epochs`, but merely until the epoch of index `epochs` is reached.
+- **verbose**: Integer. 0, 1, or 2. Verbosity mode. 0 = silent, 1 = progress bar, 2 = one line per epoch.
+- **callbacks**: List of `tf.keras.callbacks.Callback` instances. List of callbacks to apply during training. See [callbacks](https://www.tensorflow.org/versions/r1.12/api_docs/python/tf/keras/callbacks).
+- **validation_data**: This can be either
+a generator or a `Sequence` object for the validation data
+tuple `(x_val, y_val)`
+tuple `(x_val, y_val, val_sample_weights)`
+on which to evaluate the loss and any model metrics at the end of each epoch. The model will not be trained on this data.
+- **validation_steps**: Only relevant if `validation_data` is a generator. Total number of steps (batches of samples) to yield from `validation_data` generator before stopping at the end of every epoch. It should typically be equal to the number of samples of your validation dataset divided by the batch size. Optional for `Sequence`: if unspecified, will use the `len(validation_data)` as a number of steps.
+- **validation_freq**: Only relevant if validation data is provided. Integer or `collections.Container` instance (e.g. list, tuple, etc.). If an integer, specifies how many training epochs to run before a new validation run is performed, e.g. `validation_freq=2` runs validation every 2 epochs. If a Container, specifies the epochs on which to run validation, e.g. `validation_freq=[1, 2, 10]` runs validation at the end of the 1st, 2nd, and 10th epochs.
+- **class_weight**: Optional dictionary mapping class indices (integers) to a weight (float) value, used for weighting the loss function (during training only). This can be useful to tell the model to "pay more attention" to samples from an under-represented class.
+- **max_queue_size**: Integer. Maximum size for the generator queue. If unspecified, `max_queue_size` will default to 10.
+- **workers**: Integer. Maximum number of processes to spin up when using process-based threading. If unspecified, `workers` will default to 1. If 0, will execute the generator on the main thread.
+- **use_multiprocessing**: Boolean. If `True`, use process-based threading. If unspecified, `use_multiprocessing` will default to `False`. Note that because this implementation relies on multiprocessing, you should not pass non-picklable arguments to the generator as they can't be passed easily to children processes.
+- **shuffle**: Boolean. Whether to shuffle the order of the batches at the beginning of each epoch. Only used with instances of `Sequence` (`tf.keras.utils.Sequence`). Has no effect when `steps_per_epoch` is not `None`.
+initial_epoch: Integer. Epoch at which to start training (useful for resuming a previous training run).
+
+**Returns**
+- A `History` object. Its `History.history` attribute is a record of training loss values and metrics values at successive epochs, as well as validation loss values and validation metrics values (if applicable).
+
+**Raises**
+- **ValueError**: In case the generator yields data in an invalid format.
+
+**Example**
+```python
+def generate_arrays_from_file(path):
+ while True:
+ with open(path) as f:
+ for line in f:
+ # create numpy arrays of input data
+ # and labels, from each line in the file
+ x1, x2, y = process_line(line)
+ yield ({'input_1': x1, 'input_2': x2}, {'output': y})
+
+model.fit_generator(generate_arrays_from_file('/my_file.txt'),
+ steps_per_epoch=10000, epochs=10)
+```
+## evaluate_generator
+```python
+evaluate_generator(generator, steps=None, callbacks=None, max_queue_size=10, workers=1, use_multiprocessing=False, verbose=0)
+```
+Evaluates the model on a data generator.
+The generator should return the same kind of data as accepted by `test_on_batch`.
+
+**Arguments**
+
+- **generator**: Generator yielding tuples (inputs, targets) or (inputs, targets, sample_weights) or an instance of Sequence (tf.keras.utils.Sequence) object in order to avoid duplicate data when using multiprocessing.
+- **steps**: Total number of steps (batches of samples) to yield from `generator` before stopping. Optional for `Sequence`: if unspecified, will use the `len(generator)` as a number of steps.
+- **callbacks**: List of `tf.keras.callbacks.Callback` instances. List of callbacks to apply during training. See [callbacks](https://www.tensorflow.org/versions/r1.12/api_docs/python/tf/keras/callbacks).
+- **max_queue_size**: maximum size for the generator queue
+- **workers**: Integer. Maximum number of processes to spin up when using process based threading. If unspecified, `workers` will default to 1. If 0, will execute the generator on the main thread.
+- **use_multiprocessing**: if True, use process based threading. Note that because this implementation relies on multiprocessing, you should not pass non picklable arguments to the generator as they can't be passed easily to children processes.
+- **verbose**: verbosity mode, 0 or 1.
+
+**Returns**
+
+- Scalar test loss (if the model has a single output and no metrics) or list of scalars (if the model has multiple outputs and/or metrics). The attribute `model.metrics_names` will give you the display labels for the scalar outputs.
+
+**Raises**
+
+- **ValueError**: In case the generator yields data in an invalid format.
+
+## predict_generator
+```python
+predict_generator(generator, steps=None, callbacks=None, max_queue_size=10, workers=1, use_multiprocessing=False, verbose=0)
+```
+Generates predictions for the input samples from a data generator.
+The generator should return the same kind of data as accepted by `predict_on_batch`.
+
+**Arguments**
+- **generator**: Generator yielding batches of input samples or an instance of Sequence (`tf.keras.utils.Sequence`) object in order to avoid duplicate data when using multiprocessing.
+- **steps**: Total number of steps (batches of samples) to yield from `generator` before stopping. Optional for `Sequence`: if unspecified, will use the `len(generator)` as a number of steps.
+- **callbacks**: List of `tf.keras.callbacks.Callback` instances. List of callbacks to apply during training. See [callbacks](https://www.tensorflow.org/versions/r1.12/api_docs/python/tf/keras/callbacks).
+- **max_queue_size**: Maximum size for the generator queue.
+- **workers**: Integer. Maximum number of processes to spin up when using process based threading. If unspecified, `workers` will default to 1. If 0, will execute the generator on the main thread.
+- **use_multiprocessing**: If `True`, use process based threading. Note that because this implementation relies on multiprocessing, you should not pass non picklable arguments to the generator as they can't be passed easily to children processes.
+- **verbose**: verbosity mode, 0 or 1.
+
+**Returns**
+
+- Numpy array(s) of predictions.
+
+**Raises**
+
+- **ValueError**: In case the generator yields data in an invalid format.
+
+## get_layer
+```python
+get_layer(name=None, index=None)
+```
+Retrieves a layer based on either its name (unique) or index.
+If `name` and `index` are both provided, `index` will take precedence.
+Indices are based on order of horizontal graph traversal (bottom-up).
+
+**Arguments**
+- **name**: String, name of layer.
+- **index**: Integer, index of layer.
+
+**Returns**
+- A layer instance.
+
+**Raises**
+- **ValueError**: In case of invalid layer name or index.
\ No newline at end of file
diff --git a/docs/source/Models.rst b/docs/source/Models.rst
new file mode 100644
index 0000000..dd416d5
--- /dev/null
+++ b/docs/source/Models.rst
@@ -0,0 +1,12 @@
+DeepCTR Models API
+======================
+
+.. toctree::
+ Model Methods
+ FM
+ DSSM
+ YoutubeDNN
+ NCF
+ MIND
+
+
\ No newline at end of file
diff --git a/docs/source/Quick-Start.md b/docs/source/Quick-Start.md
new file mode 100644
index 0000000..6ecb6e0
--- /dev/null
+++ b/docs/source/Quick-Start.md
@@ -0,0 +1,20 @@
+# Quick-Start
+
+## Installation Guide
+Now `deepmatch` is available for python `2.7 `and `3.5, 3.6, 3.7`.
+`deepmatch` depends on tensorflow, you can specify to install the cpu version or gpu version through `pip`.
+
+### CPU version
+
+```bash
+$ pip install deepmatch[cpu]
+```
+### GPU version
+
+```bash
+$ pip install deepmatch[gpu]
+```
+## Run examples !!
+
+- [DSSM with negative sampling](./Examples.html#dssm-with-negative-sampling)
+- [YoutubeDNN with sampled softmax](./Examples.html#youtubednn-with-sampled-softmax)
\ No newline at end of file
diff --git a/docs/source/conf.py b/docs/source/conf.py
new file mode 100644
index 0000000..5f39a88
--- /dev/null
+++ b/docs/source/conf.py
@@ -0,0 +1,169 @@
+# -*- coding: utf-8 -*-
+#
+# Configuration file for the Sphinx documentation builder.
+#
+# This file does only contain a selection of the most common options. For a
+# full list see the documentation:
+# http://www.sphinx-doc.org/en/master/config
+
+# -- Path setup --------------------------------------------------------------
+
+# If extensions (or modules to document with autodoc) are in another directory,
+# add these directories to sys.path here. If the directory is relative to the
+# documentation root, use os.path.abspath to make it absolute, like shown here.
+#
+import os
+import sys
+sys.path.insert(0, os.path.abspath('../../'))
+
+
+# -- Project information -----------------------------------------------------
+
+project = 'DeepMatch'
+copyright = '2020, Weichen Shen'
+author = 'Weichen Shen'
+
+# The short X.Y version
+version = ''
+# The full version, including alpha/beta/rc tags
+release = '0.0.0'
+
+
+# -- General configuration ---------------------------------------------------
+
+# If your documentation needs a minimal Sphinx version, state it here.
+#
+# needs_sphinx = '1.0'
+
+# Add any Sphinx extension module names here, as strings. They can be
+# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
+# ones.
+extensions = [
+ 'sphinx.ext.autodoc',
+ 'sphinx.ext.mathjax',
+ 'sphinx.ext.ifconfig',
+ 'sphinx.ext.viewcode',
+ 'sphinx.ext.githubpages',
+]
+
+# Add any paths that contain templates here, relative to this directory.
+templates_path = ['_templates']
+
+# The suffix(es) of source filenames.
+# You can specify multiple suffix as a list of string:
+#
+# source_suffix = ['.rst', '.md']
+source_suffix = '.rst'
+
+# The master toctree document.
+master_doc = 'index'
+
+# The language for content autogenerated by Sphinx. Refer to documentation
+# for a list of supported languages.
+#
+# This is also used if you do content translation via gettext catalogs.
+# Usually you set "language" from the command line for these cases.
+language = None
+
+# List of patterns, relative to source directory, that match files and
+# directories to ignore when looking for source files.
+# This pattern also affects html_static_path and html_extra_path .
+exclude_patterns = []
+
+# The name of the Pygments (syntax highlighting) style to use.
+pygments_style = 'sphinx'
+
+
+# -- Options for HTML output -------------------------------------------------
+
+# The theme to use for HTML and HTML Help pages. See the documentation for
+# a list of builtin themes.
+#
+html_theme = 'alabaster'
+
+# Theme options are theme-specific and customize the look and feel of a theme
+# further. For a list of options available for each theme, see the
+# documentation.
+#
+# html_theme_options = {}
+
+# Add any paths that contain custom static files (such as style sheets) here,
+# relative to this directory. They are copied after the builtin static files,
+# so a file named "default.css" will overwrite the builtin "default.css".
+html_static_path = ['_static']
+
+# Custom sidebar templates, must be a dictionary that maps document names
+# to template names.
+#
+# The default sidebars (for documents that don't match any pattern) are
+# defined by theme itself. Builtin themes are using these templates by
+# default: ``['localtoc.html', 'relations.html', 'sourcelink.html',
+# 'searchbox.html']``.
+#
+# html_sidebars = {}
+
+
+# -- Options for HTMLHelp output ---------------------------------------------
+
+# Output file base name for HTML help builder.
+htmlhelp_basename = 'DeepMatchdoc'
+
+
+# -- Options for LaTeX output ------------------------------------------------
+
+latex_elements = {
+ # The paper size ('letterpaper' or 'a4paper').
+ #
+ # 'papersize': 'letterpaper',
+
+ # The font size ('10pt', '11pt' or '12pt').
+ #
+ # 'pointsize': '10pt',
+
+ # Additional stuff for the LaTeX preamble.
+ #
+ # 'preamble': '',
+
+ # Latex figure (float) alignment
+ #
+ # 'figure_align': 'htbp',
+}
+
+# Grouping the document tree into LaTeX files. List of tuples
+# (source start file, target name, title,
+# author, documentclass [howto, manual, or own class]).
+latex_documents = [
+ (master_doc, 'DeepMatch.tex', 'DeepMatch Documentation',
+ 'Weichen Shen', 'manual'),
+]
+
+
+# -- Options for manual page output ------------------------------------------
+
+# One entry per manual page. List of tuples
+# (source start file, name, description, authors, manual section).
+man_pages = [
+ (master_doc, 'deepmatch', 'DeepMatch Documentation',
+ [author], 1)
+]
+
+
+# -- Options for Texinfo output ----------------------------------------------
+
+# Grouping the document tree into Texinfo files. List of tuples
+# (source start file, target name, title, author,
+# dir menu entry, description, category)
+texinfo_documents = [
+ (master_doc, 'DeepMatch', 'DeepMatch Documentation',
+ author, 'DeepMatch', 'One line description of project.',
+ 'Miscellaneous'),
+]
+
+
+# -- Extension configuration -------------------------------------------------
+todo_include_todos = False
+html_theme = 'sphinx_rtd_theme'
+
+source_parsers = {
+ '.md': 'recommonmark.parser.CommonMarkParser',
+}
\ No newline at end of file
diff --git a/docs/source/deepmatch.inputs.rst b/docs/source/deepmatch.inputs.rst
new file mode 100644
index 0000000..1caf663
--- /dev/null
+++ b/docs/source/deepmatch.inputs.rst
@@ -0,0 +1,7 @@
+deepmatch.inputs module
+=======================
+
+.. automodule:: deepmatch.inputs
+ :members:
+ :undoc-members:
+ :show-inheritance:
diff --git a/docs/source/deepmatch.layers.core.rst b/docs/source/deepmatch.layers.core.rst
new file mode 100644
index 0000000..3669173
--- /dev/null
+++ b/docs/source/deepmatch.layers.core.rst
@@ -0,0 +1,7 @@
+deepmatch.layers.core module
+============================
+
+.. automodule:: deepmatch.layers.core
+ :members:
+ :undoc-members:
+ :show-inheritance:
diff --git a/docs/source/deepmatch.layers.rst b/docs/source/deepmatch.layers.rst
new file mode 100644
index 0000000..036b5bd
--- /dev/null
+++ b/docs/source/deepmatch.layers.rst
@@ -0,0 +1,17 @@
+deepmatch.layers package
+========================
+
+Submodules
+----------
+
+.. toctree::
+
+ deepmatch.layers.core
+
+Module contents
+---------------
+
+.. automodule:: deepmatch.layers
+ :members:
+ :undoc-members:
+ :show-inheritance:
diff --git a/docs/source/deepmatch.models.dssm.rst b/docs/source/deepmatch.models.dssm.rst
new file mode 100644
index 0000000..e51fc69
--- /dev/null
+++ b/docs/source/deepmatch.models.dssm.rst
@@ -0,0 +1,7 @@
+deepmatch.models.dssm module
+============================
+
+.. automodule:: deepmatch.models.dssm
+ :members:
+ :no-undoc-members:
+ :no-show-inheritance:
diff --git a/docs/source/deepmatch.models.fm.rst b/docs/source/deepmatch.models.fm.rst
new file mode 100644
index 0000000..b0acc9b
--- /dev/null
+++ b/docs/source/deepmatch.models.fm.rst
@@ -0,0 +1,7 @@
+deepmatch.models.fm module
+==========================
+
+.. automodule:: deepmatch.models.fm
+ :members:
+ :no-undoc-members:
+ :no-show-inheritance:
diff --git a/docs/source/deepmatch.models.mind.rst b/docs/source/deepmatch.models.mind.rst
new file mode 100644
index 0000000..f1a5512
--- /dev/null
+++ b/docs/source/deepmatch.models.mind.rst
@@ -0,0 +1,7 @@
+deepmatch.models.mind module
+============================
+
+.. automodule:: deepmatch.models.mind
+ :members:
+ :no-undoc-members:
+ :no-show-inheritance:
diff --git a/docs/source/deepmatch.models.ncf.rst b/docs/source/deepmatch.models.ncf.rst
new file mode 100644
index 0000000..e089438
--- /dev/null
+++ b/docs/source/deepmatch.models.ncf.rst
@@ -0,0 +1,7 @@
+deepmatch.models.ncf module
+===========================
+
+.. automodule:: deepmatch.models.ncf
+ :members:
+ :no-undoc-members:
+ :no-show-inheritance:
diff --git a/docs/source/deepmatch.models.rst b/docs/source/deepmatch.models.rst
new file mode 100644
index 0000000..0ff1641
--- /dev/null
+++ b/docs/source/deepmatch.models.rst
@@ -0,0 +1,21 @@
+deepmatch.models package
+========================
+
+Submodules
+----------
+
+.. toctree::
+
+ deepmatch.models.dssm
+ deepmatch.models.fm
+ deepmatch.models.mind
+ deepmatch.models.ncf
+ deepmatch.models.youtubednn
+
+Module contents
+---------------
+
+.. automodule:: deepmatch.models
+ :members:
+ :undoc-members:
+ :show-inheritance:
diff --git a/docs/source/deepmatch.models.youtubednn.rst b/docs/source/deepmatch.models.youtubednn.rst
new file mode 100644
index 0000000..402a564
--- /dev/null
+++ b/docs/source/deepmatch.models.youtubednn.rst
@@ -0,0 +1,7 @@
+deepmatch.models.youtubednn module
+==================================
+
+.. automodule:: deepmatch.models.youtubednn
+ :members:
+ :no-undoc-members:
+ :no-show-inheritance:
diff --git a/docs/source/deepmatch.rst b/docs/source/deepmatch.rst
new file mode 100644
index 0000000..88f3003
--- /dev/null
+++ b/docs/source/deepmatch.rst
@@ -0,0 +1,26 @@
+deepmatch package
+=================
+
+Subpackages
+-----------
+
+.. toctree::
+
+ deepmatch.layers
+ deepmatch.models
+
+Submodules
+----------
+
+.. toctree::
+
+ deepmatch.inputs
+ deepmatch.utils
+
+Module contents
+---------------
+
+.. automodule:: deepmatch
+ :members:
+ :undoc-members:
+ :show-inheritance:
diff --git a/docs/source/deepmatch.utils.rst b/docs/source/deepmatch.utils.rst
new file mode 100644
index 0000000..fa52ef1
--- /dev/null
+++ b/docs/source/deepmatch.utils.rst
@@ -0,0 +1,7 @@
+deepmatch.utils module
+======================
+
+.. automodule:: deepmatch.utils
+ :members:
+ :undoc-members:
+ :show-inheritance:
diff --git a/docs/source/index.rst b/docs/source/index.rst
new file mode 100644
index 0000000..31d42b9
--- /dev/null
+++ b/docs/source/index.rst
@@ -0,0 +1,52 @@
+.. DeepMatch documentation master file, created by
+ sphinx-quickstart on Sun Apr 5 20:44:18 2020.
+ You can adapt this file completely to your liking, but it should at least
+ contain the root `toctree` directive.
+
+Welcome to DeepMatch's documentation!
+=====================================
+
+
+
+DeepMatch is a **Easy-to-use** deep matching model library for recommendations, advertising, and search. It's easy to train models and to **export representation vectors** for user and item which can be used for **ANN search**.You can use any complex model with ``model.fit()`` and ``model.predict()`` .
+
+
+Let's `Get Started! <./Quick-Start.html>`_
+
+You can read the latest code at https://github.com/shenweichen/DeepMatch
+
+News
+-----
+
+04/06/2020 : DeepMatch first version .
+
+DisscussionGroup
+-----------------------
+
+公众号:**浅梦的学习笔记** wechat ID: **deepctrbot**
+
+.. image:: ../pics/weichennote.png
+
+.. toctree::
+ :maxdepth: 2
+ :caption: Home:
+
+ Quick-Start
+ Features
+ Examples
+ FAQ
+ History
+
+.. toctree::
+ :maxdepth: 3
+ :caption: API:
+
+ Models
+
+
+Indices and tables
+==================
+
+* :ref:`genindex`
+* :ref:`modindex`
+* :ref:`search`
diff --git a/docs/source/modules.rst b/docs/source/modules.rst
new file mode 100644
index 0000000..e1fba31
--- /dev/null
+++ b/docs/source/modules.rst
@@ -0,0 +1,7 @@
+deepmatch
+=========
+
+.. toctree::
+ :maxdepth: 4
+
+ deepmatch
diff --git a/examples/movielens_sample.txt b/examples/movielens_sample.txt
new file mode 100644
index 0000000..2d39c27
--- /dev/null
+++ b/examples/movielens_sample.txt
@@ -0,0 +1,234 @@
+user_id,movie_id,rating,timestamp,title,genres,gender,age,occupation,zip
+1,1193,5,978300760,One Flew Over the Cuckoo's Nest (1975),Drama,F,1,10,48067
+1,661,3,978302109,James and the Giant Peach (1996),Animation|Children's|Musical,F,1,10,48067
+1,914,3,978301968,My Fair Lady (1964),Musical|Romance,F,1,10,48067
+1,3408,4,978300275,Erin Brockovich (2000),Drama,F,1,10,48067
+1,2355,5,978824291,"Bug's Life, A (1998)",Animation|Children's|Comedy,F,1,10,48067
+1,1197,3,978302268,"Princess Bride, The (1987)",Action|Adventure|Comedy|Romance,F,1,10,48067
+1,1287,5,978302039,Ben-Hur (1959),Action|Adventure|Drama,F,1,10,48067
+1,2804,5,978300719,"Christmas Story, A (1983)",Comedy|Drama,F,1,10,48067
+1,594,4,978302268,Snow White and the Seven Dwarfs (1937),Animation|Children's|Musical,F,1,10,48067
+1,919,4,978301368,"Wizard of Oz, The (1939)",Adventure|Children's|Drama|Musical,F,1,10,48067
+1,595,5,978824268,Beauty and the Beast (1991),Animation|Children's|Musical,F,1,10,48067
+1,938,4,978301752,Gigi (1958),Musical,F,1,10,48067
+1,2398,4,978302281,Miracle on 34th Street (1947),Drama,F,1,10,48067
+1,2918,4,978302124,Ferris Bueller's Day Off (1986),Comedy,F,1,10,48067
+1,1035,5,978301753,"Sound of Music, The (1965)",Musical,F,1,10,48067
+1,2791,4,978302188,Airplane! (1980),Comedy,F,1,10,48067
+1,2687,3,978824268,Tarzan (1999),Animation|Children's,F,1,10,48067
+1,2018,4,978301777,Bambi (1942),Animation|Children's,F,1,10,48067
+1,3105,5,978301713,Awakenings (1990),Drama,F,1,10,48067
+1,2797,4,978302039,Big (1988),Comedy|Fantasy,F,1,10,48067
+1,2321,3,978302205,Pleasantville (1998),Comedy,F,1,10,48067
+1,720,3,978300760,Wallace & Gromit: The Best of Aardman Animation (1996),Animation,F,1,10,48067
+1,1270,5,978300055,Back to the Future (1985),Comedy|Sci-Fi,F,1,10,48067
+1,527,5,978824195,Schindler's List (1993),Drama|War,F,1,10,48067
+1,2340,3,978300103,Meet Joe Black (1998),Romance,F,1,10,48067
+1,48,5,978824351,Pocahontas (1995),Animation|Children's|Musical|Romance,F,1,10,48067
+1,1097,4,978301953,E.T. the Extra-Terrestrial (1982),Children's|Drama|Fantasy|Sci-Fi,F,1,10,48067
+1,1721,4,978300055,Titanic (1997),Drama|Romance,F,1,10,48067
+1,1545,4,978824139,Ponette (1996),Drama,F,1,10,48067
+1,745,3,978824268,"Close Shave, A (1995)",Animation|Comedy|Thriller,F,1,10,48067
+1,2294,4,978824291,Antz (1998),Animation|Children's,F,1,10,48067
+1,3186,4,978300019,"Girl, Interrupted (1999)",Drama,F,1,10,48067
+1,1566,4,978824330,Hercules (1997),Adventure|Animation|Children's|Comedy|Musical,F,1,10,48067
+1,588,4,978824268,Aladdin (1992),Animation|Children's|Comedy|Musical,F,1,10,48067
+1,1907,4,978824330,Mulan (1998),Animation|Children's,F,1,10,48067
+1,783,4,978824291,"Hunchback of Notre Dame, The (1996)",Animation|Children's|Musical,F,1,10,48067
+1,1836,5,978300172,"Last Days of Disco, The (1998)",Drama,F,1,10,48067
+1,1022,5,978300055,Cinderella (1950),Animation|Children's|Musical,F,1,10,48067
+1,2762,4,978302091,"Sixth Sense, The (1999)",Thriller,F,1,10,48067
+1,150,5,978301777,Apollo 13 (1995),Drama,F,1,10,48067
+1,1,5,978824268,Toy Story (1995),Animation|Children's|Comedy,F,1,10,48067
+1,1961,5,978301590,Rain Man (1988),Drama,F,1,10,48067
+1,1962,4,978301753,Driving Miss Daisy (1989),Drama,F,1,10,48067
+1,2692,4,978301570,Run Lola Run (Lola rennt) (1998),Action|Crime|Romance,F,1,10,48067
+1,260,4,978300760,Star Wars: Episode IV - A New Hope (1977),Action|Adventure|Fantasy|Sci-Fi,F,1,10,48067
+1,1028,5,978301777,Mary Poppins (1964),Children's|Comedy|Musical,F,1,10,48067
+1,1029,5,978302205,Dumbo (1941),Animation|Children's|Musical,F,1,10,48067
+1,1207,4,978300719,To Kill a Mockingbird (1962),Drama,F,1,10,48067
+1,2028,5,978301619,Saving Private Ryan (1998),Action|Drama|War,F,1,10,48067
+1,531,4,978302149,"Secret Garden, The (1993)",Children's|Drama,F,1,10,48067
+1,3114,4,978302174,Toy Story 2 (1999),Animation|Children's|Comedy,F,1,10,48067
+1,608,4,978301398,Fargo (1996),Crime|Drama|Thriller,F,1,10,48067
+1,1246,4,978302091,Dead Poets Society (1989),Drama,F,1,10,48067
+2,1193,5,978298413,One Flew Over the Cuckoo's Nest (1975),Drama,M,56,16,70072
+2,3105,4,978298673,Awakenings (1990),Drama,M,56,16,70072
+2,2321,3,978299666,Pleasantville (1998),Comedy,M,56,16,70072
+2,1962,5,978298813,Driving Miss Daisy (1989),Drama,M,56,16,70072
+2,1207,4,978298478,To Kill a Mockingbird (1962),Drama,M,56,16,70072
+2,2028,4,978299773,Saving Private Ryan (1998),Action|Drama|War,M,56,16,70072
+2,1246,5,978299418,Dead Poets Society (1989),Drama,M,56,16,70072
+2,1357,5,978298709,Shine (1996),Drama|Romance,M,56,16,70072
+2,3068,4,978299000,"Verdict, The (1982)",Drama,M,56,16,70072
+2,1537,4,978299620,Shall We Dance? (Shall We Dansu?) (1996),Comedy,M,56,16,70072
+2,647,3,978299351,Courage Under Fire (1996),Drama|War,M,56,16,70072
+2,2194,4,978299297,"Untouchables, The (1987)",Action|Crime|Drama,M,56,16,70072
+2,648,4,978299913,Mission: Impossible (1996),Action|Adventure|Mystery,M,56,16,70072
+2,2268,5,978299297,"Few Good Men, A (1992)",Crime|Drama,M,56,16,70072
+2,2628,3,978300051,Star Wars: Episode I - The Phantom Menace (1999),Action|Adventure|Fantasy|Sci-Fi,M,56,16,70072
+2,1103,3,978298905,Rebel Without a Cause (1955),Drama,M,56,16,70072
+2,2916,3,978299809,Total Recall (1990),Action|Adventure|Sci-Fi|Thriller,M,56,16,70072
+2,3468,5,978298542,"Hustler, The (1961)",Drama,M,56,16,70072
+2,1210,4,978298151,Star Wars: Episode VI - Return of the Jedi (1983),Action|Adventure|Romance|Sci-Fi|War,M,56,16,70072
+2,1792,3,978299941,U.S. Marshalls (1998),Action|Thriller,M,56,16,70072
+2,1687,3,978300174,"Jackal, The (1997)",Action|Thriller,M,56,16,70072
+2,1213,2,978298458,GoodFellas (1990),Crime|Drama,M,56,16,70072
+2,3578,5,978298958,Gladiator (2000),Action|Drama,M,56,16,70072
+2,2881,3,978300002,Double Jeopardy (1999),Action|Thriller,M,56,16,70072
+2,3030,4,978298434,Yojimbo (1961),Comedy|Drama|Western,M,56,16,70072
+2,1217,3,978298151,Ran (1985),Drama|War,M,56,16,70072
+2,434,2,978300174,Cliffhanger (1993),Action|Adventure|Crime,M,56,16,70072
+2,2126,3,978300123,Snake Eyes (1998),Action|Crime|Mystery|Thriller,M,56,16,70072
+2,3107,2,978300002,Backdraft (1991),Action|Drama,M,56,16,70072
+2,3108,3,978299712,"Fisher King, The (1991)",Comedy|Drama|Romance,M,56,16,70072
+2,3035,4,978298625,Mister Roberts (1955),Comedy|Drama|War,M,56,16,70072
+2,1253,3,978299120,"Day the Earth Stood Still, The (1951)",Drama|Sci-Fi,M,56,16,70072
+2,1610,5,978299809,"Hunt for Red October, The (1990)",Action|Thriller,M,56,16,70072
+2,292,3,978300123,Outbreak (1995),Action|Drama|Thriller,M,56,16,70072
+2,2236,5,978299220,Simon Birch (1998),Drama,M,56,16,70072
+2,3071,4,978299120,Stand and Deliver (1987),Drama,M,56,16,70072
+2,902,2,978298905,Breakfast at Tiffany's (1961),Drama|Romance,M,56,16,70072
+2,368,4,978300002,Maverick (1994),Action|Comedy|Western,M,56,16,70072
+2,1259,5,978298841,Stand by Me (1986),Adventure|Comedy|Drama,M,56,16,70072
+2,3147,5,978298652,"Green Mile, The (1999)",Drama|Thriller,M,56,16,70072
+2,1544,4,978300174,"Lost World: Jurassic Park, The (1997)",Action|Adventure|Sci-Fi|Thriller,M,56,16,70072
+2,1293,5,978298261,Gandhi (1982),Drama,M,56,16,70072
+2,1188,4,978299620,Strictly Ballroom (1992),Comedy|Romance,M,56,16,70072
+2,3255,4,978299321,"League of Their Own, A (1992)",Comedy|Drama,M,56,16,70072
+2,3256,2,978299839,Patriot Games (1992),Action|Thriller,M,56,16,70072
+2,3257,3,978300073,"Bodyguard, The (1992)",Action|Drama|Romance|Thriller,M,56,16,70072
+2,110,5,978298625,Braveheart (1995),Action|Drama|War,M,56,16,70072
+2,2278,3,978299889,Ronin (1998),Action|Crime|Thriller,M,56,16,70072
+2,2490,3,978299966,Payback (1999),Action|Thriller,M,56,16,70072
+2,1834,4,978298813,"Spanish Prisoner, The (1997)",Drama|Thriller,M,56,16,70072
+2,3471,5,978298814,Close Encounters of the Third Kind (1977),Drama|Sci-Fi,M,56,16,70072
+2,589,4,978299773,Terminator 2: Judgment Day (1991),Action|Sci-Fi|Thriller,M,56,16,70072
+2,1690,3,978300051,Alien: Resurrection (1997),Action|Horror|Sci-Fi,M,56,16,70072
+2,3654,3,978298814,"Guns of Navarone, The (1961)",Action|Drama|War,M,56,16,70072
+2,2852,3,978298958,"Soldier's Story, A (1984)",Drama,M,56,16,70072
+2,1945,5,978298458,On the Waterfront (1954),Crime|Drama,M,56,16,70072
+2,982,4,978299269,Picnic (1955),Drama,M,56,16,70072
+2,1873,4,978298542,"Mis�rables, Les (1998)",Drama,M,56,16,70072
+2,2858,4,978298434,American Beauty (1999),Comedy|Drama,M,56,16,70072
+2,1225,5,978298391,Amadeus (1984),Drama,M,56,16,70072
+2,515,5,978298542,"Remains of the Day, The (1993)",Drama,M,56,16,70072
+2,442,3,978300025,Demolition Man (1993),Action|Sci-Fi,M,56,16,70072
+2,2312,3,978299046,Children of a Lesser God (1986),Drama,M,56,16,70072
+2,265,4,978299026,Like Water for Chocolate (Como agua para chocolate) (1992),Drama|Romance,M,56,16,70072
+2,1408,3,978299839,"Last of the Mohicans, The (1992)",Action|Romance|War,M,56,16,70072
+2,1084,3,978298813,Bonnie and Clyde (1967),Crime|Drama,M,56,16,70072
+2,3699,2,978299173,Starman (1984),Adventure|Drama|Romance|Sci-Fi,M,56,16,70072
+2,480,5,978299809,Jurassic Park (1993),Action|Adventure|Sci-Fi,M,56,16,70072
+2,1442,4,978299297,Prefontaine (1997),Drama,M,56,16,70072
+2,2067,5,978298625,Doctor Zhivago (1965),Drama|Romance|War,M,56,16,70072
+2,1265,3,978299712,Groundhog Day (1993),Comedy|Romance,M,56,16,70072
+2,1370,5,978299889,Die Hard 2 (1990),Action|Thriller,M,56,16,70072
+2,1801,3,978300002,"Man in the Iron Mask, The (1998)",Action|Drama|Romance,M,56,16,70072
+2,1372,3,978299941,Star Trek VI: The Undiscovered Country (1991),Action|Adventure|Sci-Fi,M,56,16,70072
+2,2353,4,978299861,Enemy of the State (1998),Action|Thriller,M,56,16,70072
+2,3334,4,978298958,Key Largo (1948),Crime|Drama|Film-Noir|Thriller,M,56,16,70072
+2,2427,2,978299913,"Thin Red Line, The (1998)",Action|Drama|War,M,56,16,70072
+2,590,5,978299083,Dances with Wolves (1990),Adventure|Drama|Western,M,56,16,70072
+2,1196,5,978298730,Star Wars: Episode V - The Empire Strikes Back (1980),Action|Adventure|Drama|Sci-Fi|War,M,56,16,70072
+2,1552,3,978299941,Con Air (1997),Action|Adventure|Thriller,M,56,16,70072
+2,736,4,978300100,Twister (1996),Action|Adventure|Romance|Thriller,M,56,16,70072
+2,1198,4,978298124,Raiders of the Lost Ark (1981),Action|Adventure,M,56,16,70072
+2,593,5,978298517,"Silence of the Lambs, The (1991)",Drama|Thriller,M,56,16,70072
+2,2359,3,978299666,Waking Ned Devine (1998),Comedy,M,56,16,70072
+2,95,2,978300143,Broken Arrow (1996),Action|Thriller,M,56,16,70072
+2,2717,3,978298196,Ghostbusters II (1989),Comedy|Horror,M,56,16,70072
+2,2571,4,978299773,"Matrix, The (1999)",Action|Sci-Fi|Thriller,M,56,16,70072
+2,1917,3,978300174,Armageddon (1998),Action|Adventure|Sci-Fi|Thriller,M,56,16,70072
+2,2396,4,978299641,Shakespeare in Love (1998),Comedy|Romance,M,56,16,70072
+2,3735,3,978298814,Serpico (1973),Crime|Drama,M,56,16,70072
+2,1953,4,978298775,"French Connection, The (1971)",Action|Crime|Drama|Thriller,M,56,16,70072
+2,1597,3,978300025,Conspiracy Theory (1997),Action|Mystery|Romance|Thriller,M,56,16,70072
+2,3809,3,978299712,What About Bob? (1991),Comedy,M,56,16,70072
+2,1954,5,978298841,Rocky (1976),Action|Drama,M,56,16,70072
+2,1955,4,978299200,Kramer Vs. Kramer (1979),Drama,M,56,16,70072
+2,235,3,978299351,Ed Wood (1994),Comedy|Drama,M,56,16,70072
+2,1124,5,978299418,On Golden Pond (1981),Drama,M,56,16,70072
+2,1957,5,978298750,Chariots of Fire (1981),Drama,M,56,16,70072
+2,163,4,978299809,Desperado (1995),Action|Romance|Thriller,M,56,16,70072
+2,21,1,978299839,Get Shorty (1995),Action|Comedy|Drama,M,56,16,70072
+2,165,3,978300002,Die Hard: With a Vengeance (1995),Action|Thriller,M,56,16,70072
+2,1090,2,978298580,Platoon (1986),Drama|War,M,56,16,70072
+2,380,5,978299809,True Lies (1994),Action|Adventure|Comedy|Romance,M,56,16,70072
+2,2501,5,978298600,October Sky (1999),Drama,M,56,16,70072
+2,349,4,978299839,Clear and Present Danger (1994),Action|Adventure|Thriller,M,56,16,70072
+2,457,4,978299773,"Fugitive, The (1993)",Action|Thriller,M,56,16,70072
+2,1096,4,978299386,Sophie's Choice (1982),Drama,M,56,16,70072
+2,920,5,978298775,Gone with the Wind (1939),Drama|Romance|War,M,56,16,70072
+2,459,3,978300002,"Getaway, The (1994)",Action,M,56,16,70072
+2,1527,4,978299839,"Fifth Element, The (1997)",Action|Sci-Fi,M,56,16,70072
+2,3418,4,978299809,Thelma & Louise (1991),Action|Drama,M,56,16,70072
+2,1385,3,978299966,Under Siege (1992),Action,M,56,16,70072
+2,3451,4,978298924,Guess Who's Coming to Dinner (1967),Comedy|Drama,M,56,16,70072
+2,3095,4,978298517,"Grapes of Wrath, The (1940)",Drama,M,56,16,70072
+2,780,3,978299966,Independence Day (ID4) (1996),Action|Sci-Fi|War,M,56,16,70072
+2,498,3,978299418,Mr. Jones (1993),Drama|Romance,M,56,16,70072
+2,2728,3,978298881,Spartacus (1960),Drama,M,56,16,70072
+2,2002,5,978300100,Lethal Weapon 3 (1992),Action|Comedy|Crime|Drama,M,56,16,70072
+2,1784,5,978298841,As Good As It Gets (1997),Comedy|Drama,M,56,16,70072
+2,2943,4,978298372,Indochine (1992),Drama|Romance,M,56,16,70072
+2,2006,3,978299861,"Mask of Zorro, The (1998)",Action|Adventure|Romance,M,56,16,70072
+2,318,5,978298413,"Shawshank Redemption, The (1994)",Drama,M,56,16,70072
+2,1968,2,978298881,"Breakfast Club, The (1985)",Comedy|Drama,M,56,16,70072
+2,3678,3,978299250,"Man with the Golden Arm, The (1955)",Drama,M,56,16,70072
+2,1244,3,978299143,Manhattan (1979),Comedy|Drama|Romance,M,56,16,70072
+2,356,5,978299686,Forrest Gump (1994),Comedy|Romance|War,M,56,16,70072
+2,1245,2,978299200,Miller's Crossing (1990),Drama,M,56,16,70072
+2,3893,1,978299535,Nurse Betty (2000),Comedy|Thriller,M,56,16,70072
+2,1247,5,978298652,"Graduate, The (1967)",Drama|Romance,M,56,16,70072
+3,2355,5,978298430,"Bug's Life, A (1998)",Animation|Children's|Comedy,M,25,15,55117
+3,1197,5,978297570,"Princess Bride, The (1987)",Action|Adventure|Comedy|Romance,M,25,15,55117
+3,1270,3,978298231,Back to the Future (1985),Comedy|Sci-Fi,M,25,15,55117
+3,1961,4,978297095,Rain Man (1988),Drama,M,25,15,55117
+3,260,5,978297512,Star Wars: Episode IV - A New Hope (1977),Action|Adventure|Fantasy|Sci-Fi,M,25,15,55117
+3,3114,3,978298103,Toy Story 2 (1999),Animation|Children's|Comedy,M,25,15,55117
+3,648,3,978297867,Mission: Impossible (1996),Action|Adventure|Mystery,M,25,15,55117
+3,1210,4,978297600,Star Wars: Episode VI - Return of the Jedi (1983),Action|Adventure|Romance|Sci-Fi|War,M,25,15,55117
+3,1259,5,978298296,Stand by Me (1986),Adventure|Comedy|Drama,M,25,15,55117
+3,2858,4,978297039,American Beauty (1999),Comedy|Drama,M,25,15,55117
+3,480,4,978297690,Jurassic Park (1993),Action|Adventure|Sci-Fi,M,25,15,55117
+3,1265,2,978298316,Groundhog Day (1993),Comedy|Romance,M,25,15,55117
+3,590,4,978297439,Dances with Wolves (1990),Adventure|Drama|Western,M,25,15,55117
+3,1196,4,978297539,Star Wars: Episode V - The Empire Strikes Back (1980),Action|Adventure|Drama|Sci-Fi|War,M,25,15,55117
+3,1198,5,978297570,Raiders of the Lost Ark (1981),Action|Adventure,M,25,15,55117
+3,593,3,978297018,"Silence of the Lambs, The (1991)",Drama|Thriller,M,25,15,55117
+3,2006,4,978297757,"Mask of Zorro, The (1998)",Action|Adventure|Romance,M,25,15,55117
+3,1968,4,978297068,"Breakfast Club, The (1985)",Comedy|Drama,M,25,15,55117
+3,3421,4,978298147,Animal House (1978),Comedy,M,25,15,55117
+3,1641,2,978298430,"Full Monty, The (1997)",Comedy,M,25,15,55117
+3,1394,4,978298147,Raising Arizona (1987),Comedy,M,25,15,55117
+3,3534,3,978297068,28 Days (2000),Comedy,M,25,15,55117
+3,104,4,978298486,Happy Gilmore (1996),Comedy,M,25,15,55117
+3,2735,4,978297867,"Golden Child, The (1986)",Action|Adventure|Comedy,M,25,15,55117
+3,1431,3,978297095,Beverly Hills Ninja (1997),Action|Comedy,M,25,15,55117
+3,3868,3,978298486,"Naked Gun: From the Files of Police Squad!, The (1988)",Comedy,M,25,15,55117
+3,1079,5,978298296,"Fish Called Wanda, A (1988)",Comedy,M,25,15,55117
+3,2997,3,978298147,Being John Malkovich (1999),Comedy,M,25,15,55117
+3,1615,5,978297710,"Edge, The (1997)",Adventure|Thriller,M,25,15,55117
+3,1291,4,978297600,Indiana Jones and the Last Crusade (1989),Action|Adventure,M,25,15,55117
+3,653,4,978297757,Dragonheart (1996),Action|Adventure|Fantasy,M,25,15,55117
+3,2167,5,978297600,Blade (1998),Action|Adventure|Horror,M,25,15,55117
+3,1580,3,978297663,Men in Black (1997),Action|Adventure|Comedy|Sci-Fi,M,25,15,55117
+3,3619,2,978298201,"Hollywood Knights, The (1980)",Comedy,M,25,15,55117
+3,1049,4,978297805,"Ghost and the Darkness, The (1996)",Action|Adventure,M,25,15,55117
+3,1261,1,978297663,Evil Dead II (Dead By Dawn) (1987),Action|Adventure|Comedy|Horror,M,25,15,55117
+3,552,4,978297837,"Three Musketeers, The (1993)",Action|Adventure|Comedy,M,25,15,55117
+3,1266,5,978297396,Unforgiven (1992),Western,M,25,15,55117
+3,733,5,978297757,"Rock, The (1996)",Action|Adventure|Thriller,M,25,15,55117
+3,1378,5,978297419,Young Guns (1988),Action|Comedy|Western,M,25,15,55117
+3,1379,4,978297419,Young Guns II (1990),Action|Comedy|Western,M,25,15,55117
+3,3552,5,978298459,Caddyshack (1980),Comedy,M,25,15,55117
+3,1304,5,978298166,Butch Cassidy and the Sundance Kid (1969),Action|Comedy|Western,M,25,15,55117
+3,2470,4,978297777,Crocodile Dundee (1986),Adventure|Comedy,M,25,15,55117
+3,3168,4,978297570,Easy Rider (1969),Adventure|Drama,M,25,15,55117
+3,2617,2,978297837,"Mummy, The (1999)",Action|Adventure|Horror|Thriller,M,25,15,55117
+3,3671,5,978297419,Blazing Saddles (1974),Comedy|Western,M,25,15,55117
+3,2871,4,978297539,Deliverance (1972),Adventure|Thriller,M,25,15,55117
+3,2115,4,978297777,Indiana Jones and the Temple of Doom (1984),Action|Adventure,M,25,15,55117
+3,1136,5,978298079,Monty Python and the Holy Grail (1974),Comedy,M,25,15,55117
+3,2081,4,978298504,"Little Mermaid, The (1989)",Animation|Children's|Comedy|Musical|Romance,M,25,15,55117
diff --git a/examples/preprocess.py b/examples/preprocess.py
new file mode 100644
index 0000000..91caae3
--- /dev/null
+++ b/examples/preprocess.py
@@ -0,0 +1,51 @@
+import random
+import numpy as np
+from tqdm import tqdm
+from tensorflow.python.keras.preprocessing.sequence import pad_sequences
+
+def gen_data_set(data, negsample=0):
+
+ data.sort_values("timestamp", inplace=True)
+ item_ids = data['movie_id'].unique()
+
+ train_set = []
+ test_set = []
+ for reviewerID, hist in tqdm(data.groupby('user_id')):
+ pos_list = hist['movie_id'].tolist()
+ rating_list = hist['rating'].tolist()
+
+ if negsample > 0:
+ candidate_set = list(set(item_ids) - set(pos_list))
+ neg_list = np.random.choice(candidate_set,size=len(pos_list)*negsample,replace=True)
+ for i in range(1, len(pos_list)):
+ hist = pos_list[:i]
+ if i != len(pos_list) - 1:
+ train_set.append((reviewerID, hist[::-1], pos_list[i], 1,len(hist[::-1]),rating_list[i]))
+ for negi in range(negsample):
+ train_set.append((reviewerID, hist[::-1], neg_list[i*negsample+negi], 0,len(hist[::-1])))
+ else:
+ test_set.append((reviewerID, hist[::-1], pos_list[i],1,len(hist[::-1]),rating_list[i]))
+
+ random.shuffle(train_set)
+ random.shuffle(test_set)
+
+ print(len(train_set[0]),len(test_set[0]))
+
+ return train_set,test_set
+
+def gen_model_input(train_set,user_profile,seq_max_len):
+
+ train_uid = np.array([line[0] for line in train_set])
+ train_seq = [line[1] for line in train_set]
+ train_iid = np.array([line[2] for line in train_set])
+ train_label = np.array([line[3] for line in train_set])
+ train_hist_len = np.array([line[4] for line in train_set])
+
+ train_seq_pad = pad_sequences(train_seq, maxlen=seq_max_len, padding='post', value=0)
+ train_model_input = {"user_id": train_uid, "movie_id": train_iid, "hist_movie_id": train_seq_pad,
+ "hist_len": train_hist_len}
+
+ for key in ["gender", "age", "occupation", "zip"]:
+ train_model_input[key] = user_profile.loc[train_model_input['user_id']][key].values
+
+ return train_model_input,train_label
diff --git a/examples/run_dssm_negsampling.py b/examples/run_dssm_negsampling.py
new file mode 100644
index 0000000..65568d3
--- /dev/null
+++ b/examples/run_dssm_negsampling.py
@@ -0,0 +1,103 @@
+import pandas as pd
+from deepctr.inputs import SparseFeat, VarLenSparseFeat
+from preprocess import gen_data_set, gen_model_input
+from sklearn.preprocessing import LabelEncoder
+from tensorflow.python.keras.models import Model
+
+from deepmatch.models import *
+
+if __name__ == "__main__":
+
+ data = pd.read_csvdata = pd.read_csv("./movielens_sample.txt")
+ sparse_features = ["movie_id", "user_id",
+ "gender", "age", "occupation", "zip", ]
+ SEQ_LEN = 50
+ negsample = 3
+
+ # 1.Label Encoding for sparse features,and process sequence features with `gen_date_set` and `gen_model_input`
+
+ features = ['user_id', 'movie_id', 'gender', 'age', 'occupation', 'zip']
+ feature_max_idx = {}
+ for feature in features:
+ lbe = LabelEncoder()
+ data[feature] = lbe.fit_transform(data[feature]) + 1
+ feature_max_idx[feature] = data[feature].max() + 1
+
+ user_profile = data[["user_id", "gender", "age", "occupation", "zip"]].drop_duplicates('user_id')
+
+ item_profile = data[["movie_id"]].drop_duplicates('movie_id')
+
+ user_profile.set_index("user_id", inplace=True)
+
+ user_item_list = data.groupby("user_id")['movie_id'].apply(list)
+
+ train_set, test_set = gen_data_set(data, negsample)
+
+ train_model_input, train_label = gen_model_input(train_set, user_profile, SEQ_LEN)
+ test_model_input, test_label = gen_model_input(test_set, user_profile, SEQ_LEN)
+
+ # 2.count #unique features for each sparse field and generate feature config for sequence feature
+
+ embedding_dim = 16
+
+ user_feature_columns = [SparseFeat('user_id', feature_max_idx['user_id'], embedding_dim),
+ SparseFeat("gender", feature_max_idx['gender'], embedding_dim),
+ SparseFeat("age", feature_max_idx['age'], embedding_dim),
+ SparseFeat("occupation", feature_max_idx['occupation'], embedding_dim),
+ SparseFeat("zip", feature_max_idx['zip'], embedding_dim),
+ VarLenSparseFeat(SparseFeat('hist_movie_id', feature_max_idx['movie_id'], embedding_dim,
+ embedding_name="movie_id"), SEQ_LEN, 'mean', 'hist_len'),
+ ]
+
+ item_feature_columns = [SparseFeat('movie_id', feature_max_idx['movie_id'], embedding_dim)]
+
+ # 3.Define Model and train
+
+ model = DSSM(user_feature_columns, item_feature_columns) # FM(user_feature_columns,item_feature_columns)
+
+ model.compile(optimizer='adagrad', loss="binary_crossentropy")
+
+ history = model.fit(train_model_input, train_label, # train_label,
+ batch_size=256, epochs=1, verbose=1, validation_split=0.0, )
+
+ # 4. Generate user features for testing and full item features for retrieval
+ test_user_model_input = test_model_input
+ all_item_model_input = {"movie_id": item_profile['movie_id'].values, "movie_idx": item_profile['movie_id'].values}
+
+ user_embedding_model = Model(inputs=model.user_input, outputs=model.user_embedding)
+ item_embedding_model = Model(inputs=model.item_input, outputs=model.item_embedding)
+
+ user_embs = user_embedding_model.predict(test_user_model_input, batch_size=2 ** 12)
+ item_embs = item_embedding_model.predict(all_item_model_input, batch_size=2 ** 12)
+
+ print(user_embs.shape)
+ print(item_embs.shape)
+
+ # 5. [Optional] ANN search by faiss and evaluate the result
+
+ # test_true_label = {line[0]:[line[2]] for line in test_set}
+ #
+ # import numpy as np
+ # import faiss
+ # from tqdm import tqdm
+ # from deepmatch.utils import recall_N
+ #
+ # index = faiss.IndexFlatIP(embedding_dim)
+ # # faiss.normalize_L2(item_embs)
+ # index.add(item_embs)
+ # # faiss.normalize_L2(user_embs)
+ # D, I = index.search(user_embs, 50)
+ # s = []
+ # hit = 0
+ # for i, uid in tqdm(enumerate(test_user_model_input['user_id'])):
+ # try:
+ # pred = [item_profile['movie_id'].values[x] for x in I[i]]
+ # filter_item = None
+ # recall_score = recall_N(test_true_label[uid], pred, N=50)
+ # s.append(recall_score)
+ # if test_true_label[uid] in pred:
+ # hit += 1
+ # except:
+ # print(i)
+ # print("recall", np.mean(s))
+ # print("hr", hit / len(test_user_model_input['user_id']))
diff --git a/examples/run_youtubednn_sampledsoftmax.py b/examples/run_youtubednn_sampledsoftmax.py
new file mode 100644
index 0000000..3e18afe
--- /dev/null
+++ b/examples/run_youtubednn_sampledsoftmax.py
@@ -0,0 +1,109 @@
+import pandas as pd
+from deepctr.inputs import SparseFeat, VarLenSparseFeat
+from preprocess import gen_data_set, gen_model_input
+from sklearn.preprocessing import LabelEncoder
+from tensorflow.python.keras import backend as K
+from tensorflow.python.keras.models import Model
+
+from deepmatch.models import *
+from deepmatch.utils import sampledsoftmaxloss
+
+if __name__ == "__main__":
+
+ data = pd.read_csvdata = pd.read_csv("./movielens_sample.txt")
+ sparse_features = ["movie_id", "user_id",
+ "gender", "age", "occupation", "zip", ]
+ SEQ_LEN = 50
+ negsample = 0
+
+ # 1.Label Encoding for sparse features,and process sequence features with `gen_date_set` and `gen_model_input`
+
+ features = ['user_id', 'movie_id', 'gender', 'age', 'occupation', 'zip']
+ feature_max_idx = {}
+ for feature in features:
+ lbe = LabelEncoder()
+ data[feature] = lbe.fit_transform(data[feature]) + 1
+ feature_max_idx[feature] = data[feature].max() + 1
+
+ user_profile = data[["user_id", "gender", "age", "occupation", "zip"]].drop_duplicates('user_id')
+
+ item_profile = data[["movie_id"]].drop_duplicates('movie_id')
+
+ user_profile.set_index("user_id", inplace=True)
+
+ user_item_list = data.groupby("user_id")['movie_id'].apply(list)
+
+ train_set, test_set = gen_data_set(data, negsample)
+
+ train_model_input, train_label = gen_model_input(train_set, user_profile, SEQ_LEN)
+ test_model_input, test_label = gen_model_input(test_set, user_profile, SEQ_LEN)
+
+ # 2.count #unique features for each sparse field and generate feature config for sequence feature
+
+ embedding_dim = 16
+
+ user_feature_columns = [SparseFeat('user_id', feature_max_idx['user_id'], embedding_dim),
+ SparseFeat("gender", feature_max_idx['gender'], embedding_dim),
+ SparseFeat("age", feature_max_idx['age'], embedding_dim),
+ SparseFeat("occupation", feature_max_idx['occupation'], embedding_dim),
+ SparseFeat("zip", feature_max_idx['zip'], embedding_dim),
+ VarLenSparseFeat(SparseFeat('hist_movie_id', feature_max_idx['movie_id'], embedding_dim,
+ embedding_name="movie_id"), SEQ_LEN, 'mean', 'hist_len'),
+ ]
+
+ item_feature_columns = [SparseFeat('movie_id', feature_max_idx['movie_id'], embedding_dim)]
+
+ # 3.Define Model and train
+
+ K.set_learning_phase(True)
+
+ model = YoutubeDNN(user_feature_columns, item_feature_columns, num_sampled=5, user_dnn_hidden_units=(64, 16))
+ # model = MIND(user_feature_columns,item_feature_columns,dynamic_k=True,p=1,k_max=2,num_sampled=5,user_dnn_hidden_units=(64,16),init_std=0.001)
+
+ model.compile(optimizer="adagrad", loss=sampledsoftmaxloss) # "binary_crossentropy")
+
+ history = model.fit(train_model_input, train_label, # train_label,
+ batch_size=256, epochs=1, verbose=1, validation_split=0.0, )
+
+ # 4. Generate user features for testing and full item features for retrieval
+ test_user_model_input = test_model_input
+ all_item_model_input = {"movie_id": item_profile['movie_id'].values, "movie_idx": item_profile['movie_id'].values}
+
+ user_embedding_model = Model(inputs=model.user_input, outputs=model.user_embedding)
+ item_embedding_model = Model(inputs=model.item_input, outputs=model.item_embedding)
+
+ user_embs = user_embedding_model.predict(test_user_model_input, batch_size=2 ** 12)
+ # user_embs = user_embs[:, i, :] i in [0,k_max) if MIND
+ item_embs = item_embedding_model.predict(all_item_model_input, batch_size=2 ** 12)
+
+ print(user_embs.shape)
+ print(item_embs.shape)
+
+ # 5. [Optional] ANN search by faiss and evaluate the result
+
+ # test_true_label = {line[0]:[line[2]] for line in test_set}
+ #
+ # import numpy as np
+ # import faiss
+ # from tqdm import tqdm
+ # from deepmatch.utils import recall_N
+ #
+ # index = faiss.IndexFlatIP(embedding_dim)
+ # # faiss.normalize_L2(item_embs)
+ # index.add(item_embs)
+ # # faiss.normalize_L2(user_embs)
+ # D, I = index.search(user_embs, 50)
+ # s = []
+ # hit = 0
+ # for i, uid in tqdm(enumerate(test_user_model_input['user_id'])):
+ # try:
+ # pred = [item_profile['movie_id'].values[x] for x in I[i]]
+ # filter_item = None
+ # recall_score = recall_N(test_true_label[uid], pred, N=50)
+ # s.append(recall_score)
+ # if test_true_label[uid] in pred:
+ # hit += 1
+ # except:
+ # print(i)
+ # print("recall", np.mean(s))
+ # print("hr", hit / len(test_user_model_input['user_id']))
diff --git a/setup.py b/setup.py
new file mode 100644
index 0000000..6ba0ac7
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,50 @@
+import setuptools
+
+with open("README.md", "r") as fh:
+ long_description = fh.read()
+
+REQUIRED_PACKAGES = [
+ 'h5py','requests',"deepctr==0.7.4"
+]
+
+setuptools.setup(
+ name="deepmatch",
+ version="0.0.0",
+ author="Weichen Shen",
+ author_email="wcshen1994@163.com",
+ description="Deep matching model library for recommendations, advertising, and search. It's easy to train models and to **export representation vectors** for user and item which can be used for **ANN search**.",
+ long_description=long_description,
+ long_description_content_type="text/markdown",
+ url="https://github.com/shenweichen/deepmatch",
+ download_url='https://github.com/shenweichen/deepmatch/tags',
+ packages=setuptools.find_packages(
+ exclude=["tests", "tests.models", "tests.layers"]),
+ python_requires=">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*", # '>=3.4', # 3.4.6
+ install_requires=REQUIRED_PACKAGES,
+ extras_require={
+ "cpu": ["tensorflow>=1.4.0,!=1.7.*,!=1.8.*"],
+ "gpu": ["tensorflow-gpu>=1.4.0,!=1.7.*,!=1.8.*"],
+ },
+ entry_points={
+ },
+ classifiers=(
+ "License :: OSI Approved :: Apache Software License",
+ "Operating System :: OS Independent",
+ 'Intended Audience :: Developers',
+ 'Intended Audience :: Education',
+ 'Intended Audience :: Science/Research',
+ 'Programming Language :: Python :: 3',
+ 'Programming Language :: Python :: 2.7',
+ 'Programming Language :: Python :: 3.5',
+ 'Programming Language :: Python :: 3.6',
+ 'Programming Language :: Python :: 3.7',
+ 'Topic :: Scientific/Engineering',
+ 'Topic :: Scientific/Engineering :: Artificial Intelligence',
+ 'Topic :: Software Development',
+ 'Topic :: Software Development :: Libraries',
+ 'Topic :: Software Development :: Libraries :: Python Modules',
+ ),
+ license="Apache-2.0",
+ keywords=['match', 'matching','recommendation'
+ 'deep learning', 'tensorflow', 'tensor', 'keras'],
+)