Skip to content

Commit f0c144e

Browse files
committed
implemented pooling layers
1 parent 6bff439 commit f0c144e

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import numpy as np
2+
3+
def max_pooling(input_matrix, size=2, stride=2):
4+
input_height, input_width = input_matrix.shape
5+
output_height = (input_height - size) // stride + 1
6+
output_width = (input_width - size) // stride + 1
7+
output = np.zeros((output_height, output_width))
8+
9+
for y in range(0, output_height):
10+
for x in range(0, output_width):
11+
region = input_matrix[y*stride:y*stride+size, x*stride:x*stride+size]
12+
output[y, x] = np.max(region)
13+
14+
return output
15+
16+
def average_pooling(input_matrix, size=2, stride=2):
17+
input_height, input_width = input_matrix.shape
18+
output_height = (input_height - size) // stride + 1
19+
output_width = (input_width - size) // stride + 1
20+
output = np.zeros((output_height, output_width))
21+
22+
for y in range(0, output_height):
23+
for x in range(0, output_width):
24+
region = input_matrix[y*stride:y*stride+size, x*stride:x*stride+size]
25+
output[y, x] = np.mean(region)
26+
27+
return output
28+
29+
# Example usage
30+
if __name__ == "__main__":
31+
input_matrix = np.array([
32+
[1, 3, 2, 4],
33+
[5, 6, 7, 8],
34+
[9, 1, 2, 0],
35+
[3, 4, 5, 6]
36+
])
37+
38+
max_pooled = max_pooling(input_matrix)
39+
average_pooled = average_pooling(input_matrix)
40+
41+
print("Max Pooled Output:\n", max_pooled)
42+
print("Average Pooled Output:\n", average_pooled)

0 commit comments

Comments
 (0)