Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SKLearnClassifier not working properly #20884

Closed
J-ATJ opened this issue Feb 9, 2025 · 2 comments · May be fixed by #20888
Closed

SKLearnClassifier not working properly #20884

J-ATJ opened this issue Feb 9, 2025 · 2 comments · May be fixed by #20888
Assignees

Comments

@J-ATJ
Copy link

J-ATJ commented Feb 9, 2025

Some day ago I was trying to add a Keras model to scikit-learn Pipeline using SKLearnClassifier (wrapper), but I got this error when doing .fit() (notwithstanding the model was already compiled):

RuntimeError: Given model needs to be compiled, and have a loss and an optimizer.

So I made this example code to check if the clone_model function that the API uses does indeed keep the compiled model:

from keras.layers import Dense, Input
from keras.models import Sequential, clone_model
clf = Sequential()
clf.add(Input((7,)))
clf.add(Dense(8, activation="relu"))
clf.add(Dense(1, activation="sigmoid"))
clf.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
print("Original compiled?", clf.compiled)
cloned = clone_model(clf)
print("Cloned compiled?", cloned.compiled)

And the output:

Original compiled? True
Cloned compiled? False

I think that's probably the reason why SKLearnClassifier is not working well.
Unfortunately, I couldn't find a way to solve this issue.

@J-ATJ J-ATJ changed the title SKClassifier not working properly SKLearnClassifier not working properly Feb 9, 2025
@de-odex
Copy link

de-odex commented Feb 9, 2025

I was able to work around this issue by passing a function to SKLearnClassifier instead of the instance. Not sure why this works though

E: seems like fit() calls _get_model() which always clones the model: https://github.com/keras-team/keras/blob/v3.8.0/keras/src/wrappers/sklearn_wrapper.py#L146. Doesn't happen if you use a function/callable for model=

@J-ATJ
Copy link
Author

J-ATJ commented Feb 9, 2025

I'm glad to say that the proposed solution worked perfectly, I was able to integrate the Neural Network to the SKlearn Pipeline and export it to use it in another Python project.

I was able to work around this issue by passing a function to SKLearnClassifier instead of the instance. Not sure why this works though

E: seems like fit() calls _get_model() which always clones the model: https://github.com/keras-team/keras/blob/v3.8.0/keras/src/wrappers/sklearn_wrapper.py#L146. Doesn't happen if you use a function/callable for model=

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants