Skip to content

Saving models with TSUnwindowedDataset Dataloaders: AttributeError: 'TSUnwindowedDataset' object has no attribute 'new_empty' #215

@GardarGardarsson

Description

@GardarGardarsson

Having successfully trained a regression model on a large datafile, that required me to use the TSUnwindowedDataset to circumvent memory and storage requirements, upon trying to save the model I received the following error:

~/anaconda3/envs/TSAI/lib/python3.9/site-packages/fastai/data/core.py in <listcomp>(.0)
    144     def __getitem__(self, i): return self.loaders[i]
    145     def new_empty(self):
--> 146         loaders = [dl.new(dl.dataset.new_empty()) for dl in self.loaders]
    147         return type(self)(*loaders, path=self.path, device=self.device)
    148 

`AttributeError: 'TSUnwindowedDataset' object has no attribute 'new_empty'

Following are glimpses from my code.
I'm loading the regression features and targets as Pandas DataFrames and converting them as:

X, y = df2xy(data, sample_col=None, feat_col=None, data_cols=feature_names, target_col=target_names, to3d=False)

In setting up TSUnwindowedDataset() I use the following:

window_size = 24
stride      = 1
drop_start  = 0
drop_end    = 0
seq_first   = True
horizon     = 1

dset        = TSUnwindowedDataset(X, y, 
                                  y_func      = None       , 
                                  window_size = window_size, 
                                  stride      = stride     , 
                                  horizon     = horizon    ,
                                  drop_start  = drop_start , 
                                  drop_end    = drop_end   , 
                                  seq_first   = seq_first  )
len(dset)

Then get the splits, all the while watching out for not overreaching on the returned indices given the window size, and then pass the splits along with the dataset to TSUnwindowedDatasets() as per below:

splits = get_splits(y[:-window_size], n_splits=1, valid_size=.2, stratify=True, random_state=41, shuffle=True)
dsets  = TSUnwindowedDatasets(dset, splits=splits)

Standardising the data on a pre-calculated mean and std. deviation, and constructing the TSDataLoader:

batch_tfms = TSStandardize(mean=x_avg, std=x_std) # Standardise the batch on the pre-calculated mean and std.dev 
dls        = TSDataLoaders.from_dsets(dsets.train, dsets.valid, batch_tfms=batch_tfms, bs=[64])

I then find the learning rate as demonstrated in many of your tutorials, and when it comes to training I do:

learn = ts_learner(dls, 
                   InceptionTimePlus, 
                   metrics =  [mae ,
                               rmse], 
                   cbs     =  [ShowGraph(),
                               SaveModelCallback(monitor      = 'valid_loss', 
                                                 comp         = None        ,
                                                 min_delta    = 0.0         ,  
                                                 fname        = 'model'     , 
                                                 every_epoch  = False       , 
                                                 at_end       = False       , 
                                                 with_opt     = False       , 
                                                 reset_on_fit = True        ) ] )

learn.fit_one_cycle(10, lr_max=lr)

I then used the standard line to save the trained model as per:

PATH = Path('./models/Regression.pkl')
PATH.parent.mkdir(parents=True, exist_ok=True)
learn.export(PATH)

But ultimately got:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_3698523/2688106701.py in <module>
      1 PATH = Path('./models/Regression.pkl')
      2 PATH.parent.mkdir(parents=True, exist_ok=True)
----> 3 learn.export(PATH)

~/anaconda3/envs/TSAI/lib/python3.9/site-packages/fastai/learner.py in export(self, fname, pickle_module, pickle_protocol)
    367     self._end_cleanup()
    368     old_dbunch = self.dls
--> 369     self.dls = self.dls.new_empty()
    370     state = self.opt.state_dict() if self.opt is not None else None
    371     self.opt = None

~/anaconda3/envs/TSAI/lib/python3.9/site-packages/fastai/data/core.py in new_empty(self)
    144     def __getitem__(self, i): return self.loaders[i]
    145     def new_empty(self):
--> 146         loaders = [dl.new(dl.dataset.new_empty()) for dl in self.loaders]
    147         return type(self)(*loaders, path=self.path, device=self.device)
    148 

~/anaconda3/envs/TSAI/lib/python3.9/site-packages/fastai/data/core.py in <listcomp>(.0)
    144     def __getitem__(self, i): return self.loaders[i]
    145     def new_empty(self):
--> 146         loaders = [dl.new(dl.dataset.new_empty()) for dl in self.loaders]
    147         return type(self)(*loaders, path=self.path, device=self.device)
    148 

`AttributeError: 'TSUnwindowedDataset' object has no attribute 'new_empty'

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions