调整Keras中的隐藏层数

时间:2019-05-14 08:22:36

标签: python keras scikit-learn mnist

我只是想用著名的MNIST数据集探索keras和tensorflow。 我已经应用了一些基本的神经网络,但是在调整一些超参数,尤其是层数时,由于sklearn包装器GridSearchCV,我得到了以下错误:

Parameter values for parameter (hidden_layers) need to be a sequence(but not a string) or np.ndarray.

所以您可以更好地查看我发布的代码的主要部分。

数据准备

# Extract label
X_train=train.drop(labels = ["label"],axis = 1,inplace=False)
Y_train=train['label']
del train

# Reshape to fit MLP
X_train = X_train.values.reshape(X_train.shape[0],784).astype('float32')
X_train = X_train / 255

# Label format
from keras.utils import np_utils
Y_train = keras.utils.to_categorical(Y_train, num_classes = 10)
num_classes = Y_train.shape[1]

Keras部分

from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import GridSearchCV

# Function with hyperparameters to optimize
def create_model(optimizer='adam', activation = 'sigmoid', hidden_layers=2):
  # Initialize the constructor
    model = Sequential()
      # Add an input layer
    model.add(Dense(32, activation=activation, input_shape=784))

    for i in range(hidden_layers):
        # Add one hidden layer
        model.add(Dense(16, activation=activation))

      # Add an output layer 
    model.add(Dense(num_classes, activation='softmax'))
      #compile model
    model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=
     ['accuracy'])
    return model

# Model which will be the input for the GridSearchCV function
modelCV = KerasClassifier(build_fn=create_model, verbose=0)

GridSearchCV

from keras.activations import relu, sigmoid
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.layers import Dropout
from keras.utils import np_utils

activations = [sigmoid, relu]
param_grid = dict(hidden_layers=3,activation=activations, batch_size = [256], epochs=[30])
grid = GridSearchCV(estimator=modelCV, param_grid=param_grid, scoring='accuracy')
grid_result = grid.fit(X_train, Y_train)

我只想让您知道Grid Search the number of hidden layers with keras这里已经提出过同样的问题,但答案根本不完整,我无法添加评论以答复答覆者。

谢谢!

2 个答案:

答案 0 :(得分:1)

您应该添加:

for i in range(int(hidden_layers)):
    # Add one hidden layer
    model.add(Dense(16, activation=activation))

尝试将 param_grid 的值添加为列表:

params_grid={"hidden_layers": [3]}

答案 1 :(得分:0)

设置参数隐藏层= 2时,它作为字符串出现,因此引发错误。

理想情况下,应该按顺序运行代码,这就是您错误提示的内容