将对齐窗口压缩为查询,与每层原型嵌入计算概率;子层按父概率乘局部 softmax,混合带输入相关幅度的时序原型,并按累计预测误差选择原型分裂。
x=(2,6,4);prototype_dim=8、input_length=6、output_dim=3;初始4原型、分裂数2。(2,6,3)、score=(2,1,4);增长后2层,新增 score=(2,1,5)。484;一次增长后 822。新增层原型数5,增长后须更新优化器参数组。forward() 数据流顺序拆解 · 点击任意子模块方块可同步查看对应源码切片先对输入4个嵌入通道求均值,再用时间轴MLP将6维窗口编码到8维查询。各层原型概率混合 value_bias×prototype_value,返回每层预测与分数列表。实跑初始层及一次误差统计后的增长,重新挂钩验证新增 layer_bias.1 的 (2,1,16)→(2,1,5)。
108 patch_embedding = self.patch_embedding(x.mean(dim=-1, keepdim=True).transpose(1, 2))114 raw_score = torch.einsum('...ij,...jk->...ik', patch_embedding, 115 self.prototype_embedding[i].unsqueeze(0).expand(batch, -1, -1).transpose(1, 2)) 118 score = raw_score.softmax(dim=-1)120 local_score = self._compute_local_softmax(raw_score, i)123 parent_score = all_score[i-1][:, :, parent_idx]124 score = parent_score * local_score128 value_bias = torch.abs(self.layer_bias[i](patch_embedding))130 weighted_prototype = value_bias * self.prototype_value[i].unsqueeze(0)132 ans = (score.reshape(batch, -1, 1, 1) * weighted_prototype).sum(dim=1)159 _, split_indices = torch.topk(norm_loss, num_to_split)166 new_total = len(split_set) * self.split_num + (self.prototype_num[-1] - len(split_set))
x 的时间长度必须为6,示例最后维为4。训练端另行调用 update_error_statistics、add_prototype,并在增长后更新优化器;最终预测解码由宿主完成。
from ProtoTSHierarchicalPrototypes import ProtoTSHierarchicalPrototypes prototypes = ProtoTSHierarchicalPrototypes(8, input_length=6, output_dim=3) forecasts, scores = prototypes(x)
Method 动机参考:层次概率与可检查原型有助于分析窗口模式;输出时间长度仍为input_length,宿主预测解码器负责变换到最终预测时域。
| 消融变体 | 结构配置 | 验证目的 |
|---|---|---|
| 单层原型 | 保持初始4个原型 | 建立无增长的性能与参数参照 |
| 预设增长 | 固定日程,控制最终原型总数 | 区分容量增加与误差选分裂的影响 |
| 误差驱动层次增长 | 累计 MAE + 父子条件概率 | 报告预测误差、原型使用和参数增长;本表为实验设计 |
(K,input_length,output_dim),因此每层输出保留输入窗口长度。需要宿主解码器生成最终未来时域。