Sm Jumpstart Time Series Forecasting
Introduction to SageMaker JumpStart - Time Series Forecasting with Chronos
This notebook's CI test result for us-west-2 is as follows. CI test results in other regions can be found at the end of the notebook.
In this demo notebook, we demonstrate how to use the SageMaker Python SDK to deploy a SageMaker JumpStart time series forecasting model and invoke the endpoint.
Setup
First, upgrade to the latest sagemaker SDK to ensure all available models are deployable.
Note: you may need to restart the kernel to use updated packages.
Select the desired model to deploy. The provided dropdown filters all time series forecasting models available in SageMaker JumpStart.
sagemaker.config INFO - Not applying SDK defaults from location: /etc/xdg/sagemaker/config.yaml sagemaker.config INFO - Not applying SDK defaults from location: /home/shchuro/.config/sagemaker/config.yaml
Dropdown(description='Select a JumpStart time series forecasting model:', index=2, layout=Layout(width='max-co…
Deploy model
Create a JumpStartModel object, which initializes default model configurations conditioned on the selected instance type. JumpStart already sets a default instance type, but you can deploy the model on other instance types by passing instance_type to the JumpStartModel class.
You can now deploy the model using SageMaker JumpStart. The deployment might take a few minutes.
----------!
Invoke the endpoint
This section demonstrates how to invoke the endpoint using example payloads that are retrieved programmatically from the JumpStartModel object. You can replace these example payloads with your own payloads.
Input:
{'inputs': [{'target': [0.0, 4.0, 5.0, 1.5, -3.0, -5.0, -3.0, 1.5, 5.0, 4.0, 0.0, -4.0, -5.0, -1.5, 3.0, 5.0, 3.0, -1.5, -5.0, -4.0]}],
'parameters': {'prediction_length': 10}}
Output:
{'predictions': [{'mean': [-0.488, 3.101, 3.086, 0.436, -2.867, -3.924, -2.258, 0.686, 2.456, 2.089],
'0.1': [-4.331, 1.351, -0.04, -3.467, -5.713, -5.051, -5.056, -3.247, -1.907, -1.898],
'0.5': [0.0, 3.507, 4.012, 0.0, -3.003, -4.903, -3.015, 1.501, 3.003, 3.003],
'0.9': [1.652, 4.997, 4.997, 4.11, 0.0, -2.215, 1.602, 4.997, 4.997, 4.997]}]}
===============
Input:
{'inputs': [{'target': [1.0, 2.0, 3.0, 2.0, 0.5, 2.0, 3.0, 2.0, 1.0], 'item_id': 'product_A', 'start': '2024-01-01T01:00:00'},
{'target': [5.4, 3.0, 3.0, 2.0, 1.5, 2.0, -1.0], 'item_id': 'product_B', 'start': '2024-02-02T03:00:00'}],
'parameters': {'prediction_length': 5, 'freq': '1h', 'quantile_levels': [0.05, 0.5, 0.95], 'num_samples': 30, 'batch_size': 2}}
Output:
{'predictions': [{'mean': [1.731, 1.498, 1.764, 1.632, 1.465],
'0.05': [0.224, 0.497, 0.224, 0.0, 0.0],
'0.5': [0.995, 0.995, 1.25, 1.499, 0.995],
'0.95': [4.005, 2.997, 3.278, 3.999, 3.544],
'item_id': 'product_A',
'start': '2024-01-01T10:00:00'},
{'mean': [0.084, 0.916, 0.384, 1.205, 1.481],
'0.05': [-1.273, -0.726, -1.537, -0.358, -0.863],
'0.5': [0.0, 0.872, 0.0, 1.012, 1.059],
'0.95': [2.006, 3.109, 2.552, 3.0, 4.887],
'item_id': 'product_B',
'start': '2024-02-02T10:00:00'}]}
===============
The payload for Chronos models must be structured as follows.
- inputs (required): List with at most 64 time series that need to be forecasted. Each time series is represented by a dictionary with the following keys:
- target (required): List of observed numeric time series values.
- It is recommended that each time series contains at least 30 observations.
- If any time series contains fewer than 5 observations, an error will be raised.
- item_id: String that uniquely identifies each time series.
- If provided, the ID must be unique for each time series.
- If provided, then the endpoint response will also include the item_id field for each forecast.
- start: Timestamp of the first time series observation in ISO format (
YYYY-MM-DDorYYYY-MM-DDThh:mm:ss).- If start field is provided, then freq must also be provided as part of parameters.
- If provided, then the endpoint response will also include the start field indicating the first timestamp of each forecast.
- target (required): List of observed numeric time series values.
- parameters: Optional parameters to configure the model.
- prediction_length: Integer corresponding to the number of future time series values that need to be predicted.
- Recommended to keep prediction_length <= 64 since larger values will result in inaccurate quantile forecasts. Values above 1000 will raise an error.
- quantile_levels: List of floats in range (0, 1) specifying which quantiles should should be included in the probabilistic forecast. Defaults to
[0.1, 0.5, 0.9]. - freq: Frequency of the time series observations in pandas-compatible format. For example,
1hfor hourly data or2Wfor bi-weekly data.- If freq is provided, then start must also be provided for each time series in inputs.
- num_samples: Number of sample trajectories generated by the Chronos model during inference. Larger values may improve accuracy but increase memory consumption and slow down inference. Defaults to
20. - batch_size: Number of time series processed in parallel by the model. Larger values speed up inference but may lead to out of memory errors.
- prediction_length: Integer corresponding to the number of future time series values that need to be predicted.
All keys not marked with (required) are optional.
The endpoint response contains the probabilistic (quantile) forecast for each time series included in the request.
Clean up the endpoint
Don't forget to clean up resources when finished to avoid unnecessary charges.
Notebook CI Test Results
This notebook was tested in multiple regions. The test results are as follows, except for us-west-2 which is shown at the top of the notebook.