学术
模块更新于 2026-11-20· 浏览 5· 下载 1

PDM 历史可分解混合

先分解、再分向混合:过去序列先经平均池化下采样成多尺度,再逐尺度分解出季节项和趋势项,季节项自底向上由细到粗混合,趋势项自顶向下由粗到细混合,两类信息各走各的聚合路径

时间序列时间序列时间序列预测PDMTimeMixerICLR2024

结构图

PDM 历史可分解混合结构图
PDM 历史可分解混合结构图

先分解、再分向混合:过去序列先经平均池化下采样成多尺度,再逐尺度分解出季节项和趋势项,季节项自底向上由细到粗混合,趋势项自顶向下由粗到细混合,两类信息各走各的聚合路径

论文信息

  • 论文:TimeMixer: Decomposable Multiscale Mixing for Time Series Forecasting
  • 出处:ICLR 2024

核心痛点

真实世界的时间序列中各种变化深度交织,主流的分解与多周期分析范式停留在单一尺度,微观季节细节和宏观趋势纠缠不清;把多尺度序列当作整体直接混合,性质不同的两类分量互相干扰,预测精度和效率双输。

模块亮点

  • 先分解、再分向混合:过去序列先经平均池化下采样成多尺度,再逐尺度分解出季节项和趋势项,季节项自底向上由细到粗混合,趋势项自顶向下由粗到细混合,两类信息各走各的聚合路径
  • 方向设计有依据:大周期由小周期聚合而成,细尺度细节能补全粗尺度的季节建模;趋势正相反,细尺度波动只会引入噪声,要靠粗尺度宏观知识引导;消融显示改成同向或反向混合、去掉分解直接混合,性能都会下降
  • 消融涨点扎实:去掉整个 PDM 块,PEMS04 的 MAE 从 19.21 恶化到 24.87;整体 TimeMixer 在 18 个基准上持续 SOTA,比 PatchTST 在 Weather 上 MSE 降低 9.4%、Solar-Energy 上降低 24.7%

适用场景

领域为长期 / 短期时间序列预测,覆盖 ETT、Weather、PEMS、M4 等基准;可迁移到交通、天气、能源、金融等预测场景,以及异常检测、时序分类等需要多尺度时序表征的任务。

插入位置建议

  • 在嵌入层之后、预测头之前堆叠若干个 PDM 块作为编码器主干,输入为下采样得到的多尺度序列
  • 替换 SCINet、Pyraformer 等多尺度模型的跨尺度交互模块,改成季节自底向上、趋势自顶向下的分解式混合
  • 接在自己模型的过去信息提取阶段,输出的各尺度表征直接接预测头或按尺度预测后再融合

适用任务

  • 长期
  • 短期时间序列预测
  • 覆盖 ETT

完整代码

pdm.py
import torch
import torch.nn as nn

"""ICLR2024  https://arxiv.org/abs/2405.14616
时间序列预测被广泛用于广泛的应用,例如交通规划和天气预报。然而,现实世界的时间序列通常呈现出错综复杂的时间变化,这使得预测极具挑战性。
超越了普通分解和多周期性分析的主流范式,我们以一种新颖的多尺度混合视角分析了时间变化,该视角基于一个直观但重要的观察结果,
即时间序列在不同的采样尺度中呈现出不同的模式。微观和宏观信息分别反映在精细和粗大尺度上,因此可以固有地解开复杂的变化。
基于这一观察结果,我们建议将 TimeMixer 作为一种完全基于 MLP 的架构,具有 Past-Decomposable-Mixing (PDM) 
和 Future-Multipredictor-Mixing (FMM) 模块,以充分利用过去提取和未来预测阶段的解纠缠多尺度序列。
具体而言,PDM将分解应用于多尺度序列,并进一步将分解的季节性分量和趋势分量分别在细到粗和粗到细方向上混合,从而连续聚合了微观季节性和宏观趋势信息。
FMM进一步集成了多个预测因子,以在多尺度观测中利用互补的预测能力。
因此,TimeMixer 能够在长期和短期预测任务中实现始终如一的先进性能,并具有良好的运行时效率。
"""

class moving_avg(nn.Module):
    """
    Moving average block to highlight the trend of time series
    """

    def __init__(self, kernel_size, stride):
        super(moving_avg, self).__init__()
        self.kernel_size = kernel_size
        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)
        x = torch.cat([front, x, end], dim=1)
        x = self.avg(x.permute(0, 2, 1))
        x = x.permute(0, 2, 1)
        return x


class series_decomp(nn.Module):
    """
    Series decomposition block
    """

    def __init__(self, kernel_size):
        super(series_decomp, self).__init__()
        self.moving_avg = moving_avg(kernel_size, stride=1)

    def forward(self, x):
        moving_mean = self.moving_avg(x)
        res = x - moving_mean
        return res, moving_mean


class DFT_series_decomp(nn.Module):
    """
    Series decomposition block
    """

    def __init__(self, top_k=5):
        super(DFT_series_decomp, self).__init__()
        self.top_k = top_k

    def forward(self, x):
        xf = torch.fft.rfft(x)
        freq = abs(xf)
        freq[0] = 0
        top_k_freq, top_list = torch.topk(freq, self.top_k)
        xf[freq <= top_k_freq.min()] = 0
        x_season = torch.fft.irfft(xf)
        x_trend = x - x_season
        return x_season, x_trend

class MultiScaleSeasonMixing(nn.Module):
    """
    Bottom-up mixing season pattern
    """

    def __init__(self, configs):
        super(MultiScaleSeasonMixing, self).__init__()

        self.down_sampling_layers = torch.nn.ModuleList(
            [
                nn.Sequential(
                    torch.nn.Linear(
                        configs.seq_len // (configs.down_sampling_window ** i),
                        configs.seq_len // (configs.down_sampling_window ** (i + 1)),
                    ),
                    nn.GELU(),
                    torch.nn.Linear(
                        configs.seq_len // (configs.down_sampling_window ** (i + 1)),
                        configs.seq_len // (configs.down_sampling_window ** (i + 1)),
                    ),

                )
                for i in range(configs.down_sampling_layers)
            ]
        )

    def forward(self, season_list):

        # mixing high->low
        out_high = season_list[0]
        out_low = season_list[1]
        out_season_list = [out_high.permute(0, 2, 1)]

        for i in range(len(season_list) - 1):
            out_low_res = self.down_sampling_layers[i](out_high)
            out_low = out_low + out_low_res
            out_high = out_low
            if i + 2 <= len(season_list) - 1:
                out_low = season_list[i + 2]
            out_season_list.append(out_high.permute(0, 2, 1))

        return out_season_list


class MultiScaleTrendMixing(nn.Module):
    """
    Top-down mixing trend pattern
    """

    def __init__(self, configs):
        super(MultiScaleTrendMixing, self).__init__()

        self.up_sampling_layers = torch.nn.ModuleList(
            [
                nn.Sequential(
                    torch.nn.Linear(
                        configs.seq_len // (configs.down_sampling_window ** (i + 1)),
                        configs.seq_len // (configs.down_sampling_window ** i),
                    ),
                    nn.GELU(),
                    torch.nn.Linear(
                        configs.seq_len // (configs.down_sampling_window ** i),
                        configs.seq_len // (configs.down_sampling_window ** i),
                    ),
                )
                for i in reversed(range(configs.down_sampling_layers))
            ])

    def forward(self, trend_list):

        # mixing low->high
        trend_list_reverse = trend_list.copy()
        trend_list_reverse.reverse()
        out_low = trend_list_reverse[0]
        out_high = trend_list_reverse[1]
        out_trend_list = [out_low.permute(0, 2, 1)]

        for i in range(len(trend_list_reverse) - 1):
            out_high_res = self.up_sampling_layers[i](out_low)
            out_high = out_high + out_high_res
            out_low = out_high
            if i + 2 <= len(trend_list_reverse) - 1:
                out_high = trend_list_reverse[i + 2]
            out_trend_list.append(out_low.permute(0, 2, 1))

        out_trend_list.reverse()
        return out_trend_list

class PastDecomposableMixing(nn.Module):
    def __init__(self, configs):
        super(PastDecomposableMixing, self).__init__()
        self.seq_len = configs.seq_len
        self.pred_len = configs.pred_len
        self.down_sampling_window = configs.down_sampling_window

        self.layer_norm = nn.LayerNorm(configs.d_model)
        self.dropout = nn.Dropout(configs.dropout)
        self.channel_independence = configs.channel_independence

        if configs.decomp_method == 'moving_avg':
            self.decompsition = series_decomp(configs.moving_avg)
        elif configs.decomp_method == "dft_decomp":
            self.decompsition = DFT_series_decomp(configs.top_k)
        else:
            raise ValueError('decompsition is error')

        if configs.channel_independence == 0:
            self.cross_layer = nn.Sequential(
                nn.Linear(in_features=configs.d_model, out_features=configs.d_ff),
                nn.GELU(),
                nn.Linear(in_features=configs.d_ff, out_features=configs.d_model),
            )

        # Mixing season
        self.mixing_multi_scale_season = MultiScaleSeasonMixing(configs)

        # Mxing trend
        self.mixing_multi_scale_trend = MultiScaleTrendMixing(configs)

        self.out_cross_layer = nn.Sequential(
            nn.Linear(in_features=configs.d_model, out_features=configs.d_ff),
            nn.GELU(),
            nn.Linear(in_features=configs.d_ff, out_features=configs.d_model),
        )

    def forward(self, x_list):
        length_list = []
        for x in x_list:
            _, T, _ = x.size()
            length_list.append(T)

        # Decompose to obtain the season and trend
        season_list = []
        trend_list = []
        for x in x_list:
            season, trend = self.decompsition(x)
            if self.channel_independence == 0:
                season = self.cross_layer(season)
                trend = self.cross_layer(trend)
            season_list.append(season.permute(0, 2, 1))
            trend_list.append(trend.permute(0, 2, 1))

        # bottom-up season mixing
        out_season_list = self.mixing_multi_scale_season(season_list)
        # top-down trend mixing
        out_trend_list = self.mixing_multi_scale_trend(trend_list)

        out_list = []
        for ori, out_season, out_trend, length in zip(x_list, out_season_list, out_trend_list,
                                                      length_list):
            out = out_season + out_trend
            if self.channel_independence:
                out = ori + self.out_cross_layer(out)
            out_list.append(out[:, :length, :])
        return out_list


class Configs:
    def __init__(self):
        self.seq_len = 96
        self.pred_len = 24
        self.down_sampling_window = 2
        self.down_sampling_layers = 3
        self.d_model = 512
        self.d_ff = 2048
        self.dropout = 0.1
        self.channel_independence = 1
        self.decomp_method = 'moving_avg'
        self.moving_avg = 25
        self.top_k = 5


if __name__ == '__main__':
    configs = Configs()
    block = PastDecomposableMixing(configs)

    # Creating a list of random input tensors with varying sequence lengths
    input_tensors = [torch.rand((8, configs.seq_len // (configs.down_sampling_window ** i), configs.d_model)) for i in
                     range(configs.down_sampling_layers + 1)]

    # Forward pass
    output_tensors = block(input_tensors)

    for i, (input_tensor, output_tensor) in enumerate(zip(input_tensors, output_tensors)):
        print(f"Input {i} size:", input_tensor.size())
        print(f"Output {i} size:", output_tensor.size())

环境与依赖

Python 3.9+

继续浏览

下一篇PeakConv 峰值卷积把 CFAR 装进卷积:把感受野重定义为峰值感受野 PRF,端到端学习"中心响应减干扰响应"的峰值检测逻辑,固定阈值变成可学习权重

相关资源