Skip to content

create_tuner_stats

airt.keras.experiments.create_tuner_stats(tuner: Tuner, *, num_models: int = 10, max_epochs: int = 50, batch_size: int = 8, patience: int = 10, verbose: int = 0) -> pd.DataFrame ยค

Calculates statistics for the best models found by Keras Tuner

Parameters:

Name Type Description Default
tuner Tuner

an instance of Keras Tuner

required
num_models int

number of best models to use for calculating statistics

10
max_epochs int

maximum number of epochs used in runs

50
batch_size int

batch_size

8
patience int

maximum number of epochs with worse objective before stopping trial early

10
verbose int

verbosity level of Model.fit function

0

Returns:

Type Description
pd.DataFrame

A dataframe with statistics

Source code in airt/keras/experiments.py
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
def create_tuner_stats(
    tuner: Tuner,
    *,
    num_models: int = 10,
    max_epochs: int = 50,
    batch_size: int = 8,
    patience: int = 10,
    verbose: int = 0,
) -> pd.DataFrame:
    """Calculates statistics for the best models found by Keras Tuner

    Args:
        tuner: an instance of Keras Tuner
        num_models: number of best models to use for calculating statistics
        max_epochs: maximum number of epochs used in runs
        batch_size: batch_size
        patience: maximum number of epochs with worse objective before stopping trial early
        verbose: verbosity level of `Model.fit` function

    Returns:
        A dataframe with statistics
    """
    stats = None

    train_df, test_df = get_train_n_test_data(tuner.project_name)
    train_ds, test_ds = df2ds(train_df), df2ds(test_df)

    for hp in tuner.get_best_hyperparameters(num_trials=num_models):
        new_entry = _create_model_stats(
            tuner,
            hp,
            stats=stats,
            max_epochs=max_epochs,
            num_runs=10,
            top_runs=5,
            batch_size=batch_size,
            patience=patience,
            verbose=verbose,
            train_ds=train_ds,
            test_ds=test_ds,
        )
        if stats is None:
            stats = new_entry
        else:
            stats = pd.concat([stats, new_entry]).reset_index(drop=True)

        try:
            display(stats.sort_values(f"{tuner.oracle.objective.name}_mean"))  # type: ignore
        # nosemgrep
        except Exception as e:  # nosec
            pass

    return stats.sort_values(f"{tuner.oracle.objective.name}_mean")  # type: ignore