简体   繁体   English

使用 GridSearchCV 进行神经网络的超参数调优

[英]Hyper-parameter Tuning Using GridSearchCV for Neural Network

I am trying to perform hyper-parameter tuning using GridSearchCV for Artificial Neural Network.我正在尝试使用 GridSearchCV 进行人工神经网络的超参数调整。 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它给了我以下错误: 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.非常感谢,我真的需要帮助。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用前馈神经网络进行超参数调整和过拟合 - Mini-Batch Epoch 和交叉验证 - Hyper-parameter tuning and Over-fitting with Feed-Forward Neural Network - Mini-Batch Epoch and Cross Validation 如何使用smac进行卷积神经网络的超参数优化? - How to use smac for hyper-parameter optimization of Convolution Neural Network? 如何使用 GridSearchCV 比较多个模型以及 python 中的管道和超参数调整 - How to use GridSearchCV for comparing multiple models along with pipeline and hyper-parameter tuning in python 机器学习的超参数调优 model - Hyper-parameter Tuning for a machine learning model 使用 Keras-tuner 进行超参数调整时关于“准确性”的错误 - Error regarding "accuracy" in hyper-parameter tuning using Keras-tuner 在超参数调整期间,简单参数是否也会更改 - Does the simple parameters also change during Hyper-parameter tuning Optuna 超参数优化:定义目标之外的超参数空间 function - Optuna hyper-parameter optimization: define hyper-parameter space outside the objective function 使用 GridSearchCV 调整超参数 - Hyperparameters tuning using GridSearchCV "Pytorch 使用当前损失更新超参数,RuntimeError: Trying backing through the graph a second time" - Pytorch update hyper-parameter using current loss, RuntimeError: Trying to backward through the graph a second time 使用 GridSearchCV 进行超参数调整 - hyperparameter tuning using GridSearchCV
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM