i want to use GridSearchCV using KerasClassifier, but i am stuck at the following error message:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[54], line 42
38 model = KerasClassifier(model=create_model, verbose=1)
40 grid_search = GridSearchCV(estimator=model, param_grid=param_grid, cv=3, scoring='accuracy',error_score='raise')
---> 42 grid_search_result = grid_search.fit(X, y)
44 print("Best Parameters:", grid_search_result.best_params_)
File /opt/conda/lib/python3.10/site-packages/sklearn/model_selection/_search.py:874, in BaseSearchCV.fit(self, X, y, groups, **fit_params)
868 results = self._format_results(
869 all_candidate_params, n_splits, all_out, all_more_results
870 )
872 return results
--> 874 self._run_search(evaluate_candidates)
876 # multimetric is determined here because in the case of a callable
877 # self.scoring the return type is only known after calling
878 first_test_score = all_out[0]["test_scores"]
File /opt/conda/lib/python3.10/site-packages/sklearn/model_selection/_search.py:1388, in GridSearchCV._run_search(self, evaluate_candidates)
1386 def _run_search(self, evaluate_candidates):
1387 """Search all candidates in param_grid"""
-> 1388 evaluate_candidates(ParameterGrid(self.param_grid))
File /opt/conda/lib/python3.10/site-packages/sklearn/model_selection/_search.py:821, in BaseSearchCV.fit.<locals>.evaluate_candidates(candidate_params, cv, more_results)
813 if self.verbose > 0:
814 print(
815 "Fitting {0} folds for each of {1} candidates,"
816 " totalling {2} fits".format(
817 n_splits, n_candidates, n_candidates * n_splits
818 )
819 )
--> 821 out = parallel(
822 delayed(_fit_and_score)(
823 clone(base_estimator),
824 X,
825 y,
826 train=train,
827 test=test,
828 parameters=parameters,
829 split_progress=(split_idx, n_splits),
830 candidate_progress=(cand_idx, n_candidates),
831 **fit_and_score_kwargs,
832 )
833 for (cand_idx, parameters), (split_idx, (train, test)) in product(
834 enumerate(candidate_params), enumerate(cv.split(X, y, groups))
835 )
836 )
838 if len(out) < 1:
839 raise ValueError(
840 "No fits were performed. "
841 "Was the CV iterator empty? "
842 "Were there no candidates?"
843 )
File /opt/conda/lib/python3.10/site-packages/sklearn/utils/parallel.py:63, in Parallel.__call__(self, iterable)
58 config = get_config()
59 iterable_with_config = (
60 (_with_config(delayed_func, config), args, kwargs)
61 for delayed_func, args, kwargs in iterable
62 )
---> 63 return super().__call__(iterable_with_config)
File /opt/conda/lib/python3.10/site-packages/joblib/parallel.py:1863, in Parallel.__call__(self, iterable)
1861 output = self._get_sequential_output(iterable)
1862 next(output)
-> 1863 return output if self.return_generator else list(output)
1865 # Let's create an ID that uniquely identifies the current call. If the
1866 # call is interrupted early and that the same instance is immediately
1867 # re-used, this id will be used to prevent workers that were
1868 # concurrently finalizing a task from the previous call to run the
1869 # callback.
1870 with self._lock:
File /opt/conda/lib/python3.10/site-packages/joblib/parallel.py:1792, in Parallel._get_sequential_output(self, iterable)
1790 self.n_dispatched_batches += 1
1791 self.n_dispatched_tasks += 1
-> 1792 res = func(*args, **kwargs)
1793 self.n_completed_tasks += 1
1794 self.print_progress()
File /opt/conda/lib/python3.10/site-packages/sklearn/utils/parallel.py:123, in _FuncWrapper.__call__(self, *args, **kwargs)
121 config = {}
122 with config_context(**config):
--> 123 return self.function(*args, **kwargs)
File /opt/conda/lib/python3.10/site-packages/sklearn/model_selection/_validation.py:686, in _fit_and_score(estimator, X, y, scorer, train, test, verbose, parameters, fit_params, return_train_score, return_parameters, return_n_test_samples, return_times, return_estimator, split_progress, candidate_progress, error_score)
684 estimator.fit(X_train, **fit_params)
685 else:
--> 686 estimator.fit(X_train, y_train, **fit_params)
688 except Exception:
689 # Note fit time as time until error
690 fit_time = time.time() - start_time
File /opt/conda/lib/python3.10/site-packages/scikeras/wrappers.py:1491, in KerasClassifier.fit(self, X, y, sample_weight, **kwargs)
1489 sample_weight = 1 if sample_weight is None else sample_weight
1490 sample_weight *= compute_sample_weight(class_weight=self.class_weight, y=y)
-> 1491 super().fit(X=X, y=y, sample_weight=sample_weight, **kwargs)
1492 return self
File /opt/conda/lib/python3.10/site-packages/scikeras/wrappers.py:760, in BaseWrapper.fit(self, X, y, sample_weight, **kwargs)
755 kwargs["epochs"] = kwargs.get(
756 "epochs", getattr(self, "fit__epochs", self.epochs)
757 )
758 kwargs["initial_epoch"] = kwargs.get("initial_epoch", 0)
--> 760 self._fit(
761 X=X,
762 y=y,
763 sample_weight=sample_weight,
764 warm_start=self.warm_start,
765 **kwargs,
766 )
768 return self
File /opt/conda/lib/python3.10/site-packages/scikeras/wrappers.py:928, in BaseWrapper._fit(self, X, y, sample_weight, warm_start, epochs, initial_epoch, **kwargs)
924 X = self.feature_encoder_.transform(X)
926 self._check_model_compatibility(y)
--> 928 self._fit_keras_model(
929 X,
930 y,
931 sample_weight=sample_weight,
932 warm_start=warm_start,
933 epochs=epochs,
934 initial_epoch=initial_epoch,
935 **kwargs,
936 )
File /opt/conda/lib/python3.10/site-packages/scikeras/wrappers.py:536, in BaseWrapper._fit_keras_model(self, X, y, sample_weight, warm_start, epochs, initial_epoch, **kwargs)
532 except ValueError as e:
533 # Keras puts keys like "val_accuracy" and "loss" and
534 # "val_loss" in hist.history
535 if "Unknown metric function" not in str(e):
--> 536 raise e
537 self.history_[key] += val
File /opt/conda/lib/python3.10/site-packages/scikeras/wrappers.py:531, in BaseWrapper._fit_keras_model(self, X, y, sample_weight, warm_start, epochs, initial_epoch, **kwargs)
529 for key, val in hist.history.items():
530 try:
--> 531 key = metric_name(key)
532 except ValueError as e:
533 # Keras puts keys like "val_accuracy" and "loss" and
534 # "val_loss" in hist.history
535 if "Unknown metric function" not in str(e):
File /opt/conda/lib/python3.10/site-packages/scikeras/utils/__init__.py:111, in metric_name(metric)
105 if not (isinstance(metric, (str, Metric)) or callable(metric)):
106 raise TypeError(
107 "``metric`` must be a string, a function, an instance of"
108 " ``tf.keras.metrics.Metric`` or a type inheriting from"
109 " ``tf.keras.metrics.Metric``"
110 )
--> 111 fn_or_cls = keras_metric_get(metric)
112 if isinstance(fn_or_cls, Metric):
113 return _camel2snake(fn_or_cls.__class__.__name__)
File /opt/conda/lib/python3.10/site-packages/keras/src/metrics/__init__.py:204, in get(identifier)
202 return obj
203 else:
--> 204 raise ValueError(f"Could not interpret metric identifier: {identifier}")
ValueError: Could not interpret metric identifier: loss
I tried everything, uninstalled all packages and installed them again. Changes the specified losses and metrics but nothing changed i am still stuck. Any help is much appreciated. Here is the code for reference:
def create_model(dropout_rate=0.2, hidden_layers=1, neurons=64):
model = Sequential()
model.add(Dense(neurons, input_dim=X.shape[1], activation='relu'))
model.add(Dropout(dropout_rate))
for _ in range(hidden_layers - 1):
model.add(Dense(neurons, activation='relu'))
model.add(Dropout(dropout_rate))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam')
return model
param_grid = {
'model__neurons': [10, 20],
}
model = create_model()
model = KerasClassifier(model=create_model, verbose=1)
grid_search = GridSearchCV(estimator=model, param_grid=param_grid, cv=3, scoring='accuracy',error_score='raise')
grid_search_result = grid_search.fit(X, y)