-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeepNetwork.py
More file actions
306 lines (251 loc) · 10.7 KB
/
DeepNetwork.py
File metadata and controls
306 lines (251 loc) · 10.7 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 14 07:21:19 2018
@author: Hemanth kumar
"""
import matplotlib.pyplot as plt
from progressbar import ProgressBar
import numpy as np
from sklearn.metrics import accuracy_score
from sklearn.model_selection import KFold
from sklearn.metrics import f1_score
class DeepNetwork:
''' Deep Network by Hemanth kumar '''
def __init__(self,X_train,y_train):
''' Initialize the network with training data
X_train --> InDependent variable
y_train --> Dependent variable
'''
self.X_train=X_train
self.y_train=y_train
print("\nNetwork Initialized..........\n")
print("Number of Training samples : ",len(X_train[0]))
print("Number of Features : ",len(X_train))
print("Available Activations :\n1.relu\n2.tanh\n3.sigmoid\n")
# Function to perforn K-FOLD CROSS VALIDATION
def k_fold_crossval(self,af,layer_dim,epoch,batch_size,alpha,k):
'''
af --> list of activation function [AF in hidden layer,AF of output unit]
layer_dim --> Dimension of the network , list of number of nodes at each layer [n1,n2,n3]
epochs --> Max Epochs / Iterations
batch_size --> Batch size
alpha --> Learning rate
K --> Fold size
'''
print("\n\nK-Fold Cross Validation initiated...........\n")
print("Alpha : ",alpha)
print("Network Dimension : ",layer_dim)
print("Activations : ",af)
print("Batch Size : ",batch_size)
print("Fold size : ",k,"\n\n")
x=self.X_train.T
y=self.y_train.T
scores=[]
kf = KFold(n_splits=k)
kf.get_n_splits(x)
i=1
for train_index, test_index in kf.split(x):
X__train, X__test = x[train_index], x[test_index]
y__train, y__test = y[train_index], y[test_index]
para,J_log=self.network(X__train.T,y__train.T,af,layer_dim,epoch,batch_size,alpha,plot=False,print_info=False)
y_pred=self.predict(X__test.T,len(layer_dim),para,af)
accuracy = accuracy_score(y__test,y_pred.T)
f1=f1_score(y__test,y_pred.T)
print('\nFold '+str(i)+' Accuracy Score:',accuracy*100,'%')
print('\nFold '+str(i)+' F1 Score:',f1,'\n')
i+=1
scores.append(accuracy)
return scores
# Function to define sigmoid activation function
def sigmoid(self,z):
s = 1 / (1 + np.exp(-z))
return s
# Function to define tanh activation function
def tanh(self,z):
return np.tanh(z)
# Function to define relu activation function
def relu(self,z):
z=np.where(z<0,0,z)
return z
# Function to initialize deep network
def init_deep(self,layer_size):
'''
layer_size --> list of number of nodes at each layer
'''
np.random.seed(3)
parameters = {}
n = len(layer_size)
for l in range(1, n):
parameters['W' + str(l)] = np.random.randn(layer_size[l], layer_size[l - 1]) * 0.01
parameters['b' + str(l)] = np.zeros((layer_size[l], 1))
return parameters
# Function to perform forward propogation
def forward_prop(self,W,X,b,AF):
'''
W --> Weight matrix of np
X --> Independent Variable
b --> Bias
AF --> Activation Function
'''
Z=np.dot(W,X)+b
if(AF=='tanh'):
A=self.tanh(Z)
elif AF=='sigmoid':
A=self.sigmoid(Z)
elif AF=='relu':
A=self.relu(Z)
return (Z,A)
# Function to define derivatives of activation function
def dir_AF(self,A,AF):
'''
AF --> Activation Function of whic derivative has to be found
A --> np vector to which derivative has to be calculated using Activation function
'''
if AF=='tanh':
return 1 - np.power(A, 2)
elif AF=='sigmoid':
return A*(1-A)
elif AF=='relu':
A=np.where(A>=0,1,A)
A=np.where(A<0,0,A)
return A
# Function to predict
def predict(self,X,layer_size,para,af):
'''
X --> InDependent variable used for prediction
layer_size --> List of number of nodes at each layer
para --> Parameters of the network
af --> List of activation function [AF in hidden layer,AF of output unit]
'''
Z={}
A={}
A['A0']=X
for l in range(1,layer_size):
if l==layer_size-1:
AF=af[1]
else:
AF=af[0]
Z['Z'+str(l)],A['A'+str(l)]=self.forward_prop(para['W'+str(l)],A['A'+str(l-1)],para['b'+str(l)],AF)
y_pred=np.round(A['A'+str(layer_size-1)])
return y_pred
#Function to plot error curve for each batch
def plot_graph(J_log):
'''
J_log --> List of errors per batch
'''
plt.title('Cost V/S Batch no#')
plt.xlabel('Batch no#')
plt.ylabel('Cost')
plt.plot(range(1,len(J_log)+1,1), J_log)
plt.show()
# Function that defines DNN
def network(self,x,y,af,l_dim,epoch,batch_size,alpha=0.2,plot=True,print_info=True):
'''
Parameters:
x --> Train data InDependent variable
y --> Train data Dependent variable
af --> List of activation function [AF in hidden layer,AF of output unit]
Available af:
1.relu
2.tanh
3.sigmoid
l_dim --> Dimension of the network , list of number of nodes at each layer [n1,n2,n3]
epoch --> Max Epochs / Iterations
batch_size --> Size of batch
alpha --> Learning rate
plot --> if True plots graph of error of each batch
return value:
1.parameters of the network i.e weights and bias at each layer as a dictionary
2.cost or error at each Epoch or iteration
'''
if(print_info):
print("\n\nTraining the Deep Network ......\nInitiated................\n")
print("Network Dimension : ",l_dim)
print("Activations : ",af)
print("Alpha : ",alpha)
print("Batch Size : ",batch_size)
print("Epochs : ",epoch,"\n")
pbar = ProgressBar()
layer_dim=l_dim.copy()
layer_size=len(layer_dim)
''' Initialize Deep Network '''
para=self.init_deep(layer_dim)
m=len(x[0])
J_log=[]
J_log_outer=[]
#Calulate inital error of network
a={}
z={}
a['A0']=x
for l in range(1,layer_size):
if l==layer_size-1:
AF=af[1]
else:
AF=af[0]
z['Z'+str(l)],a['A'+str(l)]=self.forward_prop(para['W'+str(l)],a['A'+str(l-1)],para['b'+str(l)],AF)
J = (- 1 / m) * np.sum(y * np.log(a['A'+str(layer_size-1)]) + (1 - y) * (np.log(1 - a['A'+str(layer_size-1)])))
J_log_outer.append(J)
''' Main Loop '''
for i in pbar(range(epoch)):
J_log=[]
'''---------Forward propogation---------'''
for b in list(range(0,m,batch_size)):
# BATCH TRAIN BEGIN
Z={}
A={}
dZ={}
dW={}
dA={}
dB={}
if(b+batch_size>m):
ss=(m-b+batch_size)%batch_size
select=list(range(b,b+ss))
else:
select=list(range(b,b+batch_size))
#Initial Error of Batch
A['A0']=x[:,select]
for l in range(1,layer_size):
if l==layer_size-1:
AF=af[1]
else:
AF=af[0]
Z['Z'+str(l)],A['A'+str(l)]=self.forward_prop(para['W'+str(l)],A['A'+str(l-1)],para['b'+str(l)],AF)
if(plot==True):
J = (- 1 / batch_size) * np.sum(y[:,select] * np.log(A['A'+str(layer_size-1)]) + (1 - y[:,select]) * (np.log(1 - A['A'+str(layer_size-1)])))
J_log.append(J)
'''---------Back propogation-------'''
# Initialize Back prop
dA['A'+str(layer_size-1)] = - (np.divide(y[:,select], A['A'+str(layer_size-1)]) - np.divide(1 - y[:,select], 1 - A['A'+str(layer_size-1)]))
for l in reversed(range(layer_size)):
if(l==0):
break
if l==layer_size-1:
AF=af[1]
else:
AF=af[0]
# Calculate Derivatives
dZ['Z'+str(l)]=dA['A'+str(l)] * self.dir_AF(A['A'+str(l)],AF)
dW['W'+str(l)]=(1/batch_size)*np.dot(dZ['Z'+str(l)],A['A'+str(l-1)].T)
dB['B'+str(l)]=(1/batch_size)*(np.sum(dZ['Z'+str(l)],axis=1,keepdims=True))
dA['A'+str(l-1)]=np.dot(para['W'+str(l)].T,dZ['Z'+str(l)])
para_tmp={}
# Update weights and bias
for l in range(1, layer_size):
para_tmp['W' + str(l)]= para['W' + str(l)] - alpha*dW['W' + str(l)]
para_tmp['b' + str(l)]= para['b' + str(l)] - alpha*dB['B' + str(l)]
para=para_tmp
if(plot==True):
self.plot_graph(J_log)
# Calculate cost/error per epoch or iteration
a={}
z={}
a['A0']=x
for l in range(1,layer_size):
if l==layer_size-1:
AF=af[1]
else:
AF=af[0]
z['Z'+str(l)],a['A'+str(l)]=self.forward_prop(para['W'+str(l)],a['A'+str(l-1)],para['b'+str(l)],AF)
J = (- 1 / m) * np.sum(y * np.log(a['A'+str(layer_size-1)]) + (1 - y) * (np.log(1 - a['A'+str(layer_size-1)])))
J_log_outer.append(J)
return para,J_log_outer