使用KerasRegressor时遇AttributeError报错求助(TF2.15/Keras2.15)
问题:KerasRegressor超参数调优时触发AttributeError错误
我正在做学习练习,尝试用KerasRegressor为简单序列神经网络优化超参数,代码如下:
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV from scipy.stats import randint as sp_randint from keras.wrappers.scikit_learn import KerasRegressor from sklearn.metrics import mean_squared_error, make_scorer '''CREATE THE MODEL''' def design_model(features): model = Sequential(name = "My_Sequential_Model") model.add(InputLayer(input_shape=(features.shape[1],))) model.add(Dense(128, activation='relu')) model.add(Dense(1)) opt = Adam(learning_rate=0.01) model.compile(loss='mse', metrics=['mae'], optimizer=opt) return model '''TEST/PLOT THE MODEL: GRID SEARCH''' def do_grid_search(): batch_size = [6, 64] epochs = [10, 30, 61] model = KerasRegressor(build_fn=design_model, features=features_train) # KerasRegressor expects a function and not the model param_grid = dict(batch_size=batch_size, epochs=epochs) grid = GridSearchCV(estimator = model, param_grid=param_grid, scoring = make_scorer(mean_squared_error, greater_is_better=False),return_train_score = True) grid_result = grid.fit(features_train, labels_train, verbose = 0) print(grid_result) print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_)) means = grid_result.cv_results_['mean_test_score'] stds = grid_result.cv_results_['std_test_score'] params = grid_result.cv_results_['params'] for mean, stdev, param in zip(means, stds, params): print("%f (%f) with: %r" % (mean, stdev, param)) print("Traininig") means = grid_result.cv_results_['mean_train_score'] stds = grid_result.cv_results_['std_train_score'] for mean, stdev, param in zip(means, stds, params): print("%f (%f) with: %r" % (mean, stdev, param)) print("-------------- GRID SEARCH --------------------") do_grid_search()
运行后持续触发以下错误:
Traceback (most recent call last): File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\model_selection\_validation.py", line 732, in _fit_and_score estimator.fit(X_train, y_train, **fit_params) File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\wrappers\scikit_learn.py", line 167, in fit AttributeError: module 'keras.losses' has no attribute 'is_categorical_crossentropy'
使用环境:Tensorflow 2.15、Keras 2.15版本
解决方案
1. 替换KerasRegressor的导入来源
错误核心是keras.wrappers.scikit_learn下的旧版包装器调用了Keras 2.15已移除的APIkeras.losses.is_categorical_crossentropy,改用TensorFlow官方维护的包装器即可解决:
# 替换原来的导入 from tensorflow.keras.wrappers.scikit_learn import KerasRegressor
2. 补充缺失的Keras组件导入
原代码未导入核心模型组件,需补充:
from tensorflow.keras.models import Sequential from tensorflow.keras.layers import InputLayer, Dense from tensorflow.keras.optimizers import Adam
3. 修正模型构建函数的参数传递
原代码直接传入训练特征的方式不符合KerasRegressor规范,改为传递输入维度参数:
def design_model(input_dim): model = Sequential(name="My_Sequential_Model") model.add(InputLayer(input_shape=(input_dim,))) model.add(Dense(128, activation='relu')) model.add(Dense(1)) opt = Adam(learning_rate=0.01) model.compile(loss='mse', metrics=['mae'], optimizer=opt) return model
4. 调整KerasRegressor的初始化方式
创建KerasRegressor时,传递输入维度而非原始训练数据:
model = KerasRegressor(build_fn=design_model, input_dim=features_train.shape[1])
修改后的完整代码
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV from scipy.stats import randint as sp_randint from tensorflow.keras.wrappers.scikit_learn import KerasRegressor from sklearn.metrics import mean_squared_error, make_scorer from tensorflow.keras.models import Sequential from tensorflow.keras.layers import InputLayer, Dense from tensorflow.keras.optimizers import Adam '''CREATE THE MODEL''' def design_model(input_dim): model = Sequential(name="My_Sequential_Model") model.add(InputLayer(input_shape=(input_dim,))) model.add(Dense(128, activation='relu')) model.add(Dense(1)) opt = Adam(learning_rate=0.01) model.compile(loss='mse', metrics=['mae'], optimizer=opt) return model '''TEST/PLOT THE MODEL: GRID SEARCH''' def do_grid_search(): batch_size = [6, 64] epochs = [10, 30, 61] model = KerasRegressor(build_fn=design_model, input_dim=features_train.shape[1]) param_grid = dict(batch_size=batch_size, epochs=epochs) # 可简化scoring为sklearn内置的neg_mean_squared_error grid = GridSearchCV(estimator=model, param_grid=param_grid, scoring='neg_mean_squared_error', return_train_score=True) grid_result = grid.fit(features_train, labels_train, verbose=0) print(grid_result) print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_)) means = grid_result.cv_results_['mean_test_score'] stds = grid_result.cv_results_['std_test_score'] params = grid_result.cv_results_['params'] for mean, stdev, param in zip(means, stds, params): print("%f (%f) with: %r" % (mean, stdev, param)) print("Training") means = grid_result.cv_results_['mean_train_score'] stds = grid_result.cv_results_['std_train_score'] for mean, stdev, param in zip(means, stds, params): print("%f (%f) with: %r" % (mean, stdev, param)) print("-------------- GRID SEARCH --------------------") do_grid_search()
内容的提问来源于stack exchange,提问作者Code Monkey
相关产品推荐
相关产品推荐

