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, with model structure as below.
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
Release history Release notifications | RSS feed
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.0.tar.gz
(5.1 kB
view hashes)
Built Distribution
Close
Hashes for rankerNN2pmml-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9072a65d8c3eafa1f36629ad4cf7dfbbb179e91f6d2b4c05efd44ed8b9bc13a7 |
|
MD5 | e0e667de523604965c0ab79671c8e165 |
|
BLAKE2b-256 | 2632b0e3b61de183c2ff6646baad168d75b10494a4fb86e5234739c8d5b143cd |