After creating a classification model I need to use the k-Cross Fold Validation but I keep getting this error: AttributeError: 'Adam' object has no attribute '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"])
How can I resolve this? You can find below the full error:
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__.
It seems that the program is trying to create a deep copy of a Keras model with 'copy.deepcopy' but the model doesn't have the 'deepcopy' attribute and this is the reason of the error. But I cannot understand what I'm missing beacuse it worked until today...
This appears to be an issue with how Keras is imported.
First, make sure you've got Tensorflow version 2.11.0, and that you're importing Keras from there.
>>> !pip install tensorflow==2.11.0
>>> import tensorflow as tf
Then pass the Adam optimizer from tf.keras as the optimizer argument to the KerasClassifier class
keras_clf = KerasClassifier(model = model, optimizer=tf.keras.optimizers.Adam(), epochs=100, verbose=0)
ETA: This is an answer to a similar question, and includes a solution that works with Tensorflow 2.9
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With