如何将经过训练的模型传递给 KerasClassifier?

How to pass a trained model to KerasClassifier?

我有十几个预训练的 DNN,我希望将它们添加到 sklearn 集成中。问题是我似乎无法为 KerasClassifier.

提供预训练模型
classifier_models = []
# models: dict of pre-trained models.
for name, model in models: 
    try:
        # Normal Sklearn models. No need to modify.
        model._estimator_type
        classifier_models.append((name, model))
    except:
        # Pre-trained DNNs (keras) must be wrapped.
        new_model = KerasClassifier(model=model)
        # Standard procedure.
        new_model._estimator_type = 'classifier'
        classifier_models.append((name, new_model))

错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_11744/990400553.py in <module>
      3     try:
----> 4         model._estimator_type
      5         classifier_models.append((name, model))

AttributeError: 'Sequential' object has no attribute '_estimator_type'

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_11744/990400553.py in <module>
      5         classifier_models.append((name, model))
      6     except:
----> 7         new_model = KerasClassifier(model=model)
      8         new_model._estimator_type = 'classifier'
      9         classifier_models.append((name, new_model))

~\miniconda3\envs\epfl-ml\lib\site-packages\tensorflow\python\keras\wrappers\scikit_learn.py in __init__(self, build_fn, **sk_params)
     75     self.build_fn = build_fn
     76     self.sk_params = sk_params
---> 77     self.check_params(sk_params)
     78 
     79   def check_params(self, params):

~\miniconda3\envs\epfl-ml\lib\site-packages\tensorflow\python\keras\wrappers\scikit_learn.py in check_params(self, params)
     91     ]
     92     if self.build_fn is None:
---> 93       legal_params_fns.append(self.__call__)
     94     elif (not isinstance(self.build_fn, types.FunctionType) and
     95           not isinstance(self.build_fn, types.MethodType)):

AttributeError: 'KerasClassifier' object has no attribute '__call__'

我不想将 KerasClassifier 与构建函数一起使用(例如:KerasClassifier(build_fn=build_dnn()) 因为我已经有一个训练有素的网络并且需要很多时间来重新训练。

显然,解决此问题的最佳方法是使用 SciKeras,它是包装器 class 的增强版分支,可实现更多功能,包括功能模型类型和 multi-output 另外您可以使用pre-built 模型。我目前正在弄清楚与您相同的事情,但这看起来很有希望。

看起来 scikeras.wrappers.KerasClassifier 包装器的第一个变量模型可以是构建 keras 模型的函数或已经构建的模型。

来自 KerasClassifier 构造函数参数的 scikeras 文档:

模型:联合[None,可调用[…,tf.keras.Model],tf.keras.Model],默认None

Used to build the Keras Model. When called, **must return a compiled instance of a Keras Model** to be used by fit, predict, etc. If None, you must implement _keras_build_fn.

此外:

属性模型_tf.keras.Model

The instantiated and compiled Keras Model. For pre-built models, this will just be a reference to the passed Model instance.

https://www.adriangb.com/scikeras/stable/generated/scikeras.wrappers.KerasClassifier.html

所以例如你的包装器看起来像这样:

wrapped_model = KerasClassifier(模型=pre_built_model_here,优化器='Adam',...等等)