Skip to content

Model reference

The models are Hugging Face PreTrainedModels. TimberModel is the backbone that produces the embeddings; the two task models add the pretraining decoders or the finetuning head around it. Their forward methods return the typed output objects listed below.

timber.modeling_timber

TIMBER models as Hugging Face PreTrainedModel.

  • TimberModel: tokenizer -> contextualizer -> Perceiver encoder; returns the latents (the embeddings) and the intermediate tensors.
  • TimberForPretraining: TimberModel + the decoders of config.decoders (TACO forecasting, reconstruction); computes the pretraining loss.
  • TimberForPrediction: TimberModel + a regression or classification head (config.head), optionally with pretraining decoders trained jointly (e.g. TACO finetuning).

Inputs follow the data loader layout: signal is (*batch, channel, time). The leading batch axis is the entity axis: signal.shape[0] entities, each with prod(batch[1:]) views (recordings, channel chunks, time chunks), flattened entity-major.

Example::

model = TimberModel.from_pretrained("path/or/hub-id")
embeddings = model(signal, channel_indices=channels, mask_patches=False).last_hidden_state

TimberModel

Bases: TimberPreTrainedModel

Tokenizer, contextualizer and Perceiver encoder: multichannel signal -> latent embeddings.

Parameters:

Name Type Description Default
config TimberConfig

Model configuration.

required
channel_embedding_init ndarray | Tensor | str | None

Optional (num_channels, channel_embedding_dim) array, or path to a .npy file, to initialise the channel embedding. Only used when training from scratch: from_pretrained loads the trained embedding.

None
load_channel_embedding
load_channel_embedding(
    values: ndarray | Tensor | str,
) -> None

Overwrite the channel embedding with values (array or .npy path).

make_patch_mask
make_patch_mask(
    batch_size: int,
    num_patches: int,
    device: device,
    generator: Generator | None = None,
) -> PatchMask

Random mask of config.patch_mask_ratio of the num_patches (channel * patch) patches per row.

forward
forward(
    signal: Tensor,
    channel_indices: Tensor | None = None,
    timesteps: Tensor | None = None,
    patch_mask: PatchMask | Tensor | None = None,
    mask_patches: bool | None = None,
    num_encoder_blocks: int | None = None,
    output_attentions: bool = False,
    generator: Generator | None = None,
) -> TimberModelOutput

Embed multichannel signal windows.

Parameters:

Name Type Description Default
signal Tensor

(*batch, channel, time) multichannel time series.

required
channel_indices Tensor | None

(*batch, channel) channel index of every row (None: 0 .. channel - 1).

None
timesteps Tensor | None

(*batch, time) absolute timesteps (needed for time_embedding_type="timestep").

None
patch_mask PatchMask | Tensor | None

Explicit mask (a PatchMask or a (batch, channel * patch) keep mask).

None
mask_patches bool | None

Draw a random mask (default: when config.patch_mask_ratio > 0). Ignored when patch_mask is given. Use False to embed every patch at inference.

None
num_encoder_blocks int | None

Run only the first blocks of the encoder.

None
output_attentions bool

Return the encoder cross-attention and last self-attention probabilities.

False
generator Generator | None

Random generator of the patch mask.

None

TimberForPretraining

Bases: TimberPreTrainedModel

TIMBER with its pretraining decoders (TACO forecasting and/or masked reconstruction).

forward
forward(
    signal: Tensor,
    channel_indices: Tensor | None = None,
    timesteps: Tensor | None = None,
    patch_mask: PatchMask | Tensor | None = None,
    generator: Generator | None = None,
) -> TimberPretrainingOutput

Compute the pretraining loss. Arguments as in TimberModel.forward.

TimberForPrediction

Bases: TimberPreTrainedModel

TIMBER with a regression or classification head (config.head).

The decoders of config.decoders (if any) are trained jointly, e.g. TACO as an auxiliary objective during finetuning.

latent_block property
latent_block: int

Encoder block (1-based) whose latents feed the head; the last one by default.

head_loss
head_loss(logits: Tensor, labels: Tensor) -> torch.Tensor

Loss of the head.

labels is (entity, outputs) (regression) or (entity,) / (entity, 1) classes.

forward
forward(
    signal: Tensor,
    channel_indices: Tensor | None = None,
    timesteps: Tensor | None = None,
    labels: Tensor | None = None,
    num_entities: int | None = None,
    patch_mask: PatchMask | Tensor | None = None,
    generator: Generator | None = None,
    fusion_stage: str | None = None,
) -> TimberPredictionOutput

Predict an entity-level target from multichannel windows.

Parameters:

Name Type Description Default
signal Tensor

See TimberModel.forward.

required
channel_indices Tensor | None

See TimberModel.forward.

None
timesteps Tensor | None

See TimberModel.forward.

None
patch_mask PatchMask | Tensor | None

See TimberModel.forward.

None
generator Generator | None

See TimberModel.forward.

None
labels Tensor | None

(entity, outputs) regression targets, or class indices (entity,) / (entity, 1).

None
num_entities int | None

Entities in the batch (default: signal.shape[0]; the remaining batch axes are views).

None
fusion_stage str | None

Override of config.head.fusion_stage for this call (the original validation used "late" to score entities).

None

TimberModelOutput dataclass

Bases: ModelOutput

Output of TimberModel.

Attributes:

Name Type Description
last_hidden_state Tensor | None

Latents of the last computed encoder block (batch, tokens, latent_dim) (CLS token last). Without an encoder: the contextualised patches (batch, channel * patch, patch_dim).

block_latents tuple[Tensor, ...] | None

Latents after every computed encoder block.

contextualized Tensor | None

(batch, channel, patch, patch_dim) contextualised patches.

patches Tensor | None

(batch, channel, patch, patch_length) raw signal patches (targets of the objectives).

position_embeddings Tensor | None

(batch, channel, patch, position_dim).

patch_keep Tensor | None

(batch, channel * patch) boolean mask of the patches visible to the encoder, if masked.

masked_indices Tensor | None

(batch, num_masked) indices of the masked patches, if masked.

cross_attentions tuple[Tensor, ...] | None

Encoder cross-attention probabilities per block (output_attentions=True).

self_attentions tuple[Tensor, ...] | None

Probabilities of the last latent self-attention layer of every block (output_attentions=True).

TimberPretrainingOutput dataclass

Bases: ModelOutput

Output of TimberForPretraining.

Attributes:

Name Type Description
loss Tensor | None

Weighted sum of the objective losses.

loss_terms dict[str, Tensor] | None

Unweighted loss of every objective with a non-zero weight.

predictions dict[str, Tensor] | None

Decoder outputs per objective (batch, queries, patch_length).

targets dict[str, Tensor] | None

Matching targets per objective.

last_hidden_state, patch_keep

See TimberModelOutput.

TimberPredictionOutput dataclass

Bases: ModelOutput

Output of TimberForPrediction.

Attributes:

Name Type Description
loss Tensor | None

Weighted sum of the head loss and of the decoder losses (when labels are given).

loss_terms dict[str, Tensor] | None

Unweighted loss terms ("regression" / "classification" and objectives).

logits Tensor | None

Predictions used by the loss: (entity, outputs) for early and late fusion, (entity * view, outputs) for loss-level fusion.

view_logits Tensor | None

Predictions per view (entity * view, outputs) (None for early fusion).

pooled Tensor | None

Pooled latents per view (entity * view, dim).

pooling_weights Tensor | None

Attention-pooling weights (entity * view, tokens).

last_hidden_state Tensor | None

Latents read by the head.