我正在一些图像上训练我的CNN模型,并希望为超参数优化添加随机搜索,但是在使用超参数的随机搜索时遇到问题。我正在分享我的模型以及我遇到的一些代码和错误。
我尝试过 sklearn 文档示例和其他文章,但没有从那里得到任何帮助。还尝试了网格搜索,但遇到了同样的问题
def Model(Classes):
activation='relu' # or linear
dropout_rate=0.0 # or 0.2
init_mode='uniform'
weight_constraint=0 # or 4
optimizer='adam' # or SGD
lr = 0.01
momemntum=0
#model
model=Sequential()
model.add(Conv2D(20,(2,2),padding="same"))
model.add(Activation(activation))
model.add(Conv2D(20,(2,2),padding="same"))
model.add(Activation(activation))
model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
model.add(Conv2D(80,(5,5),padding="same"))
model.add(Activation(activation))
model.add(Conv2D(80,(5,5),padding="same"))
model.add(Activation(activation))
model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
model.add(Conv2D(100,(5,5),padding="same"))
model.add(Activation(activation))
model.add(Conv2D(100,(5,5),padding="same"))
model.add(Activation(activation))
model.add(MaxPooling2D(pool_size=(5,5),strides=(2,2)))
model.add(Dropout(dropout_rate))
model.add(Flatten())
model.add(Dense(units=64,activation=activation,
kernel_regularizer=regularizers.l2(0.001),
activity_regularizer=regularizers.l1(0.001)))
model.add(Dense(units=128,activation=activation,
kernel_regularizer=regularizers.l2(0.001),
activity_regularizer=regularizers.l1(0.01)))
model.add(Dense(units=Classes,activation=activation))
model.compile(loss=losses.binary_crossentropy,
optimizer=optimizer,
metrics=['accuracy'])
return model
model=Model(2)
activation = ['relu', 'tanh', 'sigmoid', 'hard_sigmoid', 'linear']
optimizer=['adam','adagrad']
epochs = [1, 10] # add 50, 100, 150 etc
batch_size = [1000, 5000] # add 5, 10, 20, 40, 60, 80, 100 etc
param_grid = dict(epochs=epochs, batch_size=batch_size)
#param_grid=dict(epochs=epochs, activation=activation ,
optimizer=optimizer)
random_search = RandomizedSearchCV(model, param_distributions=param_grid,
n_iter=10, cv=5,scoring='accuracy')
random_search.fit(X_train, y_train)
report(random_search.cv_results_)
错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-96-b29b0e381ee4> in <module>()
12
13 ---> 14 random_search.fit(X_train, y_train)
15
16 report(random_search.cv_results_)
/opt/conda/lib/python3.6/site-
packages/sklearn/model_selection/_search.py in fit(self, X, y,
groups, **fit_params)
675 n_splits = cv.get_n_splits(X, y, groups)
676
-> 677 base_estimator = clone(self.estimator)
678
679 parallel = Parallel(n_jobs=self.n_jobs,
verbose=self.verbose,
/opt/conda/lib/python3.6/site-packages/sklearn/base.py in
clone(estimator, safe)
56 "it does not seem to be a scikit-learn
estimator "
57 "as it does not implement a
'get_params' methods."
---> 58 % (repr(estimator),
type(estimator)))
59 klass = estimator.__class__
60 new_object_params = estimator.get_params(deep=False)
TypeError: Cannot clone object '<keras.engine.sequential.Sequential
object at 0x7fc268d8abe0>' (type <class
'keras.engine.sequential.Sequential'>): it does not seem to be a
scikit-learn estimator as it does not implement a 'get_params'
methods.
请帮助我解决这个问题
您需要像下面这样包装模型。
from keras.wrappers.scikit_learn import KerasClassifier
k_model = KerasClassifier(model, verbose=0)
random_search = RandomizedSearchCV(k_model, param_distributions=param_grid,
n_iter=10, cv=5,scoring='accuracy')
您可能想看看这个可重现的示例。