This project contains several DNN models for for CTR/CVR implenmented with tf and keras
Switch branches/tags
Nothing to show
Clone or download


This project implements serval models of the papers on CTR prediction with easy-to-use call interfaces.

The goal is to make it possible for everyone to use complex models with

Most of the models have been finished in keras. The tensorflow version will be added soon~ Please feel free to contact me if you have any questions!!

Support Model List

Model Paper Available Framework
AFM [IJCAI 2017]Attentional Factorization Machines: Learning the Weight of Feature Interactions via Attention Networks keras
DCN [ADKDD 2017]Deep & Cross Network for Ad Click Predictions keras,tensorflow
DeepFM [IJCAI 2017]DeepFM: A Factorization-Machine based Neural Network for CTR Prediction keras,tensorflow
MLR [arxiv 2017]Learning Piece-wise Linear Models from Large Scale Data for Ad Click Prediction keras,
NFM [SIGIR 2017]Neural Factorization Machines for Sparse Predictive Analytics keras

Keras Model


  • python3
  • tensorflow==1.4.0
  • keras==2.1.2

Quick Start

Source code

data = pd.read_pickle("./demo/small_data.pkl")
sparse_features = [ "movie_id","user_id","gender","age","occupation","zip"]
target = ['rating']
#1.Label Encoding for sparse features,and Normalization for dense fetures
for feat in sparse_features:
  lbe = LabelEncoder()
  data[feat] = lbe.fit_transform(data[feat])
#2.count #unique features for each sparse field
sparse_feature_dim = {feat:data[feat].nunique() for feat in sparse_features}
#3.generate input data for model
model_input = [data[feat].values for feat in sparse_feature_dim] 
#4.Define Model,compile and train
model = NFM({"sparse":sparse_feature_dim,"dense":[]},final_activation='linear').model

history =,data[target],

TensorFlow Model


  • python3
  • tensorflow==1.4.0
  • numpy==1.13.3
  • scikit-learn==0.19.1

Design Notes

The base base class mimics the keras model to implement the following public methods, including:

  • compile
  • save_model
  • load_mdel
  • train_on_batch
  • fit
  • test_on_batch
  • evaluate
  • predict_on_batch
  • predict

private methods:

  • _create_optimizer
  • _create_metrics
  • _compute_sample_weight

At the same time, several abstract methods are designed:

  • _get_input_data
  • _get_input_target
  • _get_output_target
  • _get_optimizer_loss
  • _build_graph

The subclass is required to call self._build_graph() at the end of the __init__ method to build the calculation graph.


  • Add tf.summary.FileWriter
  • Add custom metric function
  • Add weighted loss function
  • Encapsulate models with tf.estimator

Quick Start

Source code

Experiment Result

see docs/