title: Average pooling with window over variable length sequences
body: <p>I have a tensor <code>in</code> of shape (batch_size, features, steps) and want to get an output tensor <code>out</code> of the same shape by average pooling over the time dimension (steps) with a window size of <code>2k+1</code>, that is:</p>
  
  <pre><code>out[b,f,t] = 1/(2k+1) sum_{t'=t-k,...,t+k} in[b,f,t']
  </code></pre>
  
  <p>For time steps where there are no <code>k</code> preceding and succeeding time steps, I only want to calculate the average on the existing time steps.</p>
  
  <p>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).</p>
  
  <ul>
  <li>I know I can use <a href="https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/nn/avg_pool1d" rel="nofollow noreferrer"><code>out = tf.nn.avg_pool1d(in, ksize=2k+1, strides=1, padding="SAME", data_format="NCW")</code></a> 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.</li>
  <li>There also is <a href="https://www.tensorflow.org/api_docs/python/tf/keras/layers/GlobalAveragePooling1D" rel="nofollow noreferrer"><code>tf.keras.layers.GlobalAveragePooling1D</code></a>, but that layer always pools over the entire sequence and doesn't allow me to specify a window size.</li>
  </ul>
  
  <p>How can I perform this operation <strong>with masking and a window size</strong>?</p>