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

Scikeras and Keras Input layer #276

Open
phbillet opened this issue Jun 9, 2022 · 4 comments
Open

Scikeras and Keras Input layer #276

phbillet opened this issue Jun 9, 2022 · 4 comments

Comments

@phbillet
Copy link

phbillet commented Jun 9, 2022

I sent the following issue 16495.

Following the advice, I encounter an incompatibility between keras.wrappers.scikit_learn and scikeras.
With keras.wrappers.scikit_learn, I can train a model without Input layer.

With scikeras, it is not possible to do it, the program returns an error :

File ~/anaconda3/envs/EZStacking/lib/python3.8/site-packages/scikeras/wrappers.py:909, in BaseWrapper._fit(self, X, y, sample_weight, warm_start, epochs, initial_epoch, **kwargs)
    906 y = self.target_encoder_.transform(y)
    907 X = self.feature_encoder_.transform(X)
--> 909 self._check_model_compatibility(y)
    911 self._fit_keras_model(
    912     X,
    913     y,
   (...)
    918     **kwargs,
    919 )

File ~/anaconda3/envs/EZStacking/lib/python3.8/site-packages/scikeras/wrappers.py:540, in BaseWrapper._check_model_compatibility(self, y)
    535 # check if this is a multi-output model
    536 if getattr(self, "n_outputs_expected_", None):
    537     # n_outputs_expected_ is generated by data transformers
    538     # we recognize the attribute but do not force it to be
    539     # generated
--> 540     if self.n_outputs_expected_ != len(self.model_.outputs):
    541         raise ValueError(
    542             "Detected a Keras model input of size"
    543             f" {self.n_outputs_expected_ }, but {self.model_} has"
    544             f" {len(self.model_.outputs)} outputs"
    545         )
    546 # check that if the user gave us a loss function it ended up in
    547 # the actual model

TypeError: object of type 'NoneType' has no len()

So I decided to suppress the call self._check_model_compatibility(y) in the function _fit in the file wrappers.py.
Now it works fine, but it is not a good solution.
Is it possible to integrate Keras model without input layer ?

@phbillet
Copy link
Author

phbillet commented Jun 9, 2022

I would like to use Keras model without Input layer, because I try to build stacked model (with preprocessing pipeline). due to this architecture it is very difficult to determine the input shape, because a preprocessing step can add columns...
It could be interesting to add a new boolean that controls self._check_model_compatibility(y)...

@adriangb
Copy link
Owner

adriangb commented Jun 9, 2022

I'll look into making that backwards compatible but I believe we put that there because sklearn estimators are required to expose their input size.

Could you use the meta parameter in your build_model_fn? https://www.adriangb.com/scikeras/stable/advanced.html#meta

@phbillet
Copy link
Author

Here you can find an example of regression.
Could you help me to make it compatible with scikeras ?

@adriangb
Copy link
Owner

adriangb commented Jul 1, 2022

Could you use the meta parameter in your build_model_fn? https://www.adriangb.com/scikeras/stable/advanced.html#meta

Have you had a chance to check the documentation on how to create dynamically sized input layers?

Unfortunately the notebook you linked is a bit too complex and specific to your use case for me to rewrite and understand. If the above doesn't work, could you submit a self contained reproducibile example please?

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

No branches or pull requests

2 participants