From e4aa294617f85338564eddfe53c3dba4ffd9b812 Mon Sep 17 00:00:00 2001 From: Ruonan Wang <105281011+rnwang04@users.noreply.github.com> Date: Thu, 25 Aug 2022 16:52:19 +0800 Subject: [PATCH] Chronos: set kernel of Autoformer to odd number (#5534) * set kernel to odd --- .../bigdl/chronos/model/autoformer/Autoformer.py | 7 ++++--- .../forecaster/test_autoformer_forecaster.py | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/python/chronos/src/bigdl/chronos/model/autoformer/Autoformer.py b/python/chronos/src/bigdl/chronos/model/autoformer/Autoformer.py index c7e07751222..b12e8473414 100644 --- a/python/chronos/src/bigdl/chronos/model/autoformer/Autoformer.py +++ b/python/chronos/src/bigdl/chronos/model/autoformer/Autoformer.py @@ -72,7 +72,8 @@ def __init__(self, configs): self.loss = loss_creator(configs.loss) # Decomp - kernel_size = configs.moving_avg + # change kernei_size to odd + kernel_size = int(2 * (configs.moving_avg // 2)) + 1 self.decomp = series_decomp(kernel_size) # Embedding @@ -93,7 +94,7 @@ def __init__(self, configs): configs.d_model, configs.n_heads), configs.d_model, configs.d_ff, - moving_avg=configs.moving_avg, + moving_avg=kernel_size, dropout=configs.dropout, activation=configs.activation ) for l in range(configs.e_layers) @@ -115,7 +116,7 @@ def __init__(self, configs): configs.d_model, configs.c_out, configs.d_ff, - moving_avg=configs.moving_avg, + moving_avg=kernel_size, dropout=configs.dropout, activation=configs.activation, ) diff --git a/python/chronos/test/bigdl/chronos/forecaster/test_autoformer_forecaster.py b/python/chronos/test/bigdl/chronos/forecaster/test_autoformer_forecaster.py index 87df6b83d23..51d8750f8e5 100644 --- a/python/chronos/test/bigdl/chronos/forecaster/test_autoformer_forecaster.py +++ b/python/chronos/test/bigdl/chronos/forecaster/test_autoformer_forecaster.py @@ -224,3 +224,19 @@ def test_autoformer_forecaster_tune_save_load(self): forecaster.load(ckpt_name) evaluate2 = forecaster.evaluate(val_data) assert evaluate1[0]['val/loss'] == evaluate2[0]['val_loss'] + + def test_autoformer_forecaster_even_kernel(self): + train_loader, val_loader, test_loader = create_data(loader=True) + evaluate_list = [] + forecaster = AutoformerForecaster(past_seq_len=24, + future_seq_len=5, + input_feature_num=2, + output_feature_num=2, + label_len=12, + freq='s', + seed=0, + moving_avg=20) # even + forecaster.fit(train_loader, epochs=3, batch_size=32) + evaluate = forecaster.evaluate(val_loader) + pred = forecaster.predict(test_loader) + evaluate_list.append(evaluate)