quantlab.model.predefined.transformer.LocalformerNet#

class quantlab.model.predefined.transformer.LocalformerNet(num_features, num_labels, d_model=64, nhead=2, num_layers=2, dropout=0.0)[source]#

Bases: Module

Qlib’s Localformer (pytorch_localformer_ts.py) with an output of any width.

TransformerNet with LocalformerEncoder in place of PyTorch’s encoder and a GRU of num_layers layers and width D after it; Linear(D, L) maps the GRU’s last bar to the output. The parameter names are Qlib’s, so a Qlib state dict loads into a one-label net.

Parameters:
num_features, num_labels, d_model, nhead, num_layers, dropout

As TransformerNet; dropout also applies between GRU layers.

Examples

>>> net = LocalformerNet(num_features=4, num_labels=2, d_model=8)
>>> net(torch.randn(5, 3, 4)).shape
torch.Size([5, 2])
forward(x)[source]#

Map [B, N, F] windows to [B, L] outputs.

Examples

>>> net.forward(torch.randn(2, 6, 4)).shape
torch.Size([2, 2])