Repository: https://github.com/nok/sklearn-porter
Documentation: sklearn.neural_network.MLPClassifier
In [1]:
import sys
sys.path.append('../../../../..')
In [2]:
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle
iris_data = load_iris()
X = iris_data.data
y = iris_data.target
X = shuffle(X, random_state=0)
y = shuffle(y, random_state=0)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.4, random_state=5)
print(X_train.shape, y_train.shape)
print(X_test.shape, y_test.shape)
In [3]:
from sklearn.neural_network import MLPClassifier
clf = MLPClassifier(activation='relu', hidden_layer_sizes=50,
max_iter=500, alpha=1e-4, solver='sgd',
tol=1e-4, random_state=1, learning_rate_init=.1)
clf.fit(X_train, y_train)
Out[3]:
In [4]:
from sklearn_porter import Porter
porter = Porter(clf, language='java')
output = porter.export(export_data=True)
print(output)
In [5]:
# Save classifier:
# with open('MLPClassifier.java', 'w') as f:
# f.write(output)
# Check model data:
# $ cat data.json
# Download dependencies:
# $ wget -O gson.jar http://central.maven.org/maven2/com/google/code/gson/gson/2.8.5/gson-2.8.5.jar
# Compile model:
# $ javac -cp .:gson.jar MLPClassifier.java
# Run classification:
# $ java -cp .:gson.jar MLPClassifier data.json 1 2 3 4