quantlab.model.torch_data.TrainingPanel#

class quantlab.model.torch_data.TrainingPanel(x, target, mask, y_raw, present, timestamps, symbols)[source]#

Bases: object

The collected panel as torch tensors, shared by every dataset of a fit.

All tensors live on the same device and are indexed [T, S, ...] on the collected timestamps and symbols. A cell is present when it has at least one finite feature; the cross-section of a bar is its present symbols. The training target is computed once per fit, before any epoch, by the head’s _transform_target.

Attributes:
xtorch.Tensor

Features, [T, S, F], NaN where missing; float32, or float16 when the head stores the panel with panel_dtype="float16" (each batch is cast back to float32 before _transform_feature).

targettorch.Tensor

Training target, [T, S, L], 0 wherever mask is False.

masktorch.Tensor

[T, S] booleans, True where the cell has a valid training target: present, kept by _transform_target, and every one of its L targets finite; a cell with one label missing is masked out for all labels.

y_rawtorch.Tensor

Raw labels, [T, S, L], NaN where missing.

presenttorch.Tensor

[T, S] booleans, True where the cell has a finite feature.

timestampsnp.ndarray

The T bars.

symbolsnp.ndarray

The S symbols.

Examples

>>> panel = TrainingPanel.from_arrays(
...     np.ones((3, 2, 1)), timestamps=np.arange(3), symbols=np.array(["A", "B"]),
... )
>>> panel.present.all().item(), panel.mask.any().item(), tuple(panel.target.shape)
(True, False, (3, 2, 0))
classmethod from_arrays(x, *, timestamps, symbols, y_raw=None)[source]#

Build a panel with no training target yet (mask all False).

Parameters:
xnp.ndarray

Features, [T, S, F].

timestamps, symbolsnp.ndarray

The panel’s coordinates.

y_rawnp.ndarray, optional

Raw labels, [T, S, L]; [T, S, 0] when omitted, as at prediction time.

Raises:
ValueError

If x is not three-dimensional or y_raw does not match it.

Examples

>>> TrainingPanel.from_arrays(
...     np.zeros((2, 3, 4)), timestamps=np.arange(2), symbols=np.arange(3),
... ).x.shape
torch.Size([2, 3, 4])
to(device, x_dtype=None)[source]#

Return the panel with every tensor on device, features cast to x_dtype.

Only the features change precision; the target, the raw labels and the masks keep theirs. present was computed from the features before any cast.

Raises:
ValueError

If the cast turns a finite feature into an infinity, as a value beyond ±65504 does in float16.

Examples

>>> panel.to("cpu", torch.float16).x.dtype
torch.float16
window(t, symbols, window_bars)[source]#

Return the [len(symbols), N, F] windows ending at bar t.

Rows are bars t - N + 1 to t, oldest first; rows before the first bar of the panel are NaN. Windows may reach back across a segment boundary, which is legal: every bar there is in the past.

Examples

>>> panel.window(0, torch.tensor([1]), 2)[:, :, 0]
tensor([[nan, 1.]])