python - 无法克隆对象 ' does not seem to be a scikit-learn estimator as it does not implement a ' get_params 的方法

标签 python keras scikit-learn gridsearchcv

我有:

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 )在网格搜索期间为每个参数配置创建估计器的单独副本。

您可以尝试使用wrapper for the 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 install scikeras并将上面的导入替换为:

from scikeras.wrappers import KerasRegressor

关于python - 无法克隆对象 ' does not seem to be a scikit-learn estimator as it does not implement a ' get_params 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74877835/

相关文章:

tensorflow - 损失、准确性、验证损失、验证准确性之间有什么区别?

pandas - vectorize_sequences 的标准实现

python - 检查 float 是否等于python中的整数值

python - 根据列标题添加新行

javascript - 将使用 Tensorflow 库的 Python 代码实现到 HTML 中?

tensorflow - “模型”对象没有属性 'load_model' keras

python - 系统在运行 python 脚本时卡住

python - 在Python中选择数据结构

python - 多变量高斯过程回归: adaptation of kernels

python - Pytorch 张量的截断 SVD 分解而不转移到 cpu