quantlab.model.predefined.gats.GATsRegressor#

class quantlab.model.predefined.gats.GATsRegressor(config)[source]#

Bases: TorchModel

Qlib’s GATs on the cross-section of every bar.

Each step is one bar: every symbol with a finite feature, each with its last window_bars bars, goes through GATsNet. The training target is Qlib’s CSRankNorm of the label (cs_rank_norm), the loss is the MSE over symbols with a label, the optimizer is Adam, and gradient values are clipped at 3. Training keeps the epoch with the lowest validation loss and stops after early_stop epochs without a strict improvement, as Qlib does; without a validation segment it runs every epoch and keeps the last.

Hyperparameters, with Qlib’s Alpha158 benchmark values as defaults: window_bars (20), hidden_size (64), num_layers (2), dropout (0.7), base_model ("LSTM"), lr (1e-4), epochs (200) and early_stop (10).

Known differences from Qlib’s implementation:

  • no pretrained LSTM: Qlib copies the encoder and fc_out from its LSTM benchmark’s checkpoint, here every weight starts random;

  • the features are the model’s factors, not Qlib’s 20 filtered Alpha158 columns, and no RobustZScoreNorm is fitted on the training span: the default _transform_feature clips to ±3 and fills NaN with 0, where Qlib forward- and back-fills gaps inside a window;

  • the bars of an epoch are shuffled, as in Qlib’s Alpha360 variant; the Alpha158 variant visits them in time order;

  • a symbol whose label is missing stays in the bar’s cross-section as context and only leaves the loss; Qlib drops it from that day’s training and validation input;

  • the output has one column per label instead of one;

  • the last training batch is kept, where Qlib drops it.

Parameters:
configModelConfig

Factors, labels, dates and hyperparameters.

Examples

>>> head = GATsRegressor(ModelConfig(
...     factors=[factor], labels=[label], model_save_dir="checkpoints",
...     factor_data_strategy="read", label_data_strategy="read",
...     train_start="2024-01-01", train_end="2024-05-31",
...     test_start="2024-06-01", test_end="2024-07-18",
...     hyperparameters={"window_bars": 5, "epochs": 20, "early_stop": 5},
... )).collect()
>>> head.train().name
'GATsRegressor_total.pth'
DEFAULTS = {'base_model': 'LSTM', 'dropout': 0.7, 'early_stop': 10, 'epochs': 200, 'hidden_size': 64, 'lr': 0.0001, 'num_layers': 2, 'window_bars': 20}#

Qlib’s Alpha158 benchmark values, used for every unset hyperparameter.

property early_stop#

Epochs without a better validation loss before training stops (10).

Examples

>>> head.early_stop
5
property epochs#

The epoch cap, hyperparameters["epochs"], 200 when unset.

Raises:
ValueError

If the value is not a positive integer.

Examples

>>> head.epochs
20
property window_bars#

Bars in each symbol’s window, hyperparameters["window_bars"] (20).

Examples

>>> head.window_bars
5