Python 神经网络的GridSearchCV超参数整定

Python 神经网络的GridSearchCV超参数整定,python,tensorflow,keras,hyperparameters,gridsearchcv,Python,Tensorflow,Keras,Hyperparameters,Gridsearchcv,我正在尝试使用人工神经网络的GridSearchCV执行超参数调整。然而,我无法找出我下面的脚本有什么问题。它给了我以下错误: compile(优化器='adam',loss='mean_squared_error') ^ SyntaxError:无效语法 # Use scikit-learn to grid search the number of neurons from sklearn.model_selection import GridSearchCV from keras.model

我正在尝试使用人工神经网络的GridSearchCV执行超参数调整。然而,我无法找出我下面的脚本有什么问题。它给了我以下错误: compile(优化器='adam',loss='mean_squared_error') ^ SyntaxError:无效语法

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

非常感谢,我真的需要帮助。

你错过了一个括号ann.add(稠密(单位=1)==>ann.add(稠密(单位=1))你说得对。非常感谢你,马可,你是MVP!