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] too short time series give a ZeroDivisionError

See original GitHub issue

Describe the bug When training some models with a short time series (eg. 8 points), a ZeroDivisionError occur.

---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-29-7c617aa60865> in <module>
     12 model = NBEATSModel(input_chunk_length=1, output_chunk_length=1)
     13 
---> 14 model.fit(series)

~/.local/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             decorated(self, *args, **kwargs)
     66     return decorator

~/.local/lib/python3.8/site-packages/darts/models/torch_forecasting_model.py in fit(self, series, covariates, val_series, val_covariates, verbose)
    292         logger.info('Train dataset contains {} samples.'.format(len(train_dataset)))
    293 
--> 294         self.fit_from_dataset(train_dataset, val_dataset, verbose)
    295 
    296     @random_method

~/.local/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             decorated(self, *args, **kwargs)
     66     return decorator

~/.local/lib/python3.8/site-packages/darts/models/torch_forecasting_model.py in fit_from_dataset(self, train_dataset, val_dataset, verbose)
    345 
    346         # Train model
--> 347         self._train(train_loader, val_loader, tb_writer, verbose)
    348 
    349         # Close tensorboard writer

~/.local/lib/python3.8/site-packages/darts/models/torch_forecasting_model.py in _train(self, train_loader, val_loader, tb_writer, verbose)
    518 
    519             if epoch % self.nr_epochs_val_period == 0:
--> 520                 training_loss = total_loss / len(train_loader)
    521                 if val_loader is not None:
    522                     validation_loss = self._evaluate_validation_loss(val_loader)

ZeroDivisionError: division by zero

Seems like this is the case only for global models. Tried with:

  • NBEATSModel
  • TCNModel
  • RNNModel
  • TransformerModel

To Reproduce Run the following code snippet to get the error

from darts.models.transformer_model import TransformerModel
import pandas as pd
from darts import TimeSeries

df = pd.DataFrame({
    'time': ['2010-01-01', '2012-01-01'], # change 2012 to 2019, and the error disappear
    'values': [1, 2],
})

df['time'] =  pd.PeriodIndex(df['time'], freq='Q').to_timestamp()
df.index = df['time']
df = df.resample('Q').sum().interpolate('time').reset_index()

series = TimeSeries.from_dataframe(df, time_col='time', value_cols=['values'])

model = TransformerModel(input_chunk_length=2, output_chunk_length=1)
model.fit(series)

Expected behavior The model should train on short time series.

System (please complete the following information):

  • Python version: 3.8.8
  • darts version: 0.6.1

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
kaosdevcommented, Apr 13, 2021

Ok, found the problem. This has to do with the batch size. If you create the model passing the batch_size as an argument, the error goes away.

Maybe it’s better to get a clear error instead of a ZeroDivisionError? Or maybe handling it. E.g.

if batch_size > len(train_dataset):
   batch_size = len(train_dataset)
1reaction
kaosdevcommented, Apr 16, 2021

Thanks @kaosdev. I had the same error and reducing the batch size helped. I tried figuring out how the train_dataset length is calculated. If I have a dataset with 63 rows in total and my output_chunk_length is 12 and my input_chunk_length is 24. My model’s batch_size is 8 (LSTM - RNN). How does it calculate that I have 15 samples? Thanks. I tried figuring it out from here line 292, and here from the SequentialDataset Class. If you can help, please let me know. Thanks.

Mhh, I may be wrong but I think you should have more than 15 samples.

The formula should be (if I am right): samples = total - input - output

So in your case: 63 - 24 - 12 = 27

Batch size has nothing to do with the number of samples, it should just be less than or equal to the number of samples.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error python : [ZeroDivisionError: division by zero]
If y is 0 , the value returned is y . If y is different from 0 , the right side of the...
Read more >
ZeroDivisionError division by zero in Python error handling
If you are working in Python, and receive the following output, your code is attempting to divide a given number by zero. ZeroDivisionError....
Read more >
Errors and exceptions - Object-Oriented Programming in Python
The error is caused by a mistake in the program's logic. You won't get an error message, because no syntax or runtime error...
Read more >
What's new in 0.23.0 (May 15, 2018) - Pandas
Notice that the Series is now ordered by insertion order. This new behavior is used for all relevant pandas types ( Series ,...
Read more >
Python — Errors - Medium
There is a term called Bugs, used for referring errors or mistakes in a script. ... separately to make sure that part works...
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