我最近剛接觸lstm不久,想用GridSearchCV尋找超參數時都會出現像錯誤訊息的相關狀況,想請求詢問,謝謝!!!
from sklearn.model_selection import GridSearchCV
from keras.models import Sequential
from keras.layers import LSTM, Dense
from scikeras.wrappers import KerasRegressor
#定義LSTM模型
def create_model(neurons=1, activation='sigmoid', optimizer='adam', batch_size=32):
model = Sequential()
model.add(LSTM(neurons, activation=activation, input_shape=(8, 16)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer=optimizer, batch_size=batch_size)
return model
#創建KerasRegressor對象,傳遞建模函數
model = KerasRegressor(build_fn=create_model, verbose=0)
#定義激活函數、優化器和批量大小列表
activation = ['sigmoid', 'relu']
optimizer = ['adam', 'rmsprop']
batch_size = [16, 32, 64]
param_grid = dict(activation=activation, optimizer=optimizer, batch_size=batch_size)
#使用GridSearchCV進行參數搜索
grid = GridSearchCV(estimator=model, param_grid=param_grid, scoring='neg_mean_squared_error', cv=3)
grid_result = grid.fit(trainX, trainY)
#輸出最優參數和最優模型
print('Best: %f using %s' % (grid_result.best_score_, grid_result.best_params_))
錯誤訊息
ValueError: Invalid parameter activation for estimator KerasRegressor.
This issue can likely be resolved by setting this parameter in the KerasRegressor constructor:KerasRegressor(activation=sigmoid)
Check the list of available parameters with estimator.get_params().keys()
scikeras.wrappers.KerasRegressor 不含 activation 參數,請參閱官方文件:
https://adriangb.com/scikeras/stable/generated/scikeras.wrappers.KerasRegressor.html