forked from Alescontrela/Numpy-CNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
forward.py
72 lines (56 loc) · 2.1 KB
/
forward.py
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
'''
Description: forward operations for a convolutional neural network
Author: Alejandro Escontrela
Version: 1.0
Date: June 12th, 2018
'''
import numpy as np
#####################################################
################ Forward Operations #################
#####################################################
def convolution(image, filt, bias, s=1):
'''
Confolves `filt` over `image` using stride `s`
'''
(n_f, n_c_f, f, _) = filt.shape # filter dimensions
n_c, in_dim, _ = image.shape # image dimensions
out_dim = int((in_dim - f)/s)+1 # calculate output dimensions
assert n_c == n_c_f, "Dimensions of filter must match dimensions of input image"
out = np.zeros((n_f,out_dim,out_dim))
# convolve the filter over every part of the image, adding the bias at each step.
for curr_f in range(n_f):
curr_y = out_y = 0
while curr_y + f <= in_dim:
curr_x = out_x = 0
while curr_x + f <= in_dim:
out[curr_f, out_y, out_x] = np.sum(filt[curr_f] * image[:,curr_y:curr_y+f, curr_x:curr_x+f]) + bias[curr_f]
curr_x += s
out_x += 1
curr_y += s
out_y += 1
return out
def maxpool(image, f=2, s=2):
'''
Downsample `image` using kernel size `f` and stride `s`
'''
n_c, h_prev, w_prev = image.shape
h = int((h_prev - f)/s)+1
w = int((w_prev - f)/s)+1
downsampled = np.zeros((n_c, h, w))
for i in range(n_c):
# slide maxpool window over each part of the image and assign the max value at each step to the output
curr_y = out_y = 0
while curr_y + f <= h_prev:
curr_x = out_x = 0
while curr_x + f <= w_prev:
downsampled[i, out_y, out_x] = np.max(image[i, curr_y:curr_y+f, curr_x:curr_x+f])
curr_x += s
out_x += 1
curr_y += s
out_y += 1
return downsampled
def softmax(X):
out = np.exp(X)
return out/np.sum(out)
def categoricalCrossEntropy(probs, label):
return -np.sum(label * np.log(probs))