Created
July 17, 2019 16:33
-
-
Save neodelphis/be2ce81ba555cbb2c731cbe24e30c33a to your computer and use it in GitHub Desktop.
A naive implementation of the forward pass for a convolutional layer.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def conv_forward_naive(x, w, b, conv_param): | |
""" | |
A naive implementation of the forward pass for a convolutional layer. | |
The input consists of N data points, each with C channels, height H and | |
width W. We convolve each input with F different filters, where each filter | |
spans all C channels and has height HH and width WW. | |
Input: | |
- x: Input data of shape (N, C, H, W) | |
- w: Filter weights of shape (F, C, HH, WW) | |
- b: Biases, of shape (F,) | |
- conv_param: A dictionary with the following keys: | |
- 'stride': The number of pixels between adjacent receptive fields in the | |
horizontal and vertical directions. | |
- 'pad': The number of pixels that will be used to zero-pad the input. | |
During padding, 'pad' zeros should be placed symmetrically (i.e equally on both sides) | |
along the height and width axes of the input. Be careful not to modfiy the original | |
input x directly. | |
Returns a tuple of: | |
- out: Output data, of shape (N, F, H', W') where H' and W' are given by | |
H' = 1 + (H + 2 * pad - HH) / stride | |
W' = 1 + (W + 2 * pad - WW) / stride | |
- cache: (x, w, b, conv_param) | |
""" | |
out = None | |
pad = conv_param['pad'] | |
stride = conv_param['stride'] | |
N, C, H, W = x.shape | |
F, _, HH, WW = w.shape | |
# dimensions de la sortie (pas de tests sur la validité des choix) | |
H_ = int(1 + (H + 2 * pad - HH) / stride) | |
W_ = int(1 + (W + 2 * pad - WW) / stride) | |
# 0-padding juste sur les deux dernières dimensions de x | |
xp = np.pad(x, ((0,), (0,), (pad,), (pad, )), 'constant') | |
out = np.zeros((N, F, H_, W_)) | |
# Version sans vectorisation | |
for n in range(N): # On parcourt toutes les images | |
for f in range(F): # On parcourt tous les filtres | |
for i in range(H_): # indices du résultat | |
for j in range(W_): | |
for k in range(HH): # indices du filtre | |
for l in range(WW): | |
for c in range(C): # profondeur | |
out[n,f,i,j] += xp[n, c, stride*i+k, stride*j+l] * w[f, c, k, l] | |
out[n,f,i,j] += b[f] | |
cache = (x, w, b, conv_param) | |
return out, cache |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment