append_experiments(previous_experiments, input_combinations)

Appends new experiments to previous experiments.

Parameters:

Name Type Description Default
previous_experiments dict

Dictionary of trials and their hyperparameters.

required
input_combinations list

List of input hyperparameter combinations.

required

Returns:

Name Type Description
dict

Updated dictionary of trials and their hyperparameters.

Source code in wt_ml/tuning/wfo_tuning.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def append_experiments(previous_experiments, input_combinations):
    """Appends new experiments to previous experiments.

    Args:
        previous_experiments (dict): Dictionary of trials and their hyperparameters.
        input_combinations (list): List of input hyperparameter combinations.

    Returns:
        dict: Updated dictionary of trials and their hyperparameters.
    """
    all_combinations = input_combinations + [*previous_experiments.values()]
    trial_counter = len(previous_experiments) + 1
    unique_combinations = []
    for combo in all_combinations:
        if combo in unique_combinations:
            continue
        else:
            unique_combinations.append(combo)
    all_experiments = previous_experiments.copy()
    for combo in unique_combinations:
        if combo in previous_experiments.values():
            continue
        else:
            all_experiments[f"trial_{trial_counter}"] = combo
            trial_counter += 1
    return all_experiments

get_trial_combinations(param_options)

Creates a cartesian product of all hyperparameter combinations based on input options.

Parameters:

Name Type Description Default
param_options dict

Tuning options for hyperparamters.

required

Returns:

Name Type Description
list

List of hyperparameters combinations.

Source code in wt_ml/tuning/wfo_tuning.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def get_trial_combinations(param_options):
    """Creates a cartesian product of all hyperparameter combinations based on input options.

    Args:
        param_options (dict): Tuning options for hyperparamters.

    Returns:
        list: List of hyperparameters combinations.
    """
    param_options_flat = flatten(param_options)
    params = list(param_options_flat.keys())
    options_cartesian = list(itertools.product(*[param_options_flat[param] for param in params]))
    trial_combinations = [unflatten(dict(zip(params, combo))) for combo in options_cartesian]
    return trial_combinations

run_wfo(dataset_kwargs, hyperparameters, trial, run_dir, min_val_date=None, max_val_date=None, net_types=None, epochs=int(2 ** 10), parallel=1, val_freq=Frequency.quarter)

Runs wfo for a single trial.

Parameters:

Name Type Description Default
dataset_kwargs dict[str, Any]

Dataset related kwargs.

required
encodings dict[str, Any]

Encodings.

required
hyperparameters dict[tuple[str, ...], Any]

Updated hyperparameters for the trial.

required
trial str

Trial name.

required
run_dir str | Path

Trial directory.

required
min_val_date str | None

Minimum validation week to include in periods. Defaults to None.

None
max_val_date str | None

Maximum validaiton week to include in periods. Defaults to None.

None
net_types list[type[EconomicNetwork]] | None

Networks. Defaults to None.

None
epochs int

Epochs. Defaults to int(2**10).

int(2 ** 10)
parallel int

Number of parallel jobs for wfo periods train and predict. Defaults to 1.

1
val_freq Frequencies

Validation period frequency. Defaults to "quarter".

quarter

Raises:

Type Description
RuntimeError

Raises if an exception occurs during wfo run or saving.

Source code in wt_ml/tuning/wfo_tuning.py
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
def run_wfo(
    dataset_kwargs: dict[str, Any],
    hyperparameters: dict[tuple[str, ...], Any],
    trial: str,
    run_dir: str | Path,
    min_val_date: str | None = None,
    max_val_date: str | None = None,
    net_types: list[type[EconomicNetwork]] | None = None,
    epochs: int = int(2**10),
    parallel: int = 1,
    val_freq: Frequency = Frequency.quarter,
):
    """Runs wfo for a single trial.

    Args:
        dataset_kwargs (dict[str, Any]): Dataset related kwargs.
        encodings (dict[str, Any]): Encodings.
        hyperparameters (dict[tuple[str, ...], Any]): Updated hyperparameters for the trial.
        trial (str): Trial name.
        run_dir (str | Path): Trial directory.
        min_val_date (str | None, optional): Minimum validation week to include in periods. Defaults to None.
        max_val_date (str | None, optional): Maximum validaiton week to include in periods. Defaults to None.
        net_types (list[type[EconomicNetwork]] | None, optional): Networks. Defaults to None.
        epochs (int, optional): Epochs. Defaults to int(2**10).
        parallel (int, optional): Number of parallel jobs for wfo periods train and predict. Defaults to 1.
        val_freq (Frequencies, optional): Validation period frequency. Defaults to "quarter".

    Raises:
        RuntimeError: Raises if an exception occurs during wfo run or saving.
    """
    trial_id = int(trial.split("_")[-1])
    addons.use_loguru_logger(run_dir.parent / "wfo_log.log")
    logger.message_format = f" | {run_dir.name} | " + "{message}"
    setup_tensorflow_options(xla=False, debug=False)
    if net_types is None:
        net_types = DEFAULT_NETWORK_COMBINATION

    Network = make_multinet(net_types)

    tf.config.set_soft_device_placement(True)
    devices = tf.config.list_logical_devices("GPU")

    if len(devices) > 0:
        use_device = trial_id % len(devices)
        device = devices[use_device].name
    else:
        all_devices = tf.config.list_logical_devices()
        device = all_devices[0].name

    dataset, _ = make_dataset(**dataset_kwargs)
    model_builder = partial(build_model, hyperparameters=hyperparameters, net_combination=Network)
    with tf.device(device):
        wfo = WFORunner(
            dataset=dataset,
            model_builder=model_builder,
            val_freq=val_freq,
            min_val_date=min_val_date,
            max_val_date=max_val_date,
        )
        logger.info("WFO object created.")
        try:
            results = wfo.run(epochs=epochs, parallel=parallel, save_dir=run_dir, smoothing_window=True)
            calculated_metrics = wfo.calculate_metrics(
                results,
                weights="auto",
            )
            wfo.metrics_to_df(calculated_metrics).to_excel(run_dir / "metrics.xlsx")
            logger.info("WFO run completed and saved.")
        except Exception as e:
            logger.exception(exc=e, message="Exception occurred during wfo run.")
            raise RuntimeError("Exception occurred during wfo run") from e
        finally:
            del wfo
            logger.info("Model and wfo object deleted.")