awslabs / gluonts

Probabilistic time series modeling in Python
https://ts.gluon.ai
Apache License 2.0
4.56k stars 748 forks source link

unroll_encoder_default doesn't match the dimension #2797

Closed STARK1117 closed 1 year ago

STARK1117 commented 1 year ago

Description

unroll_encoder_default doesn't match the dimension!!!

To Reproduce

win10 mxnet-1.7.0.post2 gluonts-0.12.6 numpy-1.20.0


import pandas as pd
import numpy as np

from gluonts.dataset.common import ListDataset
from gluonts.mx.model.deepar import DeepAREstimator
from gluonts.mx.trainer import Trainer

from gluonts.mx.distribution.multivariate_gaussian import MultivariateGaussianOutput

from gluonts.evaluation.backtest import make_evaluation_predictions

N = 20  # number of time series
T = 1000  # number of timesteps
dim = 2  # dimension of the observations
prediction_length = 25
freq = '1H'

custom_datasetx = np.random.normal(size=(N, dim, T))
custom_datasetx[:, 1, :] = 5 * custom_datasetx[:, 1, :]
start = pd.Timestamp("01-01-2019", freq=freq)

train_ds = ListDataset(
    [
        {'target': x, 'start': start}
        for x in custom_datasetx[:, :, :-prediction_length]
    ],
    freq=freq,
    one_dim_target=False,
)

test_ds = ListDataset(
    [
        {'target': x, 'start': start}
        for x in custom_datasetx[:, :, :]
    ],
    freq=freq,
    one_dim_target=False,
)

epochs = 10
learning_rate = 1E-3
batch_size = 3
num_batches_per_epoch = 100

estimator = DeepAREstimator(
    prediction_length=prediction_length,
    context_length=prediction_length,
    freq=freq,
    trainer=Trainer(
        ctx="cpu",
        epochs=epochs,
        learning_rate=learning_rate,
        hybridize=False,
        num_batches_per_epoch=num_batches_per_epoch,
    ),
    batch_size=batch_size,
    distr_output=MultivariateGaussianOutput(dim=dim)
)

predictor = estimator.train(train_ds)

forecast_it, ts_it = make_evaluation_predictions(
    dataset=test_ds,  # test dataset
    predictor=predictor,  # predictor
    num_eval_samples=100,  # number of sample paths we want for evaluation
)

forecasts = list(forecast_it)
tss = list(ts_it)

C:\Users\NPU.conda\envs\GPU_gluon\python.exe F:/baigao/Tutorial/MyTest2.py 0%| | 0/1 [00:00<?, ?it/s] Traceback (most recent call last): File "F:\baigao\Tutorial\MyTest2.py", line 57, in predictor = estimator.train(train_ds) File "C:\Users\NPU.conda\envs\GPU_gluon\lib\site-packages\gluonts\mx\model\estimator.py", line 237, in train return self.train_model( File "C:\Users\NPU.conda\envs\GPU_gluon\lib\site-packages\gluonts\mx\model\estimator.py", line 214, in train_model self.trainer( File "C:\Users\NPU.conda\envs\GPU_gluon\lib\site-packages\gluonts\mx\trainer_base.py", line 420, in call epoch_loss = loop( File "C:\Users\NPU.conda\envs\GPUgluon\lib\site-packages\gluonts\mx\trainer_base.py", line 286, in loop = net(batch.values()) File "C:\Users\NPU.conda\envs\GPU_gluon\lib\site-packages\mxnet\gluon\block.py", line 682, in call out = self.forward(args) File "C:\Users\NPU.conda\envs\GPU_gluon\lib\site-packages\mxnet\gluon\block.py", line 1254, in forward return self.hybrid_forward(ndarray, x, *args, *params) File "C:\Users\NPU.conda\envs\GPU_gluon\lib\site-packages\gluonts\mx\model\deepar_network.py", line 908, in hybrid_forward outputs = self.distribution( File "C:\Users\NPU.conda\envs\GPU_gluon\lib\site-packages\gluonts\mx\model\deepar_network.py", line 847, in distribution rnnoutputs, , scale, , = self.unroll_encoder( File "C:\Users\NPU.conda\envs\GPU_gluon\lib\site-packages\gluonts\mx\model\deepar_network.py", line 684, in unroll_encoder_default is_padded_indicator = F.concat( File "", line 70, in concat File "C:\Users\NPU.conda\envs\GPU_gluon\lib\site-packages\mxnet_ctypes\ndarray.py", line 82, in _imperative_invoke check_call(_LIB.MXImperativeInvokeEx( File "C:\Users\NPU.conda\envs\GPU_gluon\lib\site-packages\mxnet\base.py", line 246, in check_call raise get_last_ffi_error() mxnet.base.MXNetError: Traceback (most recent call last): File "C:\Jenkins\workspace\mxnet-tag\mxnet\src\operator\nn\concat.cc", line 67 MXNetError: Check failed: shape_assign(&(in_shape)[i], dshape): Incompatible input shape: expected [2,-1], got [2,50,2]

Process finished with exit code 1

it turns out to be the problem of here: In C:\Users\NPU.conda\envs\GPU_gluon\lib\site-packages\gluonts\mx\model\deepar_network.py Line 691

            is_padded_indicator = F.concat(
                past_is_pad.slice_axis(
                    axis=1,
                    begin=self.history_length - self.context_length,
                    end=None,
                ),
                F.zeros_like(future_observed_values),
                dim=1,
            )

the dimension of past_is_pad.slice_axis( axis=1, begin=self.history_length - self.context_length, end=None, ) and F.zeros_like(future_observed_values) don't match

so I printed out their shape as

            print(past_is_pad.slice_axis(
                    axis=1,
                    begin=self.history_length - self.context_length,
                    end=None,
                ).shape)
            print(F.zeros_like(future_observed_values).shape)

and get (3, 25) (3, 25, 2)

Environment

(Add as much information about your environment as possible, e.g. dependencies versions.)

STARK1117 commented 1 year ago

Please help, great Thanks!!!!

STARK1117 commented 1 year ago

I tried to solve the problem mentioned above by simply adding one line

past_is_pad = np.repeat(past_is_pad.expand_dims(2),future_observed_values.shape[2], axis=2)

before

is_padded_indicator = F.concat(
    past_is_pad.slice_axis(
        axis=1,
        begin=self.history_length - self.context_length,
        end=None,
    ),
    F.zeros_like(future_observed_values),
    dim=1,
)

so that their dimension match, but then another error occurs 0%| | 0/50 [00:00<?, ?it/s] Traceback (most recent call last): File "F:\baigao\Tutorial\MyTest2.py", line 29, in predictor = estimator.train(train_dataset) File "C:\Users\NPU.conda\envs\GPU_gluon\lib\site-packages\gluonts\mx\model\estimator.py", line 237, in train return self.train_model( File "C:\Users\NPU.conda\envs\GPU_gluon\lib\site-packages\gluonts\mx\model\estimator.py", line 214, in train_model self.trainer( File "C:\Users\NPU.conda\envs\GPU_gluon\lib\site-packages\gluonts\mx\trainer_base.py", line 420, in call epoch_loss = loop( File "C:\Users\NPU.conda\envs\GPUgluon\lib\site-packages\gluonts\mx\trainer_base.py", line 286, in loop = net(batch.values()) File "C:\Users\NPU.conda\envs\GPU_gluon\lib\site-packages\mxnet\gluon\block.py", line 682, in call out = self.forward(args) File "C:\Users\NPU.conda\envs\GPU_gluon\lib\site-packages\mxnet\gluon\block.py", line 1254, in forward return self.hybrid_forward(ndarray, x, *args, **params) File "C:\Users\NPU.conda\envs\GPU_gluon\lib\site-packages\gluonts\mx\model\deepar_network.py", line 909, in hybrid_forward outputs = self.distribution( File "C:\Users\NPU.conda\envs\GPU_gluon\lib\site-packages\gluonts\mx\model\deepar_network.py", line 848, in distribution rnnoutputs, , scale, , = self.unroll_encoder( File "C:\Users\NPU.conda\envs\GPU_gluon\lib\site-packages\gluonts\mx\model\deepar_network.py", line 767, in unroll_encoder_default state = [ File "C:\Users\NPU.conda\envs\GPU_gluon\lib\site-packages\gluonts\mx\model\deepar_network.py", line 768, in F.where( File "", line 64, in where File "C:\Users\NPU.conda\envs\GPU_gluon\lib\site-packages\mxnet_ctypes\ndarray.py", line 82, in _imperative_invoke check_call(_LIB.MXImperativeInvokeEx( File "C:\Users\NPU.conda\envs\GPU_gluon\lib\site-packages\mxnet\base.py", line 246, in check_call raise get_last_ffi_error() mxnet.base.MXNetError: Traceback (most recent call last): File "c:\jenkins\workspace\mxnet-tag\mxnet\src\imperative./imperative_utils.h", line 146 MXNetError: Operator where inferring shapes failed. input shapes: [32,40,4] [32,40] [32,40] output shapes: [32,40] operator attributes:

it happens on Line 768

#This is a dummy computation to avoid deferred initialization error
#when past_is_pad is not used in the computation graph in default
#unrolling mode.
state = [
    F.where(
        is_padded_indicator.slice_axis(axis=1, begin=0, end=1).repeat(
            repeats=self.num_cells, axis=1
        ),
        bs,
        s,
    )
    for bs, s in zip(begin_state, state)
]

I see it's a dummy computation, so I commented them out and everything solved. Where I had the following outputs:

C:\Users\NPU.conda\envs\GPU_gluon\python.exe F:/baigao/Tutorial/MyTest2.py 100%|██████████| 100/100 [00:17<00:00, 5.57it/s, epoch=1/10, avg_epoch_loss=4.75] 100%|██████████| 100/100 [00:18<00:00, 5.35it/s, epoch=2/10, avg_epoch_loss=4.51] 100%|██████████| 100/100 [00:18<00:00, 5.30it/s, epoch=3/10, avg_epoch_loss=4.48] 100%|██████████| 100/100 [00:19<00:00, 5.20it/s, epoch=4/10, avg_epoch_loss=4.48] 100%|██████████| 100/100 [00:19<00:00, 5.07it/s, epoch=5/10, avg_epoch_loss=4.44] 100%|██████████| 100/100 [00:18<00:00, 5.31it/s, epoch=6/10, avg_epoch_loss=4.43] 100%|██████████| 100/100 [00:19<00:00, 5.25it/s, epoch=7/10, avg_epoch_loss=4.44] 100%|██████████| 100/100 [00:18<00:00, 5.27it/s, epoch=8/10, avg_epoch_loss=4.41] 100%|██████████| 100/100 [00:19<00:00, 5.24it/s, epoch=9/10, avg_epoch_loss=4.41] 100%|██████████| 100/100 [00:18<00:00, 5.28it/s, epoch=10/10, avg_epoch_loss=4.41]

But I am not quite sure if it's appropriate to do so. Looking forward to further assistance.

abdulfatir commented 1 year ago

dim = 2 # dimension of the observations

Hi! It looks like you're using a multivariate dataset with DeepAR. DeepAR is a univariate model. It only supports one-dimensional inputs. You might want to try a multivariate model instead. The list of available models can be found here. If you want something similar to DeepAR in the multivariate setting, use the DeepVAR model.