Skip to content

Finetuning heads

The head (paper module H) turns the latents of an entity into a prediction: a regression vector or class logits. It is what changes between tasks. The paper's method pools the latents with attention and applies a small MLP; the fMRI experiments use mean pooling (see the table below).

What it does

The head reads the latent embedding e of each view and predicts the target. It has three stages:

  • Pooling summarises the latent queries of a view into one vector:
  • "attention" (the paper's method): a softmax over a learned linear score of each latent query, so that the queries that matter most for the target get the largest weights; the weights are returned for inspection;
  • "mean": the average of the latent queries (the fMRI experiments);
  • "weighted_mean": a softmax-weighted average with one learned weight per latent query;
  • "cls_token": the last token, which needs use_cls_token=True in the encoder. An optional Linear + GELU projection (projection_dim) and input dropout (pooling_input_dropout) apply to every token before pooling.
  • View fusion combines the views of an entity (recordings, channel chunks, time chunks). fusion_stage decides where: "early" averages the pooled views before the MLP, so there is one prediction per entity; "late" predicts for each view and averages the predictions; "loss" keeps one prediction per view and computes the loss on each, with the entity's label repeated.
  • MLP maps the fused vector to num_outputs values, through num_hidden_layers hidden layers (0 in the experiments: a single linear layer) with dropout.

The loss depends on the task. Regression uses the Huber loss, as in the paper. Classification uses cross-entropy, with label_smoothing (0.05 in experiment 4).

Options that matter

Option Default Experiments Effect
task "regression" "regression" (Huber) or "classification" (cross-entropy).
num_outputs 1 Size of the prediction: regression targets, or classes.
pooling "mean" "mean" How the latents of a view are summarised.
fusion_stage "late" Where the views of an entity are combined.
num_hidden_layers 0 0 Hidden layers of the MLP; 0 is a linear layer.
dropout 0.5 0.3 (experiments 3 to 5) Dropout of the MLP.
label_smoothing 0.0 0.05 (experiment 4) Smoothing of the classification targets.

Reference

timber.modules.heads

Finetuning heads (paper module H): latent pooling, view fusion and an MLP.

An entity contributes several views (recordings, channel chunks, time chunks) to a batch. fusion_stage sets where the views of an entity are combined:

  • "early": average the pooled latents of the views, then apply the MLP once per entity;
  • "late": apply the MLP per view, then average the predictions (fuse_views);
  • "loss": keep one prediction per view and repeat the entity label for each view.

PredictionHead

Bases: Module

Latents -> prediction: optional projection, dropout, pooling, early view fusion, MLP.

Parameters:

Name Type Description Default
latent_dim int

Channels of the encoder latents.

required
num_tokens int

Latent tokens (including the CLS token, if any).

required
output_dim int

Outputs (regression targets, or classes).

required
pooling PoolingType

See LatentPooling.

'mean'
fusion_stage FusionStage

"early" averages the pooled views before the MLP; "late" and "loss" return one prediction per view (fused by the loss).

'late'
projection_dim int | None

Optional Linear + GELU projection of every token before pooling.

None
pooling_input_dropout float

Dropout on the tokens before pooling.

0.0
num_hidden_layers int

See MLPHead.

0
widening_factor float

See MLPHead.

1.0
dropout float

See MLPHead.

0.5
use_input_norm bool

See MLPHead.

False
forward
forward(
    latents: Tensor,
    num_entities: int,
    fusion_stage: FusionStage | None = None,
) -> dict[str, torch.Tensor | None]

Predict from the latents of every view.

Parameters:

Name Type Description Default
latents Tensor

(entity * view, tokens, latent_dim), entity-major.

required
num_entities int

Entities in the batch.

required
fusion_stage FusionStage | None

Override of the configured fusion stage (e.g. "late" for validation).

None

Returns:

Type Description
dict[str, Tensor | None]

prediction ((entity, output_dim) for early fusion, else (entity * view, output_dim)),

dict[str, Tensor | None]

pooled ((entity * view, dim)) and pooling_weights (attention pooling only).

LatentPooling

Bases: Module

Pool latent tokens (batch, tokens, dim) into (batch, dim).

Parameters:

Name Type Description Default
pooling PoolingType

"mean", "weighted_mean" (softmax of learned per-token weights), "cls_token" (last token) or "attention" (softmax of a learned linear score).

required
latent_dim int

Channels of the tokens (for "attention").

required
num_tokens int

Number of tokens (for "weighted_mean").

required
attention_dropout float

Dropout on the attention-pooling weights (training only).

0.1
forward
forward(
    latents: Tensor,
) -> tuple[torch.Tensor, torch.Tensor | None]

Return the pooled latents (batch, dim) and, for attention pooling, the weights (batch, tokens).

MLPHead

Bases: Module

MLP with num_hidden_layers hidden layers of size widening_factor * input_dim.

Layout (as in the original implementation): [LayerNorm] -> (Linear -> act -> Dropout) x n -> Linear; without hidden layers the dropout is applied to the input: Dropout -> Linear.

forward
forward(x: Tensor) -> torch.Tensor

(..., input_dim) -> (..., output_dim).

fuse_views

fuse_views(x: Tensor, num_entities: int) -> torch.Tensor

Average the views of each entity: (entity * view, dim) -> (entity, dim) (entity-major).