Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 网格使用keras搜索隐藏层的数量_Python_Neural Network_Keras_Hyperparameters - Fatal编程技术网

Python 网格使用keras搜索隐藏层的数量

Python 网格使用keras搜索隐藏层的数量,python,neural-network,keras,hyperparameters,Python,Neural Network,Keras,Hyperparameters,我正在尝试使用Keras和sklearn优化我的神经网络的超参数。 我正在使用KerasClassifier(这是一个分类问题)。 我试图优化隐藏层的数量。 我不知道如何使用keras(实际上我想知道如何设置函数create_model以最大化隐藏层的数量) 谁能帮帮我吗 我的代码(只是重要部分): 如果要使隐藏层的数量成为超参数,则必须将其作为参数添加到KerasClassifierbuild\u fn中,如: def create_model(optimizer='adam', activa

我正在尝试使用Keras和sklearn优化我的神经网络的超参数。 我正在使用KerasClassifier(这是一个分类问题)。 我试图优化隐藏层的数量。 我不知道如何使用keras(实际上我想知道如何设置函数create_model以最大化隐藏层的数量) 谁能帮帮我吗

我的代码(只是重要部分):


如果要使隐藏层的数量成为超参数,则必须将其作为参数添加到
KerasClassifier
build\u fn
中,如:

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_分布中

还有一件事,您可能应该将用于输出层的
激活
与其他层分开。
不同类别的激活函数适用于二元分类中使用的隐藏层和输出层。

Keras模型在这方面是静态的。您必须自己创建许多模型,看看哪一个性能更好。
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