-
Notifications
You must be signed in to change notification settings - Fork 50
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
Sequential model doesn't have outputs #207
Comments
Hmm, let me take a look. |
I think you need to add an Input layer: def model():
model = tf.keras.Sequential()
modeo.add(tf.keras.layers.Input(shape=(8,)))
model.add(tf.keras.layers.Dense(8)) # this does not have to be 8
... Also, related to #206 , it seems like |
Did #207 (comment) solve your problem @stsievert ? Happy to dig deeper otherwise. |
Yes, this code works: from sklearn.datasets import make_classification
from scikeras.wrappers import KerasClassifier
import tensorflow as tf
def model():
model = tf.keras.Sequential()
model.add(tf.keras.layers.Input(shape=(8,)))
model.add(tf.keras.layers.Dense(8))
model.add(tf.keras.layers.Dense(1))
return model
X, y = make_classification(n_features=8)
est = KerasClassifier(model=model, loss="binary_crossentropy")
est.fit(X, y=y) I think this error should be caught. |
We can leave this open to track it. Ideally, we can come up with a larger category of "Keras Model does not have inputs or outputs". By the way, I believe that the reason Keras allows this is because Model is just a layer, and thus can be chained, like I did in #200 |
This should be easy to catch: With Without |
Eh, I like specific and narrow PRs. Let's keep them separate. |
Hi, I'm encountering a similar issue when trying to wrap a TensorFlow Decision Forests model: from sklearn.datasets import make_classification
import tensorflow_decision_forests as tfdf
from scikeras.wrappers import KerasClassifier
X, y = make_classification(1000, 20, n_informative=10, random_state=0)
model = tfdf.keras.CartModel()
model.compile()
clf = KerasClassifier(model=model)
clf.fit(X, y)
Is there a way to get this to work? |
Does that model have an input layer? Can you post the results of model.summary()? |
I trained a
|
So I suppose the answer to your question is no, the model does not have an input layer. Are such models compatible with SciKeras? |
Correct. But Keras allows you to compose models, so you can use your pretrained model as an layer in a new model (kinda) and then just add an input layer before it (and maybe an output layer, depending on your model). Take a look at this SO question: https://datascience.stackexchange.com/a/21620. Let me know if that works or if it gives you trouble, I can try to write an example closer to your use case if needed. |
inputs = Input(shape=(20,))
initial_model = tfdf.keras.CartModel()
outputs = Flatten()(initial_model(inputs))
model = Model(inputs=inputs, outputs=outputs)
model.compile(loss='mse') The above seems to work, although I get a warning printed:
Have I done something wrong? Do you have to pre-train the |
A couple things:
2.I would troubleshoot this by using Keras directly: instead of giving your model building function to SciKeras just do |
The original model definitely works as expected - it's a default model from TensorFlow Decision Forests and does indeed train and predict correctly. It looks like the warning is something unique to TFDF (I think Keras' functional API is confusing it for whatever reason), however the wrapped model seems to be working correctly. And you were right about the Thanks a lot for your help. It's much appreciated. |
Shouldn't this code work?
This throws a
ValueError: object of type NoneType [self.model_.outputs] has no len()
.Full traceback
The text was updated successfully, but these errors were encountered: