TrafficWheel/model/EXP/trash/EXP25.py

197 lines
6.5 KiB
Python
Executable File

import torch
import torch.nn as nn
import torch.nn.functional as F
class DynamicTanh(nn.Module):
"""
Dynamic tanh activation with learnable scaling (alpha) and affine transformation (weight, bias).
"""
def __init__(self, normalized_shape, channels_last=True, alpha_init_value=0.5):
super().__init__()
self.normalized_shape = normalized_shape
self.alpha_init_value = alpha_init_value
self.channels_last = channels_last
# learnable scale for tanh
self.alpha = nn.Parameter(torch.full((1,), alpha_init_value))
# affine parameters
self.weight = nn.Parameter(torch.ones(normalized_shape))
self.bias = nn.Parameter(torch.zeros(normalized_shape))
def forward(self, x):
# scaled tanh
x = torch.tanh(self.alpha * x)
# affine transform
if self.channels_last:
x = x * self.weight + self.bias
else:
# channels_first: assume shape (B, C, H, W)
x = x * self.weight[:, None, None] + self.bias[:, None, None]
return x
def extra_repr(self):
return f"normalized_shape={self.normalized_shape}, alpha_init_value={self.alpha_init_value}, channels_last={self.channels_last}"
class DynamicGraphConstructor(nn.Module):
def __init__(self, node_num, embed_dim):
super().__init__()
self.nodevec1 = nn.Parameter(torch.randn(node_num, embed_dim), requires_grad=True)
self.nodevec2 = nn.Parameter(torch.randn(node_num, embed_dim), requires_grad=True)
def forward(self):
adj = torch.matmul(self.nodevec1, self.nodevec2.T)
adj = F.relu(adj)
adj = F.softmax(adj, dim=-1)
return adj
class GraphConvBlock(nn.Module):
def __init__(self, input_dim, output_dim):
super().__init__()
self.theta = nn.Linear(input_dim, output_dim)
self.residual = (input_dim == output_dim)
if not self.residual:
self.res_proj = nn.Linear(input_dim, output_dim)
def forward(self, x, adj):
res = x
x = torch.matmul(adj, x)
x = self.theta(x)
x = x + (res if self.residual else self.res_proj(res))
return F.relu(x)
class MANBA_Block(nn.Module):
"""
Multi-head attention + feed-forward network with DynamicTanh replacing LayerNorm.
"""
def __init__(self, input_dim, hidden_dim):
super().__init__()
self.attn = nn.MultiheadAttention(embed_dim=input_dim, num_heads=4, batch_first=True)
self.ffn = nn.Sequential(
nn.Linear(input_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, input_dim)
)
# replace LayerNorm with DynamicTanh
self.norm1 = DynamicTanh(normalized_shape=input_dim, channels_last=True)
self.norm2 = DynamicTanh(normalized_shape=input_dim, channels_last=True)
def forward(self, x):
# self-attention
res = x
x_attn, _ = self.attn(x, x, x)
x = self.norm1(res + x_attn)
# feed-forward
res2 = x
x_ffn = self.ffn(x)
x = self.norm2(res2 + x_ffn)
return x
class SandwichBlock(nn.Module):
def __init__(self, num_nodes, embed_dim, hidden_dim):
super().__init__()
self.manba1 = MANBA_Block(hidden_dim, hidden_dim * 2)
self.graph_constructor = DynamicGraphConstructor(num_nodes, embed_dim)
self.gc = GraphConvBlock(hidden_dim, hidden_dim)
self.manba2 = MANBA_Block(hidden_dim, hidden_dim * 2)
def forward(self, h):
h1 = self.manba1(h)
adj = self.graph_constructor()
h2 = self.gc(h1, adj)
h3 = self.manba2(h2)
return h3
class MLP(nn.Module):
def __init__(self, in_dim, hidden_dims, out_dim, activation=nn.ReLU):
super().__init__()
dims = [in_dim] + hidden_dims + [out_dim]
layers = []
for i in range(len(dims) - 2):
layers.append(nn.Linear(dims[i], dims[i+1]))
layers.append(activation())
layers.append(nn.Linear(dims[-2], dims[-1]))
self.net = nn.Sequential(*layers)
def forward(self, x):
return self.net(x)
class EXP(nn.Module):
def __init__(self, args):
super().__init__()
self.horizon = args['horizon']
self.output_dim = args['output_dim']
self.seq_len = args.get('in_len', 12)
self.hidden_dim = args.get('hidden_dim', 64)
self.num_nodes = args['num_nodes']
self.embed_dim = args.get('embed_dim', 16)
# discrete time embeddings
self.time_slots = args.get('time_slots', 24 * 60 // args.get('time_slot', 5))
self.time_embedding = nn.Embedding(self.time_slots, self.hidden_dim)
self.day_embedding = nn.Embedding(7, self.hidden_dim)
# input projection for flow history
self.input_proj = MLP(
in_dim = self.seq_len,
hidden_dims = [self.hidden_dim],
out_dim = self.hidden_dim
)
# two Sandwich blocks
self.sandwich1 = SandwichBlock(self.num_nodes, self.embed_dim, self.hidden_dim)
self.sandwich2 = SandwichBlock(self.num_nodes, self.embed_dim, self.hidden_dim)
# output projection
self.out_proj = MLP(
in_dim = self.hidden_dim,
hidden_dims = [2 * self.hidden_dim],
out_dim = self.horizon * self.output_dim
)
def forward(self, x):
"""
x: (B, T, N, D_total) where
x[...,0]=flow, x[...,1]=time_in_day (scaled), x[...,2]=day_in_week
"""
x_flow = x[..., 0] # (B, T, N)
x_time = x[..., 1] # (B, T, N)
x_day = x[..., 2] # (B, T, N)
B, T, N = x_flow.shape
assert T == self.seq_len, "Input sequence length mismatch"
# project flow history
x_flat = x_flow.permute(0, 2, 1).reshape(B * N, T)
h0 = self.input_proj(x_flat).view(B, N, self.hidden_dim)
# time embeddings at last step
t_idx = (x_time[:, -1, :] * (self.time_slots - 1)).long()
d_idx = x_day[:, -1, :].long()
time_emb = self.time_embedding(t_idx)
day_emb = self.day_embedding(d_idx)
# inject time features
h0 = h0 + time_emb + day_emb
# Sandwich + residuals
h1 = self.sandwich1(h0) + h0
h2 = self.sandwich2(h1)
# output
out = self.out_proj(h2)
out = out.view(B, N, self.horizon, self.output_dim)
out = out.permute(0, 2, 1, 3)
return out
# Example usage:
# args = {'horizon':12, 'output_dim':1, 'num_nodes':170}
# model = EXP(args)
# print(model)