forked from RubensZimbres/Repo-2017
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Neural Network Architecture
126 lines (105 loc) · 4.02 KB
/
Neural Network Architecture
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
import keras
import pandas as pd
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation,Merge
from keras.optimizers import SGD
from scipy.interpolate import spline
from keras.callbacks import LearningRateScheduler
from sklearn.preprocessing import StandardScaler
from sklearn import preprocessing
from keras.layers.normalization import BatchNormalization
from sklearn.preprocessing import MinMaxScaler
from sklearn import datasets
iris = datasets.load_iris()
sd=[]
class LossHistory(keras.callbacks.Callback):
def on_train_begin(self, logs={}):
self.losses = [1,1]
def on_epoch_end(self, batch, logs={}):
self.losses.append(logs.get('loss'))
sd.append(step_decay(len(self.losses)))
print('lr:', step_decay(len(self.losses)))
def step_decay(losses):
if float(2*np.sqrt(np.array(history.losses[-1])))<0.23:
lrate=0.06
momentum=0.3
decay_rate=2e-6
return lrate
else:
lrate=0.06
return lrate
learning_rate = 0.07
decay_rate = 5e-6
momentum = 0.7
scaler = MinMaxScaler(feature_range=(0, 1))
X_train=scaler.fit_transform(iris.data)
X_train2=scaler.fit_transform(iris.data[:,2:4])
X_train_right = scaler.fit_transform(iris.data[:,2:4])
X_train_left = scaler.fit_transform(iris.data[:,0:2])
Y_train = np.array(pd.get_dummies(iris.target))
### SIMPLE NEURAL NETS
model0=Sequential()
model0.add(Dense(5, input_dim=4, init='glorot_uniform'))
model0.add(Dense(3))
model0.add(Activation('sigmoid'))
sgd = SGD(lr=learning_rate,momentum=momentum, decay=decay_rate, nesterov=False)
model0.compile(loss = 'binary_crossentropy', optimizer = sgd, metrics = ['accuracy'])
model0.fit(X_train, Y_train,
batch_size = 16, nb_epoch = 100, verbose = 1)
res0 = model0.predict_classes([X_train])
acc0=((res0-iris.target)==0).sum()/len(res0)
### SIMPLE NEURAL NETS WITH VARIABLE SELECTION
model2=Sequential()
model2.add(Dense(5, input_dim=2, init='glorot_uniform'))
model2.add(Dense(3))
model2.add(Activation('sigmoid'))
sgd = SGD(lr=learning_rate,momentum=momentum, decay=decay_rate, nesterov=False)
model2.compile(loss = 'binary_crossentropy', optimizer = sgd, metrics = ['accuracy'])
model2.fit(X_train2, Y_train,
batch_size = 16, nb_epoch = 100, verbose = 1)
res2 = model2.predict_classes([X_train2])
acc2=((res2-iris.target)==0).sum()/len(res2)
### PARALLEL NEURAL NETS
model_left=Sequential()
model_left.add(Dense(5, input_dim=2, init='glorot_uniform'))
model_left.add(Dropout(0.5))
model_left.add(Dense(3))
model_right=Sequential()
model_right.add(Dense(5, input_dim=2, init='glorot_uniform'))
model_right.add(Dense(3))
# mode concat, dot, sum
model3=Sequential()
model3.add(Merge([model_left,model_right],mode = 'sum'))
model3.add(Dense(3, init = 'glorot_uniform'))
model3.add(Activation('sigmoid'))
model3.compile(loss = 'binary_crossentropy', optimizer = sgd, metrics = ['accuracy'])
model3.fit([X_train_left, X_train_right], Y_train,
batch_size = 16, nb_epoch = 100, verbose = 1)
# 1
res = model3.predict_classes([X_train_left,X_train_right])
acc3=((res-iris.target)==0).sum()/len(res)
print('Accuracy Simple Neural Net (4 features):',acc0)
print('Accuracy Simple Neural Net + Variable Selection (2 features):',acc2)
print('Accuracy Parallel Neural Net (4 features):',acc3)
ss2=[acc0,acc2,acc3]
algos=[" Simple NN 4 features", "NN + Var Select 2 features", "Parallel NN 4 features"]
s2 = pd.Series(
ss2,
index = [" Simple NN 4 features", "NN + Variable Selection 2 features", "Parallel NN 4 features"]
)
plt.figure(figsize=(9,6))
plt.title("NEURAL NET ARCHITECTURE COMPARISON - IRIS CLASSIFICATION TASK"+'\n'+"100 EPOCHS - LR=0.07 - MOMENTUM=0.7")
plt.ylabel('MODEL SCORE')
plt.xlabel('MODEL TYPE')
ax = plt.gca()
ax.tick_params(axis='x', colors='black')
ax.tick_params(axis='y', colors='black')
for i in range(0,len(algos)):
ax.text(i,ss2[i],round(ss2[i],4),ha='center', va='bottom')
my_colors = 'rgb'
s2.plot( kind='bar', color=my_colors)
plt.axhline(0, color='black')
plt.xticks(rotation=0)
plt.ylim(.85,1)
plt.show()