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
4.95k stars 625 forks source link

Training is fine, but can't do prediction: AttributeError: 'LSTM_FCN' object has no attribute 'get_X_preds' #723

Closed langslike closed 1 year ago

langslike commented 1 year ago

Hi, I am using tsai to do multi-in-one-out forecasting, the training goes well, but when I do prediction, AttributeError: 'LSTM_FCN' object has no attribute 'get_X_preds' occured, I tried other models like InceptionTime, still do not work.

this is the output of my data dls.one_batch() (TSTensor(samples:64, vars:136, len:20, device=cuda:0, dtype=torch.float32), tensor([ 0.0008, 0.0010, 0.0017, -0.0006, 0.0127, 0.0083, 0.0083, 0.0202, -0.0016, -0.0087, 0.0044, -0.0128, -0.0180, 0.0085, -0.0021, 0.0049, -0.0034, 0.0132, 0.0071, -0.0017, 0.0119, -0.0275, 0.0007, -0.0180, -0.0058, 0.0127, 0.0512, 0.0010, 0.0198, 0.0242, -0.0242, 0.0003, -0.0232, 0.0053, -0.0082, 0.0391, -0.0026, -0.0048, 0.0031, 0.0197, 0.0063, 0.0058, -0.0189, -0.0057, -0.0273, -0.0087, -0.0216, -0.0091, -0.0055, 0.0309, -0.0065, -0.0021, 0.0034, -0.0033, 0.0043, 0.0137, 0.0102, 0.0121, -0.0179, -0.0181, 0.0018, 0.0070, -0.0147, 0.0113], device='cuda:0', dtype=torch.float64))

this is my model definition:

from tsai.models.RNN_FCN import LSTM_FCN model = LSTM_FCN(dls.vars, dls.c, shuffle = False)

from tsai.learner import ts_learner from fastai.metrics import rmse,mae from tsai.callback.core import ShowGraph learn = ts_learner(dls, model, metrics=[mae, rmse],cbs=ShowGraph()) learn.fit_one_cycle(50, lr_max=5e-4)

the training goes well: epoch train_loss valid_loss mae _rmse time
... 47 0.000213 0.000259 0.011582 0.016097 00:00
48 0.000206 0.000262 0.011748 0.016197 00:00
49 0.000204 0.000261 0.011661 0.016145 00:00

this is my inference code: import sklearn.metrics as skm probas, targets, preds = learn.get_X_preds(X[splits[2]]) #my dataset has train, val and test 3 parts skm.mean_squared_error(y[splits[2]], preds, squared=False)

this is the error code:

AttributeError Traceback (most recent call last) Cell In[56], line 2 1 import sklearn.metrics as skm ----> 2 probas, targets, preds = learn.get_X_preds(X[splits[2]]) 3 skm.mean_squared_error(y[splits[2]], preds, squared=False)

File d:\Anaconda\envs\py39\lib\site-packages\fastcore\basics.py:496, in GetAttr.getattr(self, k) 494 if self._component_attr_filter(k): 495 attr = getattr(self,self._default,None) --> 496 if attr is not None: return getattr(attr,k) 497 raise AttributeError(k)

File d:\Anaconda\envs\py39\lib\site-packages\torch\nn\modules\module.py:1207, in Module.getattr(self, name) 1205 if name in modules: 1206 return modules[name] -> 1207 raise AttributeError("'{}' object has no attribute '{}'".format( 1208 type(self).name, name))

AttributeError: 'LSTM_FCN' object has no attribute 'get_X_preds'

this is my env: os : Windows-10-10.0.19041-SP0 python : 3.9.16 tsai : 0.3.5 fastai : 2.7.10 fastcore : 1.5.27 torch : 1.12.1+cu113 device : 1 gpu (['NVIDIA GeForce GTX 1650']) cpu cores : 4 threads per cpu : 2 RAM : 15.84 GB GPU memory : [4.0] GB

oguiza commented 1 year ago

Hi @langslike, Could you please run this code and see if it works?

X = np.random.rand(100, 2, 50)
y = np.random.rand(100)
splits = TSSplitter(valid_size=.2, test_size=.3, show_plot=False)(y)
tfms = [None, TSRegression()]
batch_tfms = TSStandardize(by_sample=True)
dls = get_ts_dls(X, y, splits, tfms=tfms, batch_tfms=batch_tfms)
reg = ts_learner(dls, arch="LSTM_FCN", metrics=[rmse, mae])
reg.fit_one_cycle(5, 3e-4)
probas, targets, preds = reg.get_X_preds(X[splits[2]])
probas.shape
langslike commented 1 year ago

I added one line to import get_ts_dls: from tsai.data.core import get_ts_dls then I run your code, still error:

AttributeError Traceback (most recent call last) Cell In[11], line 11 9 reg = ts_learner(dls, arch="LSTM_FCN", metrics=[rmse, mae]) 10 reg.fit_one_cycle(5, 3e-4) ---> 11 probas, targets, preds = reg.get_X_preds(X[splits[2]]) 12 probas.shape

File d:\Anaconda\envs\py39\lib\site-packages\fastcore\basics.py:496, in GetAttr.getattr(self, k) 494 if self._component_attr_filter(k): 495 attr = getattr(self,self._default,None) --> 496 if attr is not None: return getattr(attr,k) 497 raise AttributeError(k)

File d:\Anaconda\envs\py39\lib\site-packages\torch\nn\modules\module.py:1207, in Module.getattr(self, name) 1205 if name in modules: 1206 return modules[name] -> 1207 raise AttributeError("'{}' object has no attribute '{}'".format( 1208 type(self).name, name))

AttributeError: 'LSTM_FCN' object has no attribute 'get_X_preds'

oguiza commented 1 year ago

Hmmm, that's very strange. Could you install tsai from GitHub using?

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

Try rerunning your code again.

langslike commented 1 year ago

tried install from github, still the same error. but I added one line to my code, everything is fine: from tsai.all import * I kind of resist import package in this way, cause there maybe namespace conflict and it's confusing to find the source code.But if I only import the objects I will use, I get the error. I am interested in the underhood cause of this kind of error, is the fastai style import necessary?

oguiza commented 1 year ago

You don’t need to use fastai’s import style. I don’t use it in most projects with my customers. But in that case you need to import all the necessary code. In your case you are missing this:

from tsai.inference import get_X_preds

Here's an end-to-end example of how this might work using specific function imports:

# TRAINING
from tsai.data.external import get_UCR_data
from tsai.data.core import TSClassification, get_ts_dls
from tsai.data.preprocessing import TSStandardize
from tsai.learner import ts_learner
from tsai.metrics import accuracy
from tsai.callback.core import ShowGraph

X, y, splits = get_UCR_data('LSST', split_data=False)
tfms = [None, TSClassification()]
batch_tfms = TSStandardize(by_sample=True)
dls = get_ts_dls(X, y, splits=splits, tfms=tfms, batch_tfms=batch_tfms)
learn = ts_learner(dls, "InceptionTimePlus", metrics=accuracy, cbs=[ShowGraph()])
learn.fit_one_cycle(1, 1e-2)
learn.export("test.pt")
# INFERENCE
from fastai.learner import load_learner
from tsai.inference import get_X_preds

inference_learn = load_learner("test.pt")
preds, *_ = inference_learn.get_X_preds(X[splits[1]])

This requires that you know exactly where each function lives. What I normally do is start using from tsai.basics import * in functions (for convenience and speed), and once the notebook or script works I replace the * with the functions required.

langslike commented 1 year ago

Well explained, thanks.