代码:时序数据分解为奇数列与偶数列再合并来自SCINET

看见SCINET源码,把时序数据分解成了奇数列与偶数列,好奇之下,重构一下代码~

X->(even,odd)再通过 Concat&Realign合并奇序列与偶序列。

首先所谓的even sequence和odd sequence的意思就是分别为从x序列采样从0这个位置采样0,2,...,2n;从1这个位置采样1,3,...,2n±1;

废话不多说,来看代码~,Splitting与zip_up_the_pants都是从Scinet源码里面拿出来的~,我只是简单测试了一下,方便更深入了解论文机制

测试代码如下:这是ipynb格式文件,请自行空行

import torch
from torch import nn 
class Splitting(nn.Module):
    def __init__(self):
        super(Splitting, self).__init__()

    def even(self, x):
        return x[:, ::2, :]

    def odd(self, x):
        return x[:, 1::2, :]

    def forward(self, x):
        '''Returns the odd and even part'''
        return (self.even(x), self.odd(x))

def zip_up_the_pants(even, odd):
    even = even.permute(1, 0, 2)
    odd = odd.permute(1, 0, 2) #L, B, D
    even_len = even.shape[0]
    odd_len = odd.shape[0]
    mlen = min((odd_len, even_len))
    _ = []
    for i in range(mlen):
        _.append(even[i].unsqueeze(0))
        _.append(odd[i].unsqueeze(0))
    if odd_len < even_len: 
        _.append(even[-1].unsqueeze(0))
    return torch.cat(_,0).permute(1,0,2) #B, L, D

x = torch.randn(8,107,7)
x.shape

split = Splitting()
even,odd = split(x)
print(even.shape)
print(odd.shape)

x_reconstruction = zip_up_the_pants(even,odd)
x_reconstruction.shape

猜你喜欢

转载自blog.csdn.net/weixin_43332715/article/details/127105230
今日推荐