Skip to main content

Exporter of pairwise ranker with Neural Nets as underlying model into PMML.

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rankerNN2pmml-0.1.2.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rankerNN2pmml-0.1.2-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file rankerNN2pmml-0.1.2.tar.gz.

File metadata

  • Download URL: rankerNN2pmml-0.1.2.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for rankerNN2pmml-0.1.2.tar.gz
Algorithm Hash digest
SHA256 b181288d99e8de3b754687c09765386e9bd7af592b5fdd2e9604a27eeac25bf4
MD5 344b40e62aba77573d306c52f11480df
BLAKE2b-256 9537904185468d856e20f2370e0e84222a4653d728cd6ed8d63d5972dd0fa7a3

See more details on using hashes here.

File details

Details for the file rankerNN2pmml-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: rankerNN2pmml-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for rankerNN2pmml-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5436887af8134714f30b877ddda2dbcefe5bd775229bf5f7ba78536faac43903
MD5 6bbc1d677b3b80225c51ea13169fb5f2
BLAKE2b-256 1bbcfe372b99f0baa363a090feec728fd616cba9f73cdd8b46ab569f6605e5dc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page