Skip to main content

Keras2pmml is simple exporter for Keras models (for supported models see bellow) into PMML text format which address the problems mentioned bellow.

Storing predictive models using binary format (e.g. Pickle) may be dangerous from several perspectives - naming few:

  • binary compatibility:you update the libraries and may not be able to open the model serialized with older version

  • dangerous code: when you would use model made by someone else

  • interpretability: model cannot be easily opened and reviewed by human

  • etc.

In addition the PMML is able to persist scaling of the raw input features which helps gradient descent to run smoothly through optimization space.

Installation

To install keras2pmml, simply:

$ pip install keras2pmml

Example

Example on Iris data - for more examples see the examples folder.

from keras2pmml import keras2pmml
from sklearn.datasets import load_iris
import numpy as np
import theano
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import StandardScaler
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers.core import Dense

iris = load_iris()
X = iris.data
y = iris.target

theano.config.floatX = 'float32'
X = X.astype(theano.config.floatX)
y = y.astype(np.int32)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.3)

std = StandardScaler()
X_train_scaled = std.fit_transform(X_train)
X_test_scaled = std.transform(X_test)
y_train_ohe = np_utils.to_categorical(y_train)
y_test_ohe = np_utils.to_categorical(y_test)

model = Sequential()
model.add(Dense(input_dim=X_train.shape[1], output_dim=5, activation='tanh'))
model.add(Dense(input_dim=5, output_dim=y_test_ohe.shape[1], activation='sigmoid'))
model.compile(loss='categorical_crossentropy', optimizer='sgd')
model.fit(X_train_scaled, y_train_ohe, nb_epoch=10, batch_size=1, verbose=3,
          validation_data=(X_test_scaled, y_test_ohe))

params = {
    'feature_names': ['sepal_length', 'sepal_width', 'petal_length', 'petal_width'],
    'target_values': ['setosa', 'virginica', 'versicolor'],
    'target_name': 'specie',
    'copyright': 'Václav Čadek',
    'description': 'Simple Keras model for Iris dataset.',
    'model_name': 'Iris Model'
}

keras2pmml(estimator=model, transformer=std, file='keras_iris.pmml', **params)

Params explained

  • estimator: Keras model to be exported as PMML (for supported models - see bellow).

  • transformer: if provided (and it’s supported - see bellow) then scaling is applied to data fields.

  • file: name of the file where the PMML will be exported.

  • feature_names: when provided and have same shape as input layer, then features will have custom names, otherwise generic names (x0,…, xn-1) will be used.

  • target_values: when provided and have same shape as output layer, then target values will have custom names, otherwise generic names (y0,…, yn-1) will be used.

  • target_name: when provided then target variable will have custom name, otherwise generic name class will be used.

  • copyright: who is the author of the model.

  • description: optional parameter that sets description within PMML document.

  • model_name: optional parameter that sets model_name within PMML document.

What is supported?

  • Models
    • keras.models.Sequential

  • Activation functions
    • tanh

    • sigmoid/logistic

  • Scalers
    • sklearn.preprocessing.StandardScaler

    • sklearn.preprocessing.MinMaxScaler

License

This software is licensed under MIT licence.

Release files for keras2pmml 0.1.0

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

Source distribution (sdist)

Source distribution for keras2pmml 0.1.0
File Size Uploaded
keras2pmml-0.1.0.tar.gz 5.0 kB Details

Release files / keras2pmml-0.1.0.tar.gz

Download URL keras2pmml-0.1.0.tar.gz
Size 5.0 kB
Tags Source
SHA-256 checksum
How to use checksums
2af5a5a5824b0bdcb864eb83a7e7dd2d7a6ae1cad73c7b8f5dc4d1b6f06db3df
BLAKE2b-256 checksum
How to use checksums
273ab18e5936899dd2ab327ec687aa2001fb5fb48a87230806b828be983f9554
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.0 This release

1 release file

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