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

Facing Runtime error while trying to use L1Loss function in multivariate timeseries forecasting using patchTST. #883

Open MALIK2k21 opened 4 months ago

MALIK2k21 commented 4 months ago

Hi!

I am trying to train a patchTST model for multivariate prediction and I am trying to set loss_func to nn.L1Loss in TSForecaster but I keep getting this error with L1Loss:

RuntimeError                              Traceback (most recent call last)
Cell In[53], line 6
      4 n_epochs = 50
      5 lr_max = 0.0001
----> 6 learn.fit_one_cycle(n_epochs, lr_max=lr_max)
      8 learn.export('patchTST.pt')

File ~\.conda\envs\py310\lib\site-packages\fastai\callback\schedule.py:119, in fit_one_cycle(self, n_epoch, lr_max, div, div_final, pct_start, wd, moms, cbs, reset_opt, start_epoch)
    116 lr_max = np.array([h['lr'] for h in self.opt.hypers])
    117 scheds = {'lr': combined_cos(pct_start, lr_max/div, lr_max, lr_max/div_final),
    118           'mom': combined_cos(pct_start, *(self.moms if moms is None else moms))}
--> 119 self.fit(n_epoch, cbs=ParamScheduler(scheds)+L(cbs), reset_opt=reset_opt, wd=wd, start_epoch=start_epoch)

File ~\.conda\envs\py310\lib\site-packages\fastai\learner.py:264, in Learner.fit(self, n_epoch, lr, wd, cbs, reset_opt, start_epoch)
    262 self.opt.set_hypers(lr=self.lr if lr is None else lr)
    263 self.n_epoch = n_epoch
--> 264 self._with_events(self._do_fit, 'fit', CancelFitException, self._end_cleanup)

File ~\.conda\envs\py310\lib\site-packages\fastai\learner.py:199, in Learner._with_events(self, f, event_type, ex, final)
    198 def _with_events(self, f, event_type, ex, final=noop):
--> 199     try: self(f'before_{event_type}');  f()
    200     except ex: self(f'after_cancel_{event_type}')
    201     self(f'after_{event_type}');  final()

File ~\.conda\envs\py310\lib\site-packages\fastai\learner.py:253, in Learner._do_fit(self)
    251 for epoch in range(self.n_epoch):
    252     self.epoch=epoch
--> 253     self._with_events(self._do_epoch, 'epoch', CancelEpochException)

File ~\.conda\envs\py310\lib\site-packages\fastai\learner.py:199, in Learner._with_events(self, f, event_type, ex, final)
    198 def _with_events(self, f, event_type, ex, final=noop):
--> 199     try: self(f'before_{event_type}');  f()
    200     except ex: self(f'after_cancel_{event_type}')
    201     self(f'after_{event_type}');  final()

File ~\.conda\envs\py310\lib\site-packages\fastai\learner.py:247, in Learner._do_epoch(self)
    246 def _do_epoch(self):
--> 247     self._do_epoch_train()
    248     self._do_epoch_validate()

File ~\.conda\envs\py310\lib\site-packages\fastai\learner.py:239, in Learner._do_epoch_train(self)
    237 def _do_epoch_train(self):
    238     self.dl = self.dls.train
--> 239     self._with_events(self.all_batches, 'train', CancelTrainException)

File ~\.conda\envs\py310\lib\site-packages\fastai\learner.py:199, in Learner._with_events(self, f, event_type, ex, final)
    198 def _with_events(self, f, event_type, ex, final=noop):
--> 199     try: self(f'before_{event_type}');  f()
    200     except ex: self(f'after_cancel_{event_type}')
    201     self(f'after_{event_type}');  final()

File ~\.conda\envs\py310\lib\site-packages\fastai\learner.py:205, in Learner.all_batches(self)
    203 def all_batches(self):
    204     self.n_iter = len(self.dl)
--> 205     for o in enumerate(self.dl): self.one_batch(*o)

File ~\.conda\envs\py310\lib\site-packages\tsai\learner.py:40, in one_batch(self, i, b)
     38 b_on_device = to_device(b, device=self.dls.device) if self.dls.device is not None else b
     39 self._split(b_on_device)
---> 40 self._with_events(self._do_one_batch, 'batch', CancelBatchException)

File ~\.conda\envs\py310\lib\site-packages\fastai\learner.py:201, in Learner._with_events(self, f, event_type, ex, final)
    199 try: self(f'before_{event_type}');  f()
    200 except ex: self(f'after_cancel_{event_type}')
--> 201 self(f'after_{event_type}');  final()

File ~\.conda\envs\py310\lib\site-packages\fastai\learner.py:172, in Learner.__call__(self, event_name)
--> 172 def __call__(self, event_name): L(event_name).map(self._call_one)

File ~\.conda\envs\py310\lib\site-packages\fastcore\foundation.py:156, in L.map(self, f, *args, **kwargs)
--> 156 def map(self, f, *args, **kwargs): return self._new(map_ex(self, f, *args, gen=False, **kwargs))

File ~\.conda\envs\py310\lib\site-packages\fastcore\basics.py:840, in map_ex(iterable, f, gen, *args, **kwargs)
    838 res = map(g, iterable)
    839 if gen: return res
--> 840 return list(res)

File ~\.conda\envs\py310\lib\site-packages\fastcore\basics.py:825, in bind.__call__(self, *args, **kwargs)
    823     if isinstance(v,_Arg): kwargs[k] = args.pop(v.i)
    824 fargs = [args[x.i] if isinstance(x, _Arg) else x for x in self.pargs] + args[self.maxi+1:]
--> 825 return self.func(*fargs, **kwargs)

File ~\.conda\envs\py310\lib\site-packages\fastai\learner.py:176, in Learner._call_one(self, event_name)
    174 def _call_one(self, event_name):
    175     if not hasattr(event, event_name): raise Exception(f'missing {event_name}')
--> 176     for cb in self.cbs.sorted('order'): cb(event_name)

File ~\.conda\envs\py310\lib\site-packages\fastai\callback\core.py:62, in Callback.__call__(self, event_name)
     60     try: res = getcallable(self, event_name)()
     61     except (CancelBatchException, CancelBackwardException, CancelEpochException, CancelFitException, CancelStepException, CancelTrainException, CancelValidException): raise
---> 62     except Exception as e: raise modify_exception(e, f'Exception occured in `{self.__class__.__name__}` when calling event `{event_name}`:\n\t{e.args[0]}', replace=True)
     63 if event_name=='after_fit': self.run=True #Reset self.run to True at each end of fit
     64 return res

File ~\.conda\envs\py310\lib\site-packages\fastai\callback\core.py:60, in Callback.__call__(self, event_name)
     58 res = None
     59 if self.run and _run: 
---> 60     try: res = getcallable(self, event_name)()
     61     except (CancelBatchException, CancelBackwardException, CancelEpochException, CancelFitException, CancelStepException, CancelTrainException, CancelValidException): raise
     62     except Exception as e: raise modify_exception(e, f'Exception occured in `{self.__class__.__name__}` when calling event `{event_name}`:\n\t{e.args[0]}', replace=True)

File ~\.conda\envs\py310\lib\site-packages\fastai\learner.py:560, in Recorder.after_batch(self)
    558 if len(self.yb) == 0: return
    559 mets = self._train_mets if self.training else self._valid_mets
--> 560 for met in mets: met.accumulate(self.learn)
    561 if not self.training: return
    562 self.lrs.append(self.opt.hypers[-1]['lr'])

File ~\.conda\envs\py310\lib\site-packages\fastai\learner.py:509, in AvgSmoothLoss.accumulate(self, learn)
    507 def accumulate(self, learn):
    508     self.count += 1
--> 509     self.val = torch.lerp(to_detach(learn.loss.mean()), self.val, self.beta)

RuntimeError: Exception occured in `Recorder` when calling event `after_batch`:
    expected dtype double for `end` but got dtype float`

AND

------------------------------ My Model---------------------------------------------

arch_config = dict(
    n_layers=2,  # number of encoder layers
    n_heads=5,  # number of heads
    d_model=155,  # dimension of model
    d_ff=132,  # dimension of fully connected network
    attn_dropout=0.0, # dropout applied to the attention weights
    dropout=0.0,  # dropout applied to all linear layers in the encoder except q,k&v projections
    patch_len=48,  # length of the patch applied to the time series to create patches
    stride=48,  # stride used when creating patches
    padding_patch=True,  # padding_patch
)
learn = TSForecaster(X, y, splits=splits, batch_size=128, path="models", pipelines=[preproc_pipe],
                     arch="PatchTST", arch_config=arch_config,loss_func=nn.L1Loss(),opt_func=Adam, metrics=[mae,mse], cbs=ShowGraph())
learn.summary()

lossfunc=nn.L1Loss()
learn = TSForecaster(X, y, splits=splits, batch_size=128, path="models", pipelines=[preproc_pipe],
                     arch="PatchTST", arch_config=arch_config, loss_func=lossfunc,opt_func=Adam, metrics=[mae,mse], cbs=[ShowGraph()])

n_epochs = 50
lr_max = 0.0001
learn.fit_one_cycle(n_epochs, lr_max=lr_max)

learn.export('patchTST.pt')

So, what is happening and how can I resolve it?