!pip install -Uqq nixtla fugue[ray]
from nixtla.utils import in_colab
IN_COLAB = in_colab()
if not IN_COLAB:
    from nixtla.utils import colab_badge
    from dotenv import load_dotenv

在 Ray 上分布式运行 TimeGPT

Ray 是一个开源统一计算框架,用于扩展 Python 工作负载。在本指南中,我们将解释如何在 Ray 之上使用 TimeGPT

大纲:

  1. 安装

  2. 加载数据

  3. 初始化 Ray

  4. 在 Ray 上使用 TimeGPT

  5. 关闭 Ray

if not IN_COLAB:
    load_dotenv()
    colab_badge('docs/tutorials/19_computing_at_scale_ray_distributed')

1. 安装

通过Fugue安装Ray。Fugue提供了一个易于使用的分布式计算接口,使用户能够在多个分布式计算框架上执行Python代码,包括Ray。

Note

您可以使用 pip 安装 fugue

pip install fugue[ray]

如果在分布式 Ray 集群上执行,请确保在所有工作节点上都安装了 nixtla 库。

2. 加载数据

您可以将数据加载为 pandas DataFrame。在本教程中,我们将使用一个包含来自不同市场的每小时电力价格的数据集。

import pandas as pd 
df = pd.read_csv(
    'https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/electricity-short.csv',
    parse_dates=['ds'],
) 
df.head()
unique_id ds y
0 BE 2016-10-22 00:00:00 70.00
1 BE 2016-10-22 01:00:00 37.10
2 BE 2016-10-22 02:00:00 37.10
3 BE 2016-10-22 03:00:00 44.75
4 BE 2016-10-22 04:00:00 37.10

3. 初始化 Ray

初始化 Ray 并将 pandas DataFrame 转换为 Ray DataFrame。

import ray
from ray.cluster_utils import Cluster
ray_cluster = Cluster(
    initialize_head=True,
    head_node_args={"num_cpus": 2}
)
ray.init(address=ray_cluster.address, ignore_reinit_error=True)
2024-05-10 11:09:17,240 WARNING cluster_utils.py:157 -- Ray cluster mode is currently experimental and untested on Windows. If you are using it and running into issues please file a report at https://github.com/ray-project/ray/issues.
2024-05-10 11:09:19,076 INFO worker.py:1564 -- Connecting to existing Ray cluster at address: 127.0.0.1:63694...
2024-05-10 11:09:19,092 INFO worker.py:1740 -- Connected to Ray cluster. View the dashboard at 127.0.0.1:8265 
ray_df = ray.data.from_pandas(df)
ray_df 

4. 在Ray上使用TimeGPT

Ray之上使用TimeGPT与非分布式情况下几乎相同。唯一的区别是您需要使用Ray数据框。

首先,实例化NixtlaClient类。

from nixtla import NixtlaClient
nixtla_client = NixtlaClient(
    # defaults to os.environ.get("NIXTLA_API_KEY")
    api_key = 'my_api_key_provided_by_nixtla'
)

👍 使用 Azure AI 端点

要使用 Azure AI 端点,请设置 base_url 参数:

nixtla_client = NixtlaClient(base_url="你的 Azure AI 端点", api_key="你的 API 密钥")

if not IN_COLAB:
    nixtla_client = NixtlaClient()

然后使用 NixtlaClient 类中的任何方法,例如 forecastcross_validation

%%capture
fcst_df = nixtla_client.forecast(ray_df, h=12)

📘 Azure AI 中可用模型

如果您正在使用 Azure AI 端点,请确保设置 model="azureai":

nixtla_client.forecast(..., model="azureai")

对于公共 API,我们支持两种模型:timegpt-1timegpt-1-long-horizon

默认情况下,使用 timegpt-1。有关如何以及何时使用 timegpt-1-long-horizon 的信息,请参见 本教程

为了可视化结果,使用 to_pandas 方法将 Ray 的输出转换为 pandas DataFrame。

fcst_df.to_pandas().tail()
unique_id ds TimeGPT
55 NP 2018-12-24 07:00:00 55.387066
56 NP 2018-12-24 08:00:00 56.115517
57 NP 2018-12-24 09:00:00 56.090714
58 NP 2018-12-24 10:00:00 55.813717
59 NP 2018-12-24 11:00:00 55.528519
%%capture
cv_df = nixtla_client.cross_validation(ray_df, h=12, freq='H', n_windows=5, step_size=2)
cv_df.to_pandas().tail()
unique_id ds cutoff TimeGPT
295 NP 2018-12-23 19:00:00 2018-12-23 11:00:00 53.632019
296 NP 2018-12-23 20:00:00 2018-12-23 11:00:00 52.512775
297 NP 2018-12-23 21:00:00 2018-12-23 11:00:00 51.894035
298 NP 2018-12-23 22:00:00 2018-12-23 11:00:00 51.06572
299 NP 2018-12-23 23:00:00 2018-12-23 11:00:00 50.32592

您还可以在 Ray 上使用 TimeGPT 的外生变量。要做到这一点,请参考 外生变量 教程。请记住,您需要使用 Ray 数据框,而不是使用 pandas 数据框。

5. 关闭 Ray

完成后,关闭 Ray 会话。

ray.shutdown()

Give us a ⭐ on Github