scikeras.wrappers.KerasClassifier causes TypeError: object of type 'NoneType' has no len()

13 views Asked by At

I am learning how to use the scikeras.wrappers library. I have written a simple tensorflow model that I want to train after wrapping it with the scikeras.wrappers.KerasClassifier:

def get_mlp_model(
        hiddenLayerOne=784, 
        hiddenLayerTwo=256, 
        dropout=0.2, 
    ):
    model = Sequential()
    model.add(Flatten())
    model.add(Dense(hiddenLayerOne, activation='relu', input_shape=(hiddenLayerOne,)))
    model.add(Dropout(dropout))
    model.add(Dense(hiddenLayerTwo, activation='relu'))
    model.add(Dropout(dropout))

    model.add(Dense(10, activation='softmax'))

    model.compile(
        optimizer=Adam(learning_rate=0.01),
        loss="sparse_categorical_crossentropy",
        metrics=["accuracy"]
    )

    return model

When I train this model on its own like I do here, I get reasonably good results:

import tensorflow as tf 
tf.random.set_seed(42)

from util.mlp import get_mlp_model
from tensorflow.keras.datasets import mnist

((trainData, trainLabels), (testData, testLabels)) = mnist.load_data()
trainData = trainData.astype("float32") / 255.0
testData = testData.astype("float32") / 255.0

model = get_mlp_model()
history = model.fit(
        x=trainData, 
        y=trainLabels,
        validation_data=(testData, testLabels),
        batch_size=8,
        epochs=20
    )

accuracy = model.evaluate(testData, testLabels)[1]
print(f"accuracy: {accuracy * 100}")

Training results:

Epoch 1/20
7500/7500 [==============================] - 40s 5ms/step - loss: 0.7856 - accuracy: 0.8111 - val_loss: 0.5982 - val_accuracy: 0.8919
Epoch 2/20
7500/7500 [==============================] - 39s 5ms/step - loss: 0.6897 - accuracy: 0.8392 - val_loss: 0.4401 - val_accuracy: 0.8860
Epoch 3/20
7500/7500 [==============================] - 37s 5ms/step - loss: 0.6637 - accuracy: 0.8449 - val_loss: 0.4441 - val_accuracy: 0.8966
Epoch 4/20
7500/7500 [==============================] - 38s 5ms/step - loss: 0.6481 - accuracy: 0.8560 - val_loss: 0.5676 - val_accuracy: 0.8270
Epoch 5/20
4988/7500 [==================>...........] - ETA: 13s - loss: 0.6619 - accuracy: 0.8550

However, simply wrapping the get_mlp_model function in scikeras.wrappers.KerasClassifier causes an error when training:

import tensorflow as tf
tf.random.set_seed(42)

from util.mlp import get_mlp_model
from scikeras.wrappers import KerasClassifier
from sklearn.model_selection import RandomizedSearchCV
from tensorflow.keras.datasets import mnist

((trainData, trainLabels), (testData, testLabels)) = mnist.load_data()
trainData = trainData.astype("float32") / 255.0
testData = testData.astype("float32") / 255.0

model = KerasClassifier(model=get_mlp_model, verbose=0)
model.fit(trainData, trainLabels)

Here is the error I am encountering:

Traceback (most recent call last):
  File "/Users/kuanhc96/Desktop/pyimagesearch/DL130-hyperparameter_tuning/intro/test.py", line 18, in <module>
    model.fit(trainData, trainLabels)
  File "/Users/kuanhc96/opt/anaconda3/envs/pyimagesearch/lib/python3.9/site-packages/scikeras/wrappers.py", line 1491, in fit
    super().fit(X=X, y=y, sample_weight=sample_weight, **kwargs)
  File "/Users/kuanhc96/opt/anaconda3/envs/pyimagesearch/lib/python3.9/site-packages/scikeras/wrappers.py", line 760, in fit
    self._fit(
  File "/Users/kuanhc96/opt/anaconda3/envs/pyimagesearch/lib/python3.9/site-packages/scikeras/wrappers.py", line 926, in _fit
    self._check_model_compatibility(y)
  File "/Users/kuanhc96/opt/anaconda3/envs/pyimagesearch/lib/python3.9/site-packages/scikeras/wrappers.py", line 549, in _check_model_compatibility
    if self.n_outputs_expected_ != len(self.model_.outputs):
TypeError: object of type 'NoneType' has no len()

any help would be appreciated!

training to begin when using KerasClassifier

0

There are 0 answers