Skip to main content

Practical Deep Learning for Time Series / Sequential Data library based on fastai & Pytorch

Project description

tsai



CI PyPI DOI PRs

Description

State-of-the-art Deep Learning library for Time Series and Sequences.

tsai is an open-source deep learning package built on top of Pytorch & fastai focused on state-of-the-art techniques for time series tasks like classification, regression, forecasting, imputation…

tsai is currently under active development by timeseriesAI.

What’s new:

During the last few releases, here are some of the most significant additions to tsai:

  • New models: PatchTST (Accepted by ICLR 2023), RNN with Attention (RNNAttention, LSTMAttention, GRUAttention), TabFusionTransformer, …
  • New datasets: we have increased the number of datasets you can download using tsai:
    • 128 univariate classification datasets
    • 30 multivariate classification datasets
    • 15 regression datasets
    • 62 forecasting datasets
    • 9 long term forecasting datasets
  • New tutorials: PatchTST. Based on some of your requests, we are planning to release additional tutorials on data preparation and forecasting.
  • New functionality: sklearn-type pipeline transforms, walk-foward cross validation, reduced RAM requirements, and a lot of new functionality to perform more accurate time series forecasts.
  • Pytorch 2.0 support.

Installation

Installation

Requires Python 3.10 or newer. As of tsai 1.0.0, support for Python 3.9 has been dropped. See the CHANGELOG for full upgrade notes if you’re coming from 0.x.

Pip install

Install the latest stable version from PyPI:

pip install tsai

For an editable install (development or bleeding-edge):

git clone https://github.com/timeseriesAI/tsai
pip install -e "tsai[dev]"

tsai installs only hard dependencies by default. Optional dependencies (sktime, tsfresh, PyWavelets, nbformat) are only needed for selected tasks; tsai will prompt you to install them when required. To install everything up front:

pip install tsai[extras]

Conda install

As of tsai 1.0.0 the conda channel is no longer updated; tsai is distributed via PyPI only. Older releases remain available on the timeseriesai conda channel, but for new installs please use pip.

Documentation

Here’s the link to the documentation.

Available models:

Here’s a list with some of the state-of-the-art models available in tsai:

plus other custom models like: TransformerModel, LSTMAttention, GRUAttention, …

How to start using tsai?

To get to know the tsai package, we’d suggest you start with this notebook in Google Colab: 01_Intro_to_Time_Series_Classification It provides an overview of a time series classification task.

We have also develop many other tutorial notebooks.

To use tsai in your own notebooks, the only thing you need to do after you have installed the package is to run this:

from tsai.all import *

Examples

These are just a few examples of how you can use tsai:

Binary, univariate classification

Training:

from tsai.basics import *

X, y, splits = get_classification_data('ECG200', split_data=False)
tfms = [None, TSClassification()]
batch_tfms = TSStandardize()
clf = TSClassifier(X, y, splits=splits, path='models', arch="InceptionTimePlus", tfms=tfms, batch_tfms=batch_tfms, metrics=accuracy, cbs=ShowGraph())
clf.fit_one_cycle(100, 3e-4)
clf.export("clf.pkl") 

Inference:

from tsai.inference import load_learner

clf = load_learner("models/clf.pkl")
probas, target, preds = clf.get_X_preds(X[splits[1]], y[splits[1]])

Multi-class, multivariate classification

Training:

from tsai.basics import *

X, y, splits = get_classification_data('LSST', split_data=False)
tfms = [None, TSClassification()]
batch_tfms = TSStandardize(by_sample=True)
mv_clf = TSClassifier(X, y, splits=splits, path='models', arch="InceptionTimePlus", tfms=tfms, batch_tfms=batch_tfms, metrics=accuracy, cbs=ShowGraph())
mv_clf.fit_one_cycle(10, 1e-2)
mv_clf.export("mv_clf.pkl")

Inference:

from tsai.inference import load_learner

mv_clf = load_learner("models/mv_clf.pkl")
probas, target, preds = mv_clf.get_X_preds(X[splits[1]], y[splits[1]])

Multivariate Regression

Training:

from tsai.basics import *

X, y, splits = get_regression_data('AppliancesEnergy', split_data=False)
tfms = [None, TSRegression()]
batch_tfms = TSStandardize(by_sample=True)
reg = TSRegressor(X, y, splits=splits, path='models', arch="TSTPlus", tfms=tfms, batch_tfms=batch_tfms, metrics=rmse, cbs=ShowGraph(), verbose=True)
reg.fit_one_cycle(100, 3e-4)
reg.export("reg.pkl")

Inference:

from tsai.inference import load_learner

reg = load_learner("models/reg.pkl")
raw_preds, target, preds = reg.get_X_preds(X[splits[1]], y[splits[1]])

The ROCKETs (RocketClassifier, RocketRegressor, MiniRocketClassifier, MiniRocketRegressor, MiniRocketVotingClassifier or MiniRocketVotingRegressor) are somewhat different models. They are not actually deep learning models (although they use convolutions) and are used in a different way.

⚠️ You’ll also need to install sktime to be able to use them. You can install it separately:

pip install sktime

or use:

pip install tsai[extras]

Training:

from sklearn.metrics import mean_squared_error, make_scorer
from tsai.data.external import get_Monash_regression_data
from tsai.models.MINIROCKET import MiniRocketRegressor

X_train, y_train, *_ = get_Monash_regression_data('AppliancesEnergy')
rmse_scorer = make_scorer(mean_squared_error, greater_is_better=False)
reg = MiniRocketRegressor(scoring=rmse_scorer)
reg.fit(X_train, y_train)
reg.save('MiniRocketRegressor')

Inference:

from sklearn.metrics import mean_squared_error
from tsai.data.external import get_Monash_regression_data
from tsai.models.MINIROCKET import load_minirocket

*_, X_test, y_test = get_Monash_regression_data('AppliancesEnergy')
reg = load_minirocket('MiniRocketRegressor')
y_pred = reg.predict(X_test)
mean_squared_error(y_test, y_pred, squared=False)

Forecasting

You can use tsai for forecast in the following scenarios:

  • univariate or multivariate time series input
  • univariate or multivariate time series output
  • single or multi-step ahead

You’ll need to: * prepare X (time series input) and the target y (see documentation) * select PatchTST or one of tsai’s models ending in Plus (TSTPlus, InceptionTimePlus, TSiTPlus, etc). The model will auto-configure a head to yield an output with the same shape as the target input y.

Single step

Training:

from tsai.basics import *

ts = get_forecasting_time_series("Sunspots").values
X, y = SlidingWindow(60, horizon=1)(ts)
splits = TimeSplitter(235)(y) 
tfms = [None, TSForecasting()]
batch_tfms = TSStandardize()
fcst = TSForecaster(X, y, splits=splits, path='models', tfms=tfms, batch_tfms=batch_tfms, bs=512, arch="TSTPlus", metrics=mae, cbs=ShowGraph())
fcst.fit_one_cycle(50, 1e-3)
fcst.export("fcst.pkl")

Inference:

from tsai.inference import load_learner

fcst = load_learner("models/fcst.pkl", cpu=False)
raw_preds, target, preds = fcst.get_X_preds(X[splits[1]], y[splits[1]])
raw_preds.shape
# torch.Size([235, 1])

Multi-step

This example show how to build a 3-step ahead univariate forecast.

Training:

from tsai.basics import *

ts = get_forecasting_time_series("Sunspots").values
X, y = SlidingWindow(60, horizon=3)(ts)
splits = TimeSplitter(235, fcst_horizon=3)(y) 
tfms = [None, TSForecasting()]
batch_tfms = TSStandardize()
fcst = TSForecaster(X, y, splits=splits, path='models', tfms=tfms, batch_tfms=batch_tfms, bs=512, arch="TSTPlus", metrics=mae, cbs=ShowGraph())
fcst.fit_one_cycle(50, 1e-3)
fcst.export("fcst.pkl")

Inference:

from tsai.inference import load_learner
fcst = load_learner("models/fcst.pkl", cpu=False)
raw_preds, target, preds = fcst.get_X_preds(X[splits[1]], y[splits[1]])
raw_preds.shape
# torch.Size([235, 3])

Input data format

The input format for all time series models and image models in tsai is the same. An np.ndarray (or array-like object like zarr, etc) with 3 dimensions:

[# samples x # variables x sequence length]

The input format for tabular models in tsai (like TabModel, TabTransformer and TabFusionTransformer) is a pandas dataframe. See example.

How to contribute to tsai?

We welcome contributions of all kinds. Development of enhancements, bug fixes, documentation, tutorial notebooks, …

We have created a guide to help you start contributing to tsai. You can read it here.

Enterprise support and consulting services:

Want to make the most out of timeseriesAI/tsai in a professional setting? Let us help. Send us an email to learn more: info@timeseriesai.co

Citing tsai

If you use tsai in your research please use the following BibTeX entry:

@Misc{tsai,
    author =       {Ignacio Oguiza},
    title =        {tsai - A state-of-the-art deep learning library for time series and sequential data},
    howpublished = {Github},
    year =         {2023},
    url =          {https://github.com/timeseriesAI/tsai}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tsai-1.0.1.tar.gz (281.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tsai-1.0.1-py3-none-any.whl (332.1 kB view details)

Uploaded Python 3

File details

Details for the file tsai-1.0.1.tar.gz.

File metadata

  • Download URL: tsai-1.0.1.tar.gz
  • Upload date:
  • Size: 281.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for tsai-1.0.1.tar.gz
Algorithm Hash digest
SHA256 7ef31c7de6bf5ff551a5e3f0fbae8ee92a2cc550b1381e82485be7cd724bc012
MD5 966436a4b2bbf7cbf659ad74e454aff2
BLAKE2b-256 080b58ebbc12e8b50b925fbaabc37fdd6b56b793f78b0c3df3daf5bad60a6c58

See more details on using hashes here.

File details

Details for the file tsai-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: tsai-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 332.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for tsai-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ad12b6b9cdef99cb041fbf0f4b4ebd7b566b10ed5c6ce7ddd99d38452d103977
MD5 e750ab13c027a6d1290c1aa0367db8ea
BLAKE2b-256 ca3c5cd6859cc7e474d9dfc772934a5f14a026ebf56c470ea83c479bfbcfad4b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page