fusionlab.nn.components.aggregate_multiscale

fusionlab.nn.components.aggregate_multiscale(lstm_output, mode='auto')[source]

Aggregate multi-scale LSTM outputs using specified temporal fusion strategy.

This function implements multiple strategies for combining outputs from multi-scale LSTMs operating at different temporal resolutions. Supports six aggregation modes: average, sum, flatten, concat, last (default fallback), and auto``[1]_. Designed for compatibility with ``MultiScaleLSTM layer outputs.

See more in User Guide.

Parameters:
  • lstm_output (list of tf.Tensor or tf.Tensor) –

    Input features from multi-scale processing: - List of 3D tensors [(B, T’, U), …] when mode != ‘auto’ - Single 2D tensor (B, U*S) when mode=None where:

    B = Batch size T’ = Variable time dimension (scale-dependent) U = LSTM units per scale S = Number of scales (len(scales))

  • mode ({'auto', 'sum', 'average', 'flatten', 'concat', 'last'}, optional) – Aggregation strategy: - auto : (Default) Concatenate last timesteps from each scale - sum : Temporal summation per scale + feature concatenation - average : Temporal mean per scale + feature concatenation - flatten : Flatten all time-feature dimensions (requires equal T’) - concat : Feature concatenation + last global timestep - last : Alias for auto (backward compatibility)

Returns:

Aggregated features with shape: - (B, U*S) for modes: average, sum, last - (B, T’*U*S) for flatten mode - (B, U*S) for concat mode (last timestep only) - (B, U*S) for auto mode

In sum: - (B, U*S) for auto/last, sum, average, concat - (B, T’*U*S) for flatten mode.

Return type:

tf.Tensor

Notes

  • Mode Comparison Table:

Mode

Temporal Handling

Requirements

Typical Use Case

auto (last)

Last step per scale

None

Default choice for variable T’

sum

Full sequence sum per scale

None

Emphasize temporal accumulation

average

Full sequence mean per scale

None

Smooth temporal patterns

flatten

Preserve all time steps

Equal T’ across scales

Fixed-length sequence models

concat

Last global step of concatenated features

Equal T’ across scales

Specialized architectures with aligned T’

Mathematical Formulation:

For S scales with outputs \(\{\mathbf{X}_s \in \mathbb{R}^{B \times T'_s \times U}\}_{s=1}^S\):

\[ \begin{align}\begin{aligned}\text{auto} &: \bigoplus_{s=1}^S \mathbf{X}_s^{(:, T'_s, :)} \quad \text{(Last step concatenation)}\\\text{sum} &: \bigoplus_{s=1}^S \sum_{t=1}^{T'_s} \mathbf{X}_s^{(:, t, :)}\\\text{average} &: \bigoplus_{s=1}^S \frac{1}{T'_s} \sum_{t=1}^{T'_s} \mathbf{X}_s^{(:, t, :)}\\\text{flatten} &: \text{vec}\left( \bigoplus_{s=1}^S \mathbf{X}_s \right)\\\text{concat} &: \left( \bigoplus_{s=1}^S \mathbf{X}_s \right)^{(:, T', :)}\end{aligned}\end{align} \]

where \(\bigoplus\) = feature concatenation, \(\text{vec}\) = flatten.

  • Critical differences between key modes 'concat' and 'last':

Aspect

concat

last (default)

Time alignment

Requires equal T’

Handles variable T’

Feature mixing

Cross-scale mixing

Scale-independent

Scale validity

Only valid when scales=[1,1,…]

Robust to arbitrary scale configurations

Examples

>>> from fusionlab.nn.components import aggregate_multiscale
>>> import tensorflow as tf

# Three scales with different time dimensions >>> outputs = [ … tf.random.normal((32, 10, 64)), # Scale 1: T’=10 … tf.random.normal((32, 5, 64)), # Scale 2: T’=5 … tf.random.normal((32, 2, 64)) # Scale 3: T’=2 … ]

# Default auto mode (last timesteps) >>> agg_auto = aggregate_multiscale(outputs, mode=’auto’) >>> agg_auto.shape (32, 192) # 64 units * 3 scales

# Last timestep aggregation (default) >>> agg_last = aggregate_multiscale(outputs, mode=’last’) >>> print(agg_last.shape) (32, 192)

# Flatten mode (requires manual padding for equal T’) >>> padded_outputs = [tf.pad(o, [[0,0],[0,3],[0,0]]) for o in outputs[:2]] >>> padded_outputs.append(outputs[2]) >>> agg_flat = aggregate_multiscale(padded_outputs, mode=’flatten’) >>> agg_flat.shape (32, 1280) # (10+3)*64*3 = 13*192 = 2496? Wait need to check dimensions

See also

MultiScaleLSTM

Base layer producing multi-scale LSTM outputs

TemporalFusionTransformer

Advanced temporal fusion architecture

HierarchicalAttention

Alternative temporal aggregation approach

References