无法克隆对象 ' 似乎不是一个 scikit-learn 估计器,因为它没有实现'get_params'方法



我有:

import numpy as np
import tensorflow as tf
from sklearn.preprocessing import minmax_scale
from sklearn.model_selection import GridSearchCV, KFold
# Extract the main and side frequency factors concentractions and catalyst concentrations from the data
main_freq_factors = np.array(FreqFac['Main frequency factors concentraction (l/(mol*s))'])
side_freq_factors = np.array(FreqFac['Side frequency factors concentraction (1/s)'])
catalyst_conc = np.array(FreqFac.iloc[:,1].values)
# Scale the data using the min-max scaler
main_freq_factors_scaled = minmax_scale(main_freq_factors)
side_freq_factors_scaled = minmax_scale(side_freq_factors)
catalyst_conc_scaled = minmax_scale(catalyst_conc)
# Combine the scaled data into a single array
X = catalyst_conc_scaled
y = np.column_stack((main_freq_factors_scaled, side_freq_factors_scaled))

X_train, X_test, y_train, y_test = train_test_split(Xsc, Ysc, test_size=0.33, random_state=42)
# Define the model
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(32, activation='relu', input_shape=(1,)))
model.add(tf.keras.layers.Dense(1, activation='linear'))
# Compile the model with the mean squared error loss function and Adam optimization algorithm
model.compile(loss='mean_squared_error', optimizer='adam')
# Define the grid of hyperparameters to search
param_grid = {
'epochs': [50, 100, 150],
'batch_size': [32, 64, 128]
}
# Create a K-fold cross-validation generator
kfold = KFold(n_splits=5, shuffle=True, random_state=42)
# Create a grid search object using the model, param_grid, and kfold
grid_search = GridSearchCV(model, param_grid, cv=kfold, scoring='neg_mean_squared_error', return_train_score=True)
# Fit the grid search object to the data
grid_search.fit(catalyst_conc.reshape(-1, 1), main_freq_factors)
# Print the results of the grid search
print(grid_search.best_params_)

但是我得到错误:

Cannot clone object '<keras.engine.sequential.Sequential object at 0x7eff87f72580>' (type <class 'keras.engine.sequential.Sequential'>): it does not seem to be a scikit-learn estimator as it does not implement a 'get_params' methods.

我做错了什么?

错误来自scikit的clone,GridSearchCV(通过BaseSearchCV)在网格搜索期间为每个参数配置创建一个单独的估计器副本。

您可以尝试使用Scikit-Learn API的包装器:

from keras.wrappers.scikit_learn import KerasRegressor
# ...
def build_fn():
# Define the model
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(32, activation='relu', input_shape=(1,)))
model.add(tf.keras.layers.Dense(1, activation='linear'))
# Compile the model with the mean squared error loss function and Adam optimization algorithm
model.compile(loss='mean_squared_error', optimizer='adam')
return model
model = KerasRegressor(build_fn)
# ...

另一个选项是pip installscikeras,并将上面的导入替换为:

from scikeras.wrappers import KerasRegressor

相关内容