Skip to content

Sequential model doesn't have outputs #207

Open
@stsievert

Description

@stsievert

Shouldn't this code work?

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.Dense(8))
    model.add(tf.keras.layers.Dense(1))
    return model

X, y = make_classification(n_features=8)
est = KerasClassifier(model=model, loss="sparse_categorical_crossentropy")
est.fit(X, y=y)

This throws a ValueError: object of type NoneType [self.model_.outputs] has no len().

Full traceback
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~/Downloads/_junk2.py in <module>
     11 X, y = make_classification(n_features=8)
     12 est = KerasClassifier(model=model, loss="sparse_categorical_crossentropy")
---> 13 est.fit(X, y=y)

~/anaconda3/envs/scikeras/lib/python3.7/site-packages/scikeras/wrappers.py in fit(self, X, y, sample_weight, **kwargs)
   1375             sample_weight = 1 if sample_weight is None else sample_weight
   1376             sample_weight *= compute_sample_weight(class_weight=self.class_weight, y=y)
-> 1377         super().fit(X=X, y=y, sample_weight=sample_weight, **kwargs)
   1378         return self
   1379

~/anaconda3/envs/scikeras/lib/python3.7/site-packages/scikeras/wrappers.py in fit(self, X, y, sample_weight, **kwargs)
    739             epochs=getattr(self, "fit__epochs", self.epochs),
    740             initial_epoch=0,
--> 741             **kwargs,
    742         )
    743

~/anaconda3/envs/scikeras/lib/python3.7/site-packages/scikeras/wrappers.py in _fit(self, X, y, sample_weight, warm_start, epochs, initial_epoch, **kwargs)
    855         X = self.feature_encoder_.transform(X)
    856
--> 857         self._check_model_compatibility(y)
    858
    859         self._fit_keras_model(

~/anaconda3/envs/scikeras/lib/python3.7/site-packages/scikeras/wrappers.py in _check_model_compatibility(self, y)
    541             # we recognize the attribute but do not force it to be
    542             # generated
--> 543             if self.n_outputs_expected_ != len(self.model_.outputs):
    544                 raise ValueError(
    545                     "Detected a Keras model input of size"

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions