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.92k stars 622 forks source link

AttributeError when trying to call get_X_preds of a TabModel Learner #804

Open GitGabbo opened 1 year ago

GitGabbo commented 1 year ago

I created a TabModel and a Learner from it:

splits = RandomSplitter()(range_of(ts_features))
procs = [Categorify, FillMissing, Normalize]
cat_names = ['gender']
cont_names = list(ts_features.columns)[1:-5]
y_names = ['target_0', 'target_1', 'target_2', 'target_3', 'target_4']
y_block = CategoryBlock()
to = TabularPandas(ts_features, cat_names=cat_names, cont_names=cont_names, y_names=y_names, 
                                splits=splits, y_block=y_block, procs=procs, inplace=True)
tab_dls = to.dataloaders(bs=16)
tab_model = build_tabular_model(TabModel, dls=tab_dls)
learnTab = Learner(tab_dls, tab_model, metrics=[accuracy_multi], loss_func=BCEWithLogitsLossFlat())
learnTab.fit_one_cycle(20, 1e-3)

Then I would like to get prediction from a test dataframe:

testTab = TabularPandas(test_df, cat_names=cat_names, cont_names=cont_names, procs=procs)
test_tab_dls = testTab.dataloaders(bs=16)
test_probas, *_ = learnTab.get_X_preds(test_tab_dls)

But I get the following AttributeError (I get the same error if I call learnTab.get_X_preds with testTab or test_df):

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[168], line 4
      1 testTab = TabularPandas(test_f_ds, cat_names=cat_names, cont_names=cont_names, procs=procs)
      2 test_tab_dls = testTab.dataloaders(bs=16)
----> 4 test_probas, *_ = learnTab.get_X_preds(testTab)

File ~\Python\Python311\site-packages\tsai\inference.py:24, in get_X_preds(self, X, y, bs, with_input, with_decoded, with_loss, act)
     22     print("with_loss set to False as y is None")
     23     with_loss = False
---> 24 dl = self.dls.valid.new_dl(X, y=y, bs=bs)
     25 output = list(self.get_preds(dl=dl, with_input=with_input, with_decoded=with_decoded, with_loss=with_loss, reorder=False, act=act))
     26 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 ~\Python\Python311\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 ~\Python\Python311\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 ~\Python\Python311\site-packages\fastcore\transform.py:212, in Pipeline.__getattr__(self, k)
--> 212 def __getattr__(self,k): return gather_attrs(self, k, 'fs')

File ~\Python\Python311\site-packages\fastcore\transform.py:173, in gather_attrs(o, k, nm)
    171 att = getattr(o,nm)
    172 res = [t for t in att.attrgot(k) if t is not None]
--> 173 if not res: raise AttributeError(k)
    174 return res[0] if len(res)==1 else L(res)

AttributeError: new_dl

What is the correct way to pass a new test dataframe to the TabModel Learner?

610268731 commented 11 months ago

I got same problem, have you solved it?