SHARE
TWEET

Untitled

a guest Jun 18th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class MaxPool2:
  2.   # ...
  3.  
  4.   def iterate_regions(self, image):
  5.     '''
  6.     Generates non-overlapping 2x2 image regions to pool over.
  7.     - image is a 2d numpy array
  8.     '''
  9.     h, w, _ = image.shape
  10.     new_h = h // 2
  11.     new_w = w // 2
  12.  
  13.     for i in range(new_h):
  14.       for j in range(new_w):
  15.         im_region = image[(i * 2):(i * 2 + 2), (j * 2):(j * 2 + 2)]
  16.         yield im_region, i, j
  17.  
  18.   def backprop(self, d_L_d_out):
  19.     '''
  20.     Performs a backward pass of the maxpool layer.
  21.     Returns the loss gradient for this layer's inputs.
  22.     - d_L_d_out is the loss gradient for this layer's outputs.
  23.     '''
  24.     d_L_d_input = np.zeros(self.last_input.shape)
  25.  
  26.     for im_region, i, j in self.iterate_regions(self.last_input):
  27.       h, w, f = im_region.shape
  28.       amax = np.amax(im_region, axis=(0, 1))
  29.  
  30.       for i2 in range(h):
  31.         for j2 in range(w):
  32.           for f2 in range(f):
  33.             # If this pixel was the max value, copy the gradient to it.
  34.             if im_region[i2, j2, f2] == amax[f2]:
  35.               d_L_d_input[i * 2 + i2, j * 2 + j2, f2] = d_L_d_out[i, j, f2]
  36.  
  37.     return d_L_d_input
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top