Open
Description
Adding GridSearch for epochs and other tune parameters
from tensorflow.keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import GridSearchCV
# Define a function to create a Keras model with variable hyperparameters
def create_model(optimizer='adam'):
model = tf.keras.models.Sequential([...])
model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'])
return model
# Wrap the model with KerasClassifier
model = KerasClassifier(build_fn=create_model, verbose=0)
# Define the grid of hyperparameters to search
param_grid = {'batch_size': [10, 20, 50],
'epochs': [10, 50, 100],
'optimizer': ['adam', 'rmsprop']}
# Perform GridSearchCV
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1, cv=3)
grid_result = grid.fit(X_train, y_train)
# Review the best parameters from the search
print(f"Best: {grid_result.best_score_} using {grid_result.best_params_}")
Metadata
Metadata
Assignees
Labels
No labels