How to add cross validation to Optuna function to tune hyperparameters for LSTM?

85 views Asked by At

I have code to tune hyperparameters in LSTM. How can I:

  1. add cross validation based on 5 folds on training dataset
  2. print avg AUC from each iteration from training dataset divided on 5 folds
  3. print AUC from test dataset (of course do not divide test dataset on folds):
def objective(trial):
    start_time = time.time()
    model = create_model(trial)
    history = model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=15, verbose=0)

    y_pred = model.predict(X_test)
    auc = roc_auc_score(y_test, y_pred)
    
    end_time = time.time()
    elapsed_time = end_time - start_time

    print("iteration no:", trial.number)
    print("AUC:", auc)
    print("hyperparameters:", trial.params)
    print("time:", elapsed_time, "sec")

    return auc 

How can I do that in Python?

0

There are 0 answers