how to convert saved model from sklearn into tensorflow/lite
Asked Answered
U

2

14

If I want to implement a classifier using the sklearn library. Is there a way to save the model or convert the file into a saved tensorflow file in order to convert it to tensorflow lite later?

Unpopular answered 13/1, 2020 at 20:47 Comment(2)
There is not a converter that is 100% foolproof to go from sklearn to tf. You might try the keras scikit api wrapper tensorflow.org/api_docs/python/tf/keras/wrappers/scikit_learn . Once you do that you can use the standard TF to TF Lite conversion process.Pythagoreanism
Thank you for your reply. but, as far as I understand, this wrapper helps to use the keras model in sklearn framework. say for example you trained the sequential NN, then you want to do cross validation from sklearn, this is when this wrapper is helpfulUnpopular
C
6

If you replicate the architecture in TensorFlow, which will be pretty easy given that scikit-learn models are usually rather simple, you can explicitly assign the parameters from the learned scikit-learn models to TensorFlow layers.

Here is an example with logistic regression turned into a single dense layer:

import tensorflow as tf
import numpy as np
from sklearn.linear_model import LogisticRegression

# some random data to train and test on
x = np.random.normal(size=(60, 21))
y = np.random.uniform(size=(60,)) > 0.5

# fit the sklearn model on the data
sklearn_model = LogisticRegression().fit(x, y)

# create a TF model with the same architecture
tf_model = tf.keras.models.Sequential()
tf_model.add(tf.keras.Input(shape=(21,)))
tf_model.add(tf.keras.layers.Dense(1))

# assign the parameters from sklearn to the TF model
tf_model.layers[0].weights[0].assign(sklearn_model.coef_.transpose())
tf_model.layers[0].bias.assign(sklearn_model.intercept_)

# verify the models do the same prediction
assert np.all((tf_model(x) > 0)[:, 0].numpy() == sklearn_model.predict(x))
Cuculiform answered 23/2, 2021 at 9:6 Comment(1)
Great! What other models/classifiers could be converted? I note too that now TF can use decision trees...Scaffold
G
1

It is not always easy to replicate a scikit model in tensorflow. For instance scitik has a lot of on the fly imputation libraries which will be a bit tricky to implement in tensorflow

Galliard answered 10/4, 2022 at 14:19 Comment(2)
This is not an "Answer", as defined by SO, at best it could be written as a Comment under the OP's question. Please read more about what qualifies as an Answer by visiting the help center, linked to at the top of every page.Mothering
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewMothering

© 2022 - 2024 — McMap. All rights reserved.