-
Notifications
You must be signed in to change notification settings - Fork 676
/
Deep Residual Learning
109 lines (90 loc) · 5.15 KB
/
Deep Residual Learning
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
import keras
import pandas as pd
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation,Merge,Lambda,GlobalAveragePooling1D,GlobalAveragePooling2D,UpSampling1D,UpSampling2D
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
import matplotlib.pyplot as plt
from sklearn import datasets
import keras.backend as K
from keras.layers.core import Reshape
iris = datasets.load_iris()
learning_rate = 0.028
decay_rate = 5e-6
momentum = 0.9
epochs=50
scaler = MinMaxScaler(feature_range=(0, 1))
X_train=scaler.fit_transform(iris.data[:,1:4])
X_train2=scaler.fit_transform(iris.data[:,0:4])
Y_train = np.array(pd.get_dummies(iris.target))
### SIMPLE NEURAL NETS WITH VARIABLE SELECTION
sgd = SGD(lr=learning_rate,momentum=momentum, decay=decay_rate, nesterov=False)
model_left=Sequential()
model_left.add(Dense(5, input_dim=4, init='glorot_uniform'))
model_left.add(Activation('relu'))
model_left.add(Dense(5))
model_left.add(Activation('relu'))
model_left.add(Dense(3))
model_left.add(Activation('sigmoid'))
model_left.add(Dense(4))
for i in range(0,6):
print(i,model_left.layers[i].name)
model_right=Sequential()
part=5
model_left.layers[part].name
get_0_layer_output = K.function([model_left.layers[0].input, K.learning_phase()],[model_left.layers[part].output])
get_0_layer_output([X_train2, 0])[0][0]
pred=[np.argmax(get_0_layer_output([X_train2, 0])[0][i]) for i in range(0,len(X_train2))]
loss=iris.target-pred
loss=loss.astype('float32')
model_right.add(Lambda(lambda x: x-np.mean(loss),input_shape=(4,),output_shape=(4,)))
model2=Sequential()
model2.add(Merge([model_left,model_right],mode = 'concat'))
model2.add(Activation('relu'))
model2.add(Reshape((8,)))
model2.add(Dense(3))
model2.add(Activation('sigmoid'))
model2.compile(loss = 'binary_crossentropy', optimizer = sgd, metrics = ['accuracy'])
model2.summary()
model2.fit([X_train2,X_train2], Y_train,
batch_size = 30, nb_epoch = 1000, verbose = 1)
res2 = model2.predict_classes([X_train2,X_train2])
acc2=((res2-iris.target)==0).sum()/len(res2)
acc2
150/150 [==============================] - 0s
Out[449]: 0.98666666666666669
____________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
====================================================================================================
dense_536 (Dense) (None, 5) 25
____________________________________________________________________________________________________
activation_568 (Activation) (None, 5) 0
____________________________________________________________________________________________________
dense_537 (Dense) (None, 5) 30
____________________________________________________________________________________________________
activation_569 (Activation) (None, 5) 0
____________________________________________________________________________________________________
dense_538 (Dense) (None, 3) 18
____________________________________________________________________________________________________
activation_570 (Activation) (None, 3) 0
____________________________________________________________________________________________________
dense_539 (Dense) (None, 4) 16
____________________________________________________________________________________________________
lambda_150 (Lambda) (None, 4) 0
____________________________________________________________________________________________________
activation_573 (Activation) (None, 8) 0 merge_104[0][0]
____________________________________________________________________________________________________
reshape_38 (Reshape) (None, 8) 0 activation_573[0][0]
____________________________________________________________________________________________________
dense_540 (Dense) (None, 3) 27 reshape_38[0][0]
____________________________________________________________________________________________________
activation_574 (Activation) (None, 3) 0 dense_540[0][0]
====================================================================================================
Total params: 116
____________________________________________________________________________________________________