How to implement Mean Over Time


I want to do mean over time in pytorch. Basically I don’t want to include the padding time steps while doing the mean. How can I do that?


If I understand your question correctly, you would like to implement a moving average filter for 1D input?
You could use a convolution with directly specified weights and apply it on your signal.

Try this code:

x = torch.randn(1, 1, 100)

mean_conv = nn.Conv1d(in_channels=1,

# Set kernel to calculate mean
kernel_weights = np.array([1., 1., 1., 1., 1.])/5 = torch.FloatTensor(kernel_weights).view(1, 1, 5)
output = mean_conv(Variable(x))

output.shape will be [1, 1, 96] since we did not use padding.