quantlab.model.predefined.master.MASTERNet#

class quantlab.model.predefined.master.MASTERNet(num_features, num_labels, gate_columns, d_model=256, t_nhead=4, s_nhead=2, dropout=0.5, beta=5.0)[source]#

Bases: Module

The MASTER network with its market gate columns anywhere among the features.

The input is one bar’s [S_t, N, F] windows. The columns gate_columns are the market features: their values at the last step go through Gate to weights that rescale the other F - G columns at every step. The rescaled features then pass Linear(F - G, D), PositionalEncoding, attention over time within each symbol (4 heads, unscaled), attention across symbols at every step (2 heads, scaled), TemporalAttention and Linear(D, L). The submodule names and order are the official ones, so an official MASTER state dict loads into a one-label net whose gate columns are the trailing ones.

Parameters:
num_featuresint

F, every column of the input, gate columns included.

num_labelsint

L, outputs per symbol; the official network has one.

gate_columnslist of int

Positions of the market features among the F columns.

d_modelint, default 256
t_nhead, s_nheadint, default 4 and 2

Heads of the attention over time and across symbols.

dropoutfloat, default 0.5

Dropout of both attention blocks.

betafloat, default 5.0

Gate temperature; a smaller beta selects features more sharply.

Raises:
ValueError

If gate_columns is empty, covers every column or repeats one, or a head count does not divide d_model.

Examples

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

Map one bar’s [S_t, N, F] windows to [S_t, L] outputs.

Examples

>>> net.forward(torch.randn(2, 3, 5)).shape
torch.Size([2, 2])
gate(market)[source]#

Return the [S_t, F - G] feature weights for [S_t, G] market features.

Each row sums to F - G.

Examples

>>> net.gate(torch.zeros(1, 2)).sum().item()
3.0