title: Simple Average Pooling with Tensorflow
body: <p>I'm trying to do some very simple average pooling on a Keras / Tensorflow Tensor (not a layer in a network). I have a list of 18 embeddings (embedding = 2D vector) and want to average pool them with a pool-size of 3 with no overlap. So the first 3 embeddings should be averaged to an embedding, then the next 3 and so on. In the end, I need a list of 18/3 = 6 embeddings.</p>
  
  <p>Here's my code so far:</p>
  
  <pre><code>    sequence_len = 3 # The number of embeddings that should be averaged
      feature_dim = 2 # the dimensions of embeddings
      input = [
          [1, 3],  # Anchor 1
          [2, 2],
          [3, 1],
          [4, 1],  # Anchor 2
          [6, 1],
          [8, 1],
          [12, 3],  # Positive 1
          [0, 6],
          [0, 0],
          [1, 1],  # Positive 2
          [1, 1],
          [1, 1],
          [5, 0],  # Negative 1
          [5, 12],
          [5, 0],
          [1, 1],  # Negative 2
          [1, 1],
          [1, 1],
      ]
      expected_out = [
          [2, 2],  # Anchor 1
          [6, 1],  # Anchor 2
          [4, 3],  # Positive 1
          [1, 1],  # Positive 2
          [5, 4],  # Negative 1
          [1, 1],  # Negative 2
      ]
      input_tensor = K.variable(input)
      output_tensor = a_function_i_cant_figure_out(input_tensor) # What's the API to call?
      assert numpy.array_equal(K.eval(output_tensor), expected_out)
  </code></pre>
  
  <p>I've spent over 2 hours on it now and I can't figure it out. I don't understand how I would have to reshape my data to get it to work with the <a href="https://www.tensorflow.org/api_docs/python/tf/nn/pool" rel="nofollow noreferrer">pooling function</a>.</p>
  
  <p>The closes I've come to a working solution is wrapping my input in another 2 lists (making it shape <code>(1,1,18,2)</code>) and calling this</p>
  
  <pre><code>output_tensor = K.pool2d(foo, (3, 2), (3, 2), pool_mode="avg", data_format="channels_first")
  </code></pre>
  
  <p>But this also averages the embeddings themselves. So instead of <code>[[2,2], [6,1], ...]</code> I'd receive <code>[2, 3.5, ...]</code></p>
  
  <p>This seems like such a trivial operation - how can I do this?</p>