如何对 Conv2D 和 MaxPooling2D 进行超调?下面的代码使用的是 ANN 模型,我们可以使用相同格式的代码来优化 CNN 吗?#A. 首先调整 Batch Size 和 Epoch#A.1 构建 CNN 模型...
如何对 Conv2D 和 MaxPooling2D 进行超调?
下面的代码使用了ANN模型,我们可以使用相同格式的代码来优化CNN吗?
#A. Tune First the Batch Size and Epoch
#A.1 Build the CNN Model for the Optimization Process
def classifier_optimization():
classifier_optimization = Sequential()
classifier_optimization.add(Dense(units = 7, kernel_initializer = "glorot_uniform", activation = "relu", input_dim = 12))
classifier_optimization.add(Dropout(rate = 0.1))
classifier_optimization.add(Dense(units = 6, kernel_initializer = "glorot_uniform", activation = "relu"))
classifier_optimization.add(Dropout(rate = 0.1))
classifier_optimization.add(Dense(units = 1, kernel_initializer = "glorot_uniform", activation = "sigmoid"))
classifier_optimization.compile(optimizer = "sgd", loss = "binary_crossentropy", metrics = ["accuracy"])
return classifier_optimization
ann_model_optimization = KerasClassifier(model=classifier_optimization, verbose = 0)
#A.2 Import the GridSearchCV class
from sklearn.model_selection import GridSearchCV
#A.3 To Set the Parameters to be optimized for the ANN Model
parameters = {"batch_size" : [50, 100, 150, 200, 250],
"epochs" : [10, 50, 100, 200, 250]}
grid_search = GridSearchCV(estimator = ann_model_optimization, param_grid = parameters, scoring = "accuracy", cv = k_fold, n_jobs = -1)
grid_search = grid_search.fit(X_standard, Y)
print(grid_search)
#A.4 To View the result of the GridSearchCV
results = pd.DataFrame(grid_search.cv_results_) [["mean_test_score" , "std_test_score", "params"]]
print(results)
#A.5 To identify the best accuracy and the best features
best_parameters = grid_search.best_params_
best_accuracy = grid_search.best_score_
print("Best Accuracy Score:", best_accuracy)
print("Best Parameters:", best_parameters)
我们尝试分别优化 Sequential、Flatten 和 Dense。你能帮忙优化 Conv2D 和 MaxPooling2D 吗