Grid Search Hyperparameters for Neural Network (Keras)

662 views Asked by At

I'm trying to optimize the hyperparameters of my neural network using both Keras and SKlearn, I am wrapping up my model with a KerasRegressor as this is a regression problem. I am trying to optimize various parameters:

  1. Number of Hidden Layers
  2. Number of Neurons per Hidden Layer
  3. Optimizer
  4. Number of Epochs and Batch_Size
  5. Activation Function

I can do this separately, but I wanted to try to run everything and all parameters at the same time :) to see if it would be possible to get an all out comparison but I keep getting the following error.

Error

PicklingError: Could not pickle the task to send it to the workers.

Here you can find my code below

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))  

Code

0

There are 0 answers