'super' object has no attribute '__sklearn_tags__' : Attribute Error

I am trying to run the example section of the book “Hands on ML “

I am trying to do cross validation on the Keras Sequential Model to identify best parameters using the RandomSearchCV.

import tensorflow as tf
from tensorflow import keras
!pip install --upgrade scikeras
from scikeras.wrappers import KerasRegressor
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import numpy as np
def build_model(n_hidden = 1, n_neurons = 30, learning_rate = 3e-3, input_shape=[8]):
  model = keras.models.Sequential()
  model.add(keras.layers.InputLayer(input_shape=input_shape))
  #options = {"input_shape": input_shape}
  for _ in range(n_hidden):
    model.add(keras.layers.Dense(n_neurons, activation="relu"))
    #options = {}
  model.add(keras.layers.Dense(1))
  optimizer = keras.optimizers.SGD(learning_rate=learning_rate)
  model.compile(loss="mse", optimizer=optimizer)
  return model

keras_reg = KerasRegressor(model = build_model)
housing_data = fetch_california_housing()
X_train_full, X_test, y_train_full, y_test = train_test_split(housing_data.data, housing_data.target)
X_train, X_valid, y_train, y_valid = train_test_split(X_train_full, y_train_full, random_state=42, test_size=0.2)

# Scaling training values
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_valid_scaled = scaler.transform(X_valid)
X_test_scaled = scaler.transform(X_test)

from scipy.stats import reciprocal
import numpy as np
from sklearn.model_selection import RandomizedSearchCV, GridSearchCV
params_distrib = {
                "model__n_hidden": [0,1,2,3],
                "model__n_neurons":np.arange(1,100),
                "model__learning_rate":reciprocal(3e-4,3e-2),
}
rnd_search_cv = RandomizedSearchCV(keras_reg, params_distrib, n_iter=10, cv=3, n_jobs=-1, verbose=1)
rnd_search_cv.fit(X_train_scaled, y_train, epochs=100,
                  validation_data=(X_valid_scaled, y_valid),
                  callbacks=[keras.callbacks.EarlyStopping(patience=10)])

I get the following error

AttributeError                            Traceback (most recent call last)
/tmp/ipython-input-1778148806.py in <cell line: 0>()
      8 }
      9 rnd_search_cv = RandomizedSearchCV(keras_reg, params_distrib, n_iter=10, cv=3, n_jobs=-1, verbose=1)
---> 10 rnd_search_cv.fit(X_train_scaled, y_train, epochs=100,
     11                   validation_data=(X_valid_scaled, y_valid),
     12                   callbacks=[keras.callbacks.EarlyStopping(patience=10)])
....

AttributeError: 'super' object has no attribute '__sklearn_tags__'

I tried to run this in the AI Tools and every ai tools gave different opinions and solutions. But everytime I end up in the same error.

So for as I understand, the sklearn.wrappers is depriciated with the new TF updates.
How do you solve this Cross validation issues ?

Hi @Desert_Giant, The error is likely due to Scikit-learn version 1.6 API changes. I successfully ran the code by downgrading to scikit-learn version 1.5.2. So, To resolve the issue try to uninstall Scikit-learn and reinstall scikit-learn==1.5.2. Kindly, refer to this gist file. Thank you!