-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDenseNetworks.py
212 lines (142 loc) · 5.2 KB
/
DenseNetworks.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 18 15:20:41 2019
@author: rohrdr
"""
import numpy as np
import abc
from Layers import Layer
from CostFunctions import LossFunction
from ActivationFunctions import ActivationFunction
class NeuralNetworks(abc.ABC):
@abc.abstractmethod
def __init__(self):
return
@abc.abstractmethod
def _forward_propagation(self):
vec = None
return vec
@abc.abstractmethod
def _backward_propagation(self):
return
@abc.abstractmethod
def _check_arrays(self, x):
assert(isinstance(x, np.ndarray))
assert((np.asarray(x).dtype.kind == "f") or (np.asarray(x).dtype.kind == "i"))
return
class DenseNN(NeuralNetworks):
"""
Neural Network with dense layers
the class implements
- the initialization of the network
- the forward propagation
- the backward propagation
- the training of the network
- the loss/cost function
"""
def __init__(self, dimensions, loss_func, learning_rate=0.02):
assert(isinstance(dimensions, list))
assert(isinstance(loss_func, LossFunction))
assert(isinstance(learning_rate, np.float))
self.lossFunc = loss_func
super().__init__()
self.nlayers = len(dimensions)
self.xdim = dimensions[0][0]
self.ydim = dimensions[-1][1]
self.learning_rate = learning_rate
self.layers = []
oldny = self.xdim
# initialize all the layers
for lay in dimensions:
assert (len(lay) == 3)
nx, ny, actfunc = lay
assert(isinstance(nx, int))
assert(oldny == nx) # do the dimensions check?
assert(isinstance(ny, int))
assert(isinstance(actfunc, ActivationFunction))
self.layers.append(Layer(nx, ny, actfunc))
oldny = ny
return
def get_loss(self, x, y):
"""
calculates the loss/cost with respect to y
"""
self._check_arrays(x)
self._check_arrays(y)
assert(x.shape[0] == self.xdim)
assert(y.shape[0] == self.ydim)
assert(len(x.shape) >= 2)
vec = self._forward_propagation(x)
assert (vec.shape[0] == y.shape[0])
loss = self.lossFunc.get_loss(vec, y)
return loss
def forward_propagation(self, x):
"""
the forward propagation with feature vector x
"""
self._check_arrays(x)
assert(x.shape[0] == self.xdim)
assert(len(x.shape) >= 2)
vec = self._forward_propagation(x)
return vec
def _forward_propagation(self, x):
super()._forward_propagation()
vec = x
for lay in self.layers:
vec = lay.get_y(vec)
return vec
def backward_propagation(self, yhat, y):
"""
the backward propagation with vectors yhat and y
"""
self._check_arrays(yhat)
self._check_arrays(y)
d_ws, d_bs = self._backward_propagation(yhat, y)
return d_ws, d_bs
def _backward_propagation(self, yhat, y):
super()._backward_propagation()
vec = self.lossFunc.get_loss_der(yhat, y)
d_ws = []
d_bs = []
self.layers.reverse()
for lay in self.layers:
vec, d_w, d_b = lay.get_grad(vec)
d_ws.append(d_w)
d_bs.append(d_b)
self.layers.reverse()
d_ws.reverse()
d_bs.reverse()
return d_ws, d_bs
def train_dn(self, x, y, maxiter=20, print_frequency=10, print_flag=True):
"""
train the network starting at x and labels y
"""
self._check_arrays(y)
self._check_arrays(x)
assert(x.shape[0] == self.xdim)
assert(len(x.shape) >= 2)
assert(isinstance(print_frequency, np.int))
for it in range(maxiter):
yhat = self._forward_propagation(x)
loss = self.lossFunc.get_loss(yhat, y)
d_ws, d_bs = self._backward_propagation(yhat, y)
for i, lay in enumerate(self.layers):
lay.update_wb(-self.learning_rate * d_ws[i], -self.learning_rate * d_bs[i])
if (it % print_frequency == 0) & print_flag:
print("Iteration: " + str(it) + " cost: " + str(loss))
if print_flag:
print("\n==================================")
print(" FINAL ITERATION RESULTS")
print("Iteration: " + str(it) + " cost: " + str(loss) + "\n")
return
def get_wandb(self):
ws = []
bs = []
for lay in self.layers:
ws.append(lay.W)
bs.append(lay.b)
return ws, bs
def _check_arrays(self, x):
super()._check_arrays(x)
return