haowei01/pytorch-examples

train models in pytorch, Learn to Rank, Collaborative Filter, Heterogeneous Treatment Effect, Uplift Modeling, etc

Python

188

29 commits

updated Mar 9, 2026

See the code

README

examples of training models in pytorch

Some implementations of Deep Learning algorithms in PyTorch.

Ranking - Learn to Rank

RankNet

Feed forward NN, minimize document pairwise cross entropy loss function

to train the model

python ranking/RankNet.py --lr 0.001 --debug --standardize

--debug print the parameter norm and parameter grad norm. This enable to evaluate whether there is gradient vanishing and gradient exploding problem --standardize makes sure input are scaled to have 0 as mean and 1.0 as standard deviation

NN structure: 136 -> 64 -> 16 -> 1, ReLU6 as activation function

optimizerlrepochloss (train)loss (eval)ndcg@10ndcg@30sec/epochFactorizationpairs/sec
adam0.001250.630020.6355080.417850.49337312loss func203739
adam0.001500.625950.6330820.423920.49771312loss func203739
adam0.0011000.622820.6324950.424380.49817312loss func203739
adam0.01250.626680.6315540.426580.50032312loss func203739
adam0.01500.621180.6292170.433170.50533312loss func203739
adam0.01250.623490.6330350.429790.50108202gradient314687
adam0.01500.617810.6304170.433970.50540202gradient314687

LambdaRank

Feed forward NN. Gradient is proportional to NDCG change of swapping two pairs of document

to choose the optimal learning rate, use smaller dataset:

python ranking/LambdaRank.py --lr 0.01 --ndcg_gain_in_train exp2 --small_dataset --debug --standardize

otherwise, use normal dataset:

OUTPUT_DIR=/tmp/ranking_output/
python ranking/LambdaRank.py --lr 0.01 --ndcg_gain_in_train exp2 --standardize \
--output_dir=$OUTPUT_DIR

to switch identity gain in NDCG in training, use --ndcg_gain_in_train identity

Total pairs per epoch are 63566774 currently each pairs are calculated twice. The following ndcg number are at eval phase and are using exp2 gain

optimizerlrepochloss (eval)ndcg@10ndcg@30sec/epochGain funcpairs/sec
adam0.001250.6386640.424700.49858204identity311602
adam0.001500.6374170.429100.50267204identity311602
adam0.01250.6352900.436670.50687204identity311602
adam0.01500.6398600.438740.50896204identity311602
adam0.0150.6455450.436270.50459208exp2304876
adam0.01250.6469030.441550.51165208exp2304876
adam0.01350.6446800.444540.51364208exp2304876

As the result compared with RankNet, LambdaRank's NDCG is generally better than RankNet, but cross entropy loss is higher This is mainly due to LambdaRank maximizing the NDCG, while RankNet minimizing the pairwise cross entropy loss.

visualize with tensorboard

tensorboard --logdir $OUTPUT_DIR --port=6006

if in a remote machine, run the tunnel through

ssh -fN $REMOTE_MACHINE -L 6006:127.0.0.1:6006

tensorboard screenshot

Dependencies:

  • pytorch-1.11
  • pandas
  • numpy
  • sklearn

install from anaconda:

conda create -n pytorch python=3.7

on Mac, use

conda install -c pytorch pytorch==1.11

use nvcc --version to check the cuda version (e.g. 9.0)

conda install pytorch torchvision cudatoolkit=9.0 -c pytorch
conda install -c anaconda pandas scikit-learn tensorboard ipython
conda install -c conda-forge matplotlib

Datasets:

use ranking/download_data.sh to prepare the data and put in the following directory

ranking/data
├── expedia
│   ├── basicPythonBenchmark.zip
│   ├── randomBenchmark.zip
│   ├── test.zip
│   ├── testOrderBenchmark.zip
│   └── train.zip
└── mslr-web10k
    ├── Fold1
    │   ├── test.txt
    │   ├── train.txt
    │   └── vali.txt
    └── MSLR-WEB10K.zip
heterogeneous-treatment-effects
inverse-propensity-score
lambdarank
learning-to-rank
ndcg
positional-bias
pytorch-implementation
pytorch-ranking
ranknet
uplift-modeling

haowei01/pytorch-examples

train models in pytorch, Learn to Rank, Collaborative Filter, Heterogeneous Treatment Effect, Uplift Modeling, etc

Python

188

29 commits

updated Mar 9, 2026

See the code

README

examples of training models in pytorch

Some implementations of Deep Learning algorithms in PyTorch.

Ranking - Learn to Rank

RankNet

Feed forward NN, minimize document pairwise cross entropy loss function

to train the model

python ranking/RankNet.py --lr 0.001 --debug --standardize

--debug print the parameter norm and parameter grad norm. This enable to evaluate whether there is gradient vanishing and gradient exploding problem --standardize makes sure input are scaled to have 0 as mean and 1.0 as standard deviation

NN structure: 136 -> 64 -> 16 -> 1, ReLU6 as activation function

optimizerlrepochloss (train)loss (eval)ndcg@10ndcg@30sec/epochFactorizationpairs/sec
adam0.001250.630020.6355080.417850.49337312loss func203739
adam0.001500.625950.6330820.423920.49771312loss func203739
adam0.0011000.622820.6324950.424380.49817312loss func203739
adam0.01250.626680.6315540.426580.50032312loss func203739
adam0.01500.621180.6292170.433170.50533312loss func203739
adam0.01250.623490.6330350.429790.50108202gradient314687
adam0.01500.617810.6304170.433970.50540202gradient314687

LambdaRank

Feed forward NN. Gradient is proportional to NDCG change of swapping two pairs of document

to choose the optimal learning rate, use smaller dataset:

python ranking/LambdaRank.py --lr 0.01 --ndcg_gain_in_train exp2 --small_dataset --debug --standardize

otherwise, use normal dataset:

OUTPUT_DIR=/tmp/ranking_output/
python ranking/LambdaRank.py --lr 0.01 --ndcg_gain_in_train exp2 --standardize \
--output_dir=$OUTPUT_DIR

to switch identity gain in NDCG in training, use --ndcg_gain_in_train identity

Total pairs per epoch are 63566774 currently each pairs are calculated twice. The following ndcg number are at eval phase and are using exp2 gain

optimizerlrepochloss (eval)ndcg@10ndcg@30sec/epochGain funcpairs/sec
adam0.001250.6386640.424700.49858204identity311602
adam0.001500.6374170.429100.50267204identity311602
adam0.01250.6352900.436670.50687204identity311602
adam0.01500.6398600.438740.50896204identity311602
adam0.0150.6455450.436270.50459208exp2304876
adam0.01250.6469030.441550.51165208exp2304876
adam0.01350.6446800.444540.51364208exp2304876

As the result compared with RankNet, LambdaRank's NDCG is generally better than RankNet, but cross entropy loss is higher This is mainly due to LambdaRank maximizing the NDCG, while RankNet minimizing the pairwise cross entropy loss.

visualize with tensorboard

tensorboard --logdir $OUTPUT_DIR --port=6006

if in a remote machine, run the tunnel through

ssh -fN $REMOTE_MACHINE -L 6006:127.0.0.1:6006

tensorboard screenshot

Dependencies:

  • pytorch-1.11
  • pandas
  • numpy
  • sklearn

install from anaconda:

conda create -n pytorch python=3.7

on Mac, use

conda install -c pytorch pytorch==1.11

use nvcc --version to check the cuda version (e.g. 9.0)

conda install pytorch torchvision cudatoolkit=9.0 -c pytorch
conda install -c anaconda pandas scikit-learn tensorboard ipython
conda install -c conda-forge matplotlib

Datasets:

use ranking/download_data.sh to prepare the data and put in the following directory

ranking/data
├── expedia
│   ├── basicPythonBenchmark.zip
│   ├── randomBenchmark.zip
│   ├── test.zip
│   ├── testOrderBenchmark.zip
│   └── train.zip
└── mslr-web10k
    ├── Fold1
    │   ├── test.txt
    │   ├── train.txt
    │   └── vali.txt
    └── MSLR-WEB10K.zip
heterogeneous-treatment-effects
inverse-propensity-score
lambdarank
learning-to-rank
ndcg
positional-bias
pytorch-implementation
pytorch-ranking
ranknet
uplift-modeling

Significant stargazers

LiuNeng

42 followers · starred Aug 2019