File size: 1,681 Bytes
3621f16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import os
import sys
import torch
import torch.nn as nn

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(PROJECT_ROOT, 'dmmv'))

class FixedMovingAvg(nn.Module):
    """Fixed Moving Average Layer."""
    def __init__(self, kernel_size=24):
        super().__init__()
        self.kernel_size = kernel_size
        self.avg = nn.AvgPool1d(kernel_size=kernel_size, stride=1, padding=kernel_size//2)
    
    def forward(self, x):
        # x: [B, SeqLen, C]
        x = x.permute(0, 2, 1)  # [B, C, SeqLen]
        out = self.avg(x)
        # Ensure output length equals input
        if out.size(2) != x.size(2):
            out = out[:, :, :x.size(2)]
        out = out.permute(0, 2, 1)
        return out

class FixedDecomp(nn.Module):
    def __init__(self, kernel_size):
        super().__init__()
        self.moving_avg = FixedMovingAvg(kernel_size)
    
    def forward(self, x):
        trend = self.moving_avg(x)
        seasonal = x - trend
        return seasonal, trend

from models.DMMV_S import Model as DMMV_S_Official

class DMMV_S_Fixed(nn.Module):
    """
    Fixed DMMV-S Model (Moving Average Decomposition).
    """
    def __init__(self, configs):
        super().__init__()
        configs.load_ckpt = False
        configs.trained_MAE_ckpt = None
        configs.ckpt = None
        configs.kernel_size = configs.period
        
        # Create official model
        self.original_model = DMMV_S_Official(configs)
        
        # Replace problematic decomposition module
        self.original_model.decompsition = FixedDecomp(configs.period)
    
    def forward(self, x):
        return self.original_model(x)