Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MaxPool2:
- # ...
- def iterate_regions(self, image):
- '''
- Generates non-overlapping 2x2 image regions to pool over.
- - image is a 2d numpy array
- '''
- h, w, _ = image.shape
- new_h = h // 2
- new_w = w // 2
- for i in range(new_h):
- for j in range(new_w):
- im_region = image[(i * 2):(i * 2 + 2), (j * 2):(j * 2 + 2)]
- yield im_region, i, j
- def backprop(self, d_L_d_out):
- '''
- Performs a backward pass of the maxpool layer.
- Returns the loss gradient for this layer's inputs.
- - d_L_d_out is the loss gradient for this layer's outputs.
- '''
- d_L_d_input = np.zeros(self.last_input.shape)
- for im_region, i, j in self.iterate_regions(self.last_input):
- h, w, f = im_region.shape
- amax = np.amax(im_region, axis=(0, 1))
- for i2 in range(h):
- for j2 in range(w):
- for f2 in range(f):
- # If this pixel was the max value, copy the gradient to it.
- if im_region[i2, j2, f2] == amax[f2]:
- d_L_d_input[i * 2 + i2, j * 2 + j2, f2] = d_L_d_out[i, j, f2]
- return d_L_d_input
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement