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 ofconfig.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 |
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
|
|
required |
channel_indices
|
Tensor | None
|
|
None
|
timesteps
|
Tensor | None
|
|
None
|
patch_mask
|
PatchMask | Tensor | None
|
Explicit mask (a |
None
|
mask_patches
|
bool | None
|
Draw a random mask (default: when |
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 |
required |
channel_indices
|
Tensor | None
|
See |
None
|
timesteps
|
Tensor | None
|
See |
None
|
patch_mask
|
PatchMask | Tensor | None
|
See |
None
|
generator
|
Generator | None
|
See |
None
|
labels
|
Tensor | None
|
|
None
|
num_entities
|
int | None
|
Entities in the batch (default: |
None
|
fusion_stage
|
str | None
|
Override of |
None
|
TimberModelOutput
dataclass
¶
Bases: ModelOutput
Output of TimberModel.
Attributes:
| Name | Type | Description |
|---|---|---|
last_hidden_state |
Tensor | None
|
Latents of the last computed encoder block |
block_latents |
tuple[Tensor, ...] | None
|
Latents after every computed encoder block. |
contextualized |
Tensor | None
|
|
patches |
Tensor | None
|
|
position_embeddings |
Tensor | None
|
|
patch_keep |
Tensor | None
|
|
masked_indices |
Tensor | None
|
|
cross_attentions |
tuple[Tensor, ...] | None
|
Encoder cross-attention probabilities per block ( |
self_attentions |
tuple[Tensor, ...] | None
|
Probabilities of the last latent self-attention layer of every block
( |
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 |
targets |
dict[str, Tensor] | None
|
Matching targets per objective. |
last_hidden_state, |
patch_keep
|
See |
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 |
loss_terms |
dict[str, Tensor] | None
|
Unweighted loss terms ( |
logits |
Tensor | None
|
Predictions used by the loss: |
view_logits |
Tensor | None
|
Predictions per view |
pooled |
Tensor | None
|
Pooled latents per view |
pooling_weights |
Tensor | None
|
Attention-pooling weights |
last_hidden_state |
Tensor | None
|
Latents read by the head. |