Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/dlinear padding for even kernel size #1695

Merged
merged 4 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions darts/models/forecasting/dlinear.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@ class _MovingAvg(nn.Module):

def __init__(self, kernel_size, stride):
super().__init__()
self.kernel_size = kernel_size
# asymmetrical padding, shorther on the ts start side
if kernel_size % 2 == 0:
self.padding_size_left = kernel_size // 2 - 1
self.padding_size_right = kernel_size // 2
else:
self.padding_size_left = (kernel_size - 1) // 2
self.padding_size_right = (kernel_size - 1) // 2
self.avg = nn.AvgPool1d(kernel_size=kernel_size, stride=stride, padding=0)

def forward(self, x):
# padding on the both ends of time series
front = x[:, 0:1, :].repeat(1, (self.kernel_size - 1) // 2, 1)
end = x[:, -1:, :].repeat(1, (self.kernel_size - 1) // 2, 1)
# padding on the both ends of time series with the extremities values
front = x[:, 0:1, :].repeat(1, self.padding_size_left, 1)
end = x[:, -1:, :].repeat(1, self.padding_size_right, 1)
x = torch.cat([front, x, end], dim=1)
x = self.avg(x.permute(0, 2, 1))
x = x.permute(0, 2, 1)
Expand Down Expand Up @@ -254,7 +260,8 @@ def __init__(
Default: False.

kernel_size
The size of the kernel for the moving average (default=25).
The size of the kernel for the moving average (default=25). If the size of the kernel is even,
the padding will be asymmetrical (shorter on the start/left side).
const_init
Whether to initialize the weights to 1/in_len. If False, the default PyTorch
initialization is used (default='True').
Expand Down
7 changes: 6 additions & 1 deletion darts/tests/models/forecasting/test_dlinear_nlinear.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,18 @@ def test_fit(self):
large_ts = tg.constant_timeseries(length=100, value=1000)
small_ts = tg.constant_timeseries(length=100, value=10)

for model_cls in [DLinearModel, NLinearModel]:
for (model_cls, kwargs) in [
(DLinearModel, {"kernel_size": 5}),
(DLinearModel, {"kernel_size": 6}),
(NLinearModel, {}),
]:
# Test basic fit and predict
model = model_cls(
input_chunk_length=1,
output_chunk_length=1,
n_epochs=10,
random_state=42,
**kwargs
)
model.fit(large_ts[:98])
pred = model.predict(n=2).values()[0]
Expand Down