-
Notifications
You must be signed in to change notification settings - Fork 0
/
layers.py
214 lines (156 loc) · 6.8 KB
/
layers.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import numpy as np
class FullyConnectedLayer:
def __init__(self, in_nodes, out_nodes):
# Method to initialize a Fully Connected Layer
# Parameters
# in_nodes - number of input nodes of this layer
# out_nodes - number of output nodes of this layer
self.in_nodes = in_nodes
self.out_nodes = out_nodes
# Stores the outgoing summation of weights * feautres
self.data = None
# Initializes the Weights and Biases using a Normal Distribution with Mean 0 and Standard Deviation 0.1
self.weights = np.random.normal(0,0.1,(in_nodes, out_nodes))
self.biases = np.random.normal(0,0.1, (1, out_nodes))
def forwardpass(self, X):
# Input
# activations : Activations from previous layer/input
# Output
# activations : Activations after one forward pass through this layer
n = X.shape[0] # batch size
# INPUT activation matrix :[n X self.in_nodes]
# OUTPUT activation matrix :[n X self.out_nodes]
fin_out = np.zeros((n,self.out_nodes))
for i in range(n):
op = np.matmul(X[i], self.weights) + self.biases
fin_out[i] = op
self.data = fin_out
return sigmoid(fin_out)
def backwardpass(self, lr, activation_prev, delta):
# Input
# lr : learning rate of the neural network
# activation_prev : Activations from previous layer
# delta : del_Error/ del_activation_curr
# Output
# new_delta : del_Error/ del_activation_prev
# Update self.weights and self.biases for this layer by backpropagation
n = activation_prev.shape[0] # batch size
new_delta = np.matmul( delta * derivative_sigmoid(self.data) , np.transpose(self.weights))
del_w = np.matmul(np.transpose(activation_prev), delta * derivative_sigmoid(self.data))
self.weights -= lr * del_w
self.biases -= lr * (delta * derivative_sigmoid(self.data)).sum(axis = 0)
return new_delta
class ConvolutionLayer:
def __init__(self, in_channels, filter_size, numfilters, stride):
# Method to initialize a Convolution Layer
# Parameters
# in_channels - list of 3 elements denoting size of input for convolution layer
# filter_size - list of 2 elements denoting size of kernel weights for convolution layer
# numfilters - number of feature maps (denoting output depth)
# stride - stride to used during convolution forward pass
self.in_depth, self.in_row, self.in_col = in_channels
self.filter_row, self.filter_col = filter_size
self.stride = stride
self.out_depth = numfilters
self.out_row = int((self.in_row - self.filter_row)/self.stride + 1)
self.out_col = int((self.in_col - self.filter_col)/self.stride + 1)
# Stores the outgoing summation of weights * feautres
self.data = None
# Initializes the Weights and Biases using a Normal Distribution with Mean 0 and Standard Deviation 0.1
self.weights = np.random.normal(0,0.1, (self.out_depth, self.in_depth, self.filter_row, self.filter_col))
self.biases = np.random.normal(0,0.1,self.out_depth)
def forwardpass(self, X):
# print('Forward CN ',self.weights.shape)
# Input
# X : Activations from previous layer/input
# Output
# activations : Activations after one forward pass through this layer
n = X.shape[0] # batch size
# INPUT activation matrix :[n X self.in_depth X self.in_row X self.in_col]
# OUTPUT activation matrix :[n X self.out_depth X self.out_row X self.out_col]
dep = self.out_depth
row = self.out_row
col = self.out_col
stri = self.stride
filter_col = self.filter_col
filter_row = self.filter_row
output_actvation_matrix = np.zeros((n, dep, row, col))
for p in range(n):
for q in range(dep):
for r in range(row):
for s in range(col):
output_actvation_matrix[p,q,r,s] = np.sum(X[p, :, r*stri : r*stri+filter_row, s*stri:s*stri + filter_col] * self.weights[q]) + self.biases[q]
return sigmoid(output_actvation_matrix)
###############################################
def backwardpass(self, lr, activation_prev, delta):
# Input
# lr : learning rate of the neural network
# activation_prev : Activations from previous layer
# delta : del_Error/ del_activation_curr
# Output
# new_delta : del_Error/ del_activation_prev
# Update self.weights and self.biases for this layer by backpropagation
n = activation_prev.shape[0] # batch size
###############################################
class AvgPoolingLayer:
def __init__(self, in_channels, filter_size, stride):
# Method to initialize a Convolution Layer
# Parameters
# in_channels - list of 3 elements denoting size of input for max_pooling layer
# filter_size - list of 2 elements denoting size of kernel weights for convolution layer
# NOTE: Here we assume filter_size = stride
# And we will ensure self.filter_size[0] = self.filter_size[1]
self.in_depth, self.in_row, self.in_col = in_channels
self.filter_row, self.filter_col = filter_size
self.stride = stride
self.out_depth = self.in_depth
self.out_row = int((self.in_row - self.filter_row)/self.stride + 1)
self.out_col = int((self.in_col - self.filter_col)/self.stride + 1)
def forwardpass(self, X):
# print('Forward MP ')
# Input
# X : Activations from previous layer/input
# Output
# activations : Activations after one forward pass through this layer
n = X.shape[0] # batch size
# INPUT activation matrix :[n X self.in_depth X self.in_row X self.in_col]
# OUTPUT activation matrix :[n X self.out_depth X self.out_row X self.out_col]
dep = self.out_depth
row = self.out_row
col = self.out_col
stri = self.stride
filter_col = self.filter_col
filter_row = self.filter_row
output_actvation_matrix = np.zeros((n, dep, row, col))
for p in range(n):
for q in range(dep):
for r in range(row):
for s in range(col):
output_actvation_matrix[p,q,r,s] = np.average(X[p, q, r*stri : r*stri+filter_row, s*stri:s*stri + filter_col])
return (output_actvation_matrix)
###############################################
def backwardpass(self, alpha, activation_prev, delta):
# Input
# lr : learning rate of the neural network
# activation_prev : Activations from previous layer
# activations_curr : Activations of current layer
# delta : del_Error/ del_activation_curr
# Output
# new_delta : del_Error/ del_activation_prev
n = activation_prev.shape[0] # batch size
raise NotImplementedError
###############################################
# Helper layer to insert between convolution and fully connected layers
class FlattenLayer:
def __init__(self):
pass
def forwardpass(self, X):
self.in_batch, self.r, self.c, self.k = X.shape
return X.reshape(self.in_batch, self.r * self.c * self.k)
def backwardpass(self, lr, activation_prev, delta):
return delta.reshape(self.in_batch, self.r, self.c, self.k)
# Helper Function for the activation and its derivative
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def derivative_sigmoid(x):
return sigmoid(x) * (1 - sigmoid(x))