网格使用keras搜索隐藏层的数量 [英] Grid Search the number of hidden layers with keras
问题描述
我正在尝试使用Keras和sklearn优化NN的超参数. 我用KerasClassifier结束了(这是一个分类问题). 我正在尝试优化隐藏层的数量. 我无法弄清楚如何使用keras(实际上我想知道如何设置函数create_model以便最大化隐藏层的数量) 谁能帮我吗?
I am trying to optimize the hyperparameters of my NN using Keras and sklearn. I am wrapping up with KerasClassifier (it´s a classification problem). I am trying to optimize the number of hidden layers. I can´t figure it out how to do it with keras (actually I am wondering how to set up the function create_model in order to maximize the number of hidden layers) Could anyone please help me?
我的代码(只是重要的部分):
My code (just the important part):
## Import `Sequential` from `keras.models`
from keras.models import Sequential
# Import `Dense` from `keras.layers`
from keras.layers import Dense
def create_model(optimizer='adam', activation = 'sigmoid'):
# Initialize the constructor
model = Sequential()
# Add an input layer
model.add(Dense(5, activation=activation, input_shape=(5,)))
# Add one hidden layer
model.add(Dense(8, activation=activation))
# Add an output layer
model.add(Dense(1, activation=activation))
#compile model
model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=
['accuracy'])
return model
my_classifier = KerasClassifier(build_fn=create_model, verbose=0)# Create
hyperparameter space
epochs = [5, 10]
batches = [5, 10, 100]
optimizers = ['rmsprop', 'adam']
activation1 = ['relu','sigmoid']
# Create grid search
grid = RandomizedSearchCV(estimator=my_classifier,
param_distributions=hyperparameters) #inserir param_distributions
# Fit grid search
grid_result = grid.fit(X_train, y_train)
# Create hyperparameter options
hyperparameters = dict(optimizer=optimizers, epochs=epochs,
batch_size=batches, activation=activation1)
# View hyperparameters of best neural network
grid_result.best_params_
推荐答案
如果要将隐藏层数设为超参数,则必须将其作为参数添加到KerasClassifier
build_fn
中,例如:
If you want to make the number of hidden layers a hyperparameter you have to add it as parameter to your KerasClassifier
build_fn
like:
def create_model(optimizer='adam', activation = 'sigmoid', hidden_layers=1):
# Initialize the constructor
model = Sequential()
# Add an input layer
model.add(Dense(5, activation=activation, input_shape=(5,)))
for i in range(hidden_layers):
# Add one hidden layer
model.add(Dense(8, activation=activation))
# Add an output layer
model.add(Dense(1, activation=activation))
#compile model
model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=
['accuracy'])
return model
然后,您可以通过将其添加到字典中来优化隐藏层的数量,该字典将传递给RandomizedSearchCV
的param_distributions
.
Then you will be able to optimize the number of hidden layers by adding it to the dictionary, which is passed to RandomizedSearchCV
's param_distributions
.
还有一件事情,您可能应该将用于输出层的activation
与其他层分开.
激活函数的不同类别适用于隐藏层和二进制分类中使用的输出层.
One more thing, you probably should separate the activation
you use for the output layer from the other layers.
Different classes of activation functions are suitable for hidden layers and for output layers used in binary classification.
这篇关于网格使用keras搜索隐藏层的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!