question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] ValueError: __len__() should return >= 0

See original GitHub issue

Describe the bug When using val_series with RNNModel, I am getting an error. When running the exact same code with NBEATS, it works. The error occurs unless my val_series has at least a lengh of 25. I have tried training series of different length and different input_chunk_length and output_chunk_length without success. I at least think this is not expected.

To Reproduce This produces an error:

model_RNN = RNNModel(input_chunk_length=10, output_chunk_length=1, n_epochs=250,
                          batch_size=1024, torch_device_str="cuda:0")
model_RNN.fit(series=ts_list_train, val_series=ts_list_val
                )

This does not produce an error:

model_NBEATS = NBEATSModel(input_chunk_length=10, output_chunk_length=1, n_epochs=250,
                          batch_size=1024, torch_device_str="cuda:0")
model_NBEATS.fit(series=ts_list_train, val_series=ts_list_val
                )

Additional context Traceback below:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/tmp/ipykernel_12641/796312022.py in <module>
      1 model_RNN = RNNModel(input_chunk_length=10, output_chunk_length=1, n_epochs=250,
      2                           batch_size=1024, torch_device_str="cuda:0")
----> 3 model_RNN.fit(series=ts_list_train, verbose=True, val_series=ts_list_val
      4                 )

~/darts/lib/python3.8/site-packages/darts/utils/torch.py in decorator(self, *args, **kwargs)
     63         with fork_rng():
     64             manual_seed(self._random_instance.randint(0, high=MAX_TORCH_SEED_VALUE))
---> 65             return decorated(self, *args, **kwargs)
     66     return decorator

~/darts/lib/python3.8/site-packages/darts/models/forecasting/torch_forecasting_model.py in fit(self, series, past_covariates, future_covariates, val_series, val_past_covariates, val_future_covariates, verbose, epochs)
    429         logger.info('Train dataset contains {} samples.'.format(len(train_dataset)))
    430 
--> 431         self.fit_from_dataset(train_dataset, val_dataset, verbose, epochs)
    432 
    433     @random_method

~/darts/lib/python3.8/site-packages/darts/utils/torch.py in decorator(self, *args, **kwargs)
     63         with fork_rng():
     64             manual_seed(self._random_instance.randint(0, high=MAX_TORCH_SEED_VALUE))
---> 65             return decorated(self, *args, **kwargs)
     66     return decorator

~/darts/lib/python3.8/site-packages/darts/models/forecasting/torch_forecasting_model.py in fit_from_dataset(self, train_dataset, val_dataset, verbose, epochs)
    465                  'The provided training time series dataset is too short for obtaining even one training point.',
    466                  logger)
--> 467         raise_if(val_dataset is not None and len(val_dataset) == 0,
    468                  'The provided validation time series dataset is too short for obtaining even one training point.',
    469                  logger)

~/darts/lib/python3.8/site-packages/darts/utils/data/shifted_dataset.py in __len__(self)
    212 
    213     def __len__(self):
--> 214         return len(self.ds_past)
    215 
    216     def __getitem__(self, idx) -> Tuple[np.ndarray, Optional[np.ndarray], Optional[np.ndarray], np.ndarray]:

ValueError: __len__() should return >= 0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
dennisbadercommented, Oct 4, 2021

Hi @JulianNyarko and thanks for writing.

RNNModel takes an optional parameter training_length which defaults to 24.

From our RNNModel docs on training_length (see here):

“The length of both input (target and covariates) and output (target) time series used during training. Generally speaking, training_length should have a higher value than input_chunk_length because otherwise during training the RNN is never run for as many iterations as it will during training. For more information on this parameter, please see darts.utils.data.ShiftedDataset”

ShiftedDataset additionally shifts the data by +1 time step. So in total, the length of your val_series TimeSeries should be >= 25.

I agree that it might be a bit hard to identify what is causing this Error. @hrzn, @pennfranc couldn’t we check for correct input at the beginning of fit?

0reactions
hrzncommented, Aug 15, 2022

The error message was improved in 0.21.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

ValueError: __len__() should return >= 0 - Stack Overflow
I want to make a transformer model by darts package by the code below : my_model = TransformerModel( input_chunk_length=12, ...
Read more >
ERROR: __len__() should return >= 0 on liveFeed
Hello everyone. I need help I am working on implementing a new data feed / live broker. I have taken Oanda as a...
Read more >
Avoid raising OverflowError in len() when __len__() returns ...
msg289779 ‑ (view) Author: Serhiy Storchaka (serhiy.storchaka) * Date: 2017‑03‑17 19:52 msg289782 ‑ (view) Author: Barry A. Warsaw (barry) * Date: 2017‑03‑17 20:28 msg290109 ‑...
Read more >
Python - Check if a list is empty or not - GeeksforGeeks
Method 1: Check the empty list using the len() With Comparison ... return 0 ... If the list contains one 0, then the...
Read more >
30. Errors and Exception Handling | Python Tutorial
If we use a input(), the input will be a string, which we have to cast ... return age except ValueError as e:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found