Skip to main content

rankerNN2pmml

Python library for converting pairwise Learning-To-Rank Neural Network models (RankNet NN, LambdaRank NN) into pmml.

Supported model structure

It supports pairwise Learning-To-Rank (LTR) algorithms such as Ranknet and LambdaRank, where the underlying model (hidden layers) is a neural network (NN) model.

Installation

pip install rankerNN2pmml

Example

Example on a RankNet model.

from keras.layers import Activation, Dense, Input, Subtract
from keras.models import Model
import random
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler, MinMaxScaler
from rankerNN2pmml import rankerNN2pmml

# generate dummy data.
INPUT_DIM = 3
X1 = 2 * np.random.uniform(size=(50, INPUT_DIM))
X2 = np.random.uniform(size=(50, INPUT_DIM))
Y = [random.randint(0,1) for _ in range(50)]

# data transformation
mms = MinMaxScaler()
mms.fit(np.concatenate((X1, X2), axis=0))
X1 = mms.transform(X1)
X2 = mms.transform(X2)

def RankNet_model(input_shape):
    # Neural network structure
    h1 = Dense(4, activation="relu", name='Relu_layer1')
    h2 = Dense(2, activation='relu', name='Relu_layer2')
    h3 = Dense(1, activation='linear', name='Identity_layer')
    # document 1 score
    input1 = Input(shape=(input_shape,), name='Input_layer1')
    x1 = h1(input1)
    x1 = h2(x1)
    x1 = h3(x1)
    # document 2 score
    input2 = Input(shape=(input_shape,), name='Input_layer2')
    x2 = h1(input2)
    x2 = h2(x2)
    x2 = h3(x2)
    # Subtract layer
    subtracted = Subtract(name='Subtract_layer')([x1, x2])
    # sigmoid
    out = Activation('sigmoid', name='Activation_layer')(subtracted)
    # build model
    model = Model(inputs=[input1, input2], outputs=out)
    return model

# build model
model = RankNet_model(INPUT_DIM)
model.compile(optimizer="adam", loss="binary_crossentropy")
# train model
model.fit([X1, X2], Y, batch_size=10, epochs=5, verbose=1)

params = {
    'feature_names': ['Feature1', 'Feature2', 'Feature3'],
    'target_name': 'score'
}
rankerNN2pmml(estimator=model, transformer=mms, file='model.pmml', **params)

Params explained

  • estimator: Keras model to be exported as PMML (see supported model structure above).
  • transformer: if provided then scaling is applied to data fields.
  • file: name of the file where PMML will be exported.
  • feature_names: when provided and have same shape as input layer, features will have custom names, otherwise generic names (x0,..., xn-1) will be used.
  • target_name: when provided target variable will have custom name, otherwise generic name score will be used.

What is supported?

  • Models (estimators)
    • keras.models.Model (see supported model structure above)
  • Activation functions
    • tanh
    • logistic (sigmoid)
    • identity
    • rectifier (Relu)
  • Transformers
    • sklearn.preprocessing.StandardScaler
    • sklearn.preprocessing.MinMaxScaler

Release files for rankerNN2pmml 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rankerNN2pmml 0.1.2
File Size Uploaded
rankerNN2pmml-0.1.2.tar.gz 5.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for rankerNN2pmml 0.1.2
File Interpreter ABI Platform
rankerNN2pmml-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 11.1 kB

Release files / rankerNN2pmml-0.1.2.tar.gz

Download URL rankerNN2pmml-0.1.2.tar.gz
Size 5.0 kB
Tags Source
SHA-256 checksum
How to use checksums
b181288d99e8de3b754687c09765386e9bd7af592b5fdd2e9604a27eeac25bf4
BLAKE2b-256 checksum
How to use checksums
9537904185468d856e20f2370e0e84222a4653d728cd6ed8d63d5972dd0fa7a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.28.1 CPython/3.7.1

Release files / rankerNN2pmml-0.1.2-py3-none-any.whl

Download URL rankerNN2pmml-0.1.2-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5436887af8134714f30b877ddda2dbcefe5bd775229bf5f7ba78536faac43903
BLAKE2b-256 checksum
How to use checksums
1bbcfe372b99f0baa363a090feec728fd616cba9f73cdd8b46ab569f6605e5dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.28.1 CPython/3.7.1

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page