gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 03:27:49 25 4
gpt4 key购买 nike

我有十几个预训练的 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,它是该包装类的增强分支,可实现更多功能,包括功能模型类型和多输出,此外您还可以使用预构建模型。我目前正在弄清楚与您相同的事情,但这看起来很有希望。

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

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

model:Union[None, Callable[..., tf.keras.Model], tf.keras.Model], default 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.

另外:

属性 model_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(model=pre_built_model_here,optimizer='Adam',...等)

关于python - 如何将经过训练的模型传递给 KerasClassifier?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71366960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com