How to save the trained model inside the callback in pickle

I encountered such a problem when the model is successfully trained, but at the same time I cannot save the trained model inside the callback and the following error pops up (_pickle.PicklingError: Can’t pickle <function error at 0x7f1ebbd3b820>: import of module ‘pages.dashboard.experiment’ failed).

You can reproduce the following code in your conscious and felt pain on yourself

This error happens in multipage application
here is the minimal code

@callback(
    Output(...),
    Input(..),
)
def example_def(_):
       ...
       cls = autosklearn.classification.AutoSklearnClassifier(
            time_left_for_this_task=120,
            per_run_time_limit=30,
            scoring_functions=[roc_auc],
        )
        cls.fit(X_train, y_train)

        pickle.dump(cls, open('AUTOSKLEARN.pkl', "wb"))
        
        #with open('classifier.pkl', 'wb') as f:
        #       pickle.dump(cls, f)

        # joblib.dump(cls, 'final_model.joblib')

Hi @Sohibjon

Can you simplify this example? Does the error only happen with autosklearn or do you get the same error if you try to pickle a DataFrame?

hello @AnnMarieW , I tried to simplify the example as much as possible and published it on GitHub, yes, I get this error when I try to save the trained AutoSklearn model, other machine learning models do not give this error, and when I run AutoSklearn in a single page application, it perfectly saves the trained model in pickle. Error is output with AutoSklearn library in multipage application, strange.