title: How to speed up a non-built in function? How to make MaxPool operation by using an elliptical or circular kernel?
body: <p>I wrote a code that does MaxPool operation on tensors. In this code, I used two for loops to move the kernel on the matrix. However, when I ran this code, I saw that the code I wrote was running 50 times slower than the ready function. You can see my own function as follows:</p>
  <pre><code>    kernel = np.ones((k,k))
      p = round(0.5*(k-1))
      pA = np.pad(image, ((p,p),(p,p)), 'constant')
      dilated_image = np.zeros_like(image)
      for i in range(image.shape[0]):
          for j in range(image.shape[1]):
              dilated_image[i,j] = np.multiply(pA[i:i+k,j:j+k],kernel).max()
      return dilated_image
  </code></pre>
  <p>You can see the results as follows:</p>
  <p>Timing result of my own function:
  <a href="https://i.stack.imgur.com/f9c0e.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/f9c0e.png" alt="Timing result of my own function" /></a></p>
  <p>Timing result of Tensorflow MaxPool function:
  <a href="https://i.stack.imgur.com/aSkQ7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aSkQ7.png" alt="enter image description here" /></a></p>
  <p>What do you think could be the cause of such a problem and how can I speed up this code? Moreover, is there any way to do make an elliptical kernel in MaxPool operation in tensorflow?</p>