salesforce / Merlion

Merlion: A Machine Learning Framework for Time Series Intelligence
BSD 3-Clause "New" or "Revised" License
3.43k stars 302 forks source link

[BUG] Chunking issue for LSTM forecasting #87

Open RabbITCybErSeC opened 2 years ago

RabbITCybErSeC commented 2 years ago

Describe the bug

Hi, I am trying to implement the LSTM method on the Itrust SWAT datasets using Merlion. However, I am running into a chunking issue for LSTM. LSTM is the only model where I encounter this issue. I resampled the training_data for every second to have a consistent interval. However, the issue remains. I wonder whether this is a bug or a problem in my code.

Lastly, do you have an example notebook with an LSTM example? So far, I wasn't able to identify any LSTM example configs. Making it hard to try something out quickly.

To Reproduce

Below I have the code for my LSTM model.

from merlion.models.forecast.prophet import Prophet, ProphetConfig
from merlion.models.forecast.smoother import MSES, MSESConfig
from merlion.models.forecast.lstm import LSTM, LSTMConfig, LSTMTrainConfig
from merlion.models.forecast.base import ForecasterBase

lstm_config = LSTMConfig(len(test_data),
                         nhid=100,
                         model_strides=(1,),
                         target_seq_index=None,
                         transform=None,
                         max_score=1,
                         threshold=None,
                         enable_calibrator=True,
                         enable_threshold=True)

training_config_lstm = LSTMTrainConfig(lr=1e-05,
                                       batch_size=500,
                                       epochs=100,
                                       seq_len=30,
                                       data_stride=1,
                                       valid_split=0.2,
                                       checkpoint_file='checkpoint.pt')

lstm = LSTM(lstm_config)
lstm.train(training_data,train_config=training_config_lstm)

Expected behavior No, chunking problem.

Screenshots

RuntimeError                              Traceback (most recent call last)
/tmp/ipykernel_62686/1285329270.py in <module>
     62 
     63 lstm = LSTM(lstm_config)
---> 64 lstm.train(training_data,train_config=training_config_lstm)
     65 #len(test_data)

~/.local/lib/python3.9/site-packages/merlion/models/forecast/lstm.py in train(self, train_data, train_config)
    303                         batch = batch.cuda()
    304                     self.optimizer.zero_grad()
--> 305                     out = self.model(batch[:, : -(self.max_forecast_steps + 1)], future=self.max_forecast_steps)
    306                     loss = F.l1_loss(out, batch[:, 1:])
    307                     loss.backward()

~/.local/lib/python3.9/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1108         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1109                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1110             return forward_call(*input, **kwargs)
   1111         # Do not call functions when jit is used
   1112         full_backward_hooks, non_full_backward_hooks = [], []

~/.local/lib/python3.9/site-packages/merlion/models/forecast/lstm.py in forward(self, input, future)
    198         :return: the predicted values including both 1-step predictions and the future step predictions
    199         """
--> 200         outputs = [rnn(input[:, ::stride]) for stride, rnn in zip(self.strides, self.rnns)]
    201         batch_sz, dim = outputs[0].shape
    202         preds = [

~/.local/lib/python3.9/site-packages/merlion/models/forecast/lstm.py in <listcomp>(.0)
    198         :return: the predicted values including both 1-step predictions and the future step predictions
    199         """
--> 200         outputs = [rnn(input[:, ::stride]) for stride, rnn in zip(self.strides, self.rnns)]
    201         batch_sz, dim = outputs[0].shape
    202         preds = [

~/.local/lib/python3.9/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1108         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1109                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1110             return forward_call(*input, **kwargs)
   1111         # Do not call functions when jit is used
   1112         full_backward_hooks, non_full_backward_hooks = [], []

~/.local/lib/python3.9/site-packages/merlion/models/forecast/lstm.py in forward(self, input)
    146         self.reset(bsz=input.size(0))
    147 
--> 148         for i, input_t in enumerate(input.chunk(input.size(1), dim=1)):
    149             self.h_t, self.c_t = self.lstm1(input_t, (self.h_t, self.c_t))
    150             self.h_t2, self.c_t2 = self.lstm2(self.h_t, (self.h_t2, self.c_t2))

RuntimeError: chunk expects `chunks` to be greater than 0, got: 0

Desktop (please complete the following information):

Additional context

Thank you for considering my issue. If any more information is required please let me know.

AlvinAi96 commented 2 years ago

I got the same problem. It seems that LSTM is hard to compatible with the self-define train config.