Class-Based Tuner Guide¶
Optimizing hyperparameters is essential for maximizing the performance
of advanced forecasting models like
TemporalFusionTransformer,
TFT,
XTFT, and
SuperXTFT. These parameters define the model’s
architecture and its training dynamics.
fusionlab-learn introduces an object-oriented approach for hyperparameter
tuning, building upon the Keras Tuner library. This approach
utilizes dedicated tuner classes, XTFTTuner and TFTTuner, which
inherit common functionalities from a _BaseTuner class (Note: _BaseTuner
is internal; users interact with XTFTTuner and TFTTuner).
This class-based approach provides a more structured and reusable way to set up and execute tuning experiments compared to the previous function-based method (which remains available for backward compatibility).
Prerequisites¶
Ensure you have Keras Tuner installed:
pip install keras-tuner -q
The Class-Based Approach¶
The core idea is to instantiate a tuner class (XTFTTuner or TFTTuner) with parameters defining the tuning process itself (like max_trials, objective, param_space), and then call its fit method with the data and task-specific parameters (like inputs, y, forecast_horizon).
Key Advantages:
- Reusability: Configure a tuner once and use its fit method
multiple times with different datasets or forecast horizons.
- Structure: Encapsulates tuning logic within objects, leading to
cleaner code.
- Flexibility: Easily extend or customize by inheriting or providing
custom model builders.
XTFTTuner¶
- API Reference:
Purpose:
Designed specifically to perform hyperparameter optimization for
XTFT and SuperXTFT
models. It ensures that the model_name passed during initialization
is one of these supported variants (“xtft”, “superxtft”, or “super_xtft”).
Functionality:
Initialization (`__init__`): You create an instance by providing tuning-process parameters (e.g., max_trials, epochs, batch_sizes, param_space). You must specify a supported model_name (defaults to “xtft”).
Fitting (`fit`): You call the
fitmethod with your input tensors (inputs = [X_static, X_dynamic, X_future]), target y, and crucial task parameters like forecast_horizon and quantiles.Tuning: The fit method executes the Keras Tuner search loop, building models using the internal factory or a custom builder, evaluating them, and finding the best set of hyperparameters across the specified batch_sizes.
Results: After fitting, the tuner instance holds the results in its attributes: best_hps_ (dictionary), best_model_ (Keras model), and tuner_ (Keras Tuner object). Results are also saved to a JSON file.
Code Example (Tuning XTFT):
1import numpy as np
2import os
3from fusionlab.nn.forecast_tuner import XTFTTuner
4
5# 1. Prepare Dummy Data (Static, Dynamic, Future)
6B, T_past, H_out = 8, 12, 6
7D_s, D_d, D_f = 3, 5, 2
8T_future_total = T_past + H_out
9
10X_static_train = np.random.rand(B, D_s).astype(np.float32)
11X_dynamic_train = np.random.rand(B, T_past, D_d).astype(np.float32)
12X_future_train = np.random.rand(
13 B, T_future_total, D_f).astype(np.float32)
14y_train = np.random.rand(B, H_out, 1).astype(np.float32)
15
16train_inputs = [X_static_train, X_dynamic_train, X_future_train]
17
18# 2. Define Minimal Search Space
19custom_param_space = {
20 'hidden_units': [16], # Fixed for speed
21 'num_heads': [2],
22 'learning_rate': [1e-3]
23}
24
25# 3. Instantiate the Tuner
26xtft_tuner_obj = XTFTTuner(
27 model_name="xtft",
28 param_space=custom_param_space,
29 max_trials=1, # Minimal for demo
30 epochs=2, # Minimal for demo
31 batch_sizes=[8], # Single small batch
32 tuner_dir="./xtft_class_tuning",
33 project_name="XTFT_Class_Tune",
34 tuner_type='random',
35 verbose=0
36)
37
38# 4. Run the Tuning by Calling fit()
39print("Starting XTFT tuning (Class-Based)...")
40best_hps, best_model, tuner = xtft_tuner_obj.fit(
41 inputs=train_inputs,
42 y=y_train,
43 forecast_horizon=H_out,
44 quantiles=None, # Point forecast
45 case_info={ # Can still pass case_info for extra details
46 'description': "My XTFT Point Forecast"
47 }
48)
49
50# 5. Display Results
51print("\nXTFT Tuning complete.")
52if best_hps:
53 print("--- Best Hyperparameters (XTFT) ---")
54 print(best_hps)
55else:
56 print("XTFT Tuning failed to find a best model.")
Expected Output:
Starting XTFT tuning (Class-Based)...
XTFT Tuning complete.
--- Best Hyperparameters (XTFT) ---
{'hidden_units': 16, 'num_heads': 2, 'dropout_rate': 0.3, 'activation': 'gelu',
'use_batch_norm': 0, 'embed_dim': 64, 'max_window_size': 10, 'memory_size': 100,
'lstm_units': 64, 'attention_units': 128, 'recurrent_dropout_rate': 0.0,
'use_residuals': 1, 'final_agg': 'average', 'multi_scale_agg': 'last',
'scales_options': 'no_scales', 'learning_rate': 0.001, 'batch_size': 8}
TFTTuner¶
- API Reference:
Purpose: Provides a dedicated tuner for Temporal Fusion Transformer models. It supports both the stricter TFT (requires all inputs, set model_name=”tft”) and the flexible TemporalFusionTransformer (handles optional inputs, set model_name=”tft_flex”).
Functionality:
Similar to fusionlab.nn.forecast_tuner.XTFTTuner:
Initialization (`__init__`): Configure the tuning process. Crucially, set model_name to either “tft” or “tft_flex”.
Fitting (`fit`): Call fit with the data and task parameters. * If model_name=”tft”, inputs must be [X_s, X_d, X_f] with non-None tensors. * If model_name=”tft_flex”, inputs can be [X_s, X_d, X_f]
where X_s and X_f can be None.
Tuning: Executes the search process.
Results: Access via best_hps_, best_model_, tuner_.
Code Example (Tuning Flexible TFT - `tft_flex`):
1import numpy as np
2import os
3from fusionlab.nn.forecast_tuner import TFTTuner
4
5# 1. Prepare Dummy Data (Dynamic only for this example)
6B, T_past, H_out = 8, 12, 6
7D_d = 5
8X_d_train_flex = np.random.rand(B, T_past, D_d).astype(np.float32)
9y_train_flex = np.random.rand(B, H_out, 1).astype(np.float32)
10
11# Inputs for flexible TFT (static and future are None)
12train_inputs_flex = [None, X_d_train_flex, None]
13
14# 2. Define Minimal Search Space
15param_space_flex = {'hidden_units': [16], 'learning_rate': [1e-3]}
16
17# 3. Instantiate the Tuner
18tft_tuner_obj = TFTTuner(
19 model_name="tft_flex", # Key: Using the flexible version
20 param_space=param_space_flex,
21 max_trials=1,
22 epochs=1,
23 batch_sizes=[4],
24 tuner_dir="./tft_flex_class_tuning",
25 project_name="TFT_Flex_Class_Tune",
26 verbose=0
27)
28
29# 4. Run Tuning
30print("\nStarting flexible TFT (tft_flex) tuning (Class-Based)...")
31best_hps_f, _, _ = tft_tuner_obj.fit(
32 inputs=train_inputs_flex,
33 y=y_train_flex,
34 forecast_horizon=H_out,
35 quantiles=None
36)
37
38# 5. Display Results
39print("Flexible TFT Tuning complete.")
40if best_hps_f:
41 print(" Best HPs (Flexible TFT):", best_hps_f)
Expected Output:
Starting flexible TFT (tft_flex) tuning (Class-Based)...
Flexible TFT Tuning complete.
Best HPs (Flexible TFT): {'hidden_units': 16, 'num_heads': 4, 'dropout_rate': 0.3,
'activation': 'relu', 'use_batch_norm': 1, 'num_lstm_layers': 2, 'lstm_units': 128,
'learning_rate': 0.001, 'batch_size': 4}
Customizing the Tuning¶
While the default settings and model builders are powerful, you can customize the process:
- param_space: Provide a dictionary in the __init__ method to
define specific ranges or choices for any hyperparameter used by the internal _model_builder_factory. See Keras Tuner documentation for how to define choices.
- model_builder: For complete control, you can write your own
function that takes hp (Keras Tuner’s HyperParameters object) and returns a compiled Keras model. Pass this function to the __init__ method. This allows you to explore entirely different architectures or compilation strategies.
This class-based approach in fusionlab-learn provides a robust and
flexible framework for efficiently finding optimal hyperparameters for
your time-series forecasting models.