最先进的时间序列和序列深度学习库。
tsai
是一个基于 Pytorch 和 fastai 构建的开源深度学习包,专注于时间序列任务(如分类、回归、预测、插补等)的最先进技术......
tsai
目前正在由 timeseriesAI 积极开发。
在最近的几个版本中,以下是tsai
的一些最重要的新增内容:
tsai
下载的数据集数量:您可以使用以下命令从 pip 安装最新的稳定版本:
pip install tsai
如果您打算自己开发 tsai,或者想要处于最前沿,您可以使用可编辑安装。首先安装 PyTorch,然后:
git clone https : // github . com / timeseriesAI / tsai
pip install - e "tsai[dev]"
注意:从 tsai 0.3.0 开始,tsai 将仅安装硬依赖项。默认情况下不会安装其他软依赖项(仅选定任务所需)(这是推荐的方法。如果您需要任何未安装的依赖项,tsai 会在必要时要求您安装)。如果您仍然想安装 tsai 及其所有依赖项,您可以通过运行以下命令来完成:
pip install tsai [ extras ]
您还可以使用 conda 安装 tsai(请注意,如果将 conda 替换为 mamba,安装过程将更快、更可靠):
conda install - c timeseriesai tsai
这是文档的链接。
以下是tsai
提供的一些最先进模型的列表:
加上其他自定义模型,例如:TransformerModel、LSTMAttention、GRUAttention,...
要了解 tsai 包,我们建议您从 Google Colab 中的这个笔记本开始: 01_Intro_to_Time_Series_Classification它提供了时间序列分类任务的概述。
我们还开发了许多其他教程笔记本。
要在您自己的笔记本中使用 tsai,安装该软件包后您唯一需要做的就是运行以下命令:
from tsai . all import *
这些只是如何使用tsai
的几个示例:
训练:
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" )
推理:
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 ]])
训练:
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" )
推理:
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 ]])
训练:
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" )
推理:
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 ]])
ROCKET(RocketClassifier、RocketRegressor、MiniRocketClassifier、MiniRocketRegressor、MiniRocketVotingClassifier 或 MiniRocketVotingRegressor)是有些不同的模型。它们实际上并不是深度学习模型(尽管它们使用卷积)并且以不同的方式使用。
pip install sktime
或使用:
pip install tsai [ extras ]
训练:
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' )
推理:
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 )
您可以在以下场景中使用 tsai 进行预测:
您需要: * 准备 X(时间序列输入)和目标 y(请参阅文档) * 选择 PatchTST 或以 Plus 结尾的 tsai 模型之一(TSTPlus、InceptionTimePlus、TSiTPlus 等)。该模型将自动配置头部以产生与目标输入 y 形状相同的输出。
训练:
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" )
推理:
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])
此示例展示如何构建提前 3 步的单变量预测。
训练:
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" )
推理:
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])
tsai中所有时间序列模型和图像模型的输入格式都是相同的。具有 3 个维度的 np.ndarray (或类似数组的对象,如 zarr 等):
[#样本x#变量x序列长度]
tsai 中表格模型的输入格式(如 TabModel、TabTransformer 和 TabFusionTransformer)是 pandas 数据框。参见示例。
我们欢迎各种贡献。开发增强功能、错误修复、文档、教程笔记本……
我们创建了一份指南来帮助您开始为 tsai 做出贡献。您可以在这里阅读。
想要在专业环境中充分利用 timeseriesAI/tsai 的优势吗?让我们帮忙吧。给我们发送电子邮件以了解更多信息:[email protected]
如果您在研究中使用 tsai,请使用以下 BibTeX 条目:
@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}
}