Skip to content

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

Open
@blaisethom

Description

@blaisethom

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 #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') 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions