I made some changes to the code in the tutorial:
from typing import Dict
import torch
from torch import nn
from pytorch_forecasting.models import BaseModel
import numpy as np
import pandas as pd
from pytorch_forecasting import TimeSeriesDataSet
from sklearn.preprocessing import StandardScaler
v1 = np.arange(10)
v2 = np.arange(10)*2
test_data = pd.DataFrame(
dict(
value1=v1,
value2=v2,
value3=v1 + v2,
group=np.repeat(np.arange(2), 5),
time_idx=np.tile(np.arange(5), 2),
)
)
transfer = StandardScaler()
data_new = transfer.fit_transform(v1.reshape(-1,1))
dataset = TimeSeriesDataSet(
test_data,
group_ids=["group"],
target="value3",
time_idx="time_idx",
min_encoder_length=2,
max_encoder_length=2,
min_prediction_length=2,
max_prediction_length=2,
time_varying_unknown_reals=["value1", "value2", "value3"],
)
print(dataset.get_parameters())
dataloader = dataset.to_dataloader(batch_size=4)
x, y = next(iter(dataloader))
print("x =", x)
print("\ny =", y)
print("\nsizes of x =")
for key, value in x.items():
print(f"\t{key} = {value.size()}")
The test_data
and data_new
is:
id | value1 | value2 | value3 | group | time_idx | data_new |
---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 | -1.5666989 |
1 | 1 | 2 | 3 | 0 | 1 | -1.21854359 |
2 | 2 | 4 | 6 | 0 | 2 | -0.87038828 |
3 | 3 | 6 | 9 | 0 | 3 | -0.52223297 |
4 | 4 | 8 | 12 | 0 | 4 | -0.17407766 |
5 | 5 | 10 | 15 | 1 | 0 | 0.17407766 |
6 | 6 | 12 | 18 | 1 | 1 | 0.52223297 |
7 | 7 | 14 | 21 | 1 | 2 | 0.87038828 |
8 | 8 | 16 | 24 | 1 | 3 | 1.21854359 |
9 | 9 | 18 | 27 | 1 | 4 | 1.5666989 |
The output is:
{'time_idx': 'time_idx', 'target': 'value3', 'group_ids': ['group'],
'weight': None, 'max_encoder_length': 2, 'min_encoder_length': 2,
'min_prediction_idx': 0, 'min_prediction_length': 2, 'max_prediction_length': 2,
'time_varying_unknown_reals': ['value1', 'value2', 'value3'],
'scalers': {'value1': StandardScaler(), 'value2': StandardScaler()}}
x = {'encoder_cat': tensor([], size=(4, 2, 0), dtype=torch.int64), 'encoder_cont': tensor([[[ 0.5222, 0.5222, 6.0000],
[ 0.8704, 0.8704, 7.0000]],
[[-1.2185, -1.2185, 1.0000],
[-0.8704, -0.8704, 2.0000]],
[[-1.5667, -1.5667, 0.0000],
[-1.2185, -1.2185, 1.0000]],
[[ 0.1741, 0.1741, 5.0000],
[ 0.5222, 0.5222, 6.0000]]]), 'encoder_target': tensor([[6, 7],
[1, 2],
[0, 1],
[5, 6]]), 'encoder_lengths': tensor([2, 2, 2, 2]), 'decoder_cat': tensor([], size=(4, 2, 0), dtype=torch.int64), 'decoder_cont': tensor([[[ 1.2185, 1.2185, 8.0000],
[ 1.5667, 1.5667, 9.0000]],
[[-0.5222, -0.5222, 3.0000],
[-0.1741, -0.1741, 4.0000]],
[[-0.8704, -0.8704, 2.0000],
[-0.5222, -0.5222, 3.0000]],
[[ 0.8704, 0.8704, 7.0000],
[ 1.2185, 1.2185, 8.0000]]]), 'decoder_target': tensor([[8, 9],
[3, 4],
[2, 3],
[7, 8]]), 'decoder_lengths': tensor([2, 2, 2, 2]), 'decoder_time_idx': tensor([[3, 4],
[3, 4],
[2, 3],
[2, 3]]), 'groups': tensor([[1],
[0],
[0],
[1]]), 'target_scale': tensor([[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.]])}
y = (tensor([[8, 9],
[3, 4],
[2, 3],
[7, 8]]), None)
sizes of x =
encoder_cat = torch.Size([4, 2, 0])
encoder_cont = torch.Size([4, 2, 3])
encoder_target = torch.Size([4, 2])
encoder_lengths = torch.Size([4])
decoder_cat = torch.Size([4, 2, 0])
decoder_cont = torch.Size([4, 2, 3])
decoder_target = torch.Size([4, 2])
decoder_lengths = torch.Size([4])
decoder_time_idx = torch.Size([4, 2])
groups = torch.Size([4, 1])
target_scale = torch.Size([4, 2])
The problem is 'target': 'value3'
and 'time_varying_unknown_reals': ['value1', 'value2', 'value3']
In my opinion, this is an autoregressive problem: Covariant1, Covariant2, X(t) -> X(t+1)
.
If value3
is removed in time_varying_unknown_reals
, it is a prediction problem: Covariant1, Covariant2 -> X
the problem is the output of the encoder_cont
, its shape is (4, 2, 3) for (batch_size, encoder_len, unknown_reals_dim).
It seems that the first and second columns are the result of value1
and value2
standardization, but the third column is not value3
, it is id
of test_data
.
I don't understand. In my opinion, the output should be:
'encoder_cont': tensor([[[ 0.5222, 0.5222, 18], [ 0.8704, 0.8704, 21]], ...
'encoder_target': tensor([[18, 21] ...
Decoder and y are the same problem, I want to predict value3
instead of id