Hyper-parameter Tuning Using GridSearchCV for Neural Network

1.6k views Asked by At

I am trying to perform hyper-parameter tuning using GridSearchCV for Artificial Neural Network. However, I cannot figure out what is wrong with my script below. It gives me the following error: ann.compile(optimizer = 'adam', loss = 'mean_squared_error') ^ SyntaxError: invalid syntax

# Use scikit-learn to grid search the number of neurons
from sklearn.model_selection import GridSearchCV
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.wrappers.scikit_learn import KerasRegressor
from keras.constraints import maxnorm
# Function to create model, required for KerasClassifier
def create_model(neurons=1, activation='relu'):
    # create model
    ann = Sequential()
    ann.add(Dense(units = neurons, activation = activation))
    ann.add(Dense(units = 1)
    # Compile model
    ann.compile(optimizer = 'adam', loss = 'mean_squared_error')
    return ann
# fix random seed for reproducibility
np.random.seed(0)
# create model
ann = KerasRegressor(build_fn = create_model, epochs = 100, batch_size = 10, verbose = 0)
# define the grid search parameters
parameters = {'neurons': [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60], 'activation': ['softmax', 'softplus', 'softsign', 'relu', 'tanh', 'sigmoid', 'hard_sigmoid', 'linear']}
grid = GridSearchCV(estimator = ann, param_grid = parameters, n_jobs = -1, cv=3)
grid_result = grid.fit(X1_train, y1_train)
# 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))

Thanks so much, I really need the help.

1

There are 1 answers

0
Paulo Fappi On

The error is in this function, follow below below

# Function to create model, required for KerasClassifier
def create_model(neurons=1, activation='relu'):
    # create model
    ann = Sequential()
    ann.add(Dense(units = neurons, activation = activation))
    ann.add(Dense(units = 1))
    # Compile model
    ann.compile(optimizer = 'adam', loss ='mean_squared_error')
    return ann