温馨提示×

如何使用Keras进行超参数调优

小樊
87
2024-04-23 14:04:48
栏目: 深度学习

在Keras中进行超参数调优通常使用GridSearchCV或RandomizedSearchCV来完成。以下是一个使用GridSearchCV进行超参数调优的示例:

  1. 定义模型和参数网格
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import GridSearchCV

def create_model(optimizer='adam', activation='relu'):
    model = Sequential()
    model.add(Dense(units=64, activation=activation, input_shape=(X_train.shape[1],)))
    model.add(Dense(units=1, activation='sigmoid'))
    model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'])
    return model

model = KerasClassifier(build_fn=create_model, epochs=5, batch_size=32)
param_grid = {'optimizer': ['adam', 'sgd'],
              'activation': ['relu', 'tanh']}
  1. 使用GridSearchCV进行超参数调优
grid = GridSearchCV(estimator=model, param_grid=param_grid, cv=3)
grid_result = grid.fit(X_train, y_train)

print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
  1. 获取最佳模型和参数
best_model = grid_result.best_estimator_
best_params = grid_result.best_params_

通过这种方法,您可以使用GridSearchCV来搜索最佳的超参数组合,以优化模型的性能。您还可以尝试使用RandomizedSearchCV来进行随机搜索超参数调优。

0