forked from intel-analytics/ipex-llm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chronos: how to create a forecaster (intel-analytics#5172)
* how to guide from_tsdataset * improve md * remove redundant message * fix known issues * add rst * move create-forecaster to new line * fix known issue * fix syntax error * fix syntax error again * add open-colab icon * fix known issues * fix some words * fix typo * fix syntax error * add some update Co-authored-by: theaperdeng <[email protected]>
- Loading branch information
1 parent
b2349b2
commit e200597
Showing
2 changed files
with
211 additions
and
1 deletion.
There are no files selected for viewing
206 changes: 206 additions & 0 deletions
206
docs/readthedocs/source/doc/Chronos/Howto/how-to-create-forecaster.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/intel-analytics/BigDL/blob/main/docs/readthedocs/source/doc/Chronos/Howto/how-to-create-forecaster.ipynb)\n", | ||
"\n", | ||
"<style>\n", | ||
" .rst-content blockquote {\n", | ||
" margin-left: 0px;\n", | ||
" }\n", | ||
" \n", | ||
" blockquote > div {\n", | ||
" margin: 1.5625em auto;\n", | ||
" padding: 20px 15px 1px;\n", | ||
" border-left: 0.2rem solid rgb(59, 136, 219); \n", | ||
" border-radius: 0.2rem;\n", | ||
" box-shadow: 0 0.2rem 0.5rem rgb(0 0 0 / 5%), 0 0 0.0625rem rgb(0 0 0 / 10%);\n", | ||
" }\n", | ||
"</style>\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"![image.png](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJsAAABHCAMAAAAnQ8XqAAAACXBIWXM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AIYwjRJTgAjNdLil1g3K4AAAAASUVORK5CYII=)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# How to create a Forecaster" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Introduction\n", | ||
"\n", | ||
"In Chronos, Forecaster (`bigdl.chronos.forecaster.Forecaster`) is the forecasting abstraction. It hides the complex logic of model's creation, training, scaling to cluster, tuning, optimization and inferencing while expose some APIs for users to control.\n", | ||
"\n", | ||
"In this guide, we will use the `TCNForecaster` and nyc_taxi datasets as examples to describe **how to create a Forecaster**." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Prepare Environments" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Before creating a forecaster, we need to install Chronos. Chronos supports deep learning backend implemented by pytorch and tensorflow and machine learning methods based on arima and prophet." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# uncomment following 1 lines for pytorch backend\n", | ||
"!pip install --pre --upgrade bigdl-chronos[pytorch]\n", | ||
"\n", | ||
"# uncomment following 2 lines for tensorflow backend\n", | ||
"# !pip install --pre --upgrade bigdl-chronos\n", | ||
"# !pip install --pre --upgrade bigdl-nano[tensorflow]\n", | ||
"\n", | ||
"# installation trick on colab (no need to do these on your own environment)\n", | ||
"!pip uninstall torchtext -y\n", | ||
"exit()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Create a forecaster\n", | ||
"We provide two ways to create a Forecaster.\n", | ||
"\n", | ||
"* Create by `Forecaster.from_tsdataset`(**Recommended if valid**)\n", | ||
"* Create by `Forecaster(...)` directly" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Before creating a Forecaster, We need to know the four parameters `past_seq_len`, `future_seq_len`, `input_feature_num`, `output_feature_num`, which represent the time step and feature column, As shown below.\n", | ||
"\n", | ||
"<img src=\"../Image/forecast-RR.png\" title=\"time series\" style=\"width:600px\">\n", | ||
"\n", | ||
"* **past_seq_len**: Sampled input length, represents the history time step length. (i.e. lookback)\n", | ||
"* **future_seq_len**: Sampled output length, represents the output time step length.(i.e. horizon)\n", | ||
"* **input_feature_num**: All feature column(s), including extra feature column(s) and target column(s).\n", | ||
"* **output_feature_num**: Only target column(s).\n", | ||
"\n", | ||
"More Forecaster info, please refer to [Time Series Forecasting OverView](https://bigdl.readthedocs.io/en/latest/doc/Chronos/Overview/forecasting.html#time-series-forecasting-overview)\n", | ||
"\n", | ||
"If you want to create a traditional statistic forecaster(e.g. [ProphetForecaster](https://bigdl.readthedocs.io/en/latest/doc/PythonAPI/Chronos/forecasters.html#prophetforecaster) or [ARIMAForecaster](https://bigdl.readthedocs.io/en/latest/doc/PythonAPI/Chronos/forecasters.html#arimaforecaster)), you may refer to their API doc directly since they are relatively easy and do not have required parameters to create them." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# create a TSDataset\n", | ||
"from bigdl.chronos.data.repo_dataset import get_public_dataset\n", | ||
"\n", | ||
"tsdataset = get_public_dataset('nyc_taxi', with_split=False)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Forecaster.from_tsdataset\n", | ||
"\n", | ||
"`from_tsdataset` is a classmethod, so you can call `Forecsater.from_tsdataset`, then input a `TSDataset` instance, where `TSDataset` is a built-in time series preprocessing class.\n", | ||
"\n", | ||
"If the `roll` or `to_torch_data_loader` method has been called by tsdataset, `past_seq_len` and `future_seq_len` do not need to be specified for from_tsdataset, otherwise both must be specified." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# uncomment following 1 lines for pytorch backend\n", | ||
"from bigdl.chronos.forecaster import TCNForecaster\n", | ||
"\n", | ||
"# uncomment following 1 lines for tensorflow backend\n", | ||
"# from bigdl.chronos.forecaster.tf import TCNForecaster\n", | ||
"\n", | ||
"tcn = TCNForecaster.from_tsdataset(tsdataset,\n", | ||
" past_seq_len=48,\n", | ||
" future_seq_len=5)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"> 📝 **Note**\n", | ||
">\n", | ||
"> We recommend to use `Forecsater.from_tsdataset` if possible. While for some reasons, some forecasters (e.g. `ProphetForecaster` and `ARIMAForecaster`) does not support this API. Or maybe you want to process your data customizedly without using `TSDataset`, you may create a forecaster directly by calling `Forecaster(...)`." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Create a forecaster directly\n", | ||
"You can also create forecaster directly, the parameters mentioned above still need to be specified." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"tcn = TCNForecaster(past_seq_len=48,\n", | ||
" future_seq_len=5,\n", | ||
" input_feature_num=2,\n", | ||
" output_feature_num=2)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3.7.13 ('chronos-deps')", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.7.13" | ||
}, | ||
"vscode": { | ||
"interpreter": { | ||
"hash": "1a7f5d41b94c9ba67b8a1438ec071a63464312bab4ac0991ee31faebf1c9c228" | ||
} | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters