I have a tensor in
of shape (batch_size, features, steps) and want to get an output tensor out
of the same shape by average pooling over the time dimension (steps) with a window size of 2k+1
, that is:
out[b,f,t] = 1/(2k+1) sum_{t'=t-k,...,t+k} in[b,f,t']
For time steps where there are no k
preceding and succeeding time steps, I only want to calculate the average on the existing time steps.
However, the sequences in the tensor have variable length and are padded with zeros accordingly, I have the sequence lengths stored in another tensor (and could e.g. create a mask with them).
- I know I can use
out = tf.nn.avg_pool1d(in, ksize=2k+1, strides=1, padding="SAME", data_format="NCW")
that performs my described pooling-operation, however it does not understand that my sequences are padded with zero and does not allow me to pass a mask with the sequence lengths. - There also is
tf.keras.layers.GlobalAveragePooling1D
, but that layer always pools over the entire sequence and doesn't allow me to specify a window size.
How can I perform this operation with masking and a window size?