skops icon indicating copy to clipboard operation
skops copied to clipboard

Bug: dump fails on keras models with RecursionError: maximum recursion depth exceeded, after calling fit

Open blaisethom opened this issue 2 years ago • 1 comments

skops io seems to work fine with scikeras models when they haven't been fit, but not when they've been fit. The error message (and potentially underlying cause) is the same as https://github.com/skops-dev/skops/issues/184

Example code (this works):

from tensorflow import keras
from sklearn.pipeline import Pipeline
from skops.io import dump
from scikeras.wrappers import KerasClassifier

# This simplifies the basic usage tutorial from https://adriangb.com/scikeras/stable/notebooks/Basic_Usage.html

def get_clf(meta):
    n_features_in_ = meta["n_features_in_"]
    model = keras.models.Sequential()
    model.add(keras.layers.Input(shape=(n_features_in_,)))
    model.add(keras.layers.Dense(1, activation="sigmoid"))
    return model
    
clf = KerasClassifier(
    model=get_clf,
    loss="binary_crossentropy"
)

pipeline = Pipeline(
    [("classifier", clf)]
)

# These both work
dump(clf, 'keras-test.skops') 
dump(pipeline, 'keras-test.skops')

However, running the following then gives a "RecursionError: maximum recursion depth exceeded" on the last line:

import numpy as np
from sklearn.datasets import make_classification
X, y = make_classification(1000, 20, n_informative=10, random_state=0)
clf.fit(X, y)
dump(clf, 'keras-test.skops') 

blaisethom avatar Aug 29 '23 14:08 blaisethom

@blaisethom What version of scikeras are you using?

lazarust avatar Sep 11 '23 23:09 lazarust