milia.represent.foundation#

Foundation-model embeddings of whole load sequences (Chronos-2).

Each meter’s sequence is embedded by a pretrained time-series model and the per-patch embeddings are pooled into one vector. The pooled vector is a representation for clustering, not a forecast, so it lives here rather than in twiga.

Functions

pool(embedding, pooling)

Collapse a per-patch embedding to one vector.

Classes

Foundation([model, pooling, batch, device])

Chronos-2 sequence embeddings as a scikit-learn transformer.

milia.represent.foundation.pool(embedding, pooling)#

Collapse a per-patch embedding to one vector.

Parameters:
  • embedding (ndarray) – Shape (patches, dim).

  • pooling (Literal['mean', 'last']) – "mean" over patches, or "last".

Return type:

ndarray

Returns:

Shape (dim,).

Examples

>>> pool(np.arange(6.0).reshape(3, 2), "mean").tolist(), pool(np.arange(6.0).reshape(3, 2), "last").tolist()
([2.0, 3.0], [4.0, 5.0])
class milia.represent.foundation.Foundation(model='amazon/chronos-2', pooling='mean', batch=64, device='cpu')#

Bases: TransformerMixin, BaseEstimator

Chronos-2 sequence embeddings as a scikit-learn transformer.

Each row of X is one sequence. The model loads on first use and is not pickled with the estimator.

Parameters:
  • model (str) – Hugging Face model id.

  • pooling (Literal['mean', 'last']) – "mean" over all patch positions (including the special tokens), or "last".

  • batch (int) – Sequences per forward pass.

  • device (str) – Torch device.

Examples

>>> import numpy as np
>>> Foundation().fit_transform(np.random.default_rng(0).random((4, 96))).shape
(4, 768)
fit(X, y=None)#

Record the sequence length; the model itself is pretrained and not fitted.

Parameters:
  • X (ArrayLike) – Sequences, one per row.

  • y (None) – Ignored; present for API consistency.

Return type:

Foundation

Returns:

The estimator.

pipeline()#

The loaded Chronos pipeline, downloaded and cached on first use.

Returns:

The chronos.Chronos2Pipeline.

Raises:

MissingExtraError – If the foundation extra is not installed.

transform(X)#

Embed every sequence and pool each embedding.

Parameters:

X (ArrayLike) – Sequences, one per row.

Return type:

ndarray

Returns:

Array of shape (n, dim).

Examples

>>> Foundation().fit_transform(np.random.default_rng(0).random((4, 96))).shape
(4, 768)