python 没有名为“keras.wrappers”的模块

7hiiyaii  于 2023-10-15  发布在  Python
关注(0)|答案(2)|浏览(841)

我在google colab上有一段代码,它允许我使用gridsearchCV优化LSTM模型,但最近出现了一条错误消息:
未找到模块错误:没有名为“keras. wrappers”的模块。
除了“keras.wrappers”之外,是否还有其他模块允许重新启动代码?
代码:

from keras.layers import Dense, LSTM, Dropout
from keras import optimizers

from sklearn.model_selection import GridSearchCV
from keras.wrappers.scikit_learn import KerasRegressor

def create_model(unit, dropout_rate, lr ):
 model=Sequential()
 model.add(LSTM(unit,return_sequences=True, input_shape=(1,5)))
 model.add(Dropout(dropout_rate))
 model.add(LSTM(unit))
 model.add(Dropout(dropout_rate))
 model.add(Dense(1))
 adam= optimizers.Adam(lr)
 model.compile(optimizer=adam, loss='mean_squared_error')

 return model

my_regressor = KerasRegressor(build_fn=create_model, verbose=2)

grid_param_LSTM = {
    'unit':   [50, 70, 120],
    'batch_size': [12, 24, 48],
    'epochs': [200],
    'lr': [0.001, 0.01, 0.1],
    'dropout_rate':[0.1, 0.2, 0.3]
}

grid_GBR = GridSearchCV(estimator=my_regressor, param_grid = grid_param_LSTM, scoring = 'neg_root_mean_squared_error',  cv = 2)
grid_GBR.fit(X_train, y_train)

print("Best: %f using %s" % (grid_GBR.best_score_, grid_GBR.best_params_))
ybzsozfc

ybzsozfc1#

这对我很有用

pip install keras==2.12.0

你可以尝试的另一种方法

pip uninstall tensorflow
pip install tensorflow==2.12.0
8hhllhi2

8hhllhi22#

我也遇到过同样的问题,我解决了上面的导入指令安装scikeras有KerasClassifier

https://adriangb.com/scikeras/stable/migration.html

相关问题