keras 属性错误:"Adam"对象没有"build"属性

cnjp1d6j  于 2023-01-21  发布在  其他
关注(0)|答案(1)|浏览(247)

创建分类模型后,我需要使用k交叉折叠验证,但我一直得到这个错误:属性错误:"Adam"对象没有属性"build"。

from scikeras.wrappers import KerasClassifier

keras_clf = KerasClassifier(model = model, optimizer="adam", epochs=100, verbose=0)
model_kResults = cross_validation(keras_clf, X, y, 5)

print(model_kResults)
print("Mean Validation Accuracy:", model_kResults["Mean Validation Accuracy"])
print("Mean Validation F1 Score:",model_kResults["Mean Validation F1 Score"])

我该如何解决这个问题?您可以在下面找到完整的错误:

in cross_validate(estimator, X, y, groups, scoring, cv, n_jobs, verbose, fit_params, pre_dispatch, return_train_score, return_estimator, error_score)
    265     # independent, and that it is pickle-able.
    266     parallel = Parallel(n_jobs=n_jobs, verbose=verbose, pre_dispatch=pre_dispatch)
--> 267     results = parallel(
    268         delayed(_fit_and_score)(
    269             clone(estimator),

/usr/local/lib/python3.8/dist-packages/joblib/parallel.py in __call__(self, iterable)
   1083             # remaining jobs.
   1084             self._iterating = False
-> 1085             if self.dispatch_one_batch(iterator):
   1086                 self._iterating = self._original_iterator is not None
   1087 

/usr/local/lib/python3.8/dist-packages/joblib/parallel.py in dispatch_one_batch(self, iterator)
    871                 big_batch_size = batch_size * n_jobs
    872 
--> 873                 islice = list(itertools.islice(iterator, big_batch_size))
    874                 if len(islice) == 0:
    875                     return False

/usr/local/lib/python3.8/dist-packages/sklearn/model_selection/_validation.py in <genexpr>(.0)
    267     results = parallel(
    268         delayed(_fit_and_score)(
--> 269             clone(estimator),
    270             X,
    271             y,

/usr/local/lib/python3.8/dist-packages/sklearn/base.py in clone(estimator, safe)
     84     new_object_params = estimator.get_params(deep=False)
     85     for name, param in new_object_params.items():
---> 86         new_object_params[name] = clone(param, safe=False)
     87     new_object = klass(**new_object_params)
     88     params_set = new_object.get_params(deep=False)

/usr/local/lib/python3.8/dist-packages/sklearn/base.py in clone(estimator, safe)
     65     elif not hasattr(estimator, "get_params") or isinstance(estimator, type):
     66         if not safe:
---> 67             return copy.deepcopy(estimator)
     68         else:
     69             if isinstance(estimator, type):

/usr/lib/python3.8/copy.py in deepcopy(x, memo, _nil)
    151             copier = getattr(x, "__deepcopy__", None)
    152             if copier is not None:
--> 153                 y = copier(memo)
    154             else:
    155                 reductor = dispatch_table.get(cls)

/usr/local/lib/python3.8/dist-packages/scikeras/_saving_utils.py in deepcopy_model(model, memo)
     81 def deepcopy_model(model: keras.Model, memo: Dict[Hashable, Any]) -> keras.Model:
     82     _, (model_bytes,) = pack_keras_model(model)
---> 83     new_model = unpack_keras_model(model_bytes)
     84     memo[model] = new_model
     85     return new_model

/usr/local/lib/python3.8/dist-packages/scikeras/_saving_utils.py in unpack_keras_model(packed_keras_model)
     51         model: keras.Model = load_model(temp_dir)
     52         model.load_weights(temp_dir)
---> 53         model.optimizer.build(model.trainable_variables)
     54         return model
     55 

/usr/local/lib/python3.8/dist-packages/keras/optimizer_v2/optimizer_v2.py in __getattribute__(self, name)
    843       if name in self._hyper:
    844         return self._get_hyper(name)
--> 845       raise e
    846 
    847   def __dir__(self):

/usr/local/lib/python3.8/dist-packages/keras/optimizer_v2/optimizer_v2.py in __getattribute__(self, name)
    833     """Overridden to support hyperparameter access."""
    834     try:
--> 835       return super(OptimizerV2, self).__getattribute__(name)
    836     except AttributeError as e:
    837       # Needed to avoid infinite recursion with __setattr__.

看起来程序正在尝试使用"copy. deepcopy"创建Keras模型的深度副本,但该模型没有"deepcopy"属性,这就是错误的原因。但我不明白我错过了什么,因为它直到今天才工作...

vhmi4jdf

vhmi4jdf1#

我把你的tensorflow版本改成了2.11.0,没问题

相关问题