Python 神经网络网格搜索超参数(Keras)

Python 神经网络网格搜索超参数(Keras),python,tensorflow,machine-learning,keras,hyperparameters,Python,Tensorflow,Machine Learning,Keras,Hyperparameters,我试图用Keras和SKlearn优化我的神经网络的超参数,我用Keras回归器结束我的模型,因为这是一个回归问题。我正在尝试优化各种参数: 隐藏层数 每个隐藏层的神经元数 优化器 纪元数和批次大小 激活函数 我可以单独执行此操作,但我想尝试同时运行所有内容和所有参数:)以查看是否有可能进行全面比较,但我一直遇到以下错误 错误 在这里你可以在下面找到我的代码 def build_regressor(optimizer='adam', activation = 'relu', hidden_lay

我试图用Keras和SKlearn优化我的神经网络的超参数,我用Keras回归器结束我的模型,因为这是一个回归问题。我正在尝试优化各种参数:

  • 隐藏层数
  • 每个隐藏层的神经元数
  • 优化器
  • 纪元数和批次大小
  • 激活函数
  • 我可以单独执行此操作,但我想尝试同时运行所有内容和所有参数:)以查看是否有可能进行全面比较,但我一直遇到以下错误

    错误 在这里你可以在下面找到我的代码

    def build_regressor(optimizer='adam', activation = 'relu', hidden_layers=1, neurons=1):
      # Initialize the constructor
        regressor = Sequential()
      # Add an input layer
        regressor.add(Dense(neurons, activation=activation, input_shape = (x_train.shape[1],)))
    
        for i in range(hidden_layers):
          # Add one hidden layer
            regressor.add(Dense(neurons, activation=activation))
    
      # Add an output layer 
        regressor.add(Dense(1, activation=activation))
      #compile model
        regressor.compile(loss='mean_squared_error', optimizer= optimizer, metrics= ['mse','mae'],  
                          epochs = epochs, batch_size = batch_size)
        return model
    
    #Wrap Model
    regressor = KerasRegressor(build_fn=build_regressor, verbose=1)
    
    # define the grid search parameters
    batch_size = [10, 20, 40, 60, 80, 100, 150]
    epochs = [10, 50, 100, 150, 200]
    neurons = [1, 32, 64, 128, 256, 512]
    optimizer = ['SGD', 'adam']
    #activation = ['relu', 'linear', 'softmax']
    
    param_grid = dict(batch_size = batch_size, epochs = epochs, neurons = neurons, optimizer = optimizer)
    
    #implement grid_search
    grid = GridSearchCV(estimator=regressor, param_grid=param_grid, n_jobs=-1, cv=3, scoring = 'r2')
    grid_result = grid.fit(x_train, y_train)
    
    #Fit Model
    results=regressor.fit(x_train,y_train)
    y_pred= regressor.predict(x_valid)
    
    # summarize results
    print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
    means = grid_result.cv_results_['mean_test_score']
    stds = grid_result.cv_results_['std_test_score']
    params = grid_result.cv_results_['params']
    for mean, stdev, param in zip(means, stds, params):
        print("%f (%f) with: %r" % (mean, stdev, param))  
    
    代码
    def build_regressor(optimizer='adam', activation = 'relu', hidden_layers=1, neurons=1):
      # Initialize the constructor
        regressor = Sequential()
      # Add an input layer
        regressor.add(Dense(neurons, activation=activation, input_shape = (x_train.shape[1],)))
    
        for i in range(hidden_layers):
          # Add one hidden layer
            regressor.add(Dense(neurons, activation=activation))
    
      # Add an output layer 
        regressor.add(Dense(1, activation=activation))
      #compile model
        regressor.compile(loss='mean_squared_error', optimizer= optimizer, metrics= ['mse','mae'],  
                          epochs = epochs, batch_size = batch_size)
        return model
    
    #Wrap Model
    regressor = KerasRegressor(build_fn=build_regressor, verbose=1)
    
    # define the grid search parameters
    batch_size = [10, 20, 40, 60, 80, 100, 150]
    epochs = [10, 50, 100, 150, 200]
    neurons = [1, 32, 64, 128, 256, 512]
    optimizer = ['SGD', 'adam']
    #activation = ['relu', 'linear', 'softmax']
    
    param_grid = dict(batch_size = batch_size, epochs = epochs, neurons = neurons, optimizer = optimizer)
    
    #implement grid_search
    grid = GridSearchCV(estimator=regressor, param_grid=param_grid, n_jobs=-1, cv=3, scoring = 'r2')
    grid_result = grid.fit(x_train, y_train)
    
    #Fit Model
    results=regressor.fit(x_train,y_train)
    y_pred= regressor.predict(x_valid)
    
    # summarize results
    print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
    means = grid_result.cv_results_['mean_test_score']
    stds = grid_result.cv_results_['std_test_score']
    params = grid_result.cv_results_['params']
    for mean, stdev, param in zip(means, stds, params):
        print("%f (%f) with: %r" % (mean, stdev, param))