title: How to perform max pooling operation over 3D convolution array?
body: <p>I'm building a convolutional neural network with numpy, and I'm not sure that my pooling treatment of the 3D (HxWxD) input image is correct. </p>
  
  <p>As an example, I have an image shaped (12x12x3) I convolve it to (6x6x3), and I want to perform max pooling such that I obtain a (3x3x3) image. To do this, I choose a filter size of (2x2) and a stride of 2. </p>
  
  <pre class="lang-py prettyprint-override"><code>output_size = int((conv.shape[0]-F)/S + 1)
  pool = np.zeros((output_size,output_volume,3)) # pool array
  for k in range(conv.shape[-1]): # loop over conv depth
      i_stride = 0 
      for i in range(output_size): 
          j_stride = 0
          for j in range(output_size):
              pool[i,j,k] = np.amax(conv[i_stride:i_stride+F,
                                                  j_stride:j_stride+F,k],0)
              j_stride+=S 
          i_stride+=S
  </code></pre>
  
  <p>For the first channel of my convolution array <code>conv[:,:,0]</code> <a href="https://i.stack.imgur.com/sdHPi.png" rel="nofollow noreferrer">I obtain the following</a>.
  Comparing this with the first channel of the max pooling array <code>pool[:,:,0]</code> <a href="https://i.stack.imgur.com/Fkbfg.png" rel="nofollow noreferrer">I get</a>. At a glance I can tell that the pooling operation is not correct, <code>conv[0:2,0:2,0]</code> (mostly gray) is most definitely not <code>pool[0,0,0]</code> (black), you'd expect it to be one of the shades of gray. So, I'm convinced that something is definitely wrong here. Either my for loop or the two comparisons I'm making are off.</p>
  
  <p>If anyone can help me better understand the pooling operation over the array with 3 dimensions, that will definitely help.</p>