You could use adaptive pooling layers (nn.AdaptiveAvgPool1d), which use a variable kernel size to create the desired output shape.
Alternatively, you could also just calculate the mean via output = output.mean(1, keepdim=True).