AttributeError: 'KerasClassifier' object has no attribute 'add'

AttributeError: 'KerasClassifier' object has no attribute 'add'

我正在尝试进行网格搜索,但我遇到无法解决的错误代码:AttributeError: 'KerasClassifier' object has no attribute 'add'。有什么想法吗?

这是我的导入:

import numpy as np
import pandas as pd

from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import GridSearchCV
from keras.models import Sequential
from keras.layers import Dense,LSTM,Dropout
from keras.wrappers.scikit_learn import KerasClassifier

然后我创建我的函数create_model:

#Create the model
def create_model(neurons=1):
    # create model
    # add 1st layer with 50 nodes/perceptons, each will process (2,6) matrix block
    model.add(LSTM(neurons, input_shape=(2, 6), return_sequences=True))
    model.add(Dropout(0.2))
    model.add(LSTM(50, return_sequences=False))
    model.add(Dropout(0.2))
    model.add(Dense(6))
    #Compile model
    model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])
    return model

创建我的函数后,我调用 KerasClassifier:

X_train=train
y_train=label
# create model
model = KerasClassifier(build_fn=create_model, epochs=100, batch_size=10, verbose=0)
# define the grid search parameters
neurons = [1, 5, 10, 15, 20, 25, 30]
param_grid = dict(neurons=neurons)
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1, cv=3)
grid_result = grid.fit(X_train, y_train)

是的。您忘记在 create_model():

的顶部添加 model=Sequential()