使用 Chronos 进行预测

Open In Colab Open In SageMaker Studio Lab

AutoGluon-TimeSeries (AG-TS) 包含 Chronos 系列预测模型。Chronos 模型在大量真实和合成时间序列数据集合上进行了预训练,这使得它们能够直接对新数据进行准确预测。

AG-TS 通过熟悉的 TimeSeriesPredictor API 提供了一种强大且易于使用 Chronos 的方式。本教程介绍如何

  • 零样本模式下使用 Chronos 模型进行预测,无需进行特定数据集训练

  • 在自定义数据上微调 Chronos 模型以提高准确性

  • 通过将 Chronos 与表格回归模型结合来处理协变量和静态特征

注意

v1.2 新特性: AutoGluon 现在推出 Chronos-Bolt⚡️ — 新的、更准确且速度高达 250 倍的 Chronos 模型。

Chronos 入门

作为零样本预测的预训练模型,Chronos 与 AG-TS 中提供的其他模型不同。具体来说,Chronos 模型并不是真正对时间序列数据进行 fit。然而,当调用 predict 时,它们会执行相对更昂贵的计算,计算量随数据集中时间序列的数量线性增长。在这方面,它们的行为类似于 ETS 或 ARIMA 等局部统计模型,所有计算都发生在推断期间。

AutoGluon 支持原始 Chronos 模型(例如 chronos-t5-large),以及新的、更准确且速度高达 250 倍的 Chronos-Bolt⚡ 模型(例如 chronos-bolt-base)。

开始使用 Chronos 最简单的方法是通过模型特定的预设。

  • (推荐) 新的快速 Chronos-Bolt️ 模型可以使用 "bolt_tiny""bolt_mini""bolt_small""bolt_base" 预设进行访问。

  • 原始 Chronos 模型可以使用 "chronos_tiny""chronos_mini""chronos_small""chronos_base""chronos_large" 预设进行访问。

请注意,大小为 small 及以上的原始 Chronos 模型需要 GPU 才能运行,而所有 Chronos-Bolt 模型都可以在 CPU 和 GPU 上运行。

此外,Chronos 可以通过预设 "medium_quality""high_quality""best_quality" 与其他时间序列模型结合使用。有关这些预设的更多详细信息,请参见 TimeSeriesPredictor.fit 的文档。

零样本预测

让我们使用 澳大利亚电力需求数据集 的一个子集来看看 Chronos-Bolt 的实际应用。

首先,我们将数据集加载为 TimeSeriesDataFrame

from autogluon.timeseries import TimeSeriesDataFrame, TimeSeriesPredictor
data = TimeSeriesDataFrame.from_path(
    "https://autogluon.s3.amazonaws.com/datasets/timeseries/australian_electricity_subset/test.csv"
)
data.head()
目标
item_id 时间戳
T000000 2013-03-10 00:00:00 5207.959961
2013-03-10 00:30:00 5002.275879
2013-03-10 01:00:00 4747.569824
2013-03-10 01:30:00 4544.880859
2013-03-10 02:00:00 4425.952148

接下来,我们创建 TimeSeriesPredictor 并选择 "bolt_small" 预设,以便在零样本模式下使用 Chronos-Bolt (Small, 48M) 模型。

prediction_length = 48
train_data, test_data = data.train_test_split(prediction_length)

predictor = TimeSeriesPredictor(prediction_length=prediction_length).fit(
    train_data, presets="bolt_small",
)
Sorting the dataframe index before generating the train/test split.
Beginning AutoGluon training...
AutoGluon will save models to '/home/ci/autogluon/docs/tutorials/timeseries/AutogluonModels/ag-20250508_205038'
=================== System Info ===================
AutoGluon Version:  1.3.1b20250508
Python Version:     3.11.9
Operating System:   Linux
Platform Machine:   x86_64
Platform Version:   #1 SMP Wed Mar 12 14:53:59 UTC 2025
CPU Count:          8
GPU Count:          1
Memory Avail:       28.76 GB / 30.95 GB (92.9%)
Disk Space Avail:   211.88 GB / 255.99 GB (82.8%)
===================================================
Setting presets to: bolt_small

Fitting with arguments:
{'enable_ensemble': True,
 'eval_metric': WQL,
 'hyperparameters': {'Chronos': {'model_path': 'bolt_small'}},
 'known_covariates_names': [],
 'num_val_windows': 1,
 'prediction_length': 48,
 'quantile_levels': [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9],
 'random_seed': 123,
 'refit_every_n_windows': 1,
 'refit_full': False,
 'skip_model_selection': True,
 'target': 'target',
 'verbosity': 2}
Inferred time series frequency: '30min'
Provided train_data has 172800 rows, 5 time series. Median time series length is 34560 (min=34560, max=34560).

Provided data contains following columns:
	target: 'target'

AutoGluon will gauge predictive performance using evaluation metric: 'WQL'
	This metric's sign has been flipped to adhere to being higher_is_better. The metric score can be multiplied by -1 to get the metric value.
===================================================

Starting training. Start time is 2025-05-08 20:50:39
Models that will be trained: ['Chronos[bolt_small]']
Training timeseries model Chronos[bolt_small].
	2.34    s     = Training runtime
Training complete. Models trained: ['Chronos[bolt_small]']
Total runtime: 2.52 s
Best model: Chronos[bolt_small]

正如承诺的,Chronos 不需要任何时间来 fit。调用 fit 仅仅是 TimeSeriesPredictor 在幕后执行一些任务的代理,例如推断时间序列的频率并将预测器的状态保存到磁盘。

让我们使用 predict 方法生成预测,并使用 plot 方法将其可视化。

predictions = predictor.predict(train_data)
predictor.plot(
    data=data,
    predictions=predictions,
    item_ids=data.item_ids[:2],
    max_history_length=200,
);
Model not specified in predict, will default to the model with the best validation score: Chronos[bolt_small]
../../_images/107494aeaeae3f9b24eb2bf9384668d6a52cf2fa2f8c5cab9c5a47f2f8b3c47a.png

微调

我们上面已经看到了 Chronos 模型如何在零样本模式下生成预测。AutoGluon 还使得在特定数据集上微调 Chronos 模型以最大化预测准确性变得容易。

以下代码片段为 Chronos-Bolt ️(Small) 模型指定了两个设置:零样本和微调。TimeSeriesPredictor 将在提供的训练数据上对预训练模型执行轻量级微调。我们添加名称后缀以便轻松识别模型的零样本和微调版本。

predictor = TimeSeriesPredictor(prediction_length=prediction_length).fit(
    train_data=train_data,
    hyperparameters={
        "Chronos": [
            {"model_path": "bolt_small", "ag_args": {"name_suffix": "ZeroShot"}},
            {"model_path": "bolt_small", "fine_tune": True, "ag_args": {"name_suffix": "FineTuned"}},
        ]
    },
    time_limit=60,  # time limit in seconds
    enable_ensemble=False,
)
Beginning AutoGluon training... Time limit = 60s
AutoGluon will save models to '/home/ci/autogluon/docs/tutorials/timeseries/AutogluonModels/ag-20250508_205044'
=================== System Info ===================
AutoGluon Version:  1.3.1b20250508
Python Version:     3.11.9
Operating System:   Linux
Platform Machine:   x86_64
Platform Version:   #1 SMP Wed Mar 12 14:53:59 UTC 2025
CPU Count:          8
GPU Count:          1
Memory Avail:       27.94 GB / 30.95 GB (90.3%)
Disk Space Avail:   211.88 GB / 255.99 GB (82.8%)
===================================================

Fitting with arguments:
{'enable_ensemble': False,
 'eval_metric': WQL,
 'hyperparameters': {'Chronos': [{'ag_args': {'name_suffix': 'ZeroShot'},
                                  'model_path': 'bolt_small'},
                                 {'ag_args': {'name_suffix': 'FineTuned'},
                                  'fine_tune': True,
                                  'model_path': 'bolt_small'}]},
 'known_covariates_names': [],
 'num_val_windows': 1,
 'prediction_length': 48,
 'quantile_levels': [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9],
 'random_seed': 123,
 'refit_every_n_windows': 1,
 'refit_full': False,
 'skip_model_selection': False,
 'target': 'target',
 'time_limit': 60,
 'verbosity': 2}
Inferred time series frequency: '30min'
Provided train_data has 172800 rows, 5 time series. Median time series length is 34560 (min=34560, max=34560).

Provided data contains following columns:
	target: 'target'

AutoGluon will gauge predictive performance using evaluation metric: 'WQL'
	This metric's sign has been flipped to adhere to being higher_is_better. The metric score can be multiplied by -1 to get the metric value.
===================================================

Starting training. Start time is 2025-05-08 20:50:44
Models that will be trained: ['ChronosZeroShot[bolt_small]', 'ChronosFineTuned[bolt_small]']
Training timeseries model ChronosZeroShot[bolt_small]. Training for up to 30.0s of the 59.9s of remaining time.
	-0.0417       = Validation score (-WQL)
	0.10    s     = Training runtime
	0.47    s     = Validation (prediction) runtime
Training timeseries model ChronosFineTuned[bolt_small]. Training for up to 59.3s of the 59.3s of remaining time.
Detected kernel version 4.14.355, which is below the recommended minimum of 5.5.0; this can cause the process to hang. It is recommended to upgrade the kernel to the minimum version or higher.
	Saving fine-tuned model to /home/ci/autogluon/docs/tutorials/timeseries/AutogluonModels/ag-20250508_205044/models/ChronosFineTuned[bolt_small]/W0/fine-tuned-ckpt
	-0.0296       = Validation score (-WQL)
	48.73   s     = Training runtime
	0.06    s     = Validation (prediction) runtime
Training complete. Models trained: ['ChronosZeroShot[bolt_small]', 'ChronosFineTuned[bolt_small]']
Total runtime: 49.39 s
Best model: ChronosFineTuned[bolt_small]
Best model score: -0.0296

这里我们只指定 "fine_tune": True,使用了 Chronos 的默认微调配置。然而,AutoGluon 可以轻松更改微调的其他参数,例如步数或学习率。

predictor.fit(
    ...,
    hyperparameters={"Chronos": {"fine_tune": True, "fine_tune_lr": 1e-4, "fine_tune_steps": 2000}},
)

有关微调选项的完整列表,请参阅 预测模型库 中的 Chronos 文档。

拟合后,我们可以在测试数据上评估这两个模型变体,并生成排行榜。

predictor.leaderboard(test_data)
Additional data provided, testing on additional data. Resulting leaderboard will be sorted according to test score (`score_test`).
/home/ci/autogluon/timeseries/src/autogluon/timeseries/metrics/abstract.py:101: FutureWarning: Passing `prediction_length` to `TimeSeriesScorer.__call__` is deprecated and will be removed in v2.0. Please set the `eval_metric.prediction_length` attribute instead.
  warnings.warn(
/home/ci/autogluon/timeseries/src/autogluon/timeseries/metrics/abstract.py:101: FutureWarning: Passing `prediction_length` to `TimeSeriesScorer.__call__` is deprecated and will be removed in v2.0. Please set the `eval_metric.prediction_length` attribute instead.
  warnings.warn(
模型 score_test score_val pred_time_test pred_time_val fit_time_marginal fit_order
0 ChronosFineTuned[bolt_small] -0.032100 -0.029566 0.390685 0.061748 48.729910 2
1 ChronosZeroShot[bolt_small] -0.041446 -0.041720 1.485227 0.466613 0.102889 1

微调带来了更准确的模型,如测试集上更好的 score_test 所示。

请注意,所有 AutoGluon-TimeSeries 模型都以“越高越好”的格式报告分数,这意味着报告时,大多数预测误差指标(如 WQL)都会乘以 -1。

纳入协变量

Chronos️ 是一个单变量模型,这意味着它仅依靠目标时间序列的历史数据进行预测。然而,在实际场景中,通常存在与目标系列相关的额外外部信息(例如,假期、促销)。利用这些信息进行预测可以提高预测准确性。

AG-TS 现在提供协变量回归器,可以与 Chronos-Bolt 等单变量模型结合使用以纳入外部信息。AG-TS 中的 covariate_regressor 是一个表格回归模型,它在已知协变量和静态特征上进行拟合,以在每个时间步预测目标列。协变量回归器的预测值从目标列中减去,然后单变量模型预测残差。

data = TimeSeriesDataFrame.from_path(
    "https://autogluon.s3.amazonaws.com/datasets/timeseries/grocery_sales/test.csv",
)
data.head()
scaled_price promotion_email promotion_homepage unit_sales
item_id 时间戳
1062_101 2018-01-01 0.879130 0.0 0.0 636.0
2018-01-08 0.994517 0.0 0.0 123.0
2018-01-15 1.005513 0.0 0.0 391.0
2018-01-22 1.000000 0.0 0.0 339.0
2018-01-29 0.883309 0.0 0.0 661.0

我们使用杂货销售数据集来演示 Chronos-Bolt 如何与协变量回归器结合使用。该数据集包含 3 个已知协变量:scaled_pricepromotion_emailpromotion_homepage,任务是预测 unit_sales

prediction_length = 8
train_data, test_data = data.train_test_split(prediction_length=prediction_length)

以下代码拟合 TimeSeriesPredictor 以预测未来 8 周的 unit_sales

请注意,在构建 TimeSeriesPredictor 时,我们指定了我们感兴趣的目标列和已知协变量的名称。

我们为 Chronos-Bolt 定义了两种配置

  • 零样本配置,仅使用 unit_sales 的历史值,不考虑协变量;

  • 使用 CatBoost 回归模型作为 covariate_regressor 的配置。请注意,在使用协变量回归器时,我们建议应用 target_scaler。目标缩放器可确保所有时间序列具有可比的尺度,通常能带来更好的准确性。

和之前一样,我们为模型名称添加后缀,以便在排行榜中更容易区分它们。

predictor = TimeSeriesPredictor(
    prediction_length=prediction_length,
    target="unit_sales",
    known_covariates_names=["scaled_price", "promotion_email", "promotion_homepage"],
).fit(
    train_data,
    hyperparameters={
        "Chronos": [
            # Zero-shot model WITHOUT covariates
            {
                "model_path": "bolt_small",
                "ag_args": {"name_suffix": "ZeroShot"},
            },
            # Chronos-Bolt (Small) combined with CatBoost on covariates
            {
                "model_path": "bolt_small",
                "covariate_regressor": "CAT",
                "target_scaler": "standard",
                "ag_args": {"name_suffix": "WithRegressor"},
            },
        ],
    },
    enable_ensemble=False,
    time_limit=60,
)
Beginning AutoGluon training... Time limit = 60s
AutoGluon will save models to '/home/ci/autogluon/docs/tutorials/timeseries/AutogluonModels/ag-20250508_205136'
=================== System Info ===================
AutoGluon Version:  1.3.1b20250508
Python Version:     3.11.9
Operating System:   Linux
Platform Machine:   x86_64
Platform Version:   #1 SMP Wed Mar 12 14:53:59 UTC 2025
CPU Count:          8
GPU Count:          1
Memory Avail:       27.71 GB / 30.95 GB (89.5%)
Disk Space Avail:   211.70 GB / 255.99 GB (82.7%)
===================================================

Fitting with arguments:
{'enable_ensemble': False,
 'eval_metric': WQL,
 'hyperparameters': {'Chronos': [{'ag_args': {'name_suffix': 'ZeroShot'},
                                  'model_path': 'bolt_small'},
                                 {'ag_args': {'name_suffix': 'WithRegressor'},
                                  'covariate_regressor': 'CAT',
                                  'model_path': 'bolt_small',
                                  'target_scaler': 'standard'}]},
 'known_covariates_names': ['scaled_price',
                            'promotion_email',
                            'promotion_homepage'],
 'num_val_windows': 1,
 'prediction_length': 8,
 'quantile_levels': [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9],
 'random_seed': 123,
 'refit_every_n_windows': 1,
 'refit_full': False,
 'skip_model_selection': False,
 'target': 'unit_sales',
 'time_limit': 60,
 'verbosity': 2}
Inferred time series frequency: 'W-MON'
Provided train_data has 7337 rows (NaN fraction=6.6%), 319 time series. Median time series length is 23 (min=23, max=23).

Provided data contains following columns:
	target: 'unit_sales'
	known_covariates:
		categorical:        []
		continuous (float): ['scaled_price', 'promotion_email', 'promotion_homepage']

To learn how to fix incorrectly inferred types, please see documentation for TimeSeriesPredictor.fit

AutoGluon will gauge predictive performance using evaluation metric: 'WQL'
	This metric's sign has been flipped to adhere to being higher_is_better. The metric score can be multiplied by -1 to get the metric value.
===================================================

Starting training. Start time is 2025-05-08 20:51:36
Models that will be trained: ['ChronosZeroShot[bolt_small]', 'ChronosWithRegressor[bolt_small]']
Training timeseries model ChronosZeroShot[bolt_small]. Training for up to 30.0s of the 59.9s of remaining time.
	-0.4523       = Validation score (-WQL)
	0.03    s     = Training runtime
	0.54    s     = Validation (prediction) runtime
Training timeseries model ChronosWithRegressor[bolt_small]. Training for up to 59.3s of the 59.3s of remaining time.
	-0.3564       = Validation score (-WQL)
	0.44    s     = Training runtime
	0.51    s     = Validation (prediction) runtime
Training complete. Models trained: ['ChronosZeroShot[bolt_small]', 'ChronosWithRegressor[bolt_small]']
Total runtime: 1.54 s
Best model: ChronosWithRegressor[bolt_small]
Best model score: -0.3564

预测器拟合完成后,我们可以在测试数据集上对其进行评估并生成排行榜。我们看到,利用协变量的模型在测试集上产生了更准确的预测。

predictor.leaderboard(test_data)
Additional data provided, testing on additional data. Resulting leaderboard will be sorted according to test score (`score_test`).
/home/ci/autogluon/timeseries/src/autogluon/timeseries/metrics/abstract.py:101: FutureWarning: Passing `prediction_length` to `TimeSeriesScorer.__call__` is deprecated and will be removed in v2.0. Please set the `eval_metric.prediction_length` attribute instead.
  warnings.warn(
/home/ci/autogluon/timeseries/src/autogluon/timeseries/metrics/abstract.py:101: FutureWarning: Passing `prediction_length` to `TimeSeriesScorer.__call__` is deprecated and will be removed in v2.0. Please set the `eval_metric.prediction_length` attribute instead.
  warnings.warn(
模型 score_test score_val pred_time_test pred_time_val fit_time_marginal fit_order
0 ChronosWithRegressor[bolt_small] -0.269775 -0.356361 0.582801 0.506140 0.441471 2
1 ChronosZeroShot[bolt_small] -0.318562 -0.452296 0.621163 0.538165 0.030210 1

请注意,协变量并非总是有效——对于某些数据集,零样本模型可能会达到更高的准确性。因此,尝试多种模型并选择在保留数据上达到最佳准确性的模型始终很重要。这在 AutoGluon 的 "high_quality""best_quality" 预设中是自动完成的。

常见问题

Chronos 的准确性如何?

在多次独立评估中,我们发现 Chronos 在零样本预测中非常有效。Chronos-Bolt (base) 的准确性通常超过统计基线模型,并且通常可与 TemporalFusionTransformerPatchTST 等深度学习模型相媲美。

我可以在哪里询问关于 Chronos 的具体问题?

AutoGluon 团队是 Chronos 的核心开发者之一。因此,您可以在 AutoGluon 的频道(例如 Discord 服务器GitHub)上提问 Chronos 相关问题。您也可以在 Chronos GitHub 页面上加入讨论。