Conv1D: how to filter multiple time series data using one single kernel?

Dear all,
I have a time series data, recorded by 10 sensors, in shape [32,10,500] (batch size, sensor number, time points). I would like to use the same 1d kernel to filter these 10 sensor data (just like the wavelet filter), like below:

input=torch.ones((32,10,1,500))
m=nn.Conv2d(in_channels=10, out_channels=10, kernel_size=(1, 21), padding=(0, 10), padding_mode=‘reflect’, groups=10)
output=m(input)

However, the m.weight.shape gives me torch.Size([10, 1, 1, 21]), which means there are 10 different filters. How can I filter the 10 sensor data with the same filter?

Thanks in advance

Basically, I just want to implement a low-pass filter on time series data.

You could move the channel dimension to the batch dimension, apply the convolution, and reshape the output back. Alternatively, you could also set the weight of each filter to the same value, but you might need to redo this operation after updating the filter.

Thanks for your reply. Did you mean something like below:

class aaa(nn.Module):
    def __init__(self):
        super().__init__()
        self.l1=nn.Conv2d(10,10,(1,10),padding='same',padding_mode='reflect',bias=False,groups=10)
    def forward(self,x):
        for i in range(self.l1.weight.shape[0]):
            self.l1.weight.data[i,:,:,:]=self.l1.weight.data[0,:,:,:]
        output=self.l1(input)
        return output

I was expecting something more elegant. Like a switch, maybe a share_weight=True? Is there such thing?

Your approach would work, but you could also use a parametrization as seen here:

conv = nn.Conv1d(3, 10, 10)
print(conv.weight)

class WeightReuse(nn.Module):
    def forward(self, w):
        out_channels = w.size(0)
        w = w[0, ...].expand(out_channels, -1, -1)
        return w

torch.nn.utils.parametrize.register_parametrization(conv, "weight", WeightReuse())
print(conv.weight)
print(conv.parametrizations.weight.original)
1 Like

Many thanks. I think I will just reset the weight. I am not familiar with the parameterizations…