我正在使用tensorflow和keras构建一个简单的 MNIST 分类模型,我想微调我的模型,所以我选择了sklearn.model_selection.GridSearchCV.
但是,当我调用该fit函数时,它说:
AttributeError: 'Sequential' object has no attribute 'loss'
我将我的代码与其他人的代码进行了比较,但仍然无法弄清楚原因。唯一的区别是我使用tensorflow.keras而不是keras.
这是我的代码:
从 tensorflow.keras.models 导入顺序,模型
从 tensorflow.keras.layers 导入 Input、Dense、Activation、Dropout、BatchNormalization
从 tensorflow.keras.datasets 导入 mnist
从 tensorflow.keras.wrappers.scikit_learn 导入 KerasClassifier
从 sklearn.model_selection 导入 GridSearchCV
...
...
...
def get_model(dropout_rate=0.2, hidden_units=512):
模型 = 顺序()
模型.添加(辍学(辍学率,输入形状=(28 * 28,)))
model.add(Dense(hidden_units, activation='relu'))
model.add(BatchNormalization())
模型.添加(辍学(辍学率))
model.add(Dense(hidden_units, activation='relu'))
model.add(BatchNormalization())
模型.添加(辍学(辍学率))
model.add(Dense(hidden_units, activation='relu'))
model.add(BatchNormalization())
模型.添加(辍学(辍学率))
模型.添加(密集(10,激活=“softmax”))
回报模式
模型 = KerasClassifier(build_fn=get_model,batch_size=128,epochs=10)
para_dict = {'dropout_rate':[0.2,0.5,0.8],'hidden_units':[128,256,512,1024]}
clf = GridSearchCV(model, para_dict, …