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.

Using multiple covariates in LSTM model throws error when fitting.

See original GitHub issue

Describe the bug When adding the list of covariates I get this error : ValueError: The provided sequence of target series must have the same length as the provided sequence of covariate series.

To Reproduce

import pandas as pd
import numpy as np
from darts import TimeSeries
from darts.models import RNNModel
from darts.dataprocessing.transformers import Scaler

# Create toy dataset
idx = pd.date_range(start=pd.Timestamp('2020-01-01'), freq='MS', periods=10 )
df = pd.DataFrame({
    'target':np.random.randint(100, 10000, size=10,),
    'var1' :np.random.randint(100, 10000, size=10,),
    'var2' :np.random.randint(100, 10000, size=10,),
    'var3' :np.random.randint(100, 10000, size=10,)
}, index= idx)

# Creating Time series target and variables
series_target = TimeSeries.from_dataframe(df,value_cols=['target'])
series_var1 = TimeSeries.from_dataframe(df,value_cols=['var1'])
series_var2 = TimeSeries.from_dataframe(df, value_cols = ['var2'])
series_var3 = TimeSeries.from_dataframe(df, value_cols = ['var3'])

# Scaling the series
scaler_target = Scaler()
scaler_var1 =  Scaler()
scaler_var2 = Scaler()
scaler_var3 = Scaler()
series_target_scaled = scaler_target.fit_transform(series_target)
series_var1_scaled = scaler_var1.fit_transform(series_var1)
series_var2_scaled = scaler_var2.fit_transform(series_var2)
series_var3_scaled = scaler_var3.fit_transform(series_var3)

# Training and Validation Split
split_time = pd.Timestamp('2020-07-01')
train_target, val_target = series_target_scaled.split_after(split_time) 
train_var1, val_var1 = series_var1_scaled.split_after(split_time) 
train_var2, val_var2 = series_var2_scaled.split_after(split_time) 
train_var3, val_var3 = series_var3_scaled.split_after(split_time) 

# Create model
model = RNNModel(
    model='LSTM',
    input_chunk_length=6,
    output_chunk_length=3,
    hidden_size=32,
    n_rnn_layers=1,
    dropout=0.2,
    batch_size=8,
    n_epochs=200,
    optimizer_kwargs={'lr': 1e-3},
    model_name='test_lstm',
    log_tensorboard=True,
    random_state=42
)

# Fitting with covariates  (error here)
model.fit(series=train_target, 
          covariates=[train_var1, train_var2, train_var3], 
          verbose=True)

Expected behavior Model to fit so I can later predict using:

# Predicting With Covariate 
pred_cov = model.predict(n=3, 
                         series= train_target, 
                         covariates= [train_var1, train_var2 , train_var2 ])

System (please complete the following information):

  • Python version: [e.g. Python 3.7.10]
  • darts version --> I’m not sure but I used !pip install 'u8darts[all]'

Additional context I’m unsure about the difference of covariate vs. multivariate. To make it work, I have to fit the same number of series and covariates in the .fit() and .predict() I only care about predicting the target and only using var1, var2, var3 to help predict the target.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
hrzncommented, Apr 10, 2022

Hi @phellstrand77, there are several ways to build a multivariate series. For example:

  • From a Pandas DataFrame containing multiple columns (one per dimension), you can do TimeSeries.from_dataframe(df, time_col, value_cols) where value_cols is the list of columns that you want to integrate.
  • From several univariate TimeSeries, you can do
from darts import concatenate
my_multivariate_series = concatenate([series1, series2, ...], axis=1)

I hope this helps.

1reaction
phellstrand77commented, Apr 18, 2022

@hrzn thank you very much, I now understand the constraints.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multivariate Time Series Forecasting with LSTMs in Keras
In this tutorial, you will discover how you can develop an LSTM model for multivariate time series forecasting with the Keras deep learning ......
Read more >
Multiple Time Series, Pre-trained Models and Covariates
This notebook serves as a tutorial for: Training a single model on multiple time series. Using a pre-trained model to obtain forecasts for...
Read more >
LSTMs for regression - Bob Rupak Roy - Medium
Our data pre-processing is complete. It's time to build our LSTM network and fit the data. #create and fit our data to the...
Read more >
181 - Multivariate time series forecasting using LSTM - YouTube
For a dataset just search online for 'yahoo finance GE' or any other stock of your interest. Then select history and download csv...
Read more >
Multivariate Time Series Forecasting Using LSTM, GRU & 1d ...
Part 1: https://www.youtube.com/watch?v=c0k-YLQGKjYThe Notebook: ...
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