随机搜索获取参数未实现
Posted
技术标签:
【中文标题】随机搜索获取参数未实现【英文标题】:Randomized Search Get param not implemented 【发布时间】:2019-06-19 08:19:19 【问题描述】:我正在一些图像上训练我的 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.
请帮我解决这个问题
【问题讨论】:
【参考方案1】:你需要像下面这样包装你的模型。
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')
你可能想看看this reproducible example。
【讨论】:
以上是关于随机搜索获取参数未实现的主要内容,如果未能解决你的问题,请参考以下文章
模板未显示通过传递参数使用视图函数从 sqlite 获取的数据(为空白)
为啥从函数返回数组作为参数时,我会从函数中的数组中获取随机值? [复制]