Skip to main content

Introduction

MLFlow is an open-source platform that allows you, among other things, to track experiments to compare different hyperparameters and results, and to serve ML models on different platforms. In this tutorial, we provide basic scripts that you can use to track experiments made with TimeGPT, or to serve TimeGPT through MLFLow. Each script is customizable to your own needs. The goal is to provide an easy-to-use template that you can extend.

Experiment Tracking with TimeGPT and MLFlow

The following scripts provide functions for logging experiment results when testing different parameter combinations in TimeGPT. Of course, to use these scripts make sure to have MLFLow installed and any other required dependencies. To install MLFlow, run pip install mlflow. Here, we use the following packages:
In all scripts, we require that you initialize the NixtlaClient and pass it to the functions. You can see how to setup your client with an API key here.

Logging experiments with forecasting

The following script can be used to log results when using the forecast method.
If you want add more explicit parameters for fine-tuning or reproduce the exact type hints of the SDK, refer to the SDK reference.

Logging experiments with cross-validation

The following is a simple script to log metrics when performing experiments with the cross_validation method. This script requires utilsforecast to compute performance metrics, so make sure pip install utilsforecast. The script allows you to log overall performance metrics across multiple windows, and also per-window metrics.
If you want add more explicit parameters for fine-tuning or reproduce the exact type hints of the SDK, refer to the SDK reference.

Logging experiments with anomaly detection

This script showcases how you can track experiments done with the detect_anomalies_online method.
If you want add more explicit parameters for fine-tuning or reproduce the exact type hints of the SDK, refer to the SDK reference.

Sample usage

With the above functions, you can now run experiments and log it to MLFLow. First, you must instantiate your client using either:
Or, if you are using a Python wheel:
Then, load your data. Here, we use the simple air passengers dataset.
After, you can set your tracking URI and experiment name for MLFlow. Note that here, we use the local filesystem for tracking.
Finally, you can run any function defined above.

Serving TimeGPT with MLFlow

You can also use MLFlow for model serving. This can be useful if you need a unified interface and serve the model on many endpoints. Although it is not required to use MLFlow to use TimeGPT, it can be a practice your organization enforces. The following script shows how you can wrap the TimeGPT model in an mlflow.pyfunc.PythonModel to save the model and call it.

Sample usage

First, you must instantiate your client using either:
Or, if you are using a Python wheel:
Then, load your data. Here, we use the simple air passengers dataset.
Then, save the model with:
Note that if you want to use another variant of TimeGPT, say timegpt-2, then you must save another instance and specify that model variant. Now, you can perform forecasting, cross-validation and anomaly detection with the saved model in MLFlow.