我已经用keras构建了ANN,并使用GridSearchCV,以便尝试不同的参数并查看哪种组合会给我最好的结果。
我想知道,我们在Dense-module的图层中定义的所有参数是否都可以在GridSearchCV中进行网格化?我对三个参数“ batch_size”,“ epochs”和“ optimizer”有把握。但是我尝试了激活功能,两天后我的计算机没有完成网格搜索!所以我停止了它,现在我正在寻找有关此问题的答案。
最后几行中的代码如下:
def classifier_builder(optimizer):
classifier = Sequential()
classifier.add(Dense(units = 8, kernel_initializer = 'uniform',
activation = 'relu', input_shape =(11,)))
classifier.add(Dense(units = 8, kernel_initializer = 'uniform',
activation = 'relu'))
classifier.add(Dense(units = 1, kernel_initializer = 'uniform',
activation = 'sigmoid'))
classifier.compile(optimizer = optimizer,
loss = 'binary_crossentropy', metrics = ['accuracy'])
return classifier
classifier = KerasClassifier(build_fn = classifier_builder)
parameters = {"batch_size":[25, 32, 50],
'epochs':[100, 500],
'optimizer':['adam', 'rmsprop']}
grid_search = GridSearchCV(estimator = classifier,
param_grid = parameters,
scoring = 'accuracy',
cv = 10)
grid_search = grid_search.fit(X_train, y_train)
所以我的问题是:是否可以像我上面对batch_size和epochs那样提供不同的激活函数和内核初始化程序?还是在各种激活函数之间进行更改不是一个好主意,因为它每次都会更改神经网络的结构?例如这样的
def classifier_builder(optimizer,activ_func1,activ_func2):
classifier = Sequential()
classifier.add(Dense(units = 8, kernel_initializer = 'uniform',
activation = activ_func1, input_shape =(11,)))
classifier.add(Dense(units = 8, kernel_initializer = 'uniform',
activation = activ_func1))
classifier.add(Dense(units = 1, kernel_initializer = 'uniform',
activation = activ_func2))
classifier.compile(optimizer = optimizer,
loss = 'binary_crossentropy', metrics = ['accuracy'])
return classifier
classifier = KerasClassifier(build_fn = classifier_builder)
parameters = {"batch_size":[25, 32, 50],
'epochs':[100, 500],
'optimizer':['adam', 'rmsprop']
'activ_func1':['relu','elu'],
'activ_func2':['hard_sigmoid','sigmoid','softplus']}
#and the rest of the code is the same
非常感谢:)