[BUG] too short time series give a ZeroDivisionError
See original GitHub issueDescribe 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:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
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.