timeseriesAI / tsai

Time series Timeseries Deep Learning Machine Learning Python Pytorch fastai | State-of-the-art Deep Learning library for Time Series and Sequences in Pytorch / fastai
https://timeseriesai.github.io/tsai/
Apache License 2.0
5.1k stars 639 forks source link

Multivariate Regression and Forecasting basic tutorials throw an error #629

Closed andreakiro closed 1 year ago

andreakiro commented 1 year ago

I wanted to test the library for multivariate regression and time series forecasting (single and multi steps ahead), but the very basic tutorials in the doc and in the README.md fails for both tasks and all throw the same error. The error is an AttributeError and occurs when calling the final $model.get_X_preds($args) function. Below the mentioned code, the exact error trace and my running environment details in case it proves useful.

Environment

$ python3 --version
> Python 3.10.8
$ pip3 show tsai
> Version: 0.3.4

Code snippet

from tsai.all import *

# for multivariate regression
X, y, splits = get_regression_data('AppliancesEnergy', split_data=False)
batch_tfms = TSStandardize(by_sample=True)
reg = TSRegressor(X, y, splits=splits, path='models', arch=TSTPlus, batch_tfms=batch_tfms, metrics=rmse)
reg.fit_one_cycle(100, 3e-4)
raw_preds, target, preds = reg.get_X_preds(X[splits[0]], y[splits[0]])

# for time series forecasting
ts = get_forecasting_time_series("Sunspots").values
X, y = SlidingWindow(60, horizon=$x)(ts)
splits = TimeSplitter(235)(y) 
batch_tfms = TSStandardize()
fcst = TSForecaster(X, y, splits=splits, path='models', batch_tfms=batch_tfms, bs=512, arch=TSTPlus, metrics=mae)
fcst.fit_one_cycle(50, 1e-3)
raw_preds, target, preds = fcst.get_X_preds(X[splits[0]], y[splits[0]])

Error trace

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In [3], line 3
      1 from tsai.inference import load_learner
      2 reg = load_learner("models/reg.pkl")
----> 3 raw_preds, target, preds = reg.get_X_preds(X[splits[0]], y[splits[0]])

File ~/miniconda3-intel/envs/tsa-310/lib/python3.10/site-packages/tsai/inference.py:17, in get_X_preds(self, X, y, bs, with_input, with_decoded, with_loss)
     15     print("cannot find loss as y=None")
     16     with_loss = False
---> 17 dl = self.dls.valid.new_dl(X, y=y, bs=bs)
     18 output = list(self.get_preds(dl=dl, with_input=with_input, with_decoded=with_decoded, with_loss=with_loss, reorder=False))
     19 if with_decoded and len(self.dls.tls) >= 2 and hasattr(self.dls.tls[-1], "tfms") and hasattr(self.dls.tls[-1].tfms, "decodes"):

File ~/miniconda3-intel/envs/tsa-310/lib/python3.10/site-packages/tsai/data/core.py:537, in NumpyDataLoader.new_dl(self, X, y, bs)
    535 if y is not None: 
    536     y = listify(y)
--> 537 ds = self.dataset.add_dataset(X, y=y)
    538 return self.new(ds, bs=min(bs, len(X)))

File ~/miniconda3-intel/envs/tsa-310/lib/python3.10/site-packages/tsai/data/core.py:489, in add_dataset(self, X, y, inplace)
    487 @patch
    488 def add_dataset(self:NumpyDatasets, X, y=None, inplace=True):
--> 489     return add_ds(self, X, y=y, inplace=inplace)
...

File ~/miniconda3-intel/envs/tsa-310/lib/python3.10/site-packages/tsai/data/core.py:120, in ToFloat.encodes(self, o)
--> 120 def encodes(self, o): return o.astype(np.float32)

AttributeError: 'list' object has no attribute 'astype'
oguiza commented 1 year ago

Hi @andreakiro,

Thank you for raising this issue. It's indeed a bug. I've already fixed it in GitHub. If you use the pip install from GitHub method and rerun your code it should work well:

   pip install git+https://github.com/timeseriesAI/tsai.git

You can see the test in this gist.

It'd be good if you can test it works and close the issue.

andreakiro commented 1 year ago

Yes, it now works on my machine as well, thanks for the super quick fix!