Skip to content

In-context learning

In-context learning (ICL) allows TiRex-2 to adapt to your data using only the context you provide, without additional training or fine-tuning. A richer context can improve forecast accuracy in two complementary ways:

  1. By extending the historical timeline to capture recurring patterns;
  2. By incorporating related signals that provide additional information about the target.

This guide demonstrates both approaches using a real industrial signal. First, we explore how a longer history helps TiRex-2 recognize recurring cycles and improve its forecast. Then, we introduce a strongly correlated signal as a covariate to show how additional information can further improve predictions.

The plots are interactive

Hover for exact values, click legend entries to show or hide traces, and use the sliders to change the context length.

Data

The data comes from the UCI hydraulic systems monitoring dataset1, from which we select two sensors: hydraulic pressure and motor power. We consider two consecutive, stable operating cycles, using the first as input context and the second as ground truth for the forecast. Each cycle captures a 60s run that has been averaged into 200 data points at a 0.3s resolution.

The preprocessed CSV contains just these two signals and two cycles, with one row per signal and cycle. Load it directly with pandas and PyTorch:

import pandas as pd
import torch

url = (
    "https://raw.githubusercontent.com/NX-AI/tirex-2/main/"
    "docs/data/in-context-learning_dataset.csv"
)
data = pd.read_csv(url).set_index(["sensor", "cycle"])

def read_signal(sensor):
    return torch.tensor(data.loc[sensor].to_numpy(), dtype=torch.float32)

# Each signal has two cycles of 200 points
pressure, motor = read_signal("pressure"), read_signal("motor")
context_cycle, target_cycle = pressure
motor_context, _ = motor

Each cycle follows the same fixed load profile, so the pattern repeats across both cycles. Although hydraulic pressure and motor power operate on very different scales, they vary together.

Forecasting the next cycle

We give TiRex-2 the first cycle as context repeated n times and ask for predicting the next 200 points:

from tirex2 import TimeseriesType, load_model

model = load_model("NX-AI/TiRex-2", device="cpu")  # or device="cuda"

n = 1                                       # number of repeated cycles in the context
context = context_cycle.repeat(n)           # (n * 200,)

ts = TimeseriesType(
    target=context.unsqueeze(0),            # (1, context_length)
    past_covariates=None,
    future_covariates=None,
)

forecast = model.forecast([ts], prediction_length=200, output_type="torch")[0]

median, p10, p90 = forecast[0, 4], forecast[0, 0], forecast[0, 8]
mae = (median - target_cycle).abs().mean().item()

The 9 quantiles correspond to levels 0.1, 0.2, ..., 0.9. We plot the median (index 4) as the point forecast and use the 0.1 and 0.9 quantiles (indices 0 and 8) to define the prediction band. Forecast accuracy is evaluated by comparing the median with the actual values of the next cycle using the mean absolute error (MAE), which measures the average absolute difference between predicted and observed values.

One cycle of context

With a single cycle of context, TiRex-2 has seen the shape only once:

The forecast is flat and cautious, with a wide band (MAE = 11.02 bar). From one cycle alone, the model cannot tell that the pattern will repeat.

ICL: context expansion

In this demonstration, we expand the context from n = 1 to 5 copies of the observed cycle (context_cycle.repeat(n)). This replays a recurring pattern to help the model recognize its periodicity; it does not add newly observed history. The next cycle remains held out for evaluation.

Try it

Move the slider or press play, to see how the forecast changes with n.

Extending the context from one to five cycles reduces the MAE by up to 88%, with most of the improvement occurring within the first three to four cycles.

ICL: variate expansion

ICL can also benefit from information across variates, not just across time. TiRex-2 can use related time series to help forecast the target, so here we provide motor power (W) as a past_covariates for hydraulic pressure. Both signals cover the same historical window, allowing the model to use their relationship when making its forecast (see Covariates):

ts = TimeseriesType(
    target=context_cycle.repeat(n).unsqueeze(0),         # (1, context_length)
    past_covariates=motor_context.repeat(n).unsqueeze(0), # (1, context_length)
    future_covariates=None,
)

forecast = model.forecast([ts], prediction_length=200, output_type="torch")[0]
# forecast.shape == (1, 9, 200)  -> only the target is forecast, not the covariate

The green forecast uses hydraulic pressure and motor power, while the red one uses hydraulic pressure only. The lower panel shows the extra signal the model sees:

Adding motor power as a covariate reduces the MAE by up to 42% compared with the univariate forecast, with the largest improvement at 2 cycles of context. The benefit gradually decreases to 19%, 13% and 5% as the hydraulic pressure history grows longer.

Scope of this example

Results reflect one context/target pair under stable conditions and may differ when operating under changing conditions. It is recommended to validate context length and covariates on held-out data from your application.

Takeaways

  • Periodic signals benefit from context spanning multiple cycles.
  • Longer context improves forecasts at little additional cost. Extending the context from 1 to 5 cycles reduces the MAE by 88%.
  • Covariates can further improve forecasts, especially when historical context is limited. Adding motor power reduces the MAE by 42% with 2 cycles of context.

  1. N. Helwig, E. Pignanelli, A. Schütze. Condition monitoring of hydraulic systems. UCI Machine Learning Repository, 2015. https://archive.ics.uci.edu/dataset/447/condition+monitoring+of+hydraulic+systems ↩