-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmaxpooling_layer.py
More file actions
36 lines (32 loc) · 1.31 KB
/
maxpooling_layer.py
File metadata and controls
36 lines (32 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import numpy as np
class MaxPoolingLayer:
# A Max Pooling layer .
def __init__(self, width, height, stride, name):
self.width = width
self.height = height
self.stride = stride
self.name = name
def forward(self, inputs):
self.inputs = inputs
C, W, H = inputs.shape
new_width = (W - self.width) // self.stride + 1
new_height = (H - self.height) // self.stride + 1
out = np.zeros((C, new_width, new_height))
for c in range(C):
for w in range(new_width):
for h in range(new_height):
out[c, w, h] = np.max(
self.inputs[c, w * self.stride:w * self.stride + self.width, h * self.stride:h * self.stride + self.height])
return out
def backward(self, dy):
C, W, H = self.inputs.shape
dx = np.zeros(self.inputs.shape)
for c in range(C):
for w in range(0, W, self.width):
for h in range(0, H, self.height):
st = np.argmax(self.inputs[c, w:w + self.width, h:h + self.height])
(idx, idy) = np.unravel_index(st, (self.width, self.height))
dx[c, w + idx, h + idy] = dy[c, w // self.width, h // self.height]
return dx
def extract(self):
return