forked from exo7math/deepmath-exo7
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpooling.py
More file actions
73 lines (48 loc) · 1.59 KB
/
Copy pathpooling.py
File metadata and controls
73 lines (48 loc) · 1.59 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import numpy as np
def max_pooling(A,k):
n, p = A.shape
B = A.reshape(n//k,k,p//k,k)
C = B.transpose((0, 2, 1, 3))
D = C.max(axis=(2,3))
return D
def average_pooling(A,k):
n, p = A.shape
B = A.reshape(n//k,k,p//k,k)
C = B.transpose((0, 2, 1, 3))
D = C.mean(axis=(2,3))
return D
# Un exemple
A = np.arange(24) # liste d'entier
np.random.shuffle(A) # mélange aléatoire
A = A.reshape((4,6)) # matrice
print("\n===== Exemple =====")
print("Matrice de départ :\n",A)
print("Taille de la matrice de départ", A.shape)
B = max_pooling(A,2)
print("Matrice après max-pooling :\n", B)
print("Taille de la matrice d'arrivée", B.shape)
C = average_pooling(A,2)
print("Matrice après pooling en moyenne :\n", C)
print("Taille de la matrice d'arrivée", C.shape)
################################
################################
# Exemple pas à pas pour comprendre comment
# fonctionne les fonctions ci-dessus
def exemple():
N = 6 # taille de la matrice initiale
k = 3 # taille du pooling
# matrice résultatnte sera de taille N//k
A = np.arange(N**2).reshape((N,N))
n, p = A.shape
B = A.reshape(n//k,k,p//k,k)
C = B.transpose((0, 2, 1, 3))
D = C.max(axis=(2,3))
print("===== Exemple pas à pas =====")
print("Matrice de départ :\n", A)
print("Taille de la matrice de départ", A.shape)
print('Première sous matrice : \n', C[0,0])
print('Max de cette sous matrice', C[0,0].max())
print('Matrice des max : \n', C.max(axis=(2,3)))
print('Matrice des moyennes :\n', C.mean(axis=(2,3)))
return
exemple()