TimesNet:将一维时间序列转换为二维张量的建模

FreeGuideOnline 最新 2026-06-21

python

1. 周期发现

fft_result = torch.fft.rfft(x, dim=1) amplitudes = torch.abs(fft_result)[:, :, 1:] # 排除直流分量 _, topk_indices = torch.topk(amplitudes[:,0], k) # 取第一批次的频率 frequencies = topk_indices + 1 periods = torch.ceil(T / frequencies).int()

2. 并行处理每个周期

outs = [] for p in periods: # 填充与重塑 pad_len = p * (T // p + 1) - T x_padded = F.pad(x, (0,0,0, pad_len)) x_2d = x_padded.reshape(B, p, -1, C) # B, H, W, C x_2d = x_2d.permute(0, 3, 1, 2) # B, C, H, W # 二维卷积 (Inception) out_2d = inception(x_2d) # 保持 H, W 不变 out_1d = out_2d.reshape(B, -1, p * W) # 展回一维 out_1d = out_1d[:, :, :T] # 截断至原始长度 outs.append(out_1d)

3. 自适应融合

weights = softmax(learnable_params) output = sum(w * o for w, o in zip(weights, outs)) output = x + output # 残差